2021-08-22 22:14:47 -04:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import os from 'os'
|
2021-09-11 11:10:44 -04:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as glob from '@actions/glob'
|
|
|
|
import * as cache from '@actions/cache'
|
2021-09-11 14:08:18 -04:00
|
|
|
import * as exec from '@actions/exec'
|
2021-08-22 22:14:47 -04:00
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
import {AbstractCache} from './cache-utils'
|
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
|
2021-09-11 16:50:34 -04:00
|
|
|
'~/.gradle/wrapper/dists/*/*/*.zip.txt' // Only wrapper zips are required : We do not want to cache the exploded distributions
|
2021-08-22 22:14:47 -04:00
|
|
|
]
|
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
const DEDUPLCIATED_PATHS = [
|
|
|
|
'~/.gradle/wrapper/dists/*/*/*.zip',
|
2021-09-11 17:40:26 -04:00
|
|
|
'~/.gradle/caches/*/generated-gradle-jars/*.jar',
|
|
|
|
'~/.gradle/caches/modules-*/files-*/**/*.jar'
|
2021-09-11 16:50:34 -04:00
|
|
|
]
|
|
|
|
const MARKER_FILE_EXTENSION = '.txt'
|
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
export class GradleUserHomeCache extends AbstractCache {
|
|
|
|
constructor() {
|
|
|
|
super('gradle', 'Gradle User Home')
|
2021-08-22 22:14:47 -04:00
|
|
|
}
|
|
|
|
|
2021-09-11 11:10:44 -04:00
|
|
|
async restore(): Promise<void> {
|
|
|
|
await super.restore()
|
2021-09-11 14:08:18 -04:00
|
|
|
await this.reportCacheEntrySize()
|
2021-09-11 16:50:34 -04:00
|
|
|
await this.restoreDeduplicatedPaths()
|
|
|
|
await this.reportCacheEntrySize()
|
2021-09-11 14:08:18 -04:00
|
|
|
}
|
2021-09-11 11:10:44 -04:00
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
private async restoreDeduplicatedPaths(): Promise<void> {
|
|
|
|
const markerFilePatterns = DEDUPLCIATED_PATHS.map(targetPath => {
|
|
|
|
return targetPath + MARKER_FILE_EXTENSION
|
|
|
|
}).join('\n')
|
2021-09-11 11:10:44 -04:00
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
core.info(`Using marker file patterns: ${markerFilePatterns}`)
|
|
|
|
const globber = await glob.create(markerFilePatterns)
|
|
|
|
const markerFiles = await globber.glob()
|
2021-09-11 14:08:18 -04:00
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
for (const markerFile of markerFiles) {
|
|
|
|
const targetFile = markerFile.substring(
|
2021-09-11 14:08:18 -04:00
|
|
|
0,
|
2021-09-11 16:50:34 -04:00
|
|
|
markerFile.length - MARKER_FILE_EXTENSION.length
|
2021-09-11 14:08:18 -04:00
|
|
|
)
|
|
|
|
core.info(
|
2021-09-11 16:50:34 -04:00
|
|
|
`Found marker file: ${markerFile}. Looking for ${targetFile}`
|
2021-09-11 14:08:18 -04:00
|
|
|
)
|
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
if (!fs.existsSync(targetFile)) {
|
|
|
|
const key = path.relative(this.getGradleUserHome(), targetFile)
|
|
|
|
const cacheKey = `gradle-dedup-${key}`
|
|
|
|
core.info(`Cache key: ${cacheKey}. Cache path: ${targetFile}`)
|
2021-09-11 14:08:18 -04:00
|
|
|
|
|
|
|
const restoreKey = await cache.restoreCache(
|
2021-09-11 16:50:34 -04:00
|
|
|
[targetFile],
|
2021-09-11 14:08:18 -04:00
|
|
|
cacheKey
|
|
|
|
)
|
|
|
|
if (restoreKey) {
|
|
|
|
core.info(
|
2021-09-11 16:50:34 -04:00
|
|
|
`Restored ${cacheKey} from cache to ${targetFile}`
|
2021-09-11 14:08:18 -04:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
core.info(
|
2021-09-11 16:50:34 -04:00
|
|
|
`Did NOT restore from ${cacheKey} to ${targetFile}`
|
2021-09-11 14:08:18 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-11 16:50:34 -04:00
|
|
|
core.info(`Target file already exists: ${targetFile}`)
|
2021-09-11 14:08:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async reportCacheEntrySize(): Promise<void> {
|
|
|
|
const gradleUserHome = path.resolve(os.homedir(), '.gradle')
|
|
|
|
if (!fs.existsSync(gradleUserHome)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
core.info('Gradle User Home cache entry size summary')
|
|
|
|
await exec.exec('du', ['-h', '-c', '-t', '5M'], {
|
|
|
|
cwd: gradleUserHome,
|
|
|
|
ignoreReturnCode: true
|
|
|
|
})
|
|
|
|
core.info('-----------')
|
|
|
|
}
|
|
|
|
|
2021-09-11 11:10:44 -04:00
|
|
|
async save(): Promise<void> {
|
2021-09-11 16:50:34 -04:00
|
|
|
await this.cacheDeduplicatedPaths()
|
2021-09-11 14:08:18 -04:00
|
|
|
await super.save()
|
|
|
|
}
|
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
private async cacheDeduplicatedPaths(): Promise<void> {
|
|
|
|
const targetFilePatterns = DEDUPLCIATED_PATHS.join('\n')
|
|
|
|
core.info(`Using target file patterns: ${targetFilePatterns}`)
|
|
|
|
const globber = await glob.create(targetFilePatterns)
|
|
|
|
const targetFiles = await globber.glob()
|
2021-09-11 11:10:44 -04:00
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
for (const targetFile of targetFiles) {
|
|
|
|
core.info(`Deduplicate caching: ${targetFile}`)
|
2021-09-11 11:10:44 -04:00
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
const markerFile = `${targetFile}${MARKER_FILE_EXTENSION}`
|
2021-09-11 11:10:44 -04:00
|
|
|
|
2021-09-11 16:50:34 -04:00
|
|
|
if (!fs.existsSync(markerFile)) {
|
|
|
|
const key = path.relative(this.getGradleUserHome(), targetFile)
|
|
|
|
const cacheKey = `gradle-dedup-${key}`
|
|
|
|
core.info(`Cache key: ${cacheKey}. Cache path: ${targetFile}`)
|
2021-09-11 11:10:44 -04:00
|
|
|
|
|
|
|
try {
|
2021-09-11 16:50:34 -04:00
|
|
|
await cache.saveCache([targetFile], cacheKey)
|
2021-09-11 11:10:44 -04:00
|
|
|
} catch (error) {
|
|
|
|
// Fail on validation errors or non-errors (the latter to keep Typescript happy)
|
|
|
|
if (
|
|
|
|
error instanceof cache.ValidationError ||
|
|
|
|
!(error instanceof Error)
|
|
|
|
) {
|
|
|
|
throw error
|
|
|
|
}
|
2021-09-11 14:08:18 -04:00
|
|
|
// TODO : Avoid warning for reserve cache error: this is expected
|
2021-09-11 11:10:44 -04:00
|
|
|
core.warning(error.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the marker file and delete the original
|
2021-09-11 16:50:34 -04:00
|
|
|
fs.writeFileSync(markerFile, 'dummy')
|
2021-09-11 11:10:44 -04:00
|
|
|
} else {
|
2021-09-11 16:50:34 -04:00
|
|
|
core.info(`Marker file already exists: ${markerFile}`)
|
2021-09-11 11:10:44 -04:00
|
|
|
}
|
2021-09-11 14:08:18 -04:00
|
|
|
|
|
|
|
// TODO : Should not need to delete. Just exclude from cache path.
|
2021-09-11 16:50:34 -04:00
|
|
|
// Delete the target file
|
|
|
|
fs.unlinkSync(targetFile)
|
2021-09-11 14:08:18 -04:00
|
|
|
}
|
2021-09-11 11:10:44 -04:00
|
|
|
}
|
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
protected cacheOutputExists(): boolean {
|
|
|
|
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
|
2021-09-11 16:50:34 -04:00
|
|
|
const dir = path.resolve(this.getGradleUserHome(), 'caches')
|
2021-09-06 13:16:08 -04:00
|
|
|
return fs.existsSync(dir)
|
2021-08-22 22:14:47 -04:00
|
|
|
}
|
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
protected getCachePath(): string[] {
|
|
|
|
return CACHE_PATH
|
2021-08-22 22:14:47 -04:00
|
|
|
}
|
2021-09-11 16:50:34 -04:00
|
|
|
|
|
|
|
protected getGradleUserHome(): string {
|
|
|
|
return path.resolve(os.homedir(), '.gradle')
|
|
|
|
}
|
2021-08-22 22:14:47 -04:00
|
|
|
}
|