2020-06-15 12:59:55 +02:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as os from 'os'
|
|
|
|
|
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as cache from '@actions/cache'
|
|
|
|
|
|
|
|
import * as github from './github-utils'
|
|
|
|
import * as crypto from './crypto-utils'
|
|
|
|
|
|
|
|
const DEPENDENCIES_CACHE_PATH = 'DEPENDENCIES_CACHE_PATH'
|
|
|
|
const DEPENDENCIES_CACHE_KEY = 'DEPENDENCIES_CACHE_KEY'
|
|
|
|
const DEPENDENCIES_CACHE_RESULT = 'DEPENDENCIES_CACHE_RESULT'
|
|
|
|
|
|
|
|
export async function restoreCachedDependencies(
|
|
|
|
rootDir: string
|
|
|
|
): Promise<void> {
|
2020-06-15 14:30:25 +02:00
|
|
|
if (isDependenciesCacheDisabled()) return
|
|
|
|
|
2020-06-15 12:59:55 +02:00
|
|
|
const cachePath = path.resolve(os.homedir(), '.gradle/caches/modules-2')
|
2020-06-22 16:40:58 +02:00
|
|
|
if (fs.existsSync(cachePath)) return
|
2020-06-15 12:59:55 +02:00
|
|
|
core.saveState(DEPENDENCIES_CACHE_PATH, cachePath)
|
|
|
|
|
2020-06-15 13:05:25 +02:00
|
|
|
const inputCacheExact = github.inputBoolean('dependencies-cache-exact')
|
2020-06-15 15:58:20 +02:00
|
|
|
const cacheKeyGlobs = inputCacheKeyGlobs('dependencies-cache-key')
|
2020-06-15 12:59:55 +02:00
|
|
|
|
|
|
|
const hash = await crypto.hashFiles(rootDir, cacheKeyGlobs)
|
|
|
|
const cacheKeyPrefix = 'dependencies-'
|
|
|
|
const cacheKey = `${cacheKeyPrefix}${hash}`
|
|
|
|
core.saveState(DEPENDENCIES_CACHE_KEY, cacheKey)
|
|
|
|
|
2020-06-15 13:05:25 +02:00
|
|
|
const cacheResult = await cache.restoreCache(
|
|
|
|
[cachePath],
|
|
|
|
cacheKey,
|
|
|
|
inputCacheExact ? [] : [cacheKeyPrefix]
|
|
|
|
)
|
2020-06-15 13:09:47 +02:00
|
|
|
|
|
|
|
if (!cacheResult) {
|
|
|
|
core.info('Dependencies cache not found, expect dependencies download.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:59:55 +02:00
|
|
|
core.saveState(DEPENDENCIES_CACHE_RESULT, cacheResult)
|
2020-06-15 13:09:47 +02:00
|
|
|
core.info(`Dependencies restored from cache key: ${cacheResult}`)
|
|
|
|
return
|
2020-06-15 12:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function cacheDependencies(): Promise<void> {
|
2020-06-15 14:30:25 +02:00
|
|
|
if (isDependenciesCacheDisabled()) return
|
|
|
|
|
2020-06-15 12:59:55 +02:00
|
|
|
const cachePath = core.getState(DEPENDENCIES_CACHE_PATH)
|
|
|
|
const cacheKey = core.getState(DEPENDENCIES_CACHE_KEY)
|
|
|
|
const cacheResult = core.getState(DEPENDENCIES_CACHE_RESULT)
|
|
|
|
|
|
|
|
if (!cachePath || !fs.existsSync(cachePath)) {
|
|
|
|
core.debug('No dependencies to cache.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheResult && cacheKey === cacheResult) {
|
|
|
|
core.info(
|
|
|
|
`Dependencies cache hit occurred on the cache key ${cacheKey}, not saving cache.`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:22:41 +02:00
|
|
|
const locksDeleted = tryDeleteFiles([
|
2020-06-15 15:41:53 +02:00
|
|
|
path.resolve(cachePath, 'modules-2.lock')
|
2020-06-15 14:22:41 +02:00
|
|
|
])
|
|
|
|
if (!locksDeleted) {
|
|
|
|
core.warning(
|
2020-06-15 15:43:21 +02:00
|
|
|
'Unable to delete dependencies lock files, try using --no-daemon or stopping the daemon last if you have several Gradle steps, not saving cache.'
|
2020-06-15 14:22:41 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:59:55 +02:00
|
|
|
try {
|
|
|
|
await cache.saveCache([cachePath], 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
|
|
|
|
}
|
2020-06-15 14:22:41 +02:00
|
|
|
|
2020-06-15 15:58:20 +02:00
|
|
|
export function tryDeleteFiles(filePaths: string[]): boolean {
|
2020-06-15 14:22:41 +02:00
|
|
|
let failure = false
|
|
|
|
for (const filePath of filePaths) {
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(filePath)
|
|
|
|
} catch (error) {
|
|
|
|
failure = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !failure
|
|
|
|
}
|
2020-06-15 14:30:25 +02:00
|
|
|
|
|
|
|
function isDependenciesCacheDisabled(): boolean {
|
2020-06-15 15:57:09 +02:00
|
|
|
return !github.inputBoolean('dependencies-cache-enabled', false)
|
2020-06-15 14:30:25 +02:00
|
|
|
}
|
2020-06-15 15:58:20 +02:00
|
|
|
|
|
|
|
export function inputCacheKeyGlobs(input: string): string[] {
|
|
|
|
const inputValue = github.inputArrayOrNull(input)
|
|
|
|
return inputValue
|
|
|
|
? inputValue
|
|
|
|
: [
|
|
|
|
'**/*.gradle',
|
|
|
|
'**/*.gradle.kts',
|
|
|
|
'**/gradle.properties',
|
|
|
|
'gradle/**'
|
|
|
|
]
|
|
|
|
}
|