2021-08-22 22:14:47 -04:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import os from 'os'
|
|
|
|
|
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as cache from '@actions/cache'
|
2021-08-24 14:57:17 -04:00
|
|
|
import {
|
2021-09-05 19:10:47 -04:00
|
|
|
generateCacheKey,
|
2021-08-24 14:57:17 -04:00
|
|
|
isCacheReadEnabled,
|
2021-09-05 19:10:47 -04:00
|
|
|
isCacheSaveEnabled
|
2021-08-24 14:57:17 -04:00
|
|
|
} from './cache-utils'
|
2021-08-22 22:14:47 -04:00
|
|
|
|
2021-08-24 14:57:17 -04:00
|
|
|
const CACHE_NAME = 'gradle-user-home'
|
2021-08-22 22:14:47 -04:00
|
|
|
const CACHE_PATH = [
|
|
|
|
'~/.gradle/caches/*', // All directories in 'caches'
|
|
|
|
'~/.gradle/notifications/*', // Prevent the re-rendering of first-use message for version
|
|
|
|
'~/.gradle/wrapper/dists/*/*/*.zip' // Only wrapper zips are required : Gradle will expand these on demand
|
|
|
|
]
|
2021-08-20 15:01:43 -04:00
|
|
|
const CACHE_KEY = 'GUH_CACHE_KEY'
|
|
|
|
const CACHE_RESULT = 'GUH_CACHE_RESULT'
|
2021-08-22 22:14:47 -04:00
|
|
|
|
|
|
|
export async function restore(): Promise<void> {
|
2021-08-24 14:57:17 -04:00
|
|
|
if (!isCacheReadEnabled(CACHE_NAME)) return
|
2021-08-22 22:14:47 -04:00
|
|
|
|
|
|
|
if (gradleUserHomeExists()) {
|
|
|
|
core.debug('Gradle User Home already exists. Not restoring from cache.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 19:10:47 -04:00
|
|
|
const cacheKey = generateCacheKey('gradle')
|
2021-08-22 22:14:47 -04:00
|
|
|
|
2021-09-05 19:10:47 -04:00
|
|
|
core.saveState(CACHE_KEY, cacheKey.key)
|
2021-08-22 22:14:47 -04:00
|
|
|
|
2021-09-05 19:10:47 -04:00
|
|
|
const cacheResult = await cache.restoreCache(
|
|
|
|
CACHE_PATH,
|
|
|
|
cacheKey.key,
|
|
|
|
cacheKey.restoreKeys
|
|
|
|
)
|
2021-08-22 22:14:47 -04:00
|
|
|
|
|
|
|
if (!cacheResult) {
|
|
|
|
core.info(
|
|
|
|
'Gradle User Home cache not found. Will start with empty home.'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
core.info(`Gradle User Home restored from cache key: ${cacheResult}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function save(): Promise<void> {
|
2021-08-24 14:57:17 -04:00
|
|
|
if (!isCacheSaveEnabled(CACHE_NAME)) return
|
2021-08-22 22:14:47 -04:00
|
|
|
|
|
|
|
if (!gradleUserHomeExists()) {
|
|
|
|
core.debug('No Gradle User Home to cache.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:01:43 -04:00
|
|
|
const cacheKey = core.getState(CACHE_KEY)
|
|
|
|
const cacheResult = core.getState(CACHE_RESULT)
|
2021-08-22 22:14:47 -04:00
|
|
|
|
|
|
|
if (!cacheKey) {
|
|
|
|
core.info(
|
|
|
|
'Gradle User Home existed prior to cache restore. Not saving.'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheResult && cacheKey === cacheResult) {
|
|
|
|
core.info(
|
|
|
|
`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
core.info(`Caching Gradle User Home with cache key: ${cacheKey}`)
|
|
|
|
try {
|
|
|
|
await cache.saveCache(CACHE_PATH, cacheKey)
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name === cache.ValidationError.name) {
|
|
|
|
throw error
|
|
|
|
} else if (error.name === cache.ReserveCacheError.name) {
|
|
|
|
core.info(error.message)
|
|
|
|
} else {
|
|
|
|
core.info(`[warning] ${error.message}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
function gradleUserHomeExists(): boolean {
|
|
|
|
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
|
|
|
|
const dir = path.resolve(os.homedir(), '.gradle/caches')
|
|
|
|
return fs.existsSync(dir)
|
|
|
|
}
|