rs-cargo/src/main.ts

82 lines
2.2 KiB
TypeScript
Raw Normal View History

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';
const CROSS_REV: string = '69b8da7da287055127812c9e4b071756c2b98545';
2019-09-12 23:48:44 +03:00
async function getCross(): Promise<string> {
try {
return await io.which('cross', true);
} catch (error) {
core.debug('Unable to find cross, installing it now');
2019-09-12 23:48:44 +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-12 23:48:44 +03:00
try {
core.startGroup('Install cross');
core.warning('Git version of cross will be installed, \
see https://github.com/actions-rs/cargo/issues/1');
await exec.exec('cargo', [
'install',
'--rev',
CROSS_REV,
'--git',
'https://github.com/rust-embedded/cross.git'
]);
2019-09-12 23:48:44 +03:00
} catch (error) {
core.setFailed(error.message);
throw new Error(error);
} finally {
// It is important to chdir back!
process.chdir(cwd);
core.endGroup();
2019-09-12 23:48:44 +03:00
}
// Expecting it to be in PATH already
2019-09-12 23:48:44 +03:00
return 'cross';
}
async function run() {
const actionInput = input.parse();
let program;
if (actionInput.useCross) {
program = await getCross();
} else {
program = 'cargo';
}
let args: string[] = [];
2019-09-12 23:48:44 +03:00
if (actionInput.toolchain) {
args.push(`+${actionInput.toolchain}`);
}
args.push(actionInput.command);
2019-09-12 23:48:44 +03:00
args = args.concat(actionInput.args);
try {
await exec.exec(program, args);
} catch (error) {
core.setFailed(error.message);
throw error;
2019-09-12 23:48:44 +03:00
}
}
run();