2019-09-14 23:55:34 +03:00
|
|
|
const os = require('os');
|
|
|
|
const process = require('process');
|
|
|
|
|
2019-09-12 23:48:44 +03:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
|
|
|
import * as io from '@actions/io';
|
|
|
|
|
|
|
|
import * as input from './input';
|
|
|
|
|
2019-09-13 21:34:47 +03:00
|
|
|
const CROSS_REV: string = '69b8da7da287055127812c9e4b071756c2b98545';
|
|
|
|
|
2019-09-15 09:15:18 +03:00
|
|
|
// TODO: `core.info` function is not published yet as for `1.0.1` version,
|
|
|
|
// bundling it.
|
|
|
|
function core_info(message: string): void {
|
|
|
|
process.stdout.write(message + os.EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getCargo(): Promise<string> {
|
|
|
|
try {
|
|
|
|
return await io.which('cargo', true);
|
|
|
|
} catch (error) {
|
|
|
|
core_info('cargo is not installed by default for some virtual environments, \
|
|
|
|
see https://help.github.com/en/articles/software-in-virtual-environments-for-github-actions');
|
|
|
|
core_info('To install it, use this action: https://github.com/actions-rs/toolchain');
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getCross(cargoPath: string): Promise<string> {
|
2019-09-12 23:48:44 +03:00
|
|
|
try {
|
|
|
|
return await io.which('cross', true);
|
|
|
|
} catch (error) {
|
2019-09-13 22:45:36 +03:00
|
|
|
core.debug('Unable to find cross, installing it now');
|
2019-09-12 23:48:44 +03:00
|
|
|
}
|
|
|
|
|
2019-09-14 23:55:34 +03:00
|
|
|
// Somewhat new Rust is required to compile `cross`
|
|
|
|
// (TODO: Not sure what version exactly, should clarify)
|
|
|
|
// but if some action will set an override toolchain before this action called
|
|
|
|
// (ex. `@actions-rs/toolchain` with `toolchain: 1.31.0`)
|
|
|
|
// `cross` compilation will fail.
|
|
|
|
//
|
|
|
|
// In order to skip this problem and install `cross` globally
|
|
|
|
// using the pre-installed system Rust,
|
|
|
|
// we are going to jump to the tmpdir (skipping directory override that way)
|
|
|
|
// install `cross` from there and then jump back.
|
|
|
|
|
|
|
|
const cwd = process.cwd();
|
|
|
|
process.chdir(os.tmpdir());
|
2019-09-15 09:15:18 +03:00
|
|
|
|
2019-09-12 23:48:44 +03:00
|
|
|
try {
|
2019-09-13 22:45:36 +03:00
|
|
|
core.startGroup('Install cross');
|
2019-09-13 21:34:47 +03:00
|
|
|
core.warning('Git version of cross will be installed, \
|
2019-09-13 22:45:36 +03:00
|
|
|
see https://github.com/actions-rs/cargo/issues/1');
|
2019-09-15 09:15:18 +03:00
|
|
|
await exec.exec(cargoPath, [
|
2019-09-13 21:34:47 +03:00
|
|
|
'install',
|
|
|
|
'--rev',
|
|
|
|
CROSS_REV,
|
|
|
|
'--git',
|
|
|
|
'https://github.com/rust-embedded/cross.git'
|
|
|
|
]);
|
2019-09-12 23:48:44 +03:00
|
|
|
} catch (error) {
|
2019-09-15 09:15:18 +03:00
|
|
|
throw error;
|
2019-09-13 22:45:36 +03:00
|
|
|
} finally {
|
2019-09-14 23:55:34 +03:00
|
|
|
// It is important to chdir back!
|
|
|
|
process.chdir(cwd);
|
2019-09-13 22:45:36 +03:00
|
|
|
core.endGroup();
|
2019-09-12 23:48:44 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 21:34:47 +03:00
|
|
|
// Expecting it to be in PATH already
|
2019-09-12 23:48:44 +03:00
|
|
|
return 'cross';
|
|
|
|
}
|
|
|
|
|
2019-09-15 09:15:18 +03:00
|
|
|
async function run(): Promise<void> {
|
2019-09-12 23:48:44 +03:00
|
|
|
const actionInput = input.parse();
|
2019-09-15 09:15:18 +03:00
|
|
|
const cargo = await getCargo();
|
2019-09-12 23:48:44 +03:00
|
|
|
|
|
|
|
let program;
|
|
|
|
if (actionInput.useCross) {
|
2019-09-15 09:15:18 +03:00
|
|
|
program = await getCross(cargo);
|
2019-09-12 23:48:44 +03:00
|
|
|
} else {
|
2019-09-15 09:15:18 +03:00
|
|
|
program = cargo;
|
2019-09-12 23:48:44 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 21:34:47 +03:00
|
|
|
let args: string[] = [];
|
2019-09-12 23:48:44 +03:00
|
|
|
if (actionInput.toolchain) {
|
|
|
|
args.push(`+${actionInput.toolchain}`);
|
|
|
|
}
|
2019-09-13 21:34:47 +03:00
|
|
|
args.push(actionInput.command);
|
2019-09-12 23:48:44 +03:00
|
|
|
args = args.concat(actionInput.args);
|
|
|
|
|
2019-09-15 09:15:18 +03:00
|
|
|
await exec.exec(program, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main(): Promise<void> {
|
2019-09-12 23:48:44 +03:00
|
|
|
try {
|
2019-09-15 09:15:18 +03:00
|
|
|
await run();
|
2019-09-12 23:48:44 +03:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 09:15:18 +03:00
|
|
|
main();
|