setup-zig/index.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-10-05 10:44:40 +00:00
const os = require('os')
const path = require('path')
const semver = require('semver')
const actions = require('@actions/core')
const cache = require('@actions/tool-cache')
const {
extForPlatform,
resolveCommit,
resolveVersion
} = require('./versions')
2020-11-11 11:27:49 +00:00
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 () {
2022-11-07 12:19:37 +00:00
const version = actions.getInput('version') || 'master'
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)
}
main().catch((err) => {
console.error(err.stack)
actions.setFailed(err.message)
process.exit(1)
})