2019-10-05 10:44:40 +00:00
|
|
|
const os = require('os')
|
|
|
|
const path = require('path')
|
|
|
|
const semver = require('semver')
|
2019-10-07 08:17:11 +00:00
|
|
|
const get = require('simple-get').concat
|
2019-10-05 10:44:40 +00:00
|
|
|
const actions = require('@actions/core')
|
|
|
|
const cache = require('@actions/tool-cache')
|
|
|
|
|
2019-10-07 08:17:11 +00:00
|
|
|
function getJSON (opts) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
get({ ...opts, json: true }, (err, req, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-11 11:27:49 +00:00
|
|
|
function extForPlatform (platform) {
|
|
|
|
return {
|
|
|
|
linux: 'tar.xz',
|
|
|
|
darwin: 'tar.xz',
|
|
|
|
win32: 'zip'
|
|
|
|
}[platform]
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveCommit (platform, version) {
|
|
|
|
const ext = extForPlatform(platform)
|
|
|
|
const addrhost = {
|
|
|
|
linux: 'linux-x86_64',
|
|
|
|
darwin: 'macos-x86_64',
|
|
|
|
win32: 'windows-x86_64'
|
|
|
|
}[platform]
|
|
|
|
|
|
|
|
const downloadUrl = `https://ziglang.org/builds/zig-${addrhost}-${version}.${ext}`
|
|
|
|
const variantName = `zig-${addrhost}-${version}`
|
|
|
|
|
|
|
|
return { downloadUrl, variantName }
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resolveVersion (platform, version) {
|
|
|
|
const ext = extForPlatform(platform)
|
2019-10-05 10:44:40 +00:00
|
|
|
const host = {
|
2019-10-07 08:17:11 +00:00
|
|
|
linux: 'x86_64-linux',
|
|
|
|
darwin: 'x86_64-macos',
|
|
|
|
win32: 'x86_64-windows'
|
2020-11-11 11:27:49 +00:00
|
|
|
}[platform] || platform
|
|
|
|
|
|
|
|
const index = await getJSON({ url: 'https://ziglang.org/download/index.json' })
|
|
|
|
|
|
|
|
const availableVersions = Object.keys(index)
|
|
|
|
const useVersion = semver.valid(version)
|
|
|
|
? semver.maxSatisfying(availableVersions.filter((v) => semver.valid(v)), version)
|
|
|
|
: null
|
|
|
|
|
|
|
|
const meta = index[useVersion || version]
|
|
|
|
if (!meta || !meta[host]) {
|
|
|
|
throw new Error(`Could not find version ${version} for platform ${host}`)
|
2020-11-11 11:15:17 +00:00
|
|
|
}
|
2020-11-11 11:27:49 +00:00
|
|
|
|
|
|
|
const downloadUrl = meta[host].tarball
|
|
|
|
const variantName = path.basename(meta[host].tarball).replace(`.${ext}`, '')
|
|
|
|
|
|
|
|
return { downloadUrl, variantName }
|
|
|
|
}
|
|
|
|
|
|
|
|
async function downloadZig (platform, version) {
|
|
|
|
const ext = extForPlatform(platform)
|
|
|
|
|
|
|
|
const { downloadUrl, variantName } = version.includes('+')
|
|
|
|
? resolveCommit(platform, version)
|
|
|
|
: await resolveVersion(platform, version)
|
|
|
|
|
|
|
|
const downloadPath = await cache.downloadTool(downloadUrl)
|
|
|
|
const zigPath = ext === 'zip'
|
|
|
|
? await cache.extractZip(downloadPath)
|
|
|
|
: await cache.extractTar(downloadPath, undefined, 'x')
|
|
|
|
|
|
|
|
const binPath = path.join(zigPath, variantName)
|
|
|
|
const cachePath = await cache.cacheDir(binPath, 'zig', variantName)
|
|
|
|
|
|
|
|
return cachePath
|
2019-10-05 10:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
const version = actions.getInput('version') || '0.5.0'
|
2020-01-23 09:55:01 +00:00
|
|
|
if (semver.valid(version) && semver.lt(version, '0.3.0')) {
|
2019-10-05 10:44:40 +00:00
|
|
|
actions.setFailed('This action does not work with Zig 0.1.0 and Zig 0.2.0')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-06 13:52:14 +00:00
|
|
|
let zigPath = cache.find('zig', version)
|
|
|
|
if (!zigPath) {
|
2020-11-11 11:27:49 +00:00
|
|
|
zigPath = await downloadZig(os.platform(), version)
|
2019-10-06 13:52:14 +00:00
|
|
|
}
|
2019-10-05 10:44:40 +00:00
|
|
|
|
|
|
|
// Add the `zig` binary to the $PATH
|
|
|
|
actions.addPath(zigPath)
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:01:44 +00:00
|
|
|
main().catch((err) => {
|
|
|
|
console.error(err.stack)
|
|
|
|
actions.setFailed(err.message)
|
|
|
|
process.exit(1)
|
|
|
|
})
|