2023-05-18 12:30:28 +00:00
|
|
|
'use strict'
|
|
|
|
|
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')
|
2023-06-25 15:44:29 +00:00
|
|
|
const cache = require('@actions/cache')
|
|
|
|
const toolCache = require('@actions/tool-cache')
|
2020-11-11 11:38:41 +00:00
|
|
|
const {
|
|
|
|
extForPlatform,
|
|
|
|
resolveCommit,
|
|
|
|
resolveVersion
|
|
|
|
} = require('./versions')
|
2020-11-11 11:27:49 +00:00
|
|
|
|
2023-05-18 12:30:28 +00:00
|
|
|
const TOOL_NAME = 'zig'
|
|
|
|
|
2024-04-26 07:39:29 +00:00
|
|
|
async function downloadZig (arch, platform, version, useCache = true) {
|
2020-11-11 11:27:49 +00:00
|
|
|
const ext = extForPlatform(platform)
|
|
|
|
|
2023-11-26 06:50:06 +00:00
|
|
|
const {
|
|
|
|
downloadUrl,
|
|
|
|
fileWithoutFileType,
|
|
|
|
variantName,
|
|
|
|
version: useVersion
|
|
|
|
} = version.includes('+')
|
2024-04-26 07:39:29 +00:00
|
|
|
? resolveCommit(arch, platform, version)
|
|
|
|
: await resolveVersion(arch, platform, version)
|
2020-11-11 11:27:49 +00:00
|
|
|
|
2023-06-25 15:44:29 +00:00
|
|
|
const cachedPath = toolCache.find(TOOL_NAME, useVersion)
|
2023-05-18 12:30:28 +00:00
|
|
|
if (cachedPath) {
|
|
|
|
actions.info(`using cached zig install: ${cachedPath}`)
|
|
|
|
return cachedPath
|
|
|
|
}
|
|
|
|
|
2023-06-25 15:44:29 +00:00
|
|
|
const cacheKey = `${TOOL_NAME}-${variantName}`
|
|
|
|
if (useCache) {
|
2024-04-26 07:39:29 +00:00
|
|
|
const restorePath = path.join(process.env.RUNNER_TOOL_CACHE, TOOL_NAME, useVersion, arch)
|
2023-06-25 15:44:29 +00:00
|
|
|
actions.info(`attempting restore of ${cacheKey} to ${restorePath}`)
|
|
|
|
const restoredKey = await cache.restoreCache([restorePath], cacheKey)
|
|
|
|
if (restoredKey) {
|
|
|
|
actions.info(`using cached zig install: ${restorePath}`)
|
|
|
|
return restorePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 12:30:28 +00:00
|
|
|
actions.info(`no cached version found. downloading zig ${variantName}`)
|
2023-06-25 15:44:29 +00:00
|
|
|
const downloadPath = await toolCache.downloadTool(downloadUrl)
|
2020-11-11 11:27:49 +00:00
|
|
|
const zigPath = ext === 'zip'
|
2023-06-25 15:44:29 +00:00
|
|
|
? await toolCache.extractZip(downloadPath)
|
|
|
|
: await toolCache.extractTar(downloadPath, undefined, 'x')
|
2020-11-11 11:27:49 +00:00
|
|
|
|
2023-11-26 06:50:06 +00:00
|
|
|
const binPath = path.join(zigPath, fileWithoutFileType)
|
2023-06-25 15:44:29 +00:00
|
|
|
const cachePath = await toolCache.cacheDir(binPath, TOOL_NAME, useVersion)
|
|
|
|
|
|
|
|
if (useCache) {
|
|
|
|
actions.info(`adding zig ${useVersion} at ${cachePath} to local cache ${cacheKey}`)
|
|
|
|
await cache.saveCache([cachePath], cacheKey)
|
|
|
|
}
|
2020-11-11 11:27:49 +00:00
|
|
|
|
|
|
|
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'
|
2023-06-25 15:44:29 +00:00
|
|
|
const useCache = actions.getInput('cache') || 'true'
|
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
|
|
|
|
}
|
2023-06-25 15:44:29 +00:00
|
|
|
if (useCache !== 'false' && useCache !== 'true') {
|
|
|
|
actions.setFailed('`with.cache` must be "true" or "false"')
|
|
|
|
return
|
|
|
|
}
|
2019-10-05 10:44:40 +00:00
|
|
|
|
2024-04-26 07:39:29 +00:00
|
|
|
const zigPath = await downloadZig(os.arch(), os.platform(), version, useCache === 'true')
|
2019-10-05 10:44:40 +00:00
|
|
|
|
|
|
|
// Add the `zig` binary to the $PATH
|
|
|
|
actions.addPath(zigPath)
|
2023-05-18 12:30:28 +00:00
|
|
|
actions.info(`zig installed at ${zigPath}`)
|
2019-10-05 10:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 18:01:44 +00:00
|
|
|
main().catch((err) => {
|
|
|
|
console.error(err.stack)
|
|
|
|
actions.setFailed(err.message)
|
|
|
|
process.exit(1)
|
|
|
|
})
|