import * as cache from '@actions/cache' import * as core from '@actions/core' import * as exec from '@actions/exec' import * as glob from '@actions/glob' import * as fs from 'fs' import * as os from 'os' import * as path from 'path' import {rtxDir} from './utils' async function run(): Promise { await setToolVersions() await restoreRTXCache() await setupRTX() await exec.exec('rtx', ['--version']) await exec.exec('rtx', ['install']) await setPaths() } async function restoreRTXCache(): Promise { const cachePath = rtxDir() const fileHash = await glob.hashFiles(`**/.tool-versions\n**/.rtx.toml`) const primaryKey = `rtx-tools-${getOS()}-${os.arch()}-${fileHash}` core.saveState('PRIMARY_KEY', primaryKey) const cacheKey = await cache.restoreCache([cachePath], primaryKey) core.setOutput('cache-hit', Boolean(cacheKey)) if (!cacheKey) { core.info(`rtx cache not found for ${getOS()}-${os.arch()} tool versions`) return } core.saveState('CACHE_KEY', cacheKey) core.info(`rtx cache restored from key: ${cacheKey}`) } async function setupRTX(): Promise { const rtxBinDir = path.join(rtxDir(), 'bin') const url = `https://rtx.pub/rtx-latest-${getOS()}-${os.arch()}` await fs.promises.mkdir(rtxBinDir, {recursive: true}) await exec.exec('curl', [url, '--output', path.join(rtxBinDir, 'rtx')]) await exec.exec('chmod', ['+x', path.join(rtxBinDir, 'rtx')]) core.addPath(rtxBinDir) } // returns true if tool_versions was set async function setToolVersions(): Promise { const toolVersions = core.getInput('tool_versions', {required: false}) if (toolVersions) { await fs.promises.writeFile('.tool-versions', toolVersions, { encoding: 'utf8' }) return true } return false } function getOS(): string { switch (process.platform) { case 'darwin': return 'macos' default: return process.platform } } async function setPaths(): Promise { for (const binPath of await getBinPaths()) { core.addPath(binPath) } } async function getBinPaths(): Promise { const output = await exec.getExecOutput('rtx', ['bin-paths']) return output.stdout.split('\n') } if (require.main === module) { try { run() } catch (err) { if (err instanceof Error) { core.setFailed(err.message) } else throw err } } export {run}