win support

This commit is contained in:
Opportunity
2019-09-30 22:20:31 +08:00 Unverified
parent a363ea93aa
commit 2ffbdbc518
338 changed files with 113 additions and 73316 deletions
+42 -56
View File
@@ -6,69 +6,55 @@ const toolCache = require('@actions/tool-cache')
const os = require('os')
const path = require('path')
const semver = require('semver')
const octokit = require('@octokit/rest')
const git = require('./git')
const selectVersion = require('./selectVersion')
const xmakeRepo = { owner: 'xmake-io', repo: 'xmake' }
async function fetchVersions() {
const api = new octokit()
const tags = await api.repos.listTags({ ...xmakeRepo, per_page: 100 })
/**
* @type {Object.<string, string>}
*/
const versions = {}
tags.data.forEach(tag => {
const ver = semver.clean(tag.name)
if (ver) {
versions[ver] = tag.commit.sha
}
})
return versions
async function winInstall(version) {
let toolDir = toolCache.find('xmake', version)
if (!toolDir) {
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`
return await toolCache.downloadTool(url)
})
toolDir = await core.group("install xmake", async () => {
const binDir = path.join(os.tmpdir(), `xmake-${version}`)
await exec(`"${installer}" /S /S=${binDir}`)
const cacheDir = await toolCache.cacheDir(binDir, 'xmake', version)
await io.rmRF(binDir)
await io.rmRF(installer)
return cacheDir
})
}
core.addPath(toolDir)
}
async function selectVersion() {
let 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 }
}
async function download(sha) {
const folder = await fs.mkdtemp(path.join(os.tmpdir(), 'xmake'))
const opt = { cwd: 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', sha], opt)
await exec('git', ['submodule', 'update', '--init', '--recursive'], opt)
return folder
async function unixInstall(version, sha) {
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
})
}
core.addPath(path.join(toolDir, 'share', 'xmake'))
}
async function run() {
try {
const { version, sha } = await selectVersion()
let toolDir = toolCache.find('xmake', version)
if (!toolDir) {
const sourceDir = await core.group("download xmake", () => download(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 io.rmRF(sourceDir)
return cacheDir
})
}
core.addPath(path.join(toolDir, 'share', 'xmake'))
const { version, sha } = 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)