Loading src/cache-gradle-user-home.ts +10 −70 Changes for src/cache-gradle-user-home.ts: 10 added lines, 70 removed lines. Original line number Diff line number Diff line Loading @@ -2,86 +2,26 @@ import path from 'path' import fs from 'fs' import os from 'os' import * as core from '@actions/core' import * as cache from '@actions/cache' import {generateCacheKey} from './cache-utils' import {AbstractCache} from './cache-utils' 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 ] const CACHE_KEY = 'GUH_CACHE_KEY' const CACHE_RESULT = 'GUH_CACHE_RESULT' export async function restore(): Promise<void> { if (gradleUserHomeExists()) { core.info('Gradle User Home already exists. Not restoring from cache.') return export class GradleUserHomeCache extends AbstractCache { constructor() { super('gradle', 'Gradle User Home') } const cacheKey = generateCacheKey('gradle') core.saveState(CACHE_KEY, cacheKey.key) const cacheResult = await cache.restoreCache( CACHE_PATH, cacheKey.key, cacheKey.restoreKeys ) 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> { if (!gradleUserHomeExists()) { core.debug('No Gradle User Home to cache.') return } const cacheKey = core.getState(CACHE_KEY) const cacheResult = core.getState(CACHE_RESULT) 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 { protected cacheOutputExists(): 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) } protected getCachePath(): string[] { return CACHE_PATH } } src/cache-project-dot-gradle.ts +13 −78 Changes for src/cache-project-dot-gradle.ts: 13 added lines, 78 removed lines. Original line number Diff line number Diff line import path from 'path' import fs from 'fs' import * as core from '@actions/core' import * as cache from '@actions/cache' import {generateCacheKey} from './cache-utils' import {AbstractCache} from './cache-utils' const PATHS_TO_CACHE = [ 'configuration-cache' // Only configuration-cache is stored at present ] const CACHE_KEY = 'PROJECT_CACHE_KEY' const CACHE_RESULT = 'PROJECT_CACHE_RESULT' export async function restore(rootDir: string): Promise<void> { if (projectDotGradleDirExists(rootDir)) { core.info( 'Project .gradle directory already exists. Not restoring from cache.' ) return } const cacheKey = generateCacheKey('project') core.saveState(CACHE_KEY, cacheKey.key) const cacheResult = await cache.restoreCache( getCachePath(rootDir), cacheKey.key, cacheKey.restoreKeys ) if (!cacheResult) { core.info('Project .gradle cache not found. Will start with empty.') return export class ProjectDotGradleCache extends AbstractCache { private rootDir: string constructor(rootDir: string) { super('project', 'Project .gradle directory') this.rootDir = rootDir } core.info(`Project .gradle dir restored from cache key: ${cacheResult}`) return } export async function save(rootDir: string): Promise<void> { if (!projectDotGradleDirExists(rootDir)) { core.debug('No project .gradle dir to cache.') return } const cacheKey = core.getState(CACHE_KEY) const cacheResult = core.getState(CACHE_RESULT) if (!cacheKey) { core.info( 'Project .gradle dir 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 project .gradle dir with cache key: ${cacheKey}`) try { await cache.saveCache(getCachePath(rootDir), 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 protected cacheOutputExists(): boolean { const dir = this.getProjectDotGradleDir() return fs.existsSync(dir) } function getCachePath(rootDir: string): string[] { const dir = getProjectDotGradleDir(rootDir) protected getCachePath(): string[] { const dir = this.getProjectDotGradleDir() return PATHS_TO_CACHE.map(x => path.resolve(dir, x)) } function getProjectDotGradleDir(rootDir: string): string { core.debug(`Resolving .gradle dir in ${rootDir}`) return path.resolve(rootDir, '.gradle') private getProjectDotGradleDir(): string { return path.resolve(this.rootDir, '.gradle') } function projectDotGradleDirExists(rootDir: string): boolean { const dir = getProjectDotGradleDir(rootDir) core.debug(`Checking for existence of project .gradle dir: ${dir}`) return fs.existsSync(dir) } src/cache-utils.ts +95 −2 Changes for src/cache-utils.ts: 95 added lines, 2 removed lines. Original line number Diff line number Diff line import * as core from '@actions/core' import * as cache from '@actions/cache' import * as github from '@actions/github' import * as crypto from 'crypto' Loading @@ -25,7 +26,7 @@ function getCacheEnabledValue(cacheName: string): string { ) } export function generateCacheKey(cacheName: string): CacheKey { function generateCacheKey(cacheName: string): CacheKey { // Prefix can be used to force change all cache keys const cacheKeyPrefix = process.env['CACHE_KEY_PREFIX'] || '' Loading Loading @@ -70,7 +71,7 @@ export function hashStrings(values: string[]): string { return hash.digest('hex') } export class CacheKey { class CacheKey { key: string restoreKeys: string[] Loading @@ -79,3 +80,95 @@ export class CacheKey { this.restoreKeys = restoreKeys } } export abstract class AbstractCache { private cacheName: string private cacheDescription: string private cacheKeyStateKey: string private cacheResultStateKey: string constructor(cacheName: string, cacheDescription: string) { this.cacheName = cacheName this.cacheDescription = cacheDescription this.cacheKeyStateKey = `CACHE_KEY_${cacheName}` this.cacheResultStateKey = `CACHE_RESULT_${cacheName}` } async restore(): Promise<void> { if (this.cacheOutputExists()) { core.info( `${this.cacheDescription} already exists. Not restoring from cache.` ) return } const cacheKey = generateCacheKey(this.cacheName) core.saveState(this.cacheKeyStateKey, cacheKey.key) const cacheResult = await cache.restoreCache( this.getCachePath(), cacheKey.key, cacheKey.restoreKeys ) if (!cacheResult) { core.info( `${this.cacheDescription} cache not found. Will start with empty.` ) return } core.saveState(this.cacheResultStateKey, cacheResult) core.info( `${this.cacheDescription} restored from cache key: ${cacheResult}` ) return } async save(): Promise<void> { if (!this.cacheOutputExists()) { core.debug(`No ${this.cacheDescription} to cache.`) return } const cacheKey = core.getState(this.cacheKeyStateKey) const cacheResult = core.getState(this.cacheResultStateKey) if (!cacheKey) { core.info( `${this.cacheDescription} 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 ${this.cacheDescription} with cache key: ${cacheKey}` ) try { await cache.saveCache(this.getCachePath(), cacheKey) } 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 } core.warning(error.message) } return } protected abstract cacheOutputExists(): boolean protected abstract getCachePath(): string[] } src/caches.ts +6 −6 Changes for src/caches.ts: 6 added lines, 6 removed lines. Original line number Diff line number Diff line import * as cacheGradleUserHome from './cache-gradle-user-home' import * as cacheProjectDotGradle from './cache-project-dot-gradle' import {GradleUserHomeCache} from './cache-gradle-user-home' import {ProjectDotGradleCache} from './cache-project-dot-gradle' import * as core from '@actions/core' import {isCacheReadEnabled, isCacheSaveEnabled} from './cache-utils' Loading @@ -12,9 +12,9 @@ export async function restore(buildRootDirectory: string): Promise<void> { } core.startGroup('Restore Gradle state from cache') await cacheGradleUserHome.restore() core.saveState(BUILD_ROOT_DIR, buildRootDirectory) await cacheProjectDotGradle.restore(buildRootDirectory) new GradleUserHomeCache().restore() new ProjectDotGradleCache(buildRootDirectory).restore() core.endGroup() } Loading @@ -25,8 +25,8 @@ export async function save(): Promise<void> { } core.startGroup('Caching Gradle state') await cacheGradleUserHome.save() const buildRootDirectory = core.getState(BUILD_ROOT_DIR) await cacheProjectDotGradle.save(buildRootDirectory) new GradleUserHomeCache().save() new ProjectDotGradleCache(buildRootDirectory).save() core.endGroup() } src/main.ts +3 Changes for src/main.ts: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -36,6 +36,9 @@ export async function run(): Promise<void> { core.setFailed(`Gradle process exited with status ${result.status}`) } } catch (error) { if (!(error instanceof Error)) { throw error } core.setFailed(error.message) } } Loading Loading
src/cache-gradle-user-home.ts +10 −70 Changes for src/cache-gradle-user-home.ts: 10 added lines, 70 removed lines. Original line number Diff line number Diff line Loading @@ -2,86 +2,26 @@ import path from 'path' import fs from 'fs' import os from 'os' import * as core from '@actions/core' import * as cache from '@actions/cache' import {generateCacheKey} from './cache-utils' import {AbstractCache} from './cache-utils' 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 ] const CACHE_KEY = 'GUH_CACHE_KEY' const CACHE_RESULT = 'GUH_CACHE_RESULT' export async function restore(): Promise<void> { if (gradleUserHomeExists()) { core.info('Gradle User Home already exists. Not restoring from cache.') return export class GradleUserHomeCache extends AbstractCache { constructor() { super('gradle', 'Gradle User Home') } const cacheKey = generateCacheKey('gradle') core.saveState(CACHE_KEY, cacheKey.key) const cacheResult = await cache.restoreCache( CACHE_PATH, cacheKey.key, cacheKey.restoreKeys ) 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> { if (!gradleUserHomeExists()) { core.debug('No Gradle User Home to cache.') return } const cacheKey = core.getState(CACHE_KEY) const cacheResult = core.getState(CACHE_RESULT) 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 { protected cacheOutputExists(): 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) } protected getCachePath(): string[] { return CACHE_PATH } }
src/cache-project-dot-gradle.ts +13 −78 Changes for src/cache-project-dot-gradle.ts: 13 added lines, 78 removed lines. Original line number Diff line number Diff line import path from 'path' import fs from 'fs' import * as core from '@actions/core' import * as cache from '@actions/cache' import {generateCacheKey} from './cache-utils' import {AbstractCache} from './cache-utils' const PATHS_TO_CACHE = [ 'configuration-cache' // Only configuration-cache is stored at present ] const CACHE_KEY = 'PROJECT_CACHE_KEY' const CACHE_RESULT = 'PROJECT_CACHE_RESULT' export async function restore(rootDir: string): Promise<void> { if (projectDotGradleDirExists(rootDir)) { core.info( 'Project .gradle directory already exists. Not restoring from cache.' ) return } const cacheKey = generateCacheKey('project') core.saveState(CACHE_KEY, cacheKey.key) const cacheResult = await cache.restoreCache( getCachePath(rootDir), cacheKey.key, cacheKey.restoreKeys ) if (!cacheResult) { core.info('Project .gradle cache not found. Will start with empty.') return export class ProjectDotGradleCache extends AbstractCache { private rootDir: string constructor(rootDir: string) { super('project', 'Project .gradle directory') this.rootDir = rootDir } core.info(`Project .gradle dir restored from cache key: ${cacheResult}`) return } export async function save(rootDir: string): Promise<void> { if (!projectDotGradleDirExists(rootDir)) { core.debug('No project .gradle dir to cache.') return } const cacheKey = core.getState(CACHE_KEY) const cacheResult = core.getState(CACHE_RESULT) if (!cacheKey) { core.info( 'Project .gradle dir 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 project .gradle dir with cache key: ${cacheKey}`) try { await cache.saveCache(getCachePath(rootDir), 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 protected cacheOutputExists(): boolean { const dir = this.getProjectDotGradleDir() return fs.existsSync(dir) } function getCachePath(rootDir: string): string[] { const dir = getProjectDotGradleDir(rootDir) protected getCachePath(): string[] { const dir = this.getProjectDotGradleDir() return PATHS_TO_CACHE.map(x => path.resolve(dir, x)) } function getProjectDotGradleDir(rootDir: string): string { core.debug(`Resolving .gradle dir in ${rootDir}`) return path.resolve(rootDir, '.gradle') private getProjectDotGradleDir(): string { return path.resolve(this.rootDir, '.gradle') } function projectDotGradleDirExists(rootDir: string): boolean { const dir = getProjectDotGradleDir(rootDir) core.debug(`Checking for existence of project .gradle dir: ${dir}`) return fs.existsSync(dir) }
src/cache-utils.ts +95 −2 Changes for src/cache-utils.ts: 95 added lines, 2 removed lines. Original line number Diff line number Diff line import * as core from '@actions/core' import * as cache from '@actions/cache' import * as github from '@actions/github' import * as crypto from 'crypto' Loading @@ -25,7 +26,7 @@ function getCacheEnabledValue(cacheName: string): string { ) } export function generateCacheKey(cacheName: string): CacheKey { function generateCacheKey(cacheName: string): CacheKey { // Prefix can be used to force change all cache keys const cacheKeyPrefix = process.env['CACHE_KEY_PREFIX'] || '' Loading Loading @@ -70,7 +71,7 @@ export function hashStrings(values: string[]): string { return hash.digest('hex') } export class CacheKey { class CacheKey { key: string restoreKeys: string[] Loading @@ -79,3 +80,95 @@ export class CacheKey { this.restoreKeys = restoreKeys } } export abstract class AbstractCache { private cacheName: string private cacheDescription: string private cacheKeyStateKey: string private cacheResultStateKey: string constructor(cacheName: string, cacheDescription: string) { this.cacheName = cacheName this.cacheDescription = cacheDescription this.cacheKeyStateKey = `CACHE_KEY_${cacheName}` this.cacheResultStateKey = `CACHE_RESULT_${cacheName}` } async restore(): Promise<void> { if (this.cacheOutputExists()) { core.info( `${this.cacheDescription} already exists. Not restoring from cache.` ) return } const cacheKey = generateCacheKey(this.cacheName) core.saveState(this.cacheKeyStateKey, cacheKey.key) const cacheResult = await cache.restoreCache( this.getCachePath(), cacheKey.key, cacheKey.restoreKeys ) if (!cacheResult) { core.info( `${this.cacheDescription} cache not found. Will start with empty.` ) return } core.saveState(this.cacheResultStateKey, cacheResult) core.info( `${this.cacheDescription} restored from cache key: ${cacheResult}` ) return } async save(): Promise<void> { if (!this.cacheOutputExists()) { core.debug(`No ${this.cacheDescription} to cache.`) return } const cacheKey = core.getState(this.cacheKeyStateKey) const cacheResult = core.getState(this.cacheResultStateKey) if (!cacheKey) { core.info( `${this.cacheDescription} 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 ${this.cacheDescription} with cache key: ${cacheKey}` ) try { await cache.saveCache(this.getCachePath(), cacheKey) } 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 } core.warning(error.message) } return } protected abstract cacheOutputExists(): boolean protected abstract getCachePath(): string[] }
src/caches.ts +6 −6 Changes for src/caches.ts: 6 added lines, 6 removed lines. Original line number Diff line number Diff line import * as cacheGradleUserHome from './cache-gradle-user-home' import * as cacheProjectDotGradle from './cache-project-dot-gradle' import {GradleUserHomeCache} from './cache-gradle-user-home' import {ProjectDotGradleCache} from './cache-project-dot-gradle' import * as core from '@actions/core' import {isCacheReadEnabled, isCacheSaveEnabled} from './cache-utils' Loading @@ -12,9 +12,9 @@ export async function restore(buildRootDirectory: string): Promise<void> { } core.startGroup('Restore Gradle state from cache') await cacheGradleUserHome.restore() core.saveState(BUILD_ROOT_DIR, buildRootDirectory) await cacheProjectDotGradle.restore(buildRootDirectory) new GradleUserHomeCache().restore() new ProjectDotGradleCache(buildRootDirectory).restore() core.endGroup() } Loading @@ -25,8 +25,8 @@ export async function save(): Promise<void> { } core.startGroup('Caching Gradle state') await cacheGradleUserHome.save() const buildRootDirectory = core.getState(BUILD_ROOT_DIR) await cacheProjectDotGradle.save(buildRootDirectory) new GradleUserHomeCache().save() new ProjectDotGradleCache(buildRootDirectory).save() core.endGroup() }
src/main.ts +3 Changes for src/main.ts: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -36,6 +36,9 @@ export async function run(): Promise<void> { core.setFailed(`Gradle process exited with status ${result.status}`) } } catch (error) { if (!(error instanceof Error)) { throw error } core.setFailed(error.message) } } Loading