Compare commits

...

5 Commits

15 changed files with 276 additions and 80 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ inputs:
xmake-version: xmake-version:
required: true required: true
default: latest default: latest
description: The version to use. Should be a semver range or 'latest' description: The version to use. Should be a semver range or 'latest'. Or use branch@{branch_name} to select a branch.
runs: runs:
using: 'node12' using: 'node12'
Vendored
+1 -1
View File
@@ -43,7 +43,7 @@ async function create(ref) {
await io.mkdirP(opt.cwd); await io.mkdirP(opt.cwd);
await exec_1.exec('git', ['init'], opt); await exec_1.exec('git', ['init'], opt);
await exec_1.exec('git', ['remote', 'add', 'origin', 'https://github.com/xmake-io/xmake.git'], opt); await exec_1.exec('git', ['remote', 'add', 'origin', 'https://github.com/xmake-io/xmake.git'], opt);
await exec_1.exec('git', ['fetch'], opt); await exec_1.exec('git', ['fetch', 'origin', '+refs/pull/*:refs/remotes/origin/pull/*', '+refs/heads/*:refs/remotes/origin/*'], opt);
await exec_1.exec('git', ['checkout', ref], opt); await exec_1.exec('git', ['checkout', ref], opt);
await exec_1.exec('git', ['submodule', 'update', '--init', '--recursive'], opt); await exec_1.exec('git', ['submodule', 'update', '--init', '--recursive'], opt);
return opt.cwd; return opt.cwd;
+9
View File
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function Sha(sha) {
sha = sha.trim().toLowerCase();
if (!/^[a-f0-9]{40}$/g.test(sha))
throw new Error(`Invalid sha value ${sha}`);
return sha;
}
exports.Sha = Sha;
+38 -4
View File
@@ -17,6 +17,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = require("@actions/core"); const core = require("@actions/core");
const semver = require("semver"); const semver = require("semver");
const git_1 = require("./git"); const git_1 = require("./git");
const interfaces_1 = require("./interfaces");
let VERSIONS;
async function getVersions() {
if (VERSIONS)
return VERSIONS;
return (VERSIONS = await git_1.lsRemote());
}
class VersionImpl { class VersionImpl {
constructor(version, sha, type, toString) { constructor(version, sha, type, toString) {
this.version = version; this.version = version;
@@ -31,7 +38,7 @@ class VersionImpl {
} }
_string = new WeakMap(); _string = new WeakMap();
async function selectBranch(branch) { async function selectBranch(branch) {
const versions = await git_1.lsRemote(); const versions = await getVersions();
if (branch in versions.heads) { if (branch in versions.heads) {
return new VersionImpl(branch, versions.heads[branch], 'heads', `branch ${branch}`); return new VersionImpl(branch, versions.heads[branch], 'heads', `branch ${branch}`);
} }
@@ -39,12 +46,12 @@ async function selectBranch(branch) {
} }
async function selectPr(pr) { async function selectPr(pr) {
var _a; var _a;
const versions = await git_1.lsRemote(); const versions = await getVersions();
if (pr in versions.pull) { if (pr in versions.pull) {
const prheads = versions.pull[pr]; const prheads = versions.pull[pr];
const sha = (_a = prheads.merge) !== null && _a !== void 0 ? _a : prheads.head; const sha = (_a = prheads.merge) !== null && _a !== void 0 ? _a : prheads.head;
if (sha) { if (sha) {
return new VersionImpl(`#${pr}`, sha, 'pull', `pull request #${pr}`); return new VersionImpl(`pr#${pr}`, sha, 'pull', `pull request #${pr}`);
} }
} }
throw new Error(`Pull requrest #${pr} not found`); throw new Error(`Pull requrest #${pr} not found`);
@@ -55,7 +62,7 @@ async function selectSemver(version) {
if (!v) { if (!v) {
throw new Error(`Invalid semver`); throw new Error(`Invalid semver`);
} }
const versions = await git_1.lsRemote(); const versions = await getVersions();
const ver = semver.maxSatisfying(Object.keys(versions.tags), v); const ver = semver.maxSatisfying(Object.keys(versions.tags), v);
if (!ver) { if (!ver) {
throw new Error(`No matched releases of xmake-version ${v.format()}`); throw new Error(`No matched releases of xmake-version ${v.format()}`);
@@ -63,6 +70,28 @@ async function selectSemver(version) {
const sha = versions.tags[ver]; const sha = versions.tags[ver];
return new VersionImpl(ver, sha, 'tags', ver); return new VersionImpl(ver, sha, 'tags', ver);
} }
async function selectSha(sha) {
var _a;
const shaValue = interfaces_1.Sha(sha);
const versions = await getVersions();
for (const branch in versions.heads) {
if (versions.heads[branch] === shaValue) {
return selectBranch(branch);
}
}
for (const tag in versions.tags) {
if (versions.tags[tag] === shaValue) {
return selectSemver(tag);
}
}
for (const pr in versions.pull) {
const prData = versions.pull[pr];
if (((_a = prData.merge) !== null && _a !== void 0 ? _a : prData.head) === shaValue) {
return selectPr(Number.parseInt(pr));
}
}
return Promise.resolve(new VersionImpl(`sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`));
}
async function selectVersion(version) { async function selectVersion(version) {
// get version string // get version string
version = (version !== null && version !== void 0 ? version : core.getInput('xmake-version')) || 'latest'; version = (version !== null && version !== void 0 ? version : core.getInput('xmake-version')) || 'latest';
@@ -82,6 +111,11 @@ async function selectVersion(version) {
} }
ret = await selectPr(pr); ret = await selectPr(pr);
} }
// select sha
if (version.startsWith('sha@')) {
const sha = version.substr('sha@'.length);
ret = await selectSha(sha);
}
// select version // select version
if (semver.validRange(version)) { if (semver.validRange(version)) {
ret = await selectSemver(version); ret = await selectSemver(version);
+30 -16
View File
@@ -7,27 +7,41 @@ const toolCache = require("@actions/tool-cache");
const os = require("os"); const os = require("os");
const path = require("path"); const path = require("path");
const semver = require("semver"); const semver = require("semver");
function getInstallerUrl(version) {
const ver = version.version;
switch (version.type) {
case 'heads': {
// we only use appveyor ci artifacts for branch version
const arch = os.arch() === 'x64' ? 'x64' : 'x86';
return `https://ci.appveyor.com/api/projects/waruqi/xmake/artifacts/xmake-installer.exe?branch=${ver}&pr=false&job=Image%3A+Visual+Studio+2017%3B+Platform%3A+${arch}`;
}
case 'pull': {
throw new Error('PR builds for windows is not supported');
}
case 'sha': {
throw new Error('Sha builds for windows is not supported');
}
case 'tags': {
// we cannot use appveyor ci artifacts, the old version links may be broken.
const arch = os.arch() === 'x64' ? 'win64' : 'win32';
return semver.gt(ver, '2.2.6')
? `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.${arch}.exe`
: `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.exe`;
}
default: {
// check that we have tested all types
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _ = version.type;
throw new Error('Unknown version type');
}
}
}
async function winInstall(version) { async function winInstall(version) {
const ver = version.version; const ver = version.version;
let toolDir = toolCache.find('xmake', ver); let toolDir = toolCache.find('xmake', ver);
if (!toolDir) { if (!toolDir) {
const installer = await core.group(`download xmake ${version}`, async () => { const installer = await core.group(`download xmake ${version}`, async () => {
let url = ''; const url = getInstallerUrl(version);
if (version.type === 'heads') {
// we only use appveyor ci artifacts for branch version
const arch = os.arch() === 'x64' ? 'x64' : 'x86';
url = `https://ci.appveyor.com/api/projects/waruqi/xmake/artifacts/xmake-installer.exe?branch=${ver}&pr=false&job=Image%3A+Visual+Studio+2017%3B+Platform%3A+${arch}`;
}
else if (version.type === 'pull') {
throw new Error('PR builds for windows is not supported');
}
else {
// we cannot use appveyor ci artifacts, the old version links may be broken.
const arch = os.arch() === 'x64' ? 'win64' : 'win32';
url = semver.gt(ver, '2.2.6')
? `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.${arch}.exe`
: `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.exe`;
}
core.info(`downloading from ${url}`); core.info(`downloading from ${url}`);
const file = await toolCache.downloadTool(url); const file = await toolCache.downloadTool(url);
const exe = path.format({ ...path.parse(file), ext: '.exe', base: undefined }); const exe = path.format({ ...path.parse(file), ext: '.exe', base: undefined });
+4 -2
View File
@@ -13,7 +13,7 @@
"@actions/io@^1.0.1", "@actions/io@^1.0.1",
"@actions/tool-cache@^1.1.2", "@actions/tool-cache@^1.1.2",
"@types/jest@^25.1.4", "@types/jest@^25.1.4",
"@types/node@^13.9.8", "@types/node@^14.0.5",
"@types/semver@^7.1.0", "@types/semver@^7.1.0",
"@typescript-eslint/eslint-plugin@^2.26.0", "@typescript-eslint/eslint-plugin@^2.26.0",
"@typescript-eslint/parser@^2.26.0", "@typescript-eslint/parser@^2.26.0",
@@ -25,6 +25,7 @@
"rimraf@^3.0.2", "rimraf@^3.0.2",
"semver@^7.1.3", "semver@^7.1.3",
"ts-jest@^25.3.0", "ts-jest@^25.3.0",
"type-fest@^0.14.0",
"typescript@^3.8.3" "typescript@^3.8.3"
], ],
"lockfileEntries": { "lockfileEntries": {
@@ -97,7 +98,7 @@
"@types/jest@^25.1.4": "https://registry.npm.taobao.org/@types/jest/download/@types/jest-25.1.4.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fjest%2Fdownload%2F%40types%2Fjest-25.1.4.tgz#9e9f1e59dda86d3fd56afce71d1ea1b331f6f760", "@types/jest@^25.1.4": "https://registry.npm.taobao.org/@types/jest/download/@types/jest-25.1.4.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fjest%2Fdownload%2F%40types%2Fjest-25.1.4.tgz#9e9f1e59dda86d3fd56afce71d1ea1b331f6f760",
"@types/json-schema@^7.0.3": "https://registry.npm.taobao.org/@types/json-schema/download/@types/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339", "@types/json-schema@^7.0.3": "https://registry.npm.taobao.org/@types/json-schema/download/@types/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339",
"@types/node@*": "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.2.tgz?cache=0&sync_timestamp=1584566829623&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349", "@types/node@*": "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.2.tgz?cache=0&sync_timestamp=1584566829623&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349",
"@types/node@^13.9.8": "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.8.tgz#09976420fc80a7a00bf40680c63815ed8c7616f4", "@types/node@^14.0.5": "https://registry.npm.taobao.org/@types/node/download/@types/node-14.0.5.tgz?cache=0&sync_timestamp=1590092201933&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-14.0.5.tgz#3d03acd3b3414cf67faf999aed11682ed121f22b",
"@types/prettier@^1.19.0": "https://registry.npm.taobao.org/@types/prettier/download/@types/prettier-1.19.1.tgz#33509849f8e679e4add158959fdb086440e9553f", "@types/prettier@^1.19.0": "https://registry.npm.taobao.org/@types/prettier/download/@types/prettier-1.19.1.tgz#33509849f8e679e4add158959fdb086440e9553f",
"@types/semver@^7.1.0": "https://registry.npm.taobao.org/@types/semver/download/@types/semver-7.1.0.tgz#c8c630d4c18cd326beff77404887596f96408408", "@types/semver@^7.1.0": "https://registry.npm.taobao.org/@types/semver/download/@types/semver-7.1.0.tgz#c8c630d4c18cd326beff77404887596f96408408",
"@types/stack-utils@^1.0.1": "https://registry.npm.taobao.org/@types/stack-utils/download/@types/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e", "@types/stack-utils@^1.0.1": "https://registry.npm.taobao.org/@types/stack-utils/download/@types/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e",
@@ -616,6 +617,7 @@
"type-check@~0.3.2": "https://registry.npm.taobao.org/type-check/download/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72", "type-check@~0.3.2": "https://registry.npm.taobao.org/type-check/download/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72",
"type-detect@4.0.8": "https://registry.npm.taobao.org/type-detect/download/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c", "type-detect@4.0.8": "https://registry.npm.taobao.org/type-detect/download/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c",
"type-fest@^0.11.0": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1", "type-fest@^0.11.0": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1",
"type-fest@^0.14.0": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.14.0.tgz#758d800f2dcc0de47a946a120d91ab17883523ad",
"type-fest@^0.8.1": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d", "type-fest@^0.8.1": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d",
"typedarray-to-buffer@^3.1.5": "https://registry.npm.taobao.org/typedarray-to-buffer/download/typedarray-to-buffer-3.1.5.tgz?cache=0&sync_timestamp=1580310064722&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypedarray-to-buffer%2Fdownload%2Ftypedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080", "typedarray-to-buffer@^3.1.5": "https://registry.npm.taobao.org/typedarray-to-buffer/download/typedarray-to-buffer-3.1.5.tgz?cache=0&sync_timestamp=1580310064722&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypedarray-to-buffer%2Fdownload%2Ftypedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080",
"typescript@^3.8.3": "https://registry.npm.taobao.org/typescript/download/typescript-3.8.3.tgz?cache=0&sync_timestamp=1584688517009&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061", "typescript@^3.8.3": "https://registry.npm.taobao.org/typescript/download/typescript-3.8.3.tgz?cache=0&sync_timestamp=1584688517009&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061",
+2 -1
View File
@@ -26,7 +26,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^25.1.4", "@types/jest": "^25.1.4",
"@types/node": "^13.9.8", "@types/node": "^14.0.5",
"@types/semver": "^7.1.0", "@types/semver": "^7.1.0",
"@typescript-eslint/eslint-plugin": "^2.26.0", "@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0", "@typescript-eslint/parser": "^2.26.0",
@@ -37,6 +37,7 @@
"prettier": "^2.0.2", "prettier": "^2.0.2",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"ts-jest": "^25.3.0", "ts-jest": "^25.3.0",
"type-fest": "^0.14.0",
"typescript": "^3.8.3" "typescript": "^3.8.3"
} }
} }
+5 -10
View File
@@ -2,17 +2,12 @@ import { exec } from '@actions/exec';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import { Sha, RefDic } from './interfaces';
function makeOpt(ref: string): { cwd: string } { function makeOpt(ref: Sha): { cwd: string } {
return { cwd: path.join(os.tmpdir(), `xmake-git-${ref}`) }; return { cwd: path.join(os.tmpdir(), `xmake-git-${ref}`) };
} }
export type RefDic = {
heads: Record<string, string>;
tags: Record<string, string>;
pull: Record<number, { head?: string; merge?: string }>;
};
export async function lsRemote(): Promise<RefDic> { export async function lsRemote(): Promise<RefDic> {
let out = ''; let out = '';
await exec('git', ['ls-remote', 'https://github.com/xmake-io/xmake.git'], { await exec('git', ['ls-remote', 'https://github.com/xmake-io/xmake.git'], {
@@ -42,19 +37,19 @@ export async function lsRemote(): Promise<RefDic> {
return data; return data;
} }
export async function create(ref: string): Promise<string> { export async function create(ref: Sha): Promise<string> {
const opt = makeOpt(ref); const opt = makeOpt(ref);
await io.rmRF(opt.cwd); await io.rmRF(opt.cwd);
await io.mkdirP(opt.cwd); await io.mkdirP(opt.cwd);
await exec('git', ['init'], opt); await exec('git', ['init'], opt);
await exec('git', ['remote', 'add', 'origin', 'https://github.com/xmake-io/xmake.git'], opt); await exec('git', ['remote', 'add', 'origin', 'https://github.com/xmake-io/xmake.git'], opt);
await exec('git', ['fetch'], opt); await exec('git', ['fetch', 'origin', '+refs/pull/*:refs/remotes/origin/pull/*', '+refs/heads/*:refs/remotes/origin/*'], opt);
await exec('git', ['checkout', ref], opt); await exec('git', ['checkout', ref], opt);
await exec('git', ['submodule', 'update', '--init', '--recursive'], opt); await exec('git', ['submodule', 'update', '--init', '--recursive'], opt);
return opt.cwd; return opt.cwd;
} }
export async function cleanup(ref: string): Promise<void> { export async function cleanup(ref: Sha): Promise<void> {
const opt = makeOpt(ref); const opt = makeOpt(ref);
await io.rmRF(opt.cwd); await io.rmRF(opt.cwd);
} }
+33
View File
@@ -0,0 +1,33 @@
import { Opaque } from 'type-fest';
export type Sha = Opaque<string, 'sha'>;
export function Sha(sha: string): Sha {
sha = sha.trim().toLowerCase();
if (!/^[a-f0-9]{40}$/g.test(sha)) throw new Error(`Invalid sha value ${sha}`);
return sha as Sha;
}
export type RefDic = {
/** branches */
heads: Record<string, Sha>;
/** tags */
tags: Record<string, Sha>;
/** pull requests */
pull: Record<
number,
{
/** the current state of the pull request */
head: Sha;
/** the current branch we're merging onto */
base?: Sha;
/** merge result */
merge?: Sha;
}
>;
};
export interface Version {
version: string;
sha: Sha;
type: keyof RefDic | 'sha';
}
+1 -1
View File
@@ -6,7 +6,7 @@ import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import * as git from './git'; import * as git from './git';
import { Version } from './versions'; import { Version } from './interfaces';
export async function unixInstall(version: Version): Promise<void> { export async function unixInstall(version: Version): Promise<void> {
const ver = version.version; const ver = version.version;
+61 -1
View File
@@ -302,7 +302,7 @@ describe('selectVersion', () => {
it('should return correct pr', async () => { it('should return correct pr', async () => {
await expect(selectVersion('pr@708')).resolves.toEqual({ await expect(selectVersion('pr@708')).resolves.toEqual({
version: '#708', version: 'pr#708',
sha: 'af28dba1f52990cb7b6c3c8f69f1f1bcf017c90a', sha: 'af28dba1f52990cb7b6c3c8f69f1f1bcf017c90a',
type: 'pull', type: 'pull',
}); });
@@ -315,4 +315,64 @@ describe('selectVersion', () => {
it('should throw invalid pr', async () => { it('should throw invalid pr', async () => {
await expect(selectVersion('pr@xxx')).rejects.toThrowError('Invalid pull requrest xxx, should be a positive integer'); await expect(selectVersion('pr@xxx')).rejects.toThrowError('Invalid pull requrest xxx, should be a positive integer');
}); });
it('should return branch of sha', async () => {
await expect(selectVersion('sha@efad01e547f30d66d90e486c91c3afa0dbaceed3')).resolves.toEqual({
version: 'master',
sha: 'efad01e547f30d66d90e486c91c3afa0dbaceed3',
type: 'heads',
});
});
it('should return tag of sha', async () => {
await expect(selectVersion('sha@31bacc4f76101e6a865ec254c48d8bcba456378f')).resolves.toEqual({
version: 'v2.3.2',
sha: '31bacc4f76101e6a865ec254c48d8bcba456378f',
type: 'tags',
});
});
it('should return pr merge of sha', async () => {
await expect(selectVersion('sha@2f93475049575ed5e44699daa378239bc141aad3')).resolves.toEqual({
version: 'pr#217',
sha: '2f93475049575ed5e44699daa378239bc141aad3',
type: 'pull',
});
});
it('should not return pr head of sha if merge present', async () => {
await expect(selectVersion('sha@905a8607f3f31ab9efeb68684d40df8284683f3a')).resolves.toEqual({
version: 'sha#905a8607f3f31ab9efeb68684d40df8284683f3a',
sha: '905a8607f3f31ab9efeb68684d40df8284683f3a',
type: 'sha',
});
});
it('should return pr head of sha', async () => {
await expect(selectVersion('sha@af28dba1f52990cb7b6c3c8f69f1f1bcf017c90a')).resolves.toEqual({
version: 'pr#708',
sha: 'af28dba1f52990cb7b6c3c8f69f1f1bcf017c90a',
type: 'pull',
});
});
it('should return correct normalized sha', async () => {
await expect(selectVersion('sha@Af28dba1f52990cb7b6c3c8f69f1f1bcf017c90b')).resolves.toEqual({
version: 'sha#af28dba1f52990cb7b6c3c8f69f1f1bcf017c90b',
sha: 'af28dba1f52990cb7b6c3c8f69f1f1bcf017c90b',
type: 'sha',
});
});
it('should throw invalid length sha', async () => {
await expect(selectVersion('sha@af28dba1f52990cb7b6c3c8f69f1f1bcf017c90')).rejects.toThrowError(
'Invalid sha value af28dba1f52990cb7b6c3c8f69f1f1bcf017c90',
);
});
it('should throw invalid char sha', async () => {
await expect(selectVersion('sha@af28dba1f52990cb7b6c3c8f69f1f1bcf017c90g')).rejects.toThrowError(
'Invalid sha value af28dba1f52990cb7b6c3c8f69f1f1bcf017c90g',
);
});
}); });
+39 -12
View File
@@ -1,15 +1,17 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as semver from 'semver'; import * as semver from 'semver';
import { lsRemote, RefDic } from './git'; import { lsRemote } from './git';
import { RefDic, Sha, Version } from './interfaces';
export interface Version { let VERSIONS: RefDic | undefined;
version: string;
sha: string; async function getVersions(): Promise<RefDic> {
type: keyof RefDic; if (VERSIONS) return VERSIONS;
return (VERSIONS = await lsRemote());
} }
class VersionImpl implements Version { class VersionImpl implements Version {
constructor(readonly version: string, readonly sha: string, readonly type: keyof RefDic, toString: string) { constructor(readonly version: string, readonly sha: Sha, readonly type: Version['type'], toString: string) {
this.#string = toString; this.#string = toString;
} }
readonly #string: string; readonly #string: string;
@@ -19,7 +21,7 @@ class VersionImpl implements Version {
} }
async function selectBranch(branch: string): Promise<Version> { async function selectBranch(branch: string): Promise<Version> {
const versions = await lsRemote(); const versions = await getVersions();
if (branch in versions.heads) { if (branch in versions.heads) {
return new VersionImpl(branch, versions.heads[branch], 'heads', `branch ${branch}`); return new VersionImpl(branch, versions.heads[branch], 'heads', `branch ${branch}`);
} }
@@ -27,12 +29,12 @@ async function selectBranch(branch: string): Promise<Version> {
} }
async function selectPr(pr: number): Promise<Version> { async function selectPr(pr: number): Promise<Version> {
const versions = await lsRemote(); const versions = await getVersions();
if (pr in versions.pull) { if (pr in versions.pull) {
const prheads = versions.pull[pr]; const prheads = versions.pull[pr];
const sha = prheads.merge ?? prheads.head; const sha = prheads.merge ?? prheads.head;
if (sha) { if (sha) {
return new VersionImpl(`#${pr}`, sha, 'pull', `pull request #${pr}`); return new VersionImpl(`pr#${pr}`, sha, 'pull', `pull request #${pr}`);
} }
} }
throw new Error(`Pull requrest #${pr} not found`); throw new Error(`Pull requrest #${pr} not found`);
@@ -45,7 +47,7 @@ async function selectSemver(version: string): Promise<Version> {
throw new Error(`Invalid semver`); throw new Error(`Invalid semver`);
} }
const versions = await lsRemote(); const versions = await getVersions();
const ver = semver.maxSatisfying(Object.keys(versions.tags), v); const ver = semver.maxSatisfying(Object.keys(versions.tags), v);
if (!ver) { if (!ver) {
throw new Error(`No matched releases of xmake-version ${v.format()}`); throw new Error(`No matched releases of xmake-version ${v.format()}`);
@@ -55,6 +57,28 @@ async function selectSemver(version: string): Promise<Version> {
return new VersionImpl(ver, sha, 'tags', ver); return new VersionImpl(ver, sha, 'tags', ver);
} }
async function selectSha(sha: string): Promise<Version> {
const shaValue = Sha(sha);
const versions = await getVersions();
for (const branch in versions.heads) {
if (versions.heads[branch] === shaValue) {
return selectBranch(branch);
}
}
for (const tag in versions.tags) {
if (versions.tags[tag] === shaValue) {
return selectSemver(tag);
}
}
for (const pr in versions.pull) {
const prData = versions.pull[pr];
if ((prData.merge ?? prData.head) === shaValue) {
return selectPr(Number.parseInt(pr));
}
}
return Promise.resolve(new VersionImpl(`sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`));
}
export async function selectVersion(version?: string): Promise<Version> { export async function selectVersion(version?: string): Promise<Version> {
// get version string // get version string
version = (version ?? core.getInput('xmake-version')) || 'latest'; version = (version ?? core.getInput('xmake-version')) || 'latest';
@@ -66,7 +90,6 @@ export async function selectVersion(version?: string): Promise<Version> {
const branch = version.substr('branch@'.length); const branch = version.substr('branch@'.length);
ret = await selectBranch(branch); ret = await selectBranch(branch);
} }
// select pr // select pr
if (version.startsWith('pr@')) { if (version.startsWith('pr@')) {
const pr = Number.parseInt(version.substr('pr@'.length)); const pr = Number.parseInt(version.substr('pr@'.length));
@@ -75,6 +98,11 @@ export async function selectVersion(version?: string): Promise<Version> {
} }
ret = await selectPr(pr); ret = await selectPr(pr);
} }
// select sha
if (version.startsWith('sha@')) {
const sha = version.substr('sha@'.length);
ret = await selectSha(sha);
}
// select version // select version
if (semver.validRange(version)) { if (semver.validRange(version)) {
ret = await selectSemver(version); ret = await selectSemver(version);
@@ -82,7 +110,6 @@ export async function selectVersion(version?: string): Promise<Version> {
if (!ret) { if (!ret) {
throw new Error(`Invalid input xmake-version ${core.getInput('xmake-version')}`); throw new Error(`Invalid input xmake-version ${core.getInput('xmake-version')}`);
} }
core.info(`Selected xmake ${ret} (commit: ${ret.sha.substr(0, 8)})`); core.info(`Selected xmake ${ret} (commit: ${ret.sha.substr(0, 8)})`);
return ret; return ret;
} }
+32 -15
View File
@@ -5,27 +5,44 @@ import * as toolCache from '@actions/tool-cache';
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import { Version } from './versions'; import { Version } from './interfaces';
function getInstallerUrl(version: Version): string {
const ver = version.version;
switch (version.type) {
case 'heads': {
// we only use appveyor ci artifacts for branch version
const arch = os.arch() === 'x64' ? 'x64' : 'x86';
return `https://ci.appveyor.com/api/projects/waruqi/xmake/artifacts/xmake-installer.exe?branch=${ver}&pr=false&job=Image%3A+Visual+Studio+2017%3B+Platform%3A+${arch}`;
}
case 'pull': {
throw new Error('PR builds for windows is not supported');
}
case 'sha': {
throw new Error('Sha builds for windows is not supported');
}
case 'tags': {
// we cannot use appveyor ci artifacts, the old version links may be broken.
const arch = os.arch() === 'x64' ? 'win64' : 'win32';
return semver.gt(ver, '2.2.6')
? `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.${arch}.exe`
: `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.exe`;
}
default: {
// check that we have tested all types
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: never = version.type;
throw new Error('Unknown version type');
}
}
}
export async function winInstall(version: Version): Promise<void> { export async function winInstall(version: Version): Promise<void> {
const ver = version.version; const ver = version.version;
let toolDir = toolCache.find('xmake', ver); let toolDir = toolCache.find('xmake', ver);
if (!toolDir) { if (!toolDir) {
const installer = await core.group(`download xmake ${version}`, async () => { const installer = await core.group(`download xmake ${version}`, async () => {
let url = ''; const url = getInstallerUrl(version);
if (version.type === 'heads') {
// we only use appveyor ci artifacts for branch version
const arch = os.arch() === 'x64' ? 'x64' : 'x86';
url = `https://ci.appveyor.com/api/projects/waruqi/xmake/artifacts/xmake-installer.exe?branch=${ver}&pr=false&job=Image%3A+Visual+Studio+2017%3B+Platform%3A+${arch}`;
} else if (version.type === 'pull') {
throw new Error('PR builds for windows is not supported');
} else {
// we cannot use appveyor ci artifacts, the old version links may be broken.
const arch = os.arch() === 'x64' ? 'win64' : 'win32';
url = semver.gt(ver, '2.2.6')
? `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.${arch}.exe`
: `https://github.com/xmake-io/xmake/releases/download/${ver}/xmake-${ver}.exe`;
}
core.info(`downloading from ${url}`); core.info(`downloading from ${url}`);
const file = await toolCache.downloadTool(url); const file = await toolCache.downloadTool(url);
const exe = path.format({ ...path.parse(file), ext: '.exe', base: undefined }); const exe = path.format({ ...path.parse(file), ext: '.exe', base: undefined });
+11 -12
View File
@@ -1,13 +1,12 @@
{ {
"include": [ "include": ["src/**/*"],
"src/**/*" "compileOnSave": true,
], "compilerOptions": {
"compileOnSave": true, "strict": true,
"compilerOptions": { "moduleResolution": "node",
"strict": true, "module": "commonjs",
"moduleResolution": "node", "target": "es2018",
"module": "commonjs", "outDir": "dist",
"target": "es2018", "types": ["node", "jest"]
"outDir": "dist" }
} }
}
+9 -4
View File
@@ -457,10 +457,10 @@
resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.2.tgz?cache=0&sync_timestamp=1584566829623&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349" resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.2.tgz?cache=0&sync_timestamp=1584566829623&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349"
integrity sha1-rOGIDANZTMPoAgbZaEcVfY5/o0k= integrity sha1-rOGIDANZTMPoAgbZaEcVfY5/o0k=
"@types/node@^13.9.8": "@types/node@^14.0.5":
version "13.9.8" version "14.0.5"
resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.8.tgz#09976420fc80a7a00bf40680c63815ed8c7616f4" resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-14.0.5.tgz?cache=0&sync_timestamp=1590092201933&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-14.0.5.tgz#3d03acd3b3414cf67faf999aed11682ed121f22b"
integrity sha1-CZdkIPyAp6AL9AaAxjgV7Yx2FvQ= integrity sha1-PQOs07NBTPZ/r5ma7RFoLtEh8is=
"@types/prettier@^1.19.0": "@types/prettier@^1.19.0":
version "1.19.1" version "1.19.1"
@@ -3650,6 +3650,11 @@ type-fest@^0.11.0:
resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1" resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1"
integrity sha1-l6vwhyMQ/tiKXEZrJWgVdhReM/E= integrity sha1-l6vwhyMQ/tiKXEZrJWgVdhReM/E=
type-fest@^0.14.0:
version "0.14.0"
resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.14.0.tgz#758d800f2dcc0de47a946a120d91ab17883523ad"
integrity sha1-dY2ADy3MDeR6lGoSDZGrF4g1I60=
type-fest@^0.8.1: type-fest@^0.8.1:
version "0.8.1" version "0.8.1"
resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d" resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"