Skip to content

Commit

Permalink
Support Scarb nightlies
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaput committed Aug 10, 2023
1 parent 86dd8fa commit 087afb7
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 39 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ jobs:
echo "Latest Scarb version found is $latest_version"
echo "LATEST_VERSION=$latest_version" >> $env:GITHUB_OUTPUT
- name: "Fetch latest Scarb nightly version from GitHub releases"
id: version
shell: pwsh
run: |
$location = (Invoke-WebRequest -Uri "https://github.com/software-mansion/scarb-nightlies/releases/latest" -Method Head -MaximumRedirection 0 -SkipHttpErrorCheck -ErrorAction Ignore).Headers.Location
$latest_version = ($location -replace '^.*/v','')
echo "Latest Scarb nightly version found is $latest_version"
echo "LATEST_NIGHTLY_VERSION=$latest_version" >> $env:GITHUB_OUTPUT
- name: "Setup Scarb without `scarb-version`"
uses: ./
- run: scarb --version | grep "scarb ${{ steps.version.outputs.LATEST_VERSION }}"
Expand All @@ -46,6 +55,18 @@ jobs:
scarb-version: latest
- run: scarb --version | grep "scarb ${{ steps.version.outputs.LATEST_VERSION }}"

- name: "Setup Scarb with `scarb-version: nightly`"
uses: ./
with:
scarb-version: nightly
- run: scarb --version | grep "scarb ${{ steps.version.outputs.LATEST_NIGHTLY_VERSION }}"

- name: "Setup Scarb with `scarb-version: nightly-2023-08-10`"
uses: ./
with:
scarb-version: nightly-2023-08-10
- run: scarb --version | grep "scarb v0.6.0+nightly-2023-08-10"

- name: "Create .tool-versions file"
run: echo "scarb 0.5.0" >> .tool-versions
- name: "Setup Scarb using `.tool-versions` file"
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "0.5.1"
scarb-version: "0.6.0"
- run: scarb fmt --check
- run: scarb test
```
## Inputs
- `scarb-version` - **Optional**. A string stating an explicit Scarb version to use, or `"latest"`. When not specified, the `.tool-versions` file will be read to resolve Scarb version, and in case it is not present the latest version will be used.
- `scarb-version` - **Optional**. String, either:
- Stating an explicit Scarb version to use, for example `"0.6.0"`.
- Stating an explicit nightly tag to use, for example `"nightly-2023-08-10"`.
- `"latest"` to download latest stable version.
- `"nightly"` to download latest nightly version.
- Empty/not specified: the `.tool-versions` file will be read to resolve Scarb version, and in case it is not
present the latest stable version will be used.

## Outputs

- `scarb-prefix` - A path to where Scarb has been extracted to. The `scarb` binary will be located in the `bin`
subdirectory (`${{ steps.setup-scarb.outputs.scarb-prefix }}/bin`).
- `scarb-version` - Installed Scarb version.
- `scarb-version` - Installed Scarb version (as reported by `scarb -V`).

[scarb]: https://docs.swmansion.com/scarb
75 changes: 57 additions & 18 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60329,28 +60329,62 @@ var tool_cache = __nccwpck_require__(7784);
;// CONCATENATED MODULE: external "fs/promises"
const promises_namespaceObject = require("fs/promises");
var promises_default = /*#__PURE__*/__nccwpck_require__.n(promises_namespaceObject);
// EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js
var exec = __nccwpck_require__(1514);
// EXTERNAL MODULE: ./node_modules/@actions/http-client/lib/index.js
var lib = __nccwpck_require__(6255);
;// CONCATENATED MODULE: ./lib/versions.js




async function determineVersion(repo, versionInput) {
versionInput = versionInput?.trim();

if (!versionInput) {
async function determineVersion(version, { repo, nightliesRepo }) {
version = version?.trim();

if (!version) {
let toolVersion = await getVersionFromToolVersionsFile();
versionInput = toolVersion ?? "latest";
version = toolVersion ?? "latest";
}

if (version === "latest") {
version = await fetchLatestTag(repo);
}

if (version === "nightly") {
version = await fetchLatestTag(nightliesRepo);
}

if (versionInput === "latest") {
versionInput = await fetchLatestTag(repo);
if (version.startsWith("v")) {
version = version.substring(1);
}

return versionInput.startsWith("v")
? versionInput.substring(1)
: versionInput;
if (version.startsWith("nightly-")) {
return {
repo: nightliesRepo,
version,
};
} else {
return {
repo,
version,
};
}
}

function versionWithPrefix(version) {
return /^\d/.test(version) ? `v${version}` : version;
}

async function getFullVersionFromScarb() {
const { stdout } = await exec.getExecOutput(`scarb -V`);
const match = stdout.match(/^scarb ([^ ]+)/);
if (!match) {
throw new Error(
`Unable to determine Scarb version from 'scarb -V' output: ${stdout}`,
);
}
return match[1];
}

function fetchLatestTag(repo) {
Expand Down Expand Up @@ -60450,11 +60484,13 @@ function getOsPlatform() {




async function downloadScarb(repo, version) {
const triplet = getOsTriplet();
const basename = `scarb-v${version}-${triplet}`;
const tag = versionWithPrefix(version);
const basename = `scarb-${tag}-${triplet}`;
const extension = triplet.includes("-windows-") ? "zip" : "tar.gz";
const url = `https://github.com/${repo}/releases/download/v${version}/${basename}.${extension}`;
const url = `https://github.com/${repo}/releases/download/${tag}/${basename}.${extension}`;

const pathToTarball = await tool_cache.downloadTool(url);

Expand All @@ -60468,8 +60504,6 @@ async function downloadScarb(repo, version) {

// EXTERNAL MODULE: ./node_modules/@actions/cache/lib/cache.js
var cache = __nccwpck_require__(7799);
// EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js
var exec = __nccwpck_require__(1514);
// EXTERNAL MODULE: ./node_modules/@actions/glob/lib/glob.js
var glob = __nccwpck_require__(8090);
;// CONCATENATED MODULE: ./lib/cache-utils.js
Expand Down Expand Up @@ -60559,19 +60593,23 @@ async function restoreCache() {



const REPO = "software-mansion/scarb";

async function main() {
try {
const scarbVersionInput = core.getInput("scarb-version");
const scarbVersion = await determineVersion(REPO, scarbVersionInput);
const { repo: scarbRepo, version: scarbVersion } = await determineVersion(
scarbVersionInput,
{
repo: "software-mansion/scarb",
nightliesRepo: "software-mansion/scarb-nightlies",
},
);

const triplet = getOsTriplet();

await core.group(`Setting up Scarb v${scarbVersion}`, async () => {
let scarbPrefix = tool_cache.find("scarb", scarbVersion, triplet);
if (!scarbPrefix) {
const download = await downloadScarb(REPO, scarbVersion);
const download = await downloadScarb(scarbRepo, scarbVersion);
scarbPrefix = await tool_cache.cacheDir(
download,
"scarb",
Expand All @@ -60581,10 +60619,11 @@ async function main() {
}

core.setOutput("scarb-prefix", scarbPrefix);
core.setOutput("scarb-version", scarbVersion);
core.addPath(external_path_default().join(scarbPrefix, "bin"));
});

core.setOutput("scarb-version", await getFullVersionFromScarb());

await restoreCache().catch((e) => {
core.error(
`There was an error when restoring cache: ${
Expand Down
2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import path from "path";
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { getOsTriplet } from "./platform";
import { versionWithPrefix } from "./versions";

export async function downloadScarb(repo, version) {
const triplet = getOsTriplet();
const basename = `scarb-v${version}-${triplet}`;
const tag = versionWithPrefix(version);
const basename = `scarb-${tag}-${triplet}`;
const extension = triplet.includes("-windows-") ? "zip" : "tar.gz";
const url = `https://github.com/${repo}/releases/download/v${version}/${basename}.${extension}`;
const url = `https://github.com/${repo}/releases/download/${tag}/${basename}.${extension}`;

const pathToTarball = await tc.downloadTool(url);

Expand Down
17 changes: 11 additions & 6 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import path from "path";
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { determineVersion } from "./versions";
import { determineVersion, getFullVersionFromScarb } from "./versions";
import { downloadScarb } from "./download";
import { getOsTriplet } from "./platform";
import { restoreCache } from "./cache-restore";

const REPO = "software-mansion/scarb";

export default async function main() {
try {
const scarbVersionInput = core.getInput("scarb-version");
const scarbVersion = await determineVersion(REPO, scarbVersionInput);
const { repo: scarbRepo, version: scarbVersion } = await determineVersion(
scarbVersionInput,
{
repo: "software-mansion/scarb",
nightliesRepo: "software-mansion/scarb-nightlies",
},
);

const triplet = getOsTriplet();

await core.group(`Setting up Scarb v${scarbVersion}`, async () => {
let scarbPrefix = tc.find("scarb", scarbVersion, triplet);
if (!scarbPrefix) {
const download = await downloadScarb(REPO, scarbVersion);
const download = await downloadScarb(scarbRepo, scarbVersion);
scarbPrefix = await tc.cacheDir(
download,
"scarb",
Expand All @@ -28,10 +32,11 @@ export default async function main() {
}

core.setOutput("scarb-prefix", scarbPrefix);
core.setOutput("scarb-version", scarbVersion);
core.addPath(path.join(scarbPrefix, "bin"));
});

core.setOutput("scarb-version", await getFullVersionFromScarb());

await restoreCache().catch((e) => {
core.error(
`There was an error when restoring cache: ${
Expand Down
50 changes: 41 additions & 9 deletions lib/versions.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
import fs from "fs/promises";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import { HttpClient } from "@actions/http-client";

export async function determineVersion(repo, versionInput) {
versionInput = versionInput?.trim();
export async function determineVersion(version, { repo, nightliesRepo }) {
version = version?.trim();

if (!versionInput) {
if (!version) {
let toolVersion = await getVersionFromToolVersionsFile();
versionInput = toolVersion ?? "latest";
version = toolVersion ?? "latest";
}

if (versionInput === "latest") {
versionInput = await fetchLatestTag(repo);
if (version === "latest") {
version = await fetchLatestTag(repo);
}

return versionInput.startsWith("v")
? versionInput.substring(1)
: versionInput;
if (version === "nightly") {
version = await fetchLatestTag(nightliesRepo);
}

if (version.startsWith("v")) {
version = version.substring(1);
}

if (version.startsWith("nightly-")) {
return {
repo: nightliesRepo,
version,
};
} else {
return {
repo,
version,
};
}
}

export function versionWithPrefix(version) {
return /^\d/.test(version) ? `v${version}` : version;
}

export async function getFullVersionFromScarb() {
const { stdout } = await exec.getExecOutput(`scarb -V`);
const match = stdout.match(/^scarb ([^ ]+)/);
if (!match) {
throw new Error(
`Unable to determine Scarb version from 'scarb -V' output: ${stdout}`,
);
}
return match[1];
}

function fetchLatestTag(repo) {
Expand Down

0 comments on commit 087afb7

Please sign in to comment.