Use @actions-rs/core for cargo/cross execution.

Add problem matcher for cargo output
This commit is contained in:
svartalf 2019-09-25 10:08:33 +03:00
parent 5c0786fa0f
commit e35786a472
12 changed files with 109 additions and 116 deletions

View file

@ -2,37 +2,10 @@
* Parse action input into a some proper thing.
*/
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {input} from '@actions-rs/core';
import stringArgv from 'string-argv';
// Workaround for a GH bug: https://github.com/actions/toolkit/issues/127
//
// For input `all-features: true` it will generate the `INPUT_ALL-FEATURES: true`
// env variable, which looks too weird.
// Here we are trying to get proper name `INPUT_NO_DEFAULT_FEATURES` first,
// and if it does not exist, trying the `INPUT_NO-DEFAULT-FEATURES`
function getInput(name: string): string {
const inputFullName = name.replace(/-/g, '_');
let value = core.getInput(inputFullName);
if (value.length > 0) {
return value
}
return core.getInput(name)
}
function getInputBool(name: string): boolean {
const value = getInput(name);
if (value && (value == 'true' || value == '1')) {
return true;
} else {
return false;
}
}
// Parsed action input
export interface Input {
command: string,
@ -41,14 +14,14 @@ export interface Input {
useCross: boolean,
}
export function parse(): Input {
const command = getInput('command');
const args = stringArgv(getInput('args'));
let toolchain = getInput('toolchain');
export function get(): Input {
const command = input.getInput('command', {required: true});
const args = stringArgv(input.getInput('args'));
let toolchain = input.getInput('toolchain');
if (toolchain.startsWith('+')) {
toolchain = toolchain.slice(1);
}
const useCross = getInputBool('use-cross');
const useCross = input.getInputBool('use-cross');
return {
command: command,

View file

@ -1,79 +1,16 @@
const os = require('os');
const process = require('process');
const path = require('path');
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as input from './input';
import {Cargo, Cross} from '@actions-rs/core';
const CROSS_REV: string = '69b8da7da287055127812c9e4b071756c2b98545';
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> {
try {
return await io.which('cross', true);
} catch (error) {
core.debug('Unable to find cross, installing it now');
}
// 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());
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(cargoPath, [
'install',
'--rev',
CROSS_REV,
'--git',
'https://github.com/rust-embedded/cross.git'
]);
} catch (error) {
throw error;
} finally {
// It is important to chdir back!
process.chdir(cwd);
core.endGroup();
}
// Expecting it to be in PATH already
return 'cross';
}
async function run(): Promise<void> {
const actionInput = input.parse();
const cargo = await getCargo();
export async function run(actionInput: input.Input): Promise<void> {
let program;
if (actionInput.useCross) {
program = await getCross(cargo);
program = await Cross.getOrInstall();
} else {
program = cargo;
program = await Cargo.get();
}
let args: string[] = [];
@ -83,12 +20,17 @@ async function run(): Promise<void> {
args.push(actionInput.command);
args = args.concat(actionInput.args);
await exec.exec(program, args);
await program.call(args);
}
async function main(): Promise<void> {
const matchersPath = path.join(__dirname, '.matchers');
console.log(`##[add-matcher]${path.join(matchersPath, 'rust.json')}`);
const actionInput = input.get();
try {
await run();
await run(actionInput);
} catch (error) {
core.setFailed(error.message);
}