This commit is contained in:
Opportunity
2019-09-30 20:48:18 +08:00
Unverified
parent 2cf62ef15d
commit 1f9d39e73c
3 changed files with 35 additions and 34 deletions
+32 -33
View File
@@ -1,6 +1,7 @@
const core = require('@actions/core') const core = require('@actions/core')
const exec = require('@actions/exec').exec const exec = require('@actions/exec').exec
const fs = require('fs').promises const fs = require('fs').promises
const io = require('@actions/io')
const toolCache = require('@actions/tool-cache') const toolCache = require('@actions/tool-cache')
const os = require('os') const os = require('os')
const path = require('path') const path = require('path')
@@ -25,6 +26,21 @@ async function fetchVersions() {
return versions return versions
} }
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 ${ver} (commit: ${sha.substr(0, 8)})`)
return { version: ver, sha }
}
async function download(sha) { async function download(sha) {
const folder = await fs.mkdtemp(path.join(os.tmpdir(), 'xmake')) const folder = await fs.mkdtemp(path.join(os.tmpdir(), 'xmake'))
const opt = { cwd: folder } const opt = { cwd: folder }
@@ -36,41 +52,24 @@ async function download(sha) {
return folder return folder
} }
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}`)
core.info(`Selected xmake ${ver}`)
core.debug(`SHA: ${versions[ver]}`)
return { version, sha: versions[ver] }
}
async function run() { async function run() {
try { try {
let version = '' const { version, sha } = await selectVersion()
let sha = '' let toolDir = toolCache.find('xmake', version)
const folder = await core.group("download xmake", async () => { if (!toolDir) {
const v = await selectVersion() const sourceDir = await core.group("download xmake", () => download(sha))
version = v.version toolDir = await core.group("install xmake", async () => {
sha = v.sha await exec('make', ['build'], { cwd: sourceDir })
return await download(sha) const binDir = path.join(os.tmpdir(), `xmake-${version}-${sha}`)
}) await exec('make', ['install', `prefix=${binDir}`], { cwd: sourceDir })
await core.group("install xmake", async () => { const cacheDir = await toolCache.cacheDir(binDir, 'xmake', version)
let toolDir = toolCache.find('xmake', version) await io.rmRF(binDir)
if (!toolDir) { await io.rmRF(sourceDir)
await exec('make', ['build'], { cwd: folder }) return cacheDir
const prefix = path.join(os.tmpdir(), `xmake-${version}-${sha}`) })
await exec('make', ['install', `prefix=${prefix}`], { cwd: folder }) }
toolDir = await toolCache.cacheDir(prefix, 'xmake', version) core.addPath(path.join(toolDir, 'bin'))
} await exec('xmake --version')
core.addPath(path.join(toolDir, 'bin'))
})
} catch (error) { } catch (error) {
core.setFailed(error.message) core.setFailed(error.message)
} }
+2 -1
View File
@@ -18,7 +18,8 @@
}, },
"_requiredBy": [ "_requiredBy": [
"#USER", "#USER",
"/" "/",
"/@actions/tool-cache"
], ],
"_resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.1.tgz", "_resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.1.tgz",
"_shasum": "81a9418fe2bbdef2d2717a8e9f85188b9c565aca", "_shasum": "81a9418fe2bbdef2d2717a8e9f85188b9c565aca",
+1
View File
@@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.1.1", "@actions/core": "^1.1.1",
"@actions/exec": "^1.0.1", "@actions/exec": "^1.0.1",
"@actions/io": "^1.0.1",
"@actions/tool-cache": "^1.1.2", "@actions/tool-cache": "^1.1.2",
"@octokit/rest": "^16.30.1", "@octokit/rest": "^16.30.1",
"semver": "^6.3.0" "semver": "^6.3.0"