action-setup-zig/index.js

61 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

'use strict'
2019-10-05 04:44:40 -06: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 04:27:49 -07:00
const TOOL_NAME = 'zig'
2020-11-11 04:27:49 -07:00
async function downloadZig (platform, version) {
const ext = extForPlatform(platform)
const { downloadUrl, variantName, version: useVersion } = version.includes('+')
2020-11-11 04:27:49 -07:00
? resolveCommit(platform, version)
: await resolveVersion(platform, version)
const cachedPath = cache.find(TOOL_NAME, useVersion)
if (cachedPath) {
actions.info(`using cached zig install: ${cachedPath}`)
return cachedPath
}
actions.info(`no cached version found. downloading zig ${variantName}`)
2020-11-11 04:27:49 -07:00
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, TOOL_NAME, useVersion)
actions.info(`added zig ${useVersion} to the tool cache`)
2020-11-11 04:27:49 -07:00
return cachePath
2019-10-05 04:44:40 -06:00
}
async function main () {
2022-11-07 05:19:37 -07:00
const version = actions.getInput('version') || 'master'
if (semver.valid(version) && semver.lt(version, '0.3.0')) {
2019-10-05 04:44:40 -06:00
actions.setFailed('This action does not work with Zig 0.1.0 and Zig 0.2.0')
return
}
const zigPath = await downloadZig(os.platform(), version)
2019-10-05 04:44:40 -06:00
// Add the `zig` binary to the $PATH
actions.addPath(zigPath)
actions.info(`zig installed at ${zigPath}`)
2019-10-05 04:44:40 -06:00
}
main().catch((err) => {
console.error(err.stack)
actions.setFailed(err.message)
process.exit(1)
})