Skip to content

Commit

Permalink
✨ tool-versions option (#17)
Browse files Browse the repository at this point in the history
Solves #14

---------

Co-authored-by: Marek Kaput <marek.kaput@outlook.com>
  • Loading branch information
EvolveArt and mkaput authored Aug 30, 2023
1 parent 8273f02 commit 436177f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ jobs:
uses: ./
- run: scarb --version | grep "scarb 0.7.0"

- name: "Create .tool-versions file in subfolder"
run: mkdir ./tmp && echo "scarb 0.7.0" >> ./tmp/.tool-versions
- name: "Setup Scarb using `.tool-versions` file in subfolder"
uses: ./
with:
tool-versions: ./tmp/.tool-versions
- run: scarb --version | grep "scarb 0.7.0"

lint:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- `"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.
- `tool-versions` - **Optional**. String.
- Stating a relative or absolute path to the `.tool-versions` file.
- Should be used only if `scarb-version` is not specified.

## Outputs

Expand Down
30 changes: 27 additions & 3 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60386,9 +60386,29 @@ var lib = __nccwpck_require__(6255);



async function determineVersion(version, { repo, nightliesRepo }) {

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

if (version && toolVersionsPath) {
throw new Error(
"the `scarb-version` and `tool-versions` inputs cannot be used simultaneously",
);
}

if (toolVersionsPath) {
let toolVersion = await getVersionFromToolVersionsFile(toolVersionsPath);

if (!toolVersion) {
throw new Error(`failed to read Scarb version from: ${toolVersionsPath}`);
}
version = toolVersion;
}

if (!version) {
let toolVersion = await getVersionFromToolVersionsFile();
version = toolVersion ?? "latest";
Expand Down Expand Up @@ -60469,9 +60489,10 @@ function fetchLatestTag(repo) {
);
}

async function getVersionFromToolVersionsFile() {
async function getVersionFromToolVersionsFile(toolVersionsPath) {
try {
const toolVersions = await promises_default().readFile(".tool-versions", {
toolVersionsPath = toolVersionsPath || ".tool-versions";
const toolVersions = await promises_default().readFile(toolVersionsPath, {
encoding: "utf-8",
});
return toolVersions.match(/^scarb ([\w.-]+)/m)?.[1];
Expand Down Expand Up @@ -60681,8 +60702,11 @@ async function restoreCache() {
async function main() {
try {
const scarbVersionInput = core.getInput("scarb-version");
const toolVersionsPathInput = core.getInput("tool-versions");

const { repo: scarbRepo, version: scarbVersion } = await determineVersion(
scarbVersionInput,
toolVersionsPathInput,
{
repo: "software-mansion/scarb",
nightliesRepo: "software-mansion/scarb-nightlies",
Expand Down
2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import { restoreCache } from "./cache-restore";
export default async function main() {
try {
const scarbVersionInput = core.getInput("scarb-version");
const toolVersionsPathInput = core.getInput("tool-versions");

const { repo: scarbRepo, version: scarbVersion } = await determineVersion(
scarbVersionInput,
toolVersionsPathInput,
{
repo: "software-mansion/scarb",
nightliesRepo: "software-mansion/scarb-nightlies",
Expand Down
27 changes: 24 additions & 3 deletions lib/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@ import fs from "fs/promises";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import { HttpClient } from "@actions/http-client";
import path from "path";

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

if (version && toolVersionsPath) {
throw new Error(
"the `scarb-version` and `tool-versions` inputs cannot be used simultaneously",
);
}

if (toolVersionsPath) {
let toolVersion = await getVersionFromToolVersionsFile(toolVersionsPath);

if (!toolVersion) {
throw new Error(`failed to read Scarb version from: ${toolVersionsPath}`);
}
version = toolVersion;
}

if (!version) {
let toolVersion = await getVersionFromToolVersionsFile();
version = toolVersion ?? "latest";
Expand Down Expand Up @@ -86,9 +106,10 @@ function fetchLatestTag(repo) {
);
}

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

0 comments on commit 436177f

Please sign in to comment.