2019-10-30 19:48:49 +01:00
import * as core from "@actions/core" ;
2020-04-22 22:36:34 +02:00
import * as exec from "@actions/exec" ;
2020-03-20 21:02:11 +01:00
import * as glob from "@actions/glob" ;
2019-10-30 19:48:49 +01:00
import * as io from "@actions/io" ;
2019-11-06 19:41:45 +01:00
import * as fs from "fs" ;
2019-10-30 19:48:49 +01:00
import * as path from "path" ;
2020-03-18 14:43:56 +01:00
import * as util from "util" ;
2019-10-30 19:48:49 +01:00
import * as uuidV4 from "uuid/v4" ;
2019-11-13 16:54:39 +01:00
2020-04-17 21:46:46 +02:00
<< << << < HEAD
2020-04-22 22:36:34 +02:00
import {
CacheFilename ,
CompressionMethod ,
Outputs ,
2020-04-17 21:46:46 +02:00
RefKey ,
2020-04-22 22:36:34 +02:00
State
} from "../constants" ;
2020-04-17 21:46:46 +02:00
=== === =
import { Outputs , RefKey , State } from "../constants" ;
>>> >>> > Allow all events to access cache
2019-10-30 19:48:49 +01:00
import { ArtifactCacheEntry } from "../contracts" ;
// From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
export async function createTempDirectory ( ) : Promise < string > {
const IS_WINDOWS = process . platform === "win32" ;
let tempDirectory : string = process . env [ "RUNNER_TEMP" ] || "" ;
if ( ! tempDirectory ) {
let baseLocation : string ;
if ( IS_WINDOWS ) {
// On Windows use the USERPROFILE env variable
baseLocation = process . env [ "USERPROFILE" ] || "C:\\" ;
} else {
if ( process . platform === "darwin" ) {
baseLocation = "/Users" ;
} else {
baseLocation = "/home" ;
}
}
tempDirectory = path . join ( baseLocation , "actions" , "temp" ) ;
}
2020-03-20 21:02:11 +01:00
2019-10-30 19:48:49 +01:00
const dest = path . join ( tempDirectory , uuidV4 . default ( ) ) ;
await io . mkdirP ( dest ) ;
return dest ;
}
2019-11-06 19:41:45 +01:00
export function getArchiveFileSize ( path : string ) : number {
return fs . statSync ( path ) . size ;
}
2019-10-30 19:48:49 +01:00
export function isExactKeyMatch (
key : string ,
cacheResult? : ArtifactCacheEntry
) : boolean {
return ! ! (
cacheResult &&
cacheResult . cacheKey &&
cacheResult . cacheKey . localeCompare ( key , undefined , {
sensitivity : "accent"
} ) === 0
) ;
}
2019-11-12 22:48:02 +01:00
export function setCacheState ( state : ArtifactCacheEntry ) : void {
core . saveState ( State . CacheResult , JSON . stringify ( state ) ) ;
}
export function setCacheHitOutput ( isCacheHit : boolean ) : void {
core . setOutput ( Outputs . CacheHit , isCacheHit . toString ( ) ) ;
}
2019-10-30 19:48:49 +01:00
export function setOutputAndState (
key : string ,
cacheResult? : ArtifactCacheEntry
2019-11-12 22:48:02 +01:00
) : void {
2019-10-30 19:48:49 +01:00
setCacheHitOutput ( isExactKeyMatch ( key , cacheResult ) ) ;
// Store the cache result if it exists
cacheResult && setCacheState ( cacheResult ) ;
}
export function getCacheState ( ) : ArtifactCacheEntry | undefined {
const stateData = core . getState ( State . CacheResult ) ;
core . debug ( ` State: ${ stateData } ` ) ;
2019-11-13 22:13:00 +01:00
if ( stateData ) {
return JSON . parse ( stateData ) as ArtifactCacheEntry ;
}
return undefined ;
2019-10-30 19:48:49 +01:00
}
2019-11-21 20:37:54 +01:00
export function logWarning ( message : string ) : void {
const warningPrefix = "[warning]" ;
core . info ( ` ${ warningPrefix } ${ message } ` ) ;
}
2020-03-20 21:02:11 +01:00
export async function resolvePaths ( patterns : string [ ] ) : Promise < string [ ] > {
const paths : string [ ] = [ ] ;
const workspace = process . env [ "GITHUB_WORKSPACE" ] ? ? process . cwd ( ) ;
const globber = await glob . create ( patterns . join ( "\n" ) , {
implicitDescendants : false
} ) ;
for await ( const file of globber . globGenerator ( ) ) {
const relativeFile = path . relative ( workspace , file ) ;
core . debug ( ` Matched: ${ relativeFile } ` ) ;
// Paths are made relative so the tar entries are all relative to the root of the workspace.
paths . push ( ` ${ relativeFile } ` ) ;
2019-10-30 19:48:49 +01:00
}
2020-03-20 21:02:11 +01:00
return paths ;
2019-10-30 19:48:49 +01:00
}
2019-11-13 16:54:39 +01:00
2020-04-17 21:46:46 +02:00
// Cache token authorized for all events that are tied to a ref
2019-11-13 16:54:39 +01:00
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
export function isValidEvent ( ) : boolean {
2020-04-17 21:46:46 +02:00
return RefKey in process . env ;
2019-11-13 16:54:39 +01:00
}
2020-03-18 14:43:56 +01:00
export function unlinkFile ( path : fs.PathLike ) : Promise < void > {
return util . promisify ( fs . unlink ) ( path ) ;
}
2020-04-22 22:36:34 +02:00
2020-04-30 21:28:04 +02:00
async function getVersion ( app : string ) : Promise < string > {
2020-04-22 22:36:34 +02:00
core . debug ( ` Checking ${ app } --version ` ) ;
let versionOutput = "" ;
try {
await exec . exec ( ` ${ app } --version ` , [ ] , {
ignoreReturnCode : true ,
silent : true ,
listeners : {
stdout : ( data : Buffer ) : string = >
( versionOutput += data . toString ( ) ) ,
stderr : ( data : Buffer ) : string = >
( versionOutput += data . toString ( ) )
}
} ) ;
} catch ( err ) {
core . debug ( err . message ) ;
}
versionOutput = versionOutput . trim ( ) ;
core . debug ( versionOutput ) ;
return versionOutput ;
}
export async function getCompressionMethod ( ) : Promise < CompressionMethod > {
2020-04-30 21:28:04 +02:00
const versionOutput = await getVersion ( "zstd" ) ;
2020-04-22 22:36:34 +02:00
return versionOutput . toLowerCase ( ) . includes ( "zstd command line interface" )
? CompressionMethod . Zstd
: CompressionMethod . Gzip ;
}
export function getCacheFileName ( compressionMethod : CompressionMethod ) : string {
return compressionMethod == CompressionMethod . Zstd
? CacheFilename . Zstd
: CacheFilename . Gzip ;
}
export async function useGnuTar ( ) : Promise < boolean > {
2020-04-30 21:28:04 +02:00
const versionOutput = await getVersion ( "tar" ) ;
2020-04-22 22:36:34 +02:00
return versionOutput . toLowerCase ( ) . includes ( "gnu tar" ) ;
}