toolchain/src/args.ts

69 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-03-24 21:26:10 +08:00
import { input } from "@actions-rs/core";
import { debug } from "@actions/core";
import { existsSync, readFileSync } from "fs";
2022-01-08 13:08:21 +08:00
import { parse } from "fast-toml";
2019-09-12 21:44:29 +08:00
export interface ToolchainOptions {
2020-03-24 21:26:10 +08:00
name: string;
target: string | undefined;
default: boolean;
override: boolean;
profile: string | undefined;
components: string[] | undefined;
2019-09-12 21:44:29 +08:00
}
function determineToolchain(overrideFile: string): string {
2020-03-24 21:26:10 +08:00
const toolchainInput = input.getInput("toolchain", { required: false });
if (toolchainInput) {
debug(`using toolchain from input: ${toolchainInput}`);
2020-03-24 21:26:10 +08:00
return toolchainInput;
}
2022-01-08 13:08:21 +08:00
const toolchainPath = existsSync(overrideFile)
? overrideFile
: existsSync(`${overrideFile}.toml`)
? `${overrideFile}.toml`
2022-01-08 12:00:45 +08:00
: undefined;
if (!toolchainPath) {
2020-03-24 21:26:10 +08:00
throw new Error(
"toolchain input was not given and repository does not have a rust-toolchain file"
);
}
2022-01-08 12:00:45 +08:00
const rustToolchainFile = readFileSync(toolchainPath, {
encoding: "utf-8",
2020-03-24 21:26:10 +08:00
flag: "r",
}).trim();
2022-01-08 13:08:21 +08:00
const toolchain = rustToolchainFile.includes("[toolchain]")
? parse<{ toolchain?: { channel?: string } }>(rustToolchainFile)
?.toolchain?.channel
: rustToolchainFile;
if (!toolchain) {
throw new Error(`channel is not specified in ${toolchainPath}`);
}
debug(`using toolchain from rust-toolchain file: ${rustToolchainFile}`);
return rustToolchainFile;
}
2020-03-24 21:26:10 +08:00
export function getToolchainArgs(overrideFile: string): ToolchainOptions {
let components: string[] | undefined = input.getInputList("components");
if (components && components.length === 0) {
components = undefined;
}
return {
name: determineToolchain(overrideFile),
target: input.getInput("target") || undefined,
default: input.getInputBool("default"),
override: input.getInputBool("override"),
profile: input.getInput("profile") || undefined,
components: components,
};
}