2021-08-19 13:19:35 -04:00
/ * *
* @fileoverview this file provides methods handling dependency cache
* /
2023-03-09 07:49:35 -05:00
import { join } from 'path' ;
2021-08-19 13:19:35 -04:00
import os from 'os' ;
import * as cache from '@actions/cache' ;
import * as core from '@actions/core' ;
import * as glob from '@actions/glob' ;
const STATE_CACHE_PRIMARY_KEY = 'cache-primary-key' ;
const CACHE_MATCHED_KEY = 'cache-matched-key' ;
const CACHE_KEY_PREFIX = 'setup-java' ;
interface PackageManager {
2022-04-20 10:26:27 -04:00
id : 'maven' | 'gradle' | 'sbt' ;
2021-08-19 13:19:35 -04:00
/ * *
* Paths of the file that specify the files to cache .
* /
path : string [ ] ;
pattern : string [ ] ;
}
const supportedPackageManager : PackageManager [ ] = [
{
id : 'maven' ,
path : [ join ( os . homedir ( ) , '.m2' , 'repository' ) ] ,
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---maven
pattern : [ '**/pom.xml' ]
} ,
{
id : 'gradle' ,
2023-03-09 07:49:35 -05:00
path : [
join ( os . homedir ( ) , '.gradle' , 'caches' ) ,
join ( os . homedir ( ) , '.gradle' , 'wrapper' )
] ,
2021-08-19 13:19:35 -04:00
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---gradle
2022-04-25 07:40:12 -04:00
pattern : [
'**/*.gradle*' ,
'**/gradle-wrapper.properties' ,
'buildSrc/**/Versions.kt' ,
2022-10-17 12:34:41 -04:00
'buildSrc/**/Dependencies.kt' ,
'gradle/*.versions.toml'
2022-04-25 07:40:12 -04:00
]
2022-04-20 10:26:27 -04:00
} ,
{
id : 'sbt' ,
path : [
join ( os . homedir ( ) , '.ivy2' , 'cache' ) ,
join ( os . homedir ( ) , '.sbt' ) ,
2022-05-25 05:39:10 -04:00
getCoursierCachePath ( ) ,
2022-06-15 02:10:31 -04:00
// Some files should not be cached to avoid resolution problems.
// In particular the resolution of snapshots (ideological gap between maven/ivy).
2022-05-25 05:39:10 -04:00
'!' + join ( os . homedir ( ) , '.sbt' , '*.lock' ) ,
'!' + join ( os . homedir ( ) , '**' , 'ivydata-*.properties' )
2022-04-20 10:26:27 -04:00
] ,
2023-03-09 07:49:35 -05:00
pattern : [
'**/*.sbt' ,
'**/project/build.properties' ,
2023-04-10 03:56:26 -04:00
'**/project/**.scala' ,
'**/project/**.sbt'
2023-03-09 07:49:35 -05:00
]
2021-08-19 13:19:35 -04:00
}
] ;
2022-04-20 10:26:27 -04:00
function getCoursierCachePath ( ) : string {
if ( os . type ( ) === 'Linux' ) return join ( os . homedir ( ) , '.cache' , 'coursier' ) ;
2023-03-09 07:49:35 -05:00
if ( os . type ( ) === 'Darwin' )
return join ( os . homedir ( ) , 'Library' , 'Caches' , 'Coursier' ) ;
2022-04-20 10:26:27 -04:00
return join ( os . homedir ( ) , 'AppData' , 'Local' , 'Coursier' , 'Cache' ) ;
}
2021-08-19 13:19:35 -04:00
function findPackageManager ( id : string ) : PackageManager {
2023-03-09 07:49:35 -05:00
const packageManager = supportedPackageManager . find (
packageManager = > packageManager . id === id
) ;
2021-08-19 13:19:35 -04:00
if ( packageManager === undefined ) {
throw new Error ( ` unknown package manager specified: ${ id } ` ) ;
}
return packageManager ;
}
/ * *
* A function that generates a cache key to use .
* Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}" " .
* If there is no file matched to { @link PackageManager . path } , the generated key ends with a dash ( - ) .
* @see { @link https : //docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows#matching-a-cache-key|spec of cache key}
* /
async function computeCacheKey ( packageManager : PackageManager ) {
const hash = await glob . hashFiles ( packageManager . pattern . join ( '\n' ) ) ;
return ` ${ CACHE_KEY_PREFIX } - ${ process . env [ 'RUNNER_OS' ] } - ${ packageManager . id } - ${ hash } ` ;
}
/ * *
* Restore the dependency cache
* @param id ID of the package manager , should be "maven" or "gradle"
* /
export async function restore ( id : string ) {
const packageManager = findPackageManager ( id ) ;
const primaryKey = await computeCacheKey ( packageManager ) ;
core . debug ( ` primary key is ${ primaryKey } ` ) ;
core . saveState ( STATE_CACHE_PRIMARY_KEY , primaryKey ) ;
if ( primaryKey . endsWith ( '-' ) ) {
throw new Error (
2022-06-15 02:23:10 -04:00
` No file in ${ process . cwd ( ) } matched to [ ${
packageManager . pattern
2021-08-19 13:19:35 -04:00
} ] , make sure you have checked out the target repository `
) ;
}
2022-01-13 10:39:37 -05:00
// No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269)
const matchedKey = await cache . restoreCache ( packageManager . path , primaryKey ) ;
2021-08-19 13:19:35 -04:00
if ( matchedKey ) {
core . saveState ( CACHE_MATCHED_KEY , matchedKey ) ;
2022-04-07 04:58:22 -04:00
core . setOutput ( 'cache-hit' , matchedKey === primaryKey ) ;
2021-08-19 13:19:35 -04:00
core . info ( ` Cache restored from key: ${ matchedKey } ` ) ;
} else {
2022-04-07 04:58:22 -04:00
core . setOutput ( 'cache-hit' , false ) ;
2021-08-19 13:19:35 -04:00
core . info ( ` ${ packageManager . id } cache is not found ` ) ;
}
}
/ * *
* Save the dependency cache
* @param id ID of the package manager , should be "maven" or "gradle"
* /
export async function save ( id : string ) {
const packageManager = findPackageManager ( id ) ;
const matchedKey = core . getState ( CACHE_MATCHED_KEY ) ;
2022-09-09 07:35:58 -04:00
// Inputs are re-evaluated before the post action, so we want the original key used for restore
2021-08-19 13:19:35 -04:00
const primaryKey = core . getState ( STATE_CACHE_PRIMARY_KEY ) ;
if ( ! primaryKey ) {
core . warning ( 'Error retrieving key from state.' ) ;
return ;
} else if ( matchedKey === primaryKey ) {
// no change in target directories
2023-03-09 07:49:35 -05:00
core . info (
` Cache hit occurred on the primary key ${ primaryKey } , not saving cache. `
) ;
2021-08-19 13:19:35 -04:00
return ;
}
try {
await cache . saveCache ( packageManager . path , primaryKey ) ;
core . info ( ` Cache saved with the key: ${ primaryKey } ` ) ;
} catch ( error ) {
if ( error . name === cache . ReserveCacheError . name ) {
core . info ( error . message ) ;
} else {
if ( isProbablyGradleDaemonProblem ( packageManager , error ) ) {
core . warning (
'Failed to save Gradle cache on Windows. If tar.exe reported "Permission denied", try to run Gradle with `--no-daemon` option. Refer to https://github.com/actions/cache/issues/454 for details.'
) ;
}
throw error ;
}
}
}
/ * *
* @param packageManager the specified package manager by user
* @param error the error thrown by the saveCache
* @returns true if the given error seems related to the { @link https : //github.com/actions/cache/issues/454|running Gradle Daemon issue}.
* @see { @link https : //github.com/actions/cache/issues/454#issuecomment-840493935|why --no-daemon is necessary}
* /
2023-03-09 07:49:35 -05:00
function isProbablyGradleDaemonProblem (
packageManager : PackageManager ,
error : Error
) {
if (
packageManager . id !== 'gradle' ||
process . env [ 'RUNNER_OS' ] !== 'Windows'
) {
2021-08-19 13:19:35 -04:00
return false ;
}
const message = error . message || '' ;
return message . startsWith ( 'Tar failed with error: ' ) ;
}