support other repo

This commit is contained in:
Opportunity
2020-06-30 01:54:04 +08:00
Unverified
parent 2feaa3232f
commit 48f33e8789
66 changed files with 3280 additions and 1532 deletions
Vendored
+12 -6
View File
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanup = exports.create = exports.lsRemote = void 0;
const exec_1 = require("@actions/exec");
const io = require("@actions/io");
const os = require("os");
@@ -7,9 +8,10 @@ const path = require("path");
function makeOpt(ref) {
return { cwd: path.join(os.tmpdir(), `xmake-git-${ref}`) };
}
async function lsRemote() {
const repoUrl = (repo) => `https://github.com/${repo}.git`;
async function lsRemote(repo) {
let out = '';
await exec_1.exec('git', ['ls-remote', 'https://github.com/xmake-io/xmake.git'], {
await exec_1.exec('git', ['ls-remote', repoUrl(repo)], {
silent: true,
listeners: {
stdout: (d) => (out += d.toString()),
@@ -21,28 +23,32 @@ async function lsRemote() {
if (ref && tag && tag.startsWith('refs/')) {
const tagPath = tag.split('/').splice(1);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let ldata = data;
let ldata = data; // eslint-disable-line @typescript-eslint/no-unsafe-assignment
for (let i = 0; i < tagPath.length - 1; i++) {
const seg = tagPath[i];
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (typeof ldata[seg] === 'object') {
ldata = ldata[seg];
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ldata = ldata[seg]; // eslint-disable-line @typescript-eslint/no-unsafe-assignment
}
else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ldata = ldata[seg] = {};
}
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ldata[tagPath[tagPath.length - 1]] = ref;
}
});
return data;
}
exports.lsRemote = lsRemote;
async function create(ref) {
async function create(repo, ref) {
const opt = makeOpt(ref);
await io.rmRF(opt.cwd);
await io.mkdirP(opt.cwd);
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', repoUrl(repo)], 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', ['submodule', 'update', '--init', '--recursive'], opt);
+2 -1
View File
@@ -16,7 +16,8 @@ async function run() {
await exec_1.exec('xmake --version');
}
catch (error) {
core.setFailed(error.message);
const ex = error;
core.setFailed(ex.message);
}
}
run().catch((e) => core.error(e));
+8
View File
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Repo = exports.Sha = void 0;
function Sha(sha) {
sha = sha.trim().toLowerCase();
if (!/^[a-f0-9]{40}$/g.test(sha))
@@ -7,3 +8,10 @@ function Sha(sha) {
return sha;
}
exports.Sha = Sha;
function Repo(repo) {
repo = repo.trim();
if (!/^([^/#]+\/[^/#]+)$/g.test(repo))
throw new Error(`Invalid repo value ${repo}`);
return repo;
}
exports.Repo = Repo;
+3 -2
View File
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unixInstall = void 0;
const core = require("@actions/core");
const exec_1 = require("@actions/exec");
const io = require("@actions/io");
@@ -12,8 +13,8 @@ async function unixInstall(version) {
const ver = version.version;
let toolDir = toolCache.find('xmake', ver);
if (!toolDir) {
const sourceDir = await core.group(`download xmake ${version}`, () => git.create(version.sha));
toolDir = await core.group(`install xmake ${version}`, async () => {
const sourceDir = await core.group(`download xmake ${String(version)}`, () => git.create(version.repo, version.sha));
toolDir = await core.group(`install xmake ${String(version)}`, async () => {
await exec_1.exec('make', ['build'], { cwd: sourceDir });
const binDir = path.join(os.tmpdir(), `xmake-${version.sha}`);
await exec_1.exec('make', ['install', `prefix=${binDir}`], { cwd: sourceDir });
+39 -26
View File
@@ -14,18 +14,23 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
};
var _string;
Object.defineProperty(exports, "__esModule", { value: true });
exports.selectVersion = void 0;
const core = require("@actions/core");
const semver = require("semver");
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());
const VERSIONS = new Map();
async function getVersions(repo) {
const cache = VERSIONS.get(repo);
if (cache)
return cache;
const result = await git_1.lsRemote(repo);
VERSIONS.set(repo, result);
return result;
}
class VersionImpl {
constructor(version, sha, type, toString) {
constructor(repo, version, sha, type, toString) {
this.repo = repo;
this.version = version;
this.sha = sha;
this.type = type;
@@ -37,71 +42,79 @@ class VersionImpl {
}
}
_string = new WeakMap();
async function selectBranch(branch) {
const versions = await getVersions();
async function selectBranch(repo, branch) {
const versions = await getVersions(repo);
if (branch in versions.heads) {
return new VersionImpl(branch, versions.heads[branch], 'heads', `branch ${branch}`);
return new VersionImpl(repo, branch, versions.heads[branch], 'heads', `branch ${branch}`);
}
throw new Error(`Branch ${branch} not found`);
}
async function selectPr(pr) {
async function selectPr(repo, pr) {
var _a;
const versions = await getVersions();
const versions = await getVersions(repo);
if (pr in versions.pull) {
const prheads = versions.pull[pr];
const sha = (_a = prheads.merge) !== null && _a !== void 0 ? _a : prheads.head;
if (sha) {
return new VersionImpl(`pr#${pr}`, sha, 'pull', `pull request #${pr}`);
return new VersionImpl(repo, `pr#${pr}`, sha, 'pull', `pull request #${pr}`);
}
}
throw new Error(`Pull requrest #${pr} not found`);
}
async function selectSemver(version) {
async function selectSemver(repo, version) {
// check version valid
const v = new semver.Range(version);
if (!v) {
throw new Error(`Invalid semver`);
}
const versions = await getVersions();
const versions = await getVersions(repo);
const ver = semver.maxSatisfying(Object.keys(versions.tags), v);
if (!ver) {
throw new Error(`No matched releases of xmake-version ${v.format()}`);
}
const sha = versions.tags[ver];
return new VersionImpl(ver, sha, 'tags', ver);
return new VersionImpl(repo, ver, sha, 'tags', ver);
}
async function selectSha(sha) {
async function selectSha(repo, sha) {
var _a;
const shaValue = interfaces_1.Sha(sha);
const versions = await getVersions();
const versions = await getVersions(repo);
for (const branch in versions.heads) {
if (versions.heads[branch] === shaValue) {
return selectBranch(branch);
return selectBranch(repo, branch);
}
}
for (const tag in versions.tags) {
if (versions.tags[tag] === shaValue) {
return selectSemver(tag);
return selectSemver(repo, 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 selectPr(repo, Number.parseInt(pr));
}
}
return Promise.resolve(new VersionImpl(`sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`));
return Promise.resolve(new VersionImpl(repo, `sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`));
}
async function selectVersion(version) {
// get version string
version = (version !== null && version !== void 0 ? version : core.getInput('xmake-version')) || 'latest';
if (version.toLowerCase() === 'latest')
version = '';
let repo = interfaces_1.Repo('xmake-io/xmake');
let ret;
{
const match = /^([^/#]+\/[^/#]+)#(.+)$/.exec(version);
if (match) {
repo = interfaces_1.Repo(match[1]);
version = match[2];
}
}
// select branch
if (version.startsWith('branch@')) {
const branch = version.substr('branch@'.length);
ret = await selectBranch(branch);
ret = await selectBranch(repo, branch);
}
// select pr
if (version.startsWith('pr@')) {
@@ -109,21 +122,21 @@ async function selectVersion(version) {
if (Number.isNaN(pr) || pr <= 0) {
throw new Error(`Invalid pull requrest ${version.substr('pr@'.length)}, should be a positive integer`);
}
ret = await selectPr(pr);
ret = await selectPr(repo, pr);
}
// select sha
if (version.startsWith('sha@')) {
const sha = version.substr('sha@'.length);
ret = await selectSha(sha);
ret = await selectSha(repo, sha);
}
// select version
if (semver.validRange(version)) {
ret = await selectSemver(version);
ret = await selectSemver(repo, version);
}
if (!ret) {
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 ${String(ret)} (commit: ${ret.sha.substr(0, 8)})`);
return ret;
}
exports.selectVersion = selectVersion;
+3 -2
View File
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.winInstall = void 0;
const core = require("@actions/core");
const exec_1 = require("@actions/exec");
const io = require("@actions/io");
@@ -40,7 +41,7 @@ async function winInstall(version) {
const ver = version.version;
let toolDir = toolCache.find('xmake', ver);
if (!toolDir) {
const installer = await core.group(`download xmake ${version}`, async () => {
const installer = await core.group(`download xmake ${String(version)}`, async () => {
const url = getInstallerUrl(version);
core.info(`downloading from ${url}`);
const file = await toolCache.downloadTool(url);
@@ -49,7 +50,7 @@ async function winInstall(version) {
core.info(`downloaded to ${exe}`);
return exe;
});
toolDir = await core.group(`install xmake ${version}`, async () => {
toolDir = await core.group(`install xmake ${String(version)}`, async () => {
const binDir = path.join(os.tmpdir(), `xmake-${version.sha}`);
core.info(`installing to ${binDir}`);
await exec_1.exec(`"${installer}" /NOADMIN /S /D=${binDir}`);