2022-05-25 12:07:29 +02:00
import * as cache from '@actions/cache' ;
import * as core from '@actions/core' ;
import * as exec from '@actions/exec' ;
2023-09-15 22:43:13 +02:00
import * as _ from 'lodash'
2022-05-25 12:07:29 +02:00
import { supportedPackageManagers , PackageManagerInfo } from './package-managers' ;
2023-09-15 22:43:13 +02:00
import * as glob from "@actions/glob" ;
2022-05-25 12:07:29 +02:00
export const getCommandOutput = async ( toolCommand : string ) = > {
2023-09-15 22:43:13 +02:00
let { stdout , stderr , exitCode } = await exec . getExecOutput (
toolCommand ,
undefined ,
{ ignoreReturnCode : true }
) ;
if ( exitCode ) {
stderr = ! stderr . trim ( )
? ` The ' ${ toolCommand } ' command failed with exit code: ${ exitCode } `
: stderr ;
throw new Error ( stderr ) ;
}
return stdout . trim ( ) ;
2022-05-25 12:07:29 +02:00
} ;
export const getPackageManagerInfo = async ( packageManager : string ) = > {
2023-09-15 22:43:13 +02:00
if ( ! supportedPackageManagers [ packageManager ] ) {
throw new Error (
` It's not possible to use ${ packageManager } , please, check correctness of the package manager name spelling. `
) ;
}
const obtainedPackageManager = supportedPackageManagers [ packageManager ] ;
2022-05-25 12:07:29 +02:00
2023-09-15 22:43:13 +02:00
return obtainedPackageManager ;
2022-05-25 12:07:29 +02:00
} ;
export const getCacheDirectoryPath = async (
2023-09-15 22:43:13 +02:00
packageManagerInfo : PackageManagerInfo
2022-05-25 12:07:29 +02:00
) = > {
2023-09-15 22:43:13 +02:00
const pathOutputs = await Promise . allSettled (
packageManagerInfo . cacheFolderCommandList . map ( async command = >
getCommandOutput ( command )
)
) ;
const results = pathOutputs . map ( item = > {
if ( item . status === 'fulfilled' ) {
return item . value ;
} else {
core . info ( ` [warning]getting cache directory path failed: ${ item . reason } ` ) ;
}
2023-03-10 16:25:35 +01:00
2023-09-15 22:43:13 +02:00
return '' ;
} ) ;
2023-03-10 16:25:35 +01:00
2023-09-15 22:43:13 +02:00
const cachePaths = results . filter ( item = > item ) ;
2022-05-25 12:07:29 +02:00
2023-09-15 22:43:13 +02:00
if ( ! cachePaths . length ) {
throw new Error ( ` Could not get cache folder paths. ` ) ;
}
2022-05-25 12:07:29 +02:00
2023-09-15 22:43:13 +02:00
return cachePaths ;
2022-05-25 12:07:29 +02:00
} ;
export function isGhes ( ) : boolean {
2023-09-15 22:43:13 +02:00
const ghUrl = new URL (
process . env [ 'GITHUB_SERVER_URL' ] || 'https://github.com'
) ;
return ghUrl . hostname . toUpperCase ( ) !== 'GITHUB.COM' ;
2022-05-25 12:07:29 +02:00
}
2023-09-15 22:43:13 +02:00
/ * *
* Memoize it in order to avoid confusing multiple messages
* /
2022-05-25 12:07:29 +02:00
export function isCacheFeatureAvailable ( ) : boolean {
2023-09-15 22:43:13 +02:00
if ( cache . isFeatureAvailable ( ) ) {
return true ;
}
if ( isGhes ( ) ) {
core . warning (
'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'
) ;
return false ;
}
2022-05-25 12:07:29 +02:00
2022-12-16 23:05:54 +09:00
core . warning (
2023-09-15 22:43:13 +02:00
'The runner was not able to contact the cache service. Caching will be skipped'
2022-12-16 23:05:54 +09:00
) ;
2022-05-25 12:07:29 +02:00
return false ;
2023-09-15 22:43:13 +02:00
}
/ * *
* Checks if the caching of dependencies is requested
* - ` cache-mod ` input takes precedence over ` cache ` input if set
* /
export function needModCache ( ) : Boolean {
const cache = core . getBooleanInput ( 'cache' ) ;
const modCache = core . getInput ( 'cache-mod' ) . toLowerCase ( )
return ( modCache === 'true' || cache && modCache !== 'false' )
}
2022-05-25 12:07:29 +02:00
2023-09-15 22:43:13 +02:00
/ * *
* Checks if the caching of intermediate build files is requested
* - ` cache-mod ` input takes precedence over ` cache ` input if set
* /
export function needBuildCache ( ) : Boolean {
const cache = core . getBooleanInput ( 'cache' ) ;
const buildCache = core . getInput ( 'cache-build' ) . toLowerCase ( )
return ( buildCache === 'true' || cache && buildCache !== 'false' )
}
export function getModDependenciesPath ( ) : string {
return core . getInput ( 'cache-dependency-path' )
2022-05-25 12:07:29 +02:00
}
2023-09-15 22:43:13 +02:00
export function getBuildDependenciesPath ( ) : string {
return core . getInput ( 'cache-build-path' ) || "**/*.go"
}
export function getBuildCachePath ( ) : Promise < string > {
return getCommandOutput ( 'go env GOCACHE' )
}