Skip to content

Commit

Permalink
don't use the api for strict version (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann authored Feb 22, 2023
1 parent c406dd7 commit afedd8a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ The action can be configured with the following arguments:

- `version` - The version of earthly to install. Default is `latest`. Accepts semver style values.
- `github-token` (optional) - Token used to query earthly versions.
- `prerelease` (optional) - allow prerelease versions.
- `use-cache` (optional) - whether to use the cache to store earthly or not.
27 changes: 19 additions & 8 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71531,24 +71531,35 @@ function run() {
const osArch = os.arch();
const releaseArch = nodeArchToReleaseArch[os.arch()] || osArch;
const range = core.getInput("version");
const prerelease = core.getInput("prerelease").toUpperCase() === 'TRUE';
core.info(`Configured range: ${range}; allow prerelease: ${prerelease}`);
const version = yield (0, get_version_1.getVersionObject)(range, prerelease);
const isValidSemVer = semver.valid(range) != null;
var tag_name;
if (isValidSemVer) {
core.info(`Using provided strict version ${range}`);
tag_name = `v${range}`;
}
else {
// only grab the version from the api if the version provided by the user
// doesn't appear to be a valid semver
const prerelease = core.getInput("prerelease").toUpperCase() === 'TRUE';
core.info(`Configured range: ${range}; allow prerelease: ${prerelease}`);
const version = yield (0, get_version_1.getVersionObject)(range, prerelease);
tag_name = version.tag_name;
}
const destination = path.join(os.homedir(), `.${pkgName}`);
core.info(`Install destination is ${destination}`);
const installationDir = path.join(destination, "bin");
const installationPath = path.join(installationDir, `${pkgName}${IS_WINDOWS ? ".exe" : ""}`);
core.info(`Matched version: ${version.tag_name}`);
core.info(`Matched version: ${tag_name}`);
// first see if earthly is in the toolcache (installed locally)
const toolcacheDir = tc.find(pkgName, semver.clean(version.tag_name) || version.tag_name.substring(1), os.arch());
const toolcacheDir = tc.find(pkgName, semver.clean(tag_name) || tag_name.substring(1), os.arch());
if (toolcacheDir) {
core.addPath(toolcacheDir);
core.info(`using earthly from toolcache (${toolcacheDir})`);
return;
}
// then try to restore earthly from the github action cache
core.addPath(installationDir);
const restored = yield (0, cache_restore_1.restoreCache)(installationPath, semver.clean(version.tag_name) || version.tag_name.substring(1));
const restored = yield (0, cache_restore_1.restoreCache)(installationPath, semver.clean(tag_name) || tag_name.substring(1));
if (restored) {
yield fs.promises.chmod(installationPath, 0o755);
return;
Expand All @@ -71560,12 +71571,12 @@ function run() {
.then(() => {
core.info(`Successfully deleted pre-existing ${installationDir}`);
});
const buildURL = `https://github.com/earthly/earthly/releases/download/${version.tag_name}/${pkgName}-${releasePlatform}-${releaseArch}${IS_WINDOWS ? ".exe" : ""}`;
const buildURL = `https://github.com/earthly/earthly/releases/download/${tag_name}/${pkgName}-${releasePlatform}-${releaseArch}${IS_WINDOWS ? ".exe" : ""}`;
core.debug(`downloading ${buildURL}`);
const downloaded = yield tc.downloadTool(buildURL, installationPath);
core.debug(`successfully downloaded ${buildURL} to ${downloaded}`);
yield fs.promises.chmod(installationPath, 0o755);
yield tc.cacheDir(path.join(destination, "bin"), pkgName, semver.clean(version.tag_name) || version.tag_name.substring(1), os.arch());
yield tc.cacheDir(path.join(destination, "bin"), pkgName, semver.clean(tag_name) || tag_name.substring(1), os.arch());
core.exportVariable("FORCE_COLOR", "1");
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/lib/__tests__/get-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ describe("get-version", () => {
if (test.eq) expect(semver.eq(v.tag_name, test.eq));
});
});
describe("valid semver", () => {
it.each([
{spec: "0.4.*", valid: false},
{spec: "v0.4.1", valid: false},
{spec: "0.6.1", valid: true},
] as const)("%s is valid semantic version", async (test) => {
console.log(JSON.stringify(test));
const v = semver.valid(test.spec) != null;
expect(v == test.valid);
});
});
});
26 changes: 18 additions & 8 deletions src/setup-earthly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,19 @@ async function run() {
const releaseArch = nodeArchToReleaseArch[os.arch()] || osArch;

const range = core.getInput("version");
const prerelease = core.getInput("prerelease").toUpperCase() === 'TRUE';
core.info(`Configured range: ${range}; allow prerelease: ${prerelease}`);
const version = await getVersionObject(range, prerelease);
const isValidSemVer = semver.valid(range) != null;
var tag_name: string;
if (isValidSemVer) {
core.info(`Using provided strict version ${range}`);
tag_name = `v${range}`;
} else {
// only grab the version from the api if the version provided by the user
// doesn't appear to be a valid semver
const prerelease = core.getInput("prerelease").toUpperCase() === 'TRUE';
core.info(`Configured range: ${range}; allow prerelease: ${prerelease}`);
const version = await getVersionObject(range, prerelease);
tag_name = version.tag_name;
}

const destination = path.join(os.homedir(), `.${pkgName}`);
core.info(`Install destination is ${destination}`);
Expand All @@ -53,12 +63,12 @@ async function run() {
installationDir,
`${pkgName}${IS_WINDOWS ? ".exe" : ""}`
);
core.info(`Matched version: ${version.tag_name}`);
core.info(`Matched version: ${tag_name}`);

// first see if earthly is in the toolcache (installed locally)
const toolcacheDir = tc.find(
pkgName,
semver.clean(version.tag_name) || version.tag_name.substring(1),
semver.clean(tag_name) || tag_name.substring(1),
os.arch()
);

Expand All @@ -72,7 +82,7 @@ async function run() {
core.addPath(installationDir);
const restored = await restoreCache(
installationPath,
semver.clean(version.tag_name) || version.tag_name.substring(1)
semver.clean(tag_name) || tag_name.substring(1)
);
if (restored) {
await fs.promises.chmod(installationPath, 0o755);
Expand All @@ -88,7 +98,7 @@ async function run() {
});

const buildURL = `https://github.com/earthly/earthly/releases/download/${
version.tag_name
tag_name
}/${pkgName}-${releasePlatform}-${releaseArch}${IS_WINDOWS ? ".exe" : ""}`;

core.debug(`downloading ${buildURL}`);
Expand All @@ -100,7 +110,7 @@ async function run() {
await tc.cacheDir(
path.join(destination, "bin"),
pkgName,
semver.clean(version.tag_name) || version.tag_name.substring(1),
semver.clean(tag_name) || tag_name.substring(1),
os.arch()
);
core.exportVariable("FORCE_COLOR", "1");
Expand Down

0 comments on commit afedd8a

Please sign in to comment.