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

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, {