Support toml rust-toolchain & working-directory

Closes #126
This commit is contained in:
Boaz Berman 2021-04-05 19:12:59 +03:00
parent 88dc235639
commit 00a8bf2bdc
21 changed files with 231 additions and 138 deletions

2
.eslintignore Normal file
View File

@ -0,0 +1,2 @@
node_modules
dist

View File

@ -13,8 +13,7 @@ jobs:
- uses: actions/checkout@v1
- run: npm ci
# Temporary disabling in order to release urgent fix
# - run: npm run lint
- run: npm run lint
- run: npm run build
- run: npm run test

2
.gitignore vendored
View File

@ -92,3 +92,5 @@ typings/
# IntelliJ IDEs
.idea
.npmrc

3
.prettierignore Normal file
View File

@ -0,0 +1,3 @@
# Ignore artifacts:
node_modules
dist

1
.prettierrc.json Normal file
View File

@ -0,0 +1 @@
{}

View File

@ -1,4 +1,5 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

View File

@ -14,14 +14,14 @@ these small papercuts for you.
**Table of Contents**
* [Example workflow](#example-workflow)
* [Inputs](#inputs)
* [Outputs](#outputs)
* [Profiles](#profiles)
* [Components](#components)
* [The toolchain file](#the-toolchain-file)
* [License](#license)
* [Contribute and support](#contribute-and-support)
- [Example workflow](#example-workflow)
- [Inputs](#inputs)
- [Outputs](#outputs)
- [Profiles](#profiles)
- [Components](#components)
- [The toolchain file](#the-toolchain-file)
- [License](#license)
- [Contribute and support](#contribute-and-support)
## Example workflow
@ -57,7 +57,7 @@ See [additional recipes here](https://github.com/actions-rs/meta).
## Inputs
| Name | Required | Description | Type | Default |
| ------------ | :------: | ----------------------------------------------------------------------------------------------------------------------------------------------------| ------ | --------|
| ------------ | :------: | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------- |
| `toolchain` | | [Toolchain](https://github.com/rust-lang/rustup.rs#toolchain-specification) name to use, ex. `stable`, `nightly`, `nightly-2019-04-20`, or `1.32.0` | string | stable |
| `target` | | Additionally install specified target for this toolchain, ex. `x86_64-apple-darwin` | string | |
| `default` | | Set installed toolchain as a default toolchain | bool | false |
@ -73,7 +73,7 @@ in order to support toolchain files. See the details [below](#the-toolchain-file
Installed `rustc`, `cargo` and `rustup` versions can be fetched from the Action outputs:
| Name | Description | Example |
| ------------ | --------------------- | ------------------------------- |
| ------------ | ------------------ | ------------------------------- |
| `rustc` | Rustc version | `1.40.0 (73528e339 2019-12-16)` |
| `rustc_hash` | Rustc version hash | `73528e339` |
| `cargo` | Cargo version | `1.40.0 (bc8e4c8be 2019-11-22)` |
@ -137,8 +137,8 @@ latest `nightly` release, this Action will attempt the downgrade till it find
the most recent `nightly` with all components needed.\
Note that this behavior will work only if the following two conditions apply:
1. `toolchain` input is `nightly` exactly.
2. At least one component is provided in `components` input.
1. `toolchain` input is `nightly` exactly.
2. At least one component is provided in `components` input.
Same to the `profile` input, if installed `rustup` does not supports "components",
it will be automatically upgraded by this Action.

View File

@ -62,6 +62,37 @@ describe("actions-rs/toolchain", () => {
expect(args.name).toBe("1.39.0");
});
it("uses new style rust-toolchain file if input does not exist", function () {
const rustToolchainFile = tempWriteSync(
'[toolchain]\nchannel = "1.51.0"\ncomponents = [ "rustfmt", "clippy" ]\nprofile = "minimal"\ntargets = [ "wasm32-unknown-unknown", "thumbv2-none-eabi" ]'
);
const args = morph(() => {
return getToolchainArgs(rustToolchainFile);
}, {});
expect(args.name).toBe("1.51.0");
expect(args.components).toStrictEqual(["rustfmt", "clippy"]);
expect(args.profile).toBe("minimal");
expect(args.target).toBe("wasm32-unknown-unknown");
});
it("uses new style rust-toolchain file with toml ending if input and rust-toolchain file does not exist", function () {
const rustToolchainFile = tempWriteSync(
'[toolchain]\nchannel = "1.51.0"\ncomponents = [ "rustfmt", "clippy" ]\nprofile = "minimal"\ntargets = [ "wasm32-unknown-unknown", "thumbv2-none-eabi" ]',
"./rust-toolchain.toml"
);
const args = morph(() => {
return getToolchainArgs(rustToolchainFile.replace(/\.toml^/, ""));
}, {});
expect(args.name).toBe("1.51.0");
expect(args.components).toStrictEqual(["rustfmt", "clippy"]);
expect(args.profile).toBe("minimal");
expect(args.target).toBe("wasm32-unknown-unknown");
});
it("trims content of the override file", function () {
const rustToolchainFile = tempWriteSync("\n 1.39.0\n\n\n\n");

View File

@ -1,10 +1,13 @@
name: 'rust-toolchain'
description: 'Install the Rust toolchain'
author: 'actions-rs team'
name: "rust-toolchain"
description: "Install the Rust toolchain"
author: "actions-rs team"
branding:
icon: play-circle
color: black
inputs:
working-directory:
description: The working directory to find the rust-toolchain in.
required: false
toolchain:
description: |
Rust toolchain name.
@ -40,5 +43,5 @@ outputs:
description: Installed rustup version
runs:
using: 'node12'
main: 'dist/index.js'
using: "node12"
main: "dist/index.js"

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

5
package-lock.json generated
View File

@ -764,6 +764,11 @@
}
}
},
"@iarna/toml": {
"version": "2.2.5",
"resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.5.tgz",
"integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg=="
},
"@istanbuljs/load-nyc-config": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz",

View File

@ -10,8 +10,8 @@
},
"scripts": {
"build": "rm -rf ./dist/* && ncc build src/main.ts --minify",
"format": "prettier --write 'src/**/*.ts' '__tests__/**/*.ts'",
"lint": "tsc --noEmit && eslint 'src/**/*.ts' '__tests__/**/*.ts'",
"format": "prettier --write .",
"lint": "tsc --noEmit && eslint .",
"watch": "rm -rf ./dist/* && ncc build src/main.ts --watch",
"test": "jest -c jest.config.json",
"pretest": "git config core.hooksPath .githooks"
@ -35,7 +35,8 @@
"@actions-rs/core": "^0.1.6",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"@actions/io": "^1.0.2"
"@actions/io": "^1.0.2",
"@iarna/toml": "^2.2.5"
},
"devDependencies": {
"@types/jest": "^26.0.15",

View File

@ -1,52 +1,36 @@
import { input } from "@actions-rs/core";
import { debug } from "@actions/core";
import { existsSync, readFileSync } from "fs";
import { parseToolchainFile } from "./toolchain_file";
export interface ToolchainOptions {
name: string;
target: string | undefined;
default: boolean;
override: boolean;
profile: string | undefined;
components: string[] | undefined;
}
function determineToolchain(overrideFile: string): string {
const toolchainInput = input.getInput("toolchain", { required: false });
if (toolchainInput) {
debug(`using toolchain from input: ${toolchainInput}`);
return toolchainInput;
}
if (!existsSync(overrideFile)) {
throw new Error(
"toolchain input was not given and repository does not have a rust-toolchain file"
);
}
const rustToolchainFile = readFileSync(overrideFile, {
encoding: "utf-8",
flag: "r",
}).trim();
debug(`using toolchain from rust-toolchain file: ${rustToolchainFile}`);
return rustToolchainFile;
target?: string | undefined;
default?: boolean;
override?: boolean;
profile?: string | undefined;
components?: string[] | undefined;
}
export function getToolchainArgs(overrideFile: string): ToolchainOptions {
const toolchainInput = input.getInput("toolchain", { required: false });
let components: string[] | undefined = input.getInputList("components");
if (components && components.length === 0) {
components = undefined;
}
const toolchainFromFile = parseToolchainFile(overrideFile);
if (!toolchainInput && !toolchainFromFile) {
throw new Error(
"toolchain input was not given and repository does not have a rust-toolchain file"
);
}
return {
name: determineToolchain(overrideFile),
target: input.getInput("target") || undefined,
name: toolchainInput || toolchainFromFile?.name || "",
target: input.getInput("target") || toolchainFromFile?.target,
default: input.getInputBool("default"),
override: input.getInputBool("override"),
profile: input.getInput("profile") || undefined,
components: components,
profile: input.getInput("profile") || toolchainFromFile?.profile,
components: components || toolchainFromFile?.components,
};
}

View File

@ -5,9 +5,15 @@ import * as args from "./args";
import * as versions from "./versions";
import { RustUp, ToolchainOptions } from "@actions-rs/core";
type Profile = "minimal" | "default" | "full";
async function run(): Promise<void> {
// we use path.join to make sure this works on Windows, Linux and MacOS
const toolchainOverridePath = path.join(process.cwd(), "rust-toolchain");
const toolchainOverridePath = path.join(
process.cwd(),
core.getInput("working-directory"),
"rust-toolchain"
);
const opts = args.getToolchainArgs(toolchainOverridePath);
const rustup = await RustUp.getOrInstall();
@ -30,8 +36,7 @@ async function run(): Promise<void> {
}
if (opts.profile) {
// @ts-ignore: TS2345
await rustup.setProfile(opts.profile);
await rustup.setProfile(opts.profile as Profile);
}
const installOptions: ToolchainOptions = {
@ -90,8 +95,9 @@ async function main(): Promise<void> {
try {
await run();
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
core.setFailed(error.message);
}
}
main();
void main();

60
src/toolchain_file.ts Normal file
View File

@ -0,0 +1,60 @@
import TOML from "@iarna/toml";
import { ToolchainOptions } from "./args";
import { existsSync, readFileSync } from "fs";
import { debug } from "@actions/core";
interface RustToolchainFileToolchain {
channel: string;
targets: string[] | undefined;
profile: string | undefined;
components?: string[] | undefined;
}
interface RustToolchainFile {
toolchain: RustToolchainFileToolchain;
}
export function parseToolchainFile(
overrideFile: string
): ToolchainOptions | null {
if (!existsSync(overrideFile)) {
if (existsSync(`${overrideFile}.toml`)) {
overrideFile = `${overrideFile}.toml`;
} else {
debug("Repository does not have a rust-toolchain file");
return null;
}
}
const rustToolchainFile = readFileSync(overrideFile, {
encoding: "utf-8",
flag: "r",
}).trim();
try {
debug(`using toolchain from rust-toolchain file: ${rustToolchainFile}`);
// eslint-disable-next-line
const parsedToolchain = (TOML.parse(
rustToolchainFile
) as unknown) as RustToolchainFile;
return {
name: parsedToolchain.toolchain.channel,
target: parsedToolchain.toolchain.targets?.[0],
profile: parsedToolchain.toolchain.profile,
components: parsedToolchain.toolchain.components,
};
} catch (err) {
debug(
`using toolchain from old style rust-toolchain file: ${rustToolchainFile}`
);
return {
name: rustToolchainFile,
default: true,
override: false,
};
}
}

View File

@ -45,7 +45,7 @@ function parseShort(stdout: string): string {
async function getStdout(
exe: string,
args: string[],
options?: {}
options?: Record<string, unknown>
): Promise<string> {
let stdout = "";
const resOptions = Object.assign({}, options, {

View File

@ -1,7 +1,4 @@
{
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"__tests__/**/*.ts"
]
"include": ["src/**/*.ts", "__tests__/**/*.ts"]
}

View File

@ -26,7 +26,5 @@
"sourceMap": false,
"typeRoots": ["./types", "./node_modules/@types"]
},
"include": [
"src/**/*.ts"
]
"include": ["src", "__tests__"]
}

View File

@ -1,7 +1,7 @@
declare module "mock-env" {
function morph<T>(
callback: () => T,
vars: object,
vars: Record<string, unknown>,
toRemove?: string[]
): any;
): T;
}