target input and installing rustup if not available

This commit is contained in:
svartalf 2019-09-13 20:19:52 +03:00
parent efa07bee10
commit 7d6ef4b233
7 changed files with 883 additions and 44 deletions

View File

@ -26,9 +26,13 @@ jobs:
## Inputs ## Inputs
* `toolchain`: Toolchain name, see [rustup page](https://github.com/rust-lang/rustup.rs#toolchain-specification) for details.\ * `toolchain` (*required*): Toolchain name, see [rustup page](https://github.com/rust-lang/rustup.rs#toolchain-specification) for details.\
Examples: `stable`, `nightly`, `nightly-2019-04-20` Examples: `stable`, `nightly`, `nightly-2019-04-20`
* `target`: Additionally install specific target for this toolchain (ex. `x86_64-apple-darwin`)
* `default`: Set installed toolchain as default (executes `rustup toolchain default {TOOLCHAIN}`) * `default`: Set installed toolchain as default (executes `rustup toolchain default {TOOLCHAIN}`)
* `override`: Set installed toolchain as an override for current directory * `override`: Set installed toolchain as an override for current directory
Note: `toolchain` input is required. ## Notes
As `rustup` is not installed by default for macOS and Windows images at the moment (2019-09-13),
this Action will try its best to install it before any other operations.

View File

@ -10,7 +10,9 @@ inputs:
Rust toolchain name. Rust toolchain name.
See https://github.com/rust-lang/rustup.rs#toolchain-specification See https://github.com/rust-lang/rustup.rs#toolchain-specification
required: true target:
description: Target triple to install for this toolchain
required: false
default: default:
description: Set installed toolchain as default description: Set installed toolchain as default
default: false default: false

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

800
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,7 @@
}, },
"scripts": { "scripts": {
"build": "ncc build src/main.ts --minify", "build": "ncc build src/main.ts --minify",
"watch": "ncc build src/main.ts --watch --minify", "watch": "ncc build src/main.ts --watch",
"test": "jest" "test": "jest"
}, },
"repository": { "repository": {
@ -30,7 +30,9 @@
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.0.0", "@actions/core": "^1.0.0",
"@actions/exec": "^1.0.0" "@actions/exec": "^1.0.0",
"@actions/io": "^1.0.0",
"download": "^7.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^24.0.13", "@types/jest": "^24.0.13",

View File

@ -28,6 +28,7 @@ function inputBoolean(name: string): boolean {
export interface ToolchainOptions { export interface ToolchainOptions {
name: string, name: string,
target?: string,
default: boolean, default: boolean,
override: boolean override: boolean
} }
@ -35,7 +36,8 @@ export interface ToolchainOptions {
export function toolchain_args(): ToolchainOptions { export function toolchain_args(): ToolchainOptions {
return { return {
name: getInput('toolchain', {required: true}), name: getInput('toolchain', {required: true}),
target: getInput('target') || undefined,
default: inputBoolean('default'), default: inputBoolean('default'),
override: inputBoolean('override') override: inputBoolean('override')
} };
} }

View File

@ -1,27 +1,110 @@
const os = require('os');
const fs = require('fs');
const path = require('path');
const https = require('https');
const download = require('download');
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as args from './args'; import * as args from './args';
async function do_exec(program: string, args: string[]) { function downloadRustInit(url: string, name: string): Promise<string> {
const absPath = path.join(os.tmpdir(), name);
return new Promise((resolve, reject) => {
let req = download(url);
let output = fs.createWriteStream(absPath, {
mode: 0o755
});
req.pipe(output);
req.on('end', () => {
output.close(resolve);
});
req.on('error', reject);
output.on('error', reject);
})
.then(() => {
return absPath;
});
}
async function get_rustup(toolchain: string, target?: string): Promise<string> {
try { try {
await exec.exec(program, args); const foundPath = await io.which('rustup', true);
core.debug(`Found rustup at ${foundPath}`);
return foundPath;
} catch (error) {
core.warning('Unable to find rustup, installing it now');
}
let args = [
'-y',
'--default-toolchain',
toolchain,
];
if (target) {
args.push('--default-host');
args.push(target);
}
switch (process.platform) {
case 'darwin':
case 'linux': // Should be installed already, but just in case
const rustupSh = await downloadRustInit('https://sh.rustup.rs', 'rustup-init.sh');
await do_exec(rustupSh, args);
break;
case 'win32':
const rustupExe = await downloadRustInit('http://win.rustup.rs', 'rustup-init.exe');
await do_exec(rustupExe, args);
break;
default:
throw new Error(`Unknown platform ${process.platform}, can't install rustup`);
}
core.addPath(path.join(process.env['HOME'], '.cargo', 'bin'));
return 'rustup';
}
async function do_exec(program: string, args: string[]): Promise<number> {
try {
return await exec.exec(program, args);
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
throw error;
} }
} }
async function run() { async function run() {
let opts = args.toolchain_args(); let opts;
await do_exec('rustup', ['toolchain', 'install', opts.name]); try {
opts = args.toolchain_args();
if (opts.default) { } catch (error) {
await do_exec('rustup', ['default', opts.name]); core.setFailed(error.message);
return;
} }
if (opts.override) { const rustup = await get_rustup(opts.name, opts.target);
await do_exec('rustup', ['override', 'set', opts.name]);
} // await do_exec(rustup, ['toolchain', 'install', opts.name]);
//
// if (opts.default) {
// await do_exec(rustup, ['default', opts.name]);
// }
//
// if (opts.override) {
// await do_exec(rustup, ['override', 'set', opts.name]);
// }
//
// if (opts.target) {
// await do_exec(rustup, ['target', 'add', '--toolchain', opts.name, opts.target]);
// }
} }
run(); run();