const os = require ( 'os' ) ; const path = require ( 'path' ) ; import * as core from '@actions/core' ; import * as io from '@actions/io' ; import * as exec from '@actions/exec' ; import { Cargo } from '@actions-rs/core' ; import * as configuration from './configuration' ; export class Grcov { private readonly path : string ; private constructor ( path ) { this . path = path ; } public static async get ( ) { try { const path = await io . which ( 'grcov' , true ) ; return new Grcov ( path ) ; } catch ( error ) { core . info ( 'grcov is not installed, installing now' ) ; } const cargo = await Cargo . get ( ) ; try { core . startGroup ( 'Install grcov' ) ; await cargo . call ( [ 'install' , 'grcov' ] ) ; } catch ( error ) { throw error ; } finally { core . endGroup ( ) ; } return new Grcov ( 'grcov' ) ; } public async call ( config , archive ) { const postfix = Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ; const reportPath = config . user . outputPath ? path . resolve ( config . user . outputPath ) : path . join ( os . tmpdir ( ) , `grcov-report-${ postfix }` ) ; const args = this . buildArgs ( config , archive , reportPath ) ; try { core . startGroup ( 'Execute grcov' ) ; await exec . exec ( this . path , args ) ; } catch ( error ) { throw error ; } finally { core . endGroup ( ) ; } core . info ( `Generated coverage report at ${ reportPath }` ) ; return reportPath ; } buildArgs ( config , fromArchive , toFile ) { let args = [ fromArchive ] ; if ( config . user . branch ) { args . push ( '--branch' ) ; } if ( config . user . ignoreNotExisting ) { args . push ( '--ignore-not-existing' ) ; } if ( config . user . llvm ) { args . push ( '--llvm' ) ; } args . push ( '--commit-sha' ) ; args . push ( config . system . commitSha ) ; if ( config . user . filter ) { args . push ( '--filter' ) ; args . push ( config . user . filter ) ; } if ( config . user . ignore ) { for ( const dir of config . user . ignore ) { args . push ( '--ignore' ) ; args . push ( dir ) ; } } if ( config . user . pathMapping ) { for ( const dir of config . user . pathMapping ) { args . push ( '--path-mapping' ) ; args . push ( dir ) ; } } args . push ( '--output-path' ) ; args . push ( toFile ) ; if ( config . user . outputType ) { args . push ( '--output-type' ) ; args . push ( config . user . outputType ) ; } if ( config . user . prefixDir ) { args . push ( '--prefix-dir' ) ; args . push ( config . user . prefixDir ) ; } args . push ( '--service-name' ) ; args . push ( config . system . serviceName ) ; args . push ( '--source-dir' ) ; args . push ( config . system . workspace ) ; return args ; } }