setup-rust-action/src/rustup.ts

57 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-08-15 18:12:08 +00:00
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as toolCache from '@actions/tool-cache';
import * as path from 'path';
import * as os from 'os';
import {chmodSync, renameSync, existsSync, appendFileSync} from 'fs';
2019-08-15 18:12:08 +00:00
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
2019-08-16 00:16:04 +00:00
export async function install() {
2019-08-16 01:16:52 +00:00
// `rustup` is already installed on Linux and Windows platforms
2019-08-15 21:22:17 +00:00
if (os.platform() == 'darwin') {
2019-08-16 00:16:04 +00:00
let toolPath = await installOnUnix();
2019-08-15 18:12:08 +00:00
2019-08-16 00:16:04 +00:00
core.debug('rustup is located under: ' + toolPath);
2019-08-15 21:22:17 +00:00
core.addPath(path.join(toolPath, 'bin'));
2019-10-27 05:14:57 +00:00
} else {
// Update the GitHub managed VM version of rustup
2019-10-27 05:14:57 +00:00
// to leverage newer features like "latest latest compatible nightly"
await exec.exec('rustup', ['self', 'update']);
await exec.exec('rustup', ['set', 'profile', 'minimal']);
2019-12-04 06:01:39 +00:00
if (os.platform() == 'win32') {
let cargoPath = '';
{
const options = {
listeners: {
stdout: (data: Buffer) => {
cargoPath += data.toString();
2019-12-04 06:01:39 +00:00
}
}
};
await exec.exec('where', ['rustup.exe'], options);
}
let rustupPath = cargoPath.split('\\').slice(0, -3).concat([".rustup"]).join("\\");
let defaultClearedFilePath = `${rustupPath}\\default_cleared`;
if (!existsSync(defaultClearedFilePath)) {
2019-12-04 06:01:39 +00:00
// Github's default Windows install comes with rustup pre-installed with stable, including
// rust-docs. This removes the default stable install so that it doesn't update rust-docs.
renameSync(`${rustupPath}\\toolchains`, `${rustupPath}\\_toolchains`);
appendFileSync(defaultClearedFilePath, '');
2019-12-04 06:01:39 +00:00
}
}
2019-08-15 21:22:17 +00:00
}
2019-08-15 18:12:08 +00:00
}
2019-08-16 00:16:04 +00:00
async function installOnUnix(): Promise<string> {
let script = await toolCache.downloadTool("https://sh.rustup.rs");
2019-08-15 18:12:08 +00:00
2019-08-15 21:22:17 +00:00
chmodSync(script, '777');
await exec.exec(`"${script}"`, ['-y', '--default-toolchain', 'none', '--profile=minimal']);
2019-08-15 18:12:08 +00:00
2019-08-16 00:16:04 +00:00
return path.join(process.env['HOME'] || '', '.cargo');
2019-08-15 18:12:08 +00:00
}