From 80fcf1ca2f05215b2a70b01b79d0e73e634f44f2 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Sun, 16 Apr 2023 14:35:54 -0700 Subject: [PATCH] Install binstall by downloading. --- index.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++------ package.json | 3 ++- pnpm-lock.yaml | 21 ++++++++++++++++++++- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/index.ts b/index.ts index 085f1bf..effd379 100644 --- a/index.ts +++ b/index.ts @@ -3,6 +3,7 @@ import os from 'os'; import path from 'path'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; +import * as tc from '@actions/tool-cache'; import TOML from '@ltd/j-toml'; interface Toolchain { @@ -66,7 +67,7 @@ function detectToolchain(): Toolchain { channel: process.env.RUSTUP_TOOLCHAIN, }); } else { - core.info('Searching for rust-toolchain.toml or rust-toolchain file'); + core.info('Loading rust-toolchain.toml or rust-toolchain file'); for (const configName of ['rust-toolchain.toml', 'rust-toolchain']) { const configPath = path.join(process.cwd(), configName); @@ -128,6 +129,47 @@ async function installToolchain(toolchain: Toolchain) { await exec.exec('rustc', [`+${toolchain.channel}`, '--version', '--verbose']); } +async function downloadAndInstallBinstall(binDir: string) { + core.info('cargo-binstall does not exist, attempting to install'); + + let arch; + let file; + + switch (process.arch) { + case 'x64': + arch = 'x86_64'; + break; + case 'arm64': + arch = 'aarch64'; + break; + default: + throw new Error(`Unsupported architecture: ${process.arch}`); + } + + switch (process.platform) { + case 'linux': + file = `${arch}-unknown-linux-gnu.tgz`; + break; + case 'darwin': + file = `${arch}-apple-darwin.zip`; + break; + case 'win32': + file = `${arch}-pc-windows-msvc.zip`; + break; + default: + throw new Error(`Unsupported platform: ${process.platform}`); + } + + const url = `https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-${file}`; + const dlPath = await tc.downloadTool(url); + + if (url.endsWith('.zip')) { + await tc.extractZip(dlPath, binDir); + } else if (url.endsWith('.tgz')) { + await tc.extractTar(dlPath, binDir); + } +} + async function installBins() { const bins = core .getInput('bins') @@ -144,12 +186,8 @@ async function installBins() { const binDir = path.join(getCargoHome(), 'bin'); - core.debug('Checking if cargo-binstall has been installed'); - if (!fs.existsSync(path.join(binDir, 'cargo-binstall'))) { - core.debug('Not installed, attempting to install'); - - await exec.exec('cargo', ['install', 'cargo-binstall']); + await downloadAndInstallBinstall(binDir); } await exec.exec('cargo', ['binstall', '--no-confirm', '--log-level', 'info', ...bins]); diff --git a/package.json b/package.json index 4903275..17c6cde 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moonrepo/setup-rust", - "version": "0.2.2", + "version": "0.2.3", "description": "A GitHub action for setting up Rust and Cargo.", "main": "dist/index.js", "scripts": { @@ -18,6 +18,7 @@ "dependencies": { "@actions/core": "^1.10.0", "@actions/exec": "^1.1.1", + "@actions/tool-cache": "^2.0.1", "@ltd/j-toml": "^1.38.0" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3ee9830..d35ecbb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,6 +7,9 @@ dependencies: '@actions/exec': specifier: ^1.1.1 version: 1.1.1 + '@actions/tool-cache': + specifier: ^2.0.1 + version: 2.0.1 '@ltd/j-toml': specifier: ^1.38.0 version: 1.38.0 @@ -62,6 +65,17 @@ packages: resolution: {integrity: sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==} dev: false + /@actions/tool-cache@2.0.1: + resolution: {integrity: sha512-iPU+mNwrbA8jodY8eyo/0S/QqCKDajiR8OxWTnSk/SnYg0sj8Hp4QcUEVC1YFpHWXtrfbQrE13Jz4k4HXJQKcA==} + dependencies: + '@actions/core': 1.10.0 + '@actions/exec': 1.1.1 + '@actions/http-client': 2.1.0 + '@actions/io': 1.1.3 + semver: 6.3.0 + uuid: 3.4.0 + dev: false + /@babel/code-frame@7.21.4: resolution: {integrity: sha512-LYvhNKfwWSPpocw8GI7gpK2nq3HSDuEPC/uSYaALSJu9xjsalaaYFOq0Pwt5KmVqwEbZlDu81aLXwBOmD/Fv9g==} engines: {node: '>=6.9.0'} @@ -2226,7 +2240,6 @@ packages: /semver@6.3.0: resolution: {integrity: sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==} hasBin: true - dev: true /semver@7.3.8: resolution: {integrity: sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==} @@ -2499,6 +2512,12 @@ packages: punycode: 2.3.0 dev: true + /uuid@3.4.0: + resolution: {integrity: sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==} + deprecated: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details. + hasBin: true + dev: false + /uuid@8.3.2: resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==} hasBin: true