Skip to content

Commit

Permalink
Support Scarb nightlies (#10)
Browse files Browse the repository at this point in the history
fix #4
  • Loading branch information
mkaput authored Aug 11, 2023
1 parent 321b326 commit 8deb0a6
Show file tree
Hide file tree
Showing 7 changed files with 2,091 additions and 1,962 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ jobs:
echo "Latest Scarb version found is $latest_version"
echo "LATEST_VERSION=$latest_version" >> $env:GITHUB_OUTPUT
$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 '^.*/nightly-','nightly-')
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 +51,18 @@ jobs:
scarb-version: latest
- run: scarb --version | grep "scarb ${{ steps.version.outputs.LATEST_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: "Setup Scarb with `scarb-version: nightly`"
uses: ./
with:
scarb-version: nightly
- run: scarb --version | grep "${{ steps.version.outputs.LATEST_NIGHTLY_VERSION }}"

- 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
3,895 changes: 1,973 additions & 1,922 deletions dist/setup/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

23 changes: 20 additions & 3 deletions lib/download.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import path from "path";
import fs from "fs/promises";
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}`;

core.info(`Downloading Scarb from ${url}`);
const pathToTarball = await tc.downloadTool(url);

const extract = url.endsWith(".zip") ? tc.extractZip : tc.extractTar;
const extractedPath = await extract(pathToTarball);
const pathToCli = path.join(extractedPath, basename);

const pathToCli = await findScarbDir(extractedPath);

core.debug(`Extracted to ${pathToCli}`);
return pathToCli;
}

async function findScarbDir(extractedPath) {
for (const dirent of await fs.readdir(extractedPath, {
withFileTypes: true,
})) {
if (dirent.isDirectory() && dirent.name.startsWith("scarb-")) {
return path.join(extractedPath, dirent.name);
}
}

throw new Error(`could not find Scarb directory in ${extractedPath}`);
}
50 changes: 31 additions & 19 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
import path from "path";
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { determineVersion } from "./versions";
import {
determineVersion,
getFullVersionFromScarb,
versionWithPrefix,
} 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);
scarbPrefix = await tc.cacheDir(
download,
"scarb",
scarbVersion,
triplet,
);
}
await core.group(
`Setting up Scarb ${versionWithPrefix(scarbVersion)}`,
async () => {
let scarbPrefix = tc.find("scarb", scarbVersion, triplet);
if (!scarbPrefix) {
const download = await downloadScarb(scarbRepo, scarbVersion);
scarbPrefix = await tc.cacheDir(
download,
"scarb",
scarbVersion,
triplet,
);
}

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

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

await restoreCache().catch((e) => {
core.error(
Expand Down
54 changes: 40 additions & 14 deletions lib/versions.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
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);
}

return {
repo: version.startsWith("nightly-") ? nightliesRepo : 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 Expand Up @@ -49,7 +74,7 @@ function fetchLatestTag(repo) {
);
}

const tag = location.replace(/.*\/tag\/(v.*)(?:\/.*)?/, "$1");
const tag = location.replace(/.*\/tag\/(.*)(?:\/.*)?/, "$1");
if (!tag) {
throw new Error(
`failed to determine latest version: could not extract tag from release url`,
Expand All @@ -63,10 +88,11 @@ function fetchLatestTag(repo) {

async function getVersionFromToolVersionsFile() {
try {
return (await fs.readFile(".tool-versions", "utf-8")).match(
/^scarb ([\w.-]+)/m,
)?.[1];
const toolVersions = await fs.readFile(".tool-versions", {
encoding: "utf-8",
});
return toolVersions.match(/^scarb ([\w.-]+)/m)?.[1];
} catch (e) {
return;
return undefined;
}
}

0 comments on commit 8deb0a6

Please sign in to comment.