2019-09-14 01:19:52 +08:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const https = require('https');
|
|
|
|
|
|
|
|
const download = require('download');
|
|
|
|
|
2019-09-12 21:44:29 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
2019-09-14 01:19:52 +08:00
|
|
|
import * as io from '@actions/io';
|
2019-09-12 21:44:29 +08:00
|
|
|
|
|
|
|
import * as args from './args';
|
|
|
|
|
2019-09-14 01:19:52 +08:00
|
|
|
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> {
|
2019-09-12 21:44:29 +08:00
|
|
|
try {
|
2019-09-14 01:19:52 +08:00
|
|
|
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');
|
2019-09-15 17:24:13 +08:00
|
|
|
await exec.exec(rustupSh, args);
|
2019-09-14 01:19:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'win32':
|
|
|
|
const rustupExe = await downloadRustInit('http://win.rustup.rs', 'rustup-init.exe');
|
2019-09-15 17:24:13 +08:00
|
|
|
await exec.exec(rustupExe, args);
|
2019-09-14 01:19:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown platform ${process.platform}, can't install rustup`);
|
|
|
|
}
|
|
|
|
|
|
|
|
core.addPath(path.join(process.env['HOME'], '.cargo', 'bin'));
|
|
|
|
|
|
|
|
return 'rustup';
|
|
|
|
}
|
|
|
|
|
2019-09-12 21:44:29 +08:00
|
|
|
async function run() {
|
2019-09-15 17:24:13 +08:00
|
|
|
const opts = args.toolchain_args();
|
2019-09-14 01:19:52 +08:00
|
|
|
const rustup = await get_rustup(opts.name, opts.target);
|
|
|
|
|
2019-09-15 17:24:13 +08:00
|
|
|
await exec.exec(rustup, ['toolchain', 'install', opts.name]);
|
2019-09-14 16:57:12 +08:00
|
|
|
|
|
|
|
if (opts.default) {
|
2019-09-15 17:24:13 +08:00
|
|
|
await exec.exec(rustup, ['default', opts.name]);
|
2019-09-14 16:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.override) {
|
2019-09-15 17:24:13 +08:00
|
|
|
await exec.exec(rustup, ['override', 'set', opts.name]);
|
2019-09-14 16:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.target) {
|
2019-09-15 17:24:13 +08:00
|
|
|
await exec.exec(rustup, ['target', 'add', '--toolchain', opts.name, opts.target]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
try {
|
|
|
|
await run();
|
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
2019-09-14 16:57:12 +08:00
|
|
|
}
|
2019-09-12 21:44:29 +08:00
|
|
|
}
|
|
|
|
|
2019-09-15 17:24:13 +08:00
|
|
|
main();
|