mirror of
https://github.com/moonrepo/setup-rust.git
synced 2025-04-29 13:30:03 +00:00
Automatic compilation
This commit is contained in:
parent
128d91072a
commit
b0ca371847
13 changed files with 9885 additions and 2932 deletions
|
@ -1,6 +0,0 @@
|
||||||
coverage/
|
|
||||||
dist/
|
|
||||||
node_modules/
|
|
||||||
*.min.js
|
|
||||||
*.map
|
|
||||||
*.snap
|
|
10
.eslintrc.js
10
.eslintrc.js
|
@ -1,10 +0,0 @@
|
||||||
/* eslint-disable sort-keys */
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
root: true,
|
|
||||||
extends: ['moon', 'moon/node'],
|
|
||||||
parserOptions: {
|
|
||||||
project: 'tsconfig.json',
|
|
||||||
tsconfigRootDir: __dirname,
|
|
||||||
},
|
|
||||||
};
|
|
16
.github/workflows/ci.yml
vendored
16
.github/workflows/ci.yml
vendored
|
@ -1,16 +0,0 @@
|
||||||
name: 'Pipeline'
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- 'master'
|
|
||||||
pull_request:
|
|
||||||
jobs:
|
|
||||||
ci:
|
|
||||||
name: 'CI'
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: actions/setup-node@v3
|
|
||||||
- run: npm install -g pnpm
|
|
||||||
- run: pnpm install
|
|
||||||
- run: pnpm run check
|
|
18
.github/workflows/publish.yml
vendored
18
.github/workflows/publish.yml
vendored
|
@ -1,18 +0,0 @@
|
||||||
name: Publish
|
|
||||||
on:
|
|
||||||
release:
|
|
||||||
types: [published, edited]
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
with:
|
|
||||||
ref: ${{ github.event.release.tag_name }}
|
|
||||||
- uses: actions/setup-node@v3
|
|
||||||
- run: npm install -g pnpm
|
|
||||||
- run: pnpm install
|
|
||||||
- run: pnpm run build
|
|
||||||
- uses: JasonEtco/build-and-tag-action@v2
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ github.token }}
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +0,0 @@
|
||||||
node_modules/
|
|
||||||
dist/
|
|
|
@ -1,3 +0,0 @@
|
||||||
# Unreleased
|
|
||||||
|
|
||||||
- Initial release.
|
|
19
README.md
19
README.md
|
@ -1,19 +0,0 @@
|
||||||
# Setup Rust
|
|
||||||
|
|
||||||
A GitHub action for setting up Rust and Cargo. Will automatically install the appropriate toolchain
|
|
||||||
with `rustup` by inspecting the `RUSTUP_TOOLCHAIN` environment variable or the `rust-toolchain.toml`
|
|
||||||
configuration file.
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
# ...
|
|
||||||
jobs:
|
|
||||||
ci:
|
|
||||||
name: CI
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
# ...
|
|
||||||
- uses: moonrepo/setup-rust@v0
|
|
||||||
- run: cargo test
|
|
||||||
```
|
|
9885
dist/index.js
vendored
Normal file
9885
dist/index.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
219
index.ts
219
index.ts
|
@ -1,219 +0,0 @@
|
||||||
import fs from 'fs';
|
|
||||||
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 {
|
|
||||||
channel: string;
|
|
||||||
components: string[];
|
|
||||||
targets: string[];
|
|
||||||
profile: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ToolchainConfig {
|
|
||||||
toolchain: Partial<Toolchain>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const DEFAULT_TOOLCHAIN: Toolchain = {
|
|
||||||
channel: 'stable',
|
|
||||||
components: [],
|
|
||||||
profile: 'minimal',
|
|
||||||
targets: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
function getCargoHome(): string {
|
|
||||||
if (process.env.CARGO_HOME) {
|
|
||||||
return process.env.CARGO_HOME;
|
|
||||||
}
|
|
||||||
|
|
||||||
return path.join(os.homedir(), '.cargo');
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseConfig(configPath: string): Partial<Toolchain> {
|
|
||||||
const contents = fs.readFileSync(configPath, 'utf8').trim();
|
|
||||||
|
|
||||||
if (!contents.includes('[toolchain]')) {
|
|
||||||
core.debug('No [toolchain] section found, assuming legacy format');
|
|
||||||
|
|
||||||
return { channel: contents };
|
|
||||||
}
|
|
||||||
|
|
||||||
const config = TOML.parse(contents) as unknown as ToolchainConfig;
|
|
||||||
|
|
||||||
if (config.toolchain.channel) {
|
|
||||||
core.debug('Found channel in [toolchain] section');
|
|
||||||
|
|
||||||
return { channel: config.toolchain.channel };
|
|
||||||
}
|
|
||||||
|
|
||||||
core.debug('No channel found in [toolchain] section');
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://rust-lang.github.io/rustup/overrides.html
|
|
||||||
function detectToolchain(): Toolchain {
|
|
||||||
core.info('Detecting toolchain');
|
|
||||||
|
|
||||||
const toolchain = { ...DEFAULT_TOOLCHAIN };
|
|
||||||
|
|
||||||
if (process.env.RUSTUP_TOOLCHAIN) {
|
|
||||||
core.info('Using toolchain from RUSTUP_TOOLCHAIN environment variable');
|
|
||||||
|
|
||||||
Object.assign(toolchain, {
|
|
||||||
channel: process.env.RUSTUP_TOOLCHAIN,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (fs.existsSync(configPath)) {
|
|
||||||
core.debug(`Found ${configName}, parsing TOML`);
|
|
||||||
|
|
||||||
Object.assign(toolchain, parseConfig(configPath));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
core.info('Inheriting toolchain settings from inputs');
|
|
||||||
|
|
||||||
(Object.keys(DEFAULT_TOOLCHAIN) as (keyof typeof DEFAULT_TOOLCHAIN)[]).forEach((key) => {
|
|
||||||
const input = core.getInput(key);
|
|
||||||
|
|
||||||
if (input) {
|
|
||||||
core.debug(`Found input for ${key}: ${input}`);
|
|
||||||
|
|
||||||
if (key === 'components' || key === 'targets') {
|
|
||||||
input.split(',').forEach((part) => {
|
|
||||||
toolchain[key].push(part.trim());
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
toolchain[key] = input;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return toolchain;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function installToolchain(toolchain: Toolchain) {
|
|
||||||
core.info('Installing toolchain with rustup');
|
|
||||||
|
|
||||||
const args = ['toolchain', 'install', toolchain.channel, '--profile', toolchain.profile];
|
|
||||||
|
|
||||||
toolchain.targets.forEach((target) => {
|
|
||||||
args.push('--target', target);
|
|
||||||
});
|
|
||||||
|
|
||||||
toolchain.components.forEach((component) => {
|
|
||||||
args.push('--component', component);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (toolchain.channel === 'nightly' && toolchain.components.length > 0) {
|
|
||||||
args.push('--allow-downgrade');
|
|
||||||
}
|
|
||||||
|
|
||||||
args.push('--no-self-update');
|
|
||||||
|
|
||||||
await exec.exec('rustup', args);
|
|
||||||
await exec.exec('rustup', ['default', toolchain.channel]);
|
|
||||||
|
|
||||||
core.info('Logging installed toolchain versions');
|
|
||||||
|
|
||||||
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')
|
|
||||||
.split(',')
|
|
||||||
.map((bin) => bin.trim())
|
|
||||||
.filter(Boolean)
|
|
||||||
.map((bin) => (bin.startsWith('cargo-') ? bin : `cargo-${bin}`));
|
|
||||||
|
|
||||||
if (bins.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
core.info('Installing additional binaries');
|
|
||||||
|
|
||||||
const binDir = path.join(getCargoHome(), 'bin');
|
|
||||||
|
|
||||||
if (!fs.existsSync(path.join(binDir, 'cargo-binstall'))) {
|
|
||||||
await downloadAndInstallBinstall(binDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
await exec.exec('cargo', ['binstall', '--no-confirm', '--log-level', 'info', ...bins]);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function run() {
|
|
||||||
core.info('Setting cargo environment variables');
|
|
||||||
|
|
||||||
// Disable incremental compilation
|
|
||||||
core.exportVariable('CARGO_INCREMENTAL', '0');
|
|
||||||
|
|
||||||
// Always enable colored output
|
|
||||||
core.exportVariable('CARGO_TERM_COLOR', 'always');
|
|
||||||
|
|
||||||
try {
|
|
||||||
await installToolchain(detectToolchain());
|
|
||||||
await installBins();
|
|
||||||
} catch (error: unknown) {
|
|
||||||
core.setFailed((error as Error).message);
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
core.info('Adding ~/.cargo/bin to PATH');
|
|
||||||
|
|
||||||
core.addPath(path.join(getCargoHome(), 'bin'));
|
|
||||||
}
|
|
||||||
|
|
||||||
void run();
|
|
34
package.json
34
package.json
|
@ -1,34 +0,0 @@
|
||||||
{
|
|
||||||
"name": "@moonrepo/setup-rust",
|
|
||||||
"version": "0.2.3",
|
|
||||||
"description": "A GitHub action for setting up Rust and Cargo.",
|
|
||||||
"main": "dist/index.js",
|
|
||||||
"scripts": {
|
|
||||||
"build": "ncc build ./index.ts",
|
|
||||||
"check": "npm run lint && npm run typecheck",
|
|
||||||
"lint": "eslint --ext .ts,.js --fix .",
|
|
||||||
"typecheck": "tsc --noEmit"
|
|
||||||
},
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/moonrepo/setup-rust"
|
|
||||||
},
|
|
||||||
"author": "Miles Johnson",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"@actions/core": "^1.10.0",
|
|
||||||
"@actions/exec": "^1.1.1",
|
|
||||||
"@actions/tool-cache": "^2.0.1",
|
|
||||||
"@ltd/j-toml": "^1.38.0"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@types/node": "^18.15.10",
|
|
||||||
"@vercel/ncc": "^0.36.1",
|
|
||||||
"eslint": "^8.36.0",
|
|
||||||
"eslint-config-moon": "^2.0.2",
|
|
||||||
"prettier": "^2.8.7",
|
|
||||||
"prettier-config-moon": "^1.1.2",
|
|
||||||
"tsconfig-moon": "^1.3.0",
|
|
||||||
"typescript": "^5.0.2"
|
|
||||||
}
|
|
||||||
}
|
|
2588
pnpm-lock.yaml
generated
2588
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
|
@ -1 +0,0 @@
|
||||||
module.exports = 'prettier-config-moon';
|
|
|
@ -1,16 +0,0 @@
|
||||||
{
|
|
||||||
"extends": "tsconfig-moon/tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"allowJs": true,
|
|
||||||
"module": "Node16",
|
|
||||||
"moduleResolution": "NodeNext",
|
|
||||||
"noEmit": true,
|
|
||||||
"verbatimModuleSyntax": false
|
|
||||||
},
|
|
||||||
"include": [
|
|
||||||
".eslintrc.js",
|
|
||||||
"*.js",
|
|
||||||
"*.ts",
|
|
||||||
"tests/*.ts"
|
|
||||||
]
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue