use ls-remote

This commit is contained in:
OpportunityLiu
2020-03-20 20:36:16 +08:00
Unverified
parent 156429646f
commit 81726402f6
207 changed files with 2057 additions and 23449 deletions
+35 -18
View File
@@ -1,22 +1,39 @@
import { exec } from '@actions/exec'
import * as io from '@actions/io'
import * as os from 'os'
import * as path from 'path'
import { exec } from '@actions/exec';
import * as io from '@actions/io';
import * as os from 'os';
import * as path from 'path';
export const folder = path.join(os.tmpdir(), `xmake${Date.now()}`)
const opt = { cwd: folder }
export const folder = path.join(os.tmpdir(), `xmake${Date.now()}`);
const opt = { cwd: folder };
export async function create(ref: string) {
await io.rmRF(folder)
await io.mkdirP(folder)
await exec('git', ['init'], opt)
await exec('git', ['remote', 'add', 'origin', 'https://github.com/xmake-io/xmake.git'], opt)
await exec('git', ['fetch'], opt)
await exec('git', ['checkout', ref], opt)
await exec('git', ['submodule', 'update', '--init', '--recursive'], opt)
return folder
export async function lsRemote(): Promise<Record<string, string>> {
let out = '';
await exec('git', ['ls-remote', '--tags', 'https://github.com/xmake-io/xmake.git'], {
listeners: {
stdout: d => (out += d.toString()),
},
});
const data: Record<string, string> = {};
out.split('\n').forEach(line => {
const [ref, tag] = line.trim().split('\t');
if (ref && tag && tag.startsWith('refs/tags/v')) {
data[tag.substring('refs/tags/v'.length)] = ref;
}
});
return data;
}
export async function cleanup() {
await io.rmRF(folder)
}
export async function create(ref: string): Promise<string> {
await io.rmRF(folder);
await io.mkdirP(folder);
await exec('git', ['init'], opt);
await exec('git', ['remote', 'add', 'origin', 'https://github.com/xmake-io/xmake.git'], opt);
await exec('git', ['fetch'], opt);
await exec('git', ['checkout', ref], opt);
await exec('git', ['submodule', 'update', '--init', '--recursive'], opt);
return folder;
}
export async function cleanup(): Promise<void> {
await io.rmRF(folder);
}
+54 -56
View File
@@ -1,72 +1,70 @@
import * as core from '@actions/core'
import { exec } from '@actions/exec'
import * as _fs from 'fs'
const fs = _fs.promises
import * as io from '@actions/io'
import * as toolCache from '@actions/tool-cache'
import * as os from 'os'
import * as path from 'path'
import * as semver from 'semver'
import * as git from './git'
import { selectVersion } from './versions'
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import * as _fs from 'fs';
const fs = _fs.promises;
import * as io from '@actions/io';
import * as toolCache from '@actions/tool-cache';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as git from './git';
import { selectVersion } from './versions';
async function winInstall(version: string) {
let toolDir = toolCache.find('xmake', version)
async function winInstall(version: string): Promise<void> {
let toolDir = toolCache.find('xmake', version);
if (!toolDir) {
const installer = await core.group("download xmake", async () => {
const arch = os.arch() === 'x64' ? 'x64' : 'x86'
const installer = await core.group('download xmake', async () => {
const arch = os.arch() === 'x64' ? 'x64' : 'x86';
const url = semver.gt(version, '2.2.6')
? `https://ci.appveyor.com/api/projects/waruqi/xmake/artifacts/xmake-installer.exe?tag=v${version}&pr=false&job=Image%3A+Visual+Studio+2017%3B+Platform%3A+${arch}`
: `https://github.com/xmake-io/xmake/releases/download/v$v/xmake-v${version}.exe`
core.info(`downloading from ${url}`)
const file = await toolCache.downloadTool(url)
const exe = path.format({ ...path.parse(file), ext: '.exe', base: undefined })
await fs.rename(file, exe)
core.info(`downloaded to ${exe}`)
return exe
})
toolDir = await core.group("install xmake", async () => {
const binDir = path.join(os.tmpdir(), `xmake-${version}`)
core.info(`installing to ${binDir}`)
await exec(`"${installer}" /NOADMIN /S /D=${binDir}`)
core.info(`installed to ${binDir}`)
const cacheDir = await toolCache.cacheDir(binDir, 'xmake', version)
await io.rmRF(binDir)
await io.rmRF(installer)
return cacheDir
})
: `https://github.com/xmake-io/xmake/releases/download/v$v/xmake-v${version}.exe`;
core.info(`downloading from ${url}`);
const file = await toolCache.downloadTool(url);
const exe = path.format({ ...path.parse(file), ext: '.exe', base: undefined });
await fs.rename(file, exe);
core.info(`downloaded to ${exe}`);
return exe;
});
toolDir = await core.group('install xmake', async () => {
const binDir = path.join(os.tmpdir(), `xmake-${version}`);
core.info(`installing to ${binDir}`);
await exec(`"${installer}" /NOADMIN /S /D=${binDir}`);
core.info(`installed to ${binDir}`);
const cacheDir = await toolCache.cacheDir(binDir, 'xmake', version);
await io.rmRF(binDir);
await io.rmRF(installer);
return cacheDir;
});
}
core.addPath(toolDir)
core.addPath(toolDir);
}
async function unixInstall(version: string, sha: string) {
let toolDir = toolCache.find('xmake', version)
async function unixInstall(version: string, sha: string): Promise<void> {
let toolDir = toolCache.find('xmake', version);
if (!toolDir) {
const sourceDir = await core.group("download xmake", () => git.create(sha))
toolDir = await core.group("install xmake", async () => {
await exec('make', ['build'], { cwd: sourceDir })
const binDir = path.join(os.tmpdir(), `xmake-${version}-${sha}`)
await exec('make', ['install', `prefix=${binDir}`], { cwd: sourceDir })
const cacheDir = await toolCache.cacheDir(binDir, 'xmake', version)
await io.rmRF(binDir)
await git.cleanup()
return cacheDir
})
const sourceDir = await core.group('download xmake', () => git.create(sha));
toolDir = await core.group('install xmake', async () => {
await exec('make', ['build'], { cwd: sourceDir });
const binDir = path.join(os.tmpdir(), `xmake-${version}-${sha}`);
await exec('make', ['install', `prefix=${binDir}`], { cwd: sourceDir });
const cacheDir = await toolCache.cacheDir(binDir, 'xmake', version);
await io.rmRF(binDir);
await git.cleanup();
return cacheDir;
});
}
core.addPath(path.join(toolDir, 'share', 'xmake'))
core.addPath(path.join(toolDir, 'share', 'xmake'));
}
async function run() {
async function run(): Promise<void> {
try {
const { version, sha } = await selectVersion()
if (os.platform() === 'win32' || os.platform() === 'cygwin')
await winInstall(version)
else
await unixInstall(version, sha)
await exec('xmake --version')
const { version, sha } = await selectVersion();
if (os.platform() === 'win32' || os.platform() === 'cygwin') await winInstall(version);
else await unixInstall(version, sha);
await exec('xmake --version');
} catch (error) {
core.setFailed(error.message)
core.setFailed(error.message);
}
}
run()
run().catch(e => core.error(e));
+19 -48
View File
@@ -1,51 +1,22 @@
import * as core from "@actions/core";
import * as semver from "semver";
import { downloadTool } from "@actions/tool-cache";
import * as _fs from "fs";
const fs = _fs.promises;
import * as core from '@actions/core';
import * as semver from 'semver';
import { lsRemote } from './git';
type TagItem = {
ref: string;
node_id: string;
url: string;
object: {
sha: string;
type: string;
url: string;
};
};
export async function selectVersion(version?: string): Promise<{ version: string; sha: string }> {
version = (version ?? core.getInput('xmake-version')) || 'latest';
if (version.toLowerCase() === 'latest') version = '';
version = semver.validRange(version);
if (!version) {
throw new Error(`Invalid input xmake-version: ${core.getInput('xmake-version')}`);
}
export async function fetchVersions() {
const file = await downloadTool("https://api.github.com/repos/xmake-io/xmake/git/refs/tags");
const tags: [TagItem] = JSON.parse(await fs.readFile(file, { encoding: "utf-8" }));
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) {
version = version || core.getInput("xmake-version") || "latest";
if (version.toLowerCase() === "latest") version = "";
version = semver.validRange(version);
if (!version) {
throw new Error(
`Invalid input xmake-version: ${core.getInput("xmake-version")}`
);
}
const versions = await fetchVersions();
const ver = semver.maxSatisfying(Object.keys(versions), version);
if (!ver) {
throw new Error(
`No matched releases of xmake-version: ${version}`
);
}
const sha = versions[ver];
core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`);
return { version: ver, sha };
const versions = await lsRemote();
const ver = semver.maxSatisfying(Object.keys(versions), version);
if (!ver) {
throw new Error(`No matched releases of xmake-version: ${version}`);
}
const sha = versions[ver];
core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`);
return { version: ver, sha };
}