Compare commits

..

6 Commits

3 changed files with 59 additions and 29 deletions
+14 -9
View File
@@ -6,22 +6,27 @@ const tool_cache_1 = require("@actions/tool-cache");
const _fs = require("fs"); const _fs = require("fs");
const fs = _fs.promises; const fs = _fs.promises;
async function fetchVersions() { async function fetchVersions() {
const file = await tool_cache_1.downloadTool('https://raw.githubusercontent.com/xmake-io/github-action-setup-xmake/data/versions.json'); const file = await tool_cache_1.downloadTool("https://api.github.com/repos/xmake-io/xmake/git/refs/tags");
const versions = await fs.readFile(file, 'utf-8'); const tags = JSON.parse(await fs.readFile(file, { encoding: "utf-8" }));
return JSON.parse(versions); return tags.map(({ ref, object: { sha } }) => [ref.slice(11), sha]).reduce((o, [k, v]) => {
o[k] = v;
return o;
}, {});
} }
exports.fetchVersions = fetchVersions; exports.fetchVersions = fetchVersions;
async function selectVersion(version) { async function selectVersion(version) {
version = version || core.getInput('xmake-version') || 'latest'; version = version || core.getInput("xmake-version") || "latest";
if (version.toLowerCase() === 'latest') if (version.toLowerCase() === "latest")
version = ''; version = "";
version = semver.validRange(version); version = semver.validRange(version);
if (!version) if (!version) {
throw new Error(`Invalid input xmake-version: ${core.getInput('xmake-version')}`); throw new Error(`Invalid input xmake-version: ${core.getInput("xmake-version")}`);
}
const versions = await fetchVersions(); const versions = await fetchVersions();
const ver = semver.maxSatisfying(Object.keys(versions), version); const ver = semver.maxSatisfying(Object.keys(versions), version);
if (!ver) if (!ver) {
throw new Error(`No matched releases of xmake-version: ${version}`); throw new Error(`No matched releases of xmake-version: ${version}`);
}
const sha = versions[ver]; const sha = versions[ver];
core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`); core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`);
return { version: ver, sha }; return { version: ver, sha };
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "github-action-setup-xmake", "name": "github-action-setup-xmake",
"version": "1.0.0", "version": "1.0.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
+44 -19
View File
@@ -1,26 +1,51 @@
import * as core from '@actions/core' import * as core from "@actions/core";
import * as semver from 'semver' import * as semver from "semver";
import { downloadTool } from '@actions/tool-cache' import { downloadTool } from "@actions/tool-cache";
import * as _fs from 'fs' import * as _fs from "fs";
const fs = _fs.promises const fs = _fs.promises;
type TagItem = {
ref: string;
node_id: string;
url: string;
object: {
sha: string;
type: string;
url: string;
};
};
export async function fetchVersions() { export async function fetchVersions() {
const file = await downloadTool('https://raw.githubusercontent.com/xmake-io/github-action-setup-xmake/data/versions.json') const file = await downloadTool("https://api.github.com/repos/xmake-io/xmake/git/refs/tags");
const versions = await fs.readFile(file, 'utf-8') const tags: [TagItem] = JSON.parse(await fs.readFile(file, { encoding: "utf-8" }));
return JSON.parse(versions) as { [v: string]: string } return tags.map(({ ref, object: {sha} }) => [ref.slice(11), sha]).reduce(
(o, [k, v]) => {
o[k] = v;
return o;
},
{} as Record<string, string>
);
} }
export async function selectVersion(version?: string) { export async function selectVersion(version?: string) {
version = version || core.getInput('xmake-version') || 'latest' version = version || core.getInput("xmake-version") || "latest";
if (version.toLowerCase() === 'latest') version = '' if (version.toLowerCase() === "latest") version = "";
version = semver.validRange(version) version = semver.validRange(version);
if (!version) throw new Error(`Invalid input xmake-version: ${core.getInput('xmake-version')}`) if (!version) {
throw new Error(
`Invalid input xmake-version: ${core.getInput("xmake-version")}`
);
}
const versions = await fetchVersions() const versions = await fetchVersions();
const ver = semver.maxSatisfying(Object.keys(versions), version) const ver = semver.maxSatisfying(Object.keys(versions), version);
if (!ver) throw new Error(`No matched releases of xmake-version: ${version}`) if (!ver) {
throw new Error(
`No matched releases of xmake-version: ${version}`
);
}
const sha = versions[ver] const sha = versions[ver];
core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`) core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`);
return { version: ver, sha } return { version: ver, sha };
} }