target input and installing rustup if not available
This commit is contained in:
parent
efa07bee10
commit
7d6ef4b233
|
@ -26,9 +26,13 @@ jobs:
|
|||
|
||||
## 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`
|
||||
* `target`: Additionally install specific target for this toolchain (ex. `x86_64-apple-darwin`)
|
||||
* `default`: Set installed toolchain as default (executes `rustup toolchain default {TOOLCHAIN}`)
|
||||
* `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.
|
||||
|
|
|
@ -10,7 +10,9 @@ inputs:
|
|||
Rust toolchain name.
|
||||
|
||||
See https://github.com/rust-lang/rustup.rs#toolchain-specification
|
||||
required: true
|
||||
target:
|
||||
description: Target triple to install for this toolchain
|
||||
required: false
|
||||
default:
|
||||
description: Set installed toolchain as default
|
||||
default: false
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
|
@ -10,7 +10,7 @@
|
|||
},
|
||||
"scripts": {
|
||||
"build": "ncc build src/main.ts --minify",
|
||||
"watch": "ncc build src/main.ts --watch --minify",
|
||||
"watch": "ncc build src/main.ts --watch",
|
||||
"test": "jest"
|
||||
},
|
||||
"repository": {
|
||||
|
@ -30,7 +30,9 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@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": {
|
||||
"@types/jest": "^24.0.13",
|
||||
|
|
|
@ -28,6 +28,7 @@ function inputBoolean(name: string): boolean {
|
|||
|
||||
export interface ToolchainOptions {
|
||||
name: string,
|
||||
target?: string,
|
||||
default: boolean,
|
||||
override: boolean
|
||||
}
|
||||
|
@ -35,7 +36,8 @@ export interface ToolchainOptions {
|
|||
export function toolchain_args(): ToolchainOptions {
|
||||
return {
|
||||
name: getInput('toolchain', {required: true}),
|
||||
target: getInput('target') || undefined,
|
||||
default: inputBoolean('default'),
|
||||
override: inputBoolean('override')
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
103
src/main.ts
103
src/main.ts
|
@ -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 exec from '@actions/exec';
|
||||
import * as io from '@actions/io';
|
||||
|
||||
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 {
|
||||
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) {
|
||||
core.setFailed(error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function run() {
|
||||
let opts = args.toolchain_args();
|
||||
await do_exec('rustup', ['toolchain', 'install', opts.name]);
|
||||
|
||||
if (opts.default) {
|
||||
await do_exec('rustup', ['default', opts.name]);
|
||||
let opts;
|
||||
try {
|
||||
opts = args.toolchain_args();
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (opts.override) {
|
||||
await do_exec('rustup', ['override', 'set', opts.name]);
|
||||
}
|
||||
const rustup = await get_rustup(opts.name, opts.target);
|
||||
|
||||
// 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();
|
||||
|
|
Loading…
Reference in New Issue