From b00fdf0c6c3b5aca9dda0151b2ec3181d8f2f2c5 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Tue, 1 Aug 2023 15:14:21 -0700 Subject: [PATCH] Install rustup. --- .eslintrc.js | 3 +++ CHANGELOG.md | 1 + README.md | 5 ----- index.ts | 34 +++++++++++++++++++++++++++++++++- package.json | 3 +++ src/cargo.ts | 8 ++++---- src/rust.ts | 4 ++-- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d29e76b..c6b9788 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,4 +7,7 @@ module.exports = { project: 'tsconfig.json', tsconfigRootDir: __dirname, }, + rules: { + 'unicorn/prefer-top-level-await': 'off', + }, }; diff --git a/CHANGELOG.md b/CHANGELOG.md index 3640ea2..69c2378 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 1.0.0 +- Will now install `rustup` if it does not exist in the environment. - Added musl support to `cargo-binstall`. # 0.6.0 diff --git a/README.md b/README.md index af6ce8e..a9b4e0f 100644 --- a/README.md +++ b/README.md @@ -147,11 +147,6 @@ Outside of being evergreen, this action also supports the following features: - Assumes `rustup`, `cargo`, and other commands are available globally. This allows you to use them directly in a `run` command, without having to use `actions-rs/cargo`. -However, this action _does not_: - -- Install `rustup` if it does not exist, while `actions-rs` will. This is typically fine if using - GitHub provided runners as all Rust tooling comes pre-installed. - ### `dtolnay/rust-toolchain` Our action is very similar to theirs, which was a great implementation reference, but our action diff --git a/index.ts b/index.ts index 1337150..2cc34b8 100644 --- a/index.ts +++ b/index.ts @@ -1,8 +1,39 @@ -import path from 'path'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; import * as core from '@actions/core'; +import * as exec from '@actions/exec'; +import * as io from '@actions/io'; +import * as tc from '@actions/tool-cache'; import { CARGO_HOME, installBins, restoreCache } from './src/cargo'; import { installToolchain } from './src/rust'; +export async function installRustup() { + try { + await io.which('rustup', true); + return; + } catch { + // Doesn't exist + } + + core.info('rustup does not exist, attempting to install'); + + const scriptPath = path.join(os.tmpdir(), 'rustup-init'); + const script = await tc.downloadTool( + process.platform === 'win32' ? 'https://win.rustup.rs' : 'https://sh.rustup.rs', + scriptPath, + ); + + core.info(`Downloaded installation script to ${script}`); + + // eslint-disable-next-line no-magic-numbers + await fs.promises.chmod(script, 0o755); + + await exec.exec(script, ['--default-toolchain', 'none', '-y']); + + core.info('Installed rustup'); +} + async function run() { core.info('Setting cargo environment variables'); @@ -14,6 +45,7 @@ async function run() { core.addPath(path.join(CARGO_HOME, 'bin')); try { + await installRustup(); await installToolchain(); await installBins(); diff --git a/package.json b/package.json index 677a723..5458d02 100644 --- a/package.json +++ b/package.json @@ -34,5 +34,8 @@ "prettier-config-moon": "^1.1.2", "tsconfig-moon": "^1.3.0", "typescript": "^5.1.6" + }, + "engines": { + "node": ">=16.0.0" } } diff --git a/src/cargo.ts b/src/cargo.ts index 2655d3a..7cfe4f0 100644 --- a/src/cargo.ts +++ b/src/cargo.ts @@ -1,7 +1,7 @@ -import crypto from 'crypto'; -import fs from 'fs'; -import os from 'os'; -import path from 'path'; +import crypto from 'node:crypto'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; import * as cache from '@actions/cache'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; diff --git a/src/rust.ts b/src/rust.ts index 6aac44f..d693b4a 100644 --- a/src/rust.ts +++ b/src/rust.ts @@ -1,7 +1,7 @@ /* eslint-disable import/no-mutable-exports */ -import fs from 'fs'; -import path from 'path'; +import fs from 'node:fs'; +import path from 'node:path'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; import TOML from '@ltd/j-toml';