Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Scarb version from .tool-versions file #6

Merged
merged 10 commits into from
Jul 31, 2023
38 changes: 31 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,48 @@ on:

jobs:
test:
name: test ${{ matrix.os }} scarb:${{ matrix.scarb-version }}
name: test ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
scarb-version:
- "0.4.1"
- latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: ./

- name: "Fetch latest Scarb version from GitHub releases"
id: version
shell: pwsh
run: |
$location = (Invoke-WebRequest -Uri "https://github.com/software-mansion/scarb/releases/latest" -Method Head -MaximumRedirection 0 -SkipHttpErrorCheck -ErrorAction Ignore).Headers.Location
$latest_version = ($location -replace '^.*/v','')
echo "Latest Scarb version found is $latest_version"
echo "LATEST_VERSION=$latest_version" >> $env:GITHUB_OUTPUT

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

- name: "Setup Scarb with `scarb-version: 0.4.1`"
uses: ./
with:
scarb-version: ${{ matrix.scarb-version }}
- run: scarb --version
scarb-version: 0.4.1
- run: scarb --version | grep "scarb 0.4.1"

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

- name: "Create .tool-versions file"
run: echo "scarb 0.5.0" >> .tool-versions
- name: "Setup Scarb using `.tool-versions` file"
uses: ./
- run: scarb --version | grep "scarb 0.5.0"

lint:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ branding:
inputs:
scarb-version:
description: Scarb version to use
default: "latest"
required: false
outputs:
scarb-prefix:
description: The prefix of the installed Scarb
Expand Down
33 changes: 23 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Large diffs are not rendered by default.

30 changes: 20 additions & 10 deletions lib/versions.js
szymmis marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import fs from "fs/promises";
import * as core from "@actions/core";
import { HttpClient } from "@actions/http-client";

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

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

if (versionInput.startsWith("v")) {
versionInput = versionInput.substring(1);
if (!versionInput) {
let toolVersion = await getVersionFromToolVersionsFile();
versionInput = toolVersion ?? "latest";
}

if (!versionInput) {
throw new Error(`'scarb-input' value must not be empty`);
if (versionInput === "latest") {
versionInput = await fetchLatestTag(repo);
}

return versionInput;
return versionInput.startsWith("v")
? versionInput.substring(1)
: versionInput;
}

function fetchLatestTag(repo) {
Expand Down Expand Up @@ -60,3 +60,13 @@ function fetchLatestTag(repo) {
},
);
}

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