gradle-build-action/src/caches.ts

65 lines
2.4 KiB
TypeScript
Raw Normal View History

import {GradleUserHomeCache} from './cache-gradle-user-home'
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
import * as core from '@actions/core'
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
import {CacheListener} from './cache-base'
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
2021-10-30 13:39:21 +00:00
const CACHE_LISTENER = 'CACHE_LISTENER'
export async function restore(buildRootDirectory: string): Promise<void> {
if (isCacheDisabled()) {
core.info('Cache is disabled: will not restore state from previous builds.')
2021-09-06 01:55:49 +00:00
return
}
2021-09-06 19:23:36 +00:00
await core.group('Restore Gradle state from cache', async () => {
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
const cacheListener = new CacheListener()
await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener)
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
if (cacheListener.fullyRestored) {
// Only restore the configuration-cache if the Gradle Home is fully restored
await projectDotGradleCache.restore(cacheListener)
} else {
// Otherwise, prepare the cache key for later save()
core.info('Gradle Home cache not fully restored: not restoring configuration-cache state')
projectDotGradleCache.prepareCacheKey()
}
2021-10-30 13:39:21 +00:00
core.saveState(CACHE_LISTENER, cacheListener.stringify())
2021-09-06 19:23:36 +00:00
})
}
export async function save(): Promise<void> {
2021-10-30 13:39:21 +00:00
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
if (isCacheReadOnly()) {
core.info('Cache is read-only: will not save state for use in subsequent builds.')
2021-10-30 13:39:21 +00:00
logCachingReport(cacheListener)
2021-09-06 01:55:49 +00:00
return
}
2021-09-06 19:23:36 +00:00
await core.group('Caching Gradle state', async () => {
const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
return Promise.all([
2021-10-30 13:39:21 +00:00
new GradleUserHomeCache(buildRootDirectory).save(cacheListener),
new ProjectDotGradleCache(buildRootDirectory).save(cacheListener)
2021-09-06 19:23:36 +00:00
])
})
2021-10-30 13:39:21 +00:00
logCachingReport(cacheListener)
}
2021-10-30 13:39:21 +00:00
function logCachingReport(listener: CacheListener): void {
core.info('---------- CACHING REPORT -------------')
for (const entry of listener.cacheEntries) {
core.info(`${entry.entryName}
Requested Key: ${entry.requestedKey ?? ''}
Restored Key : ${entry.restoredKey ?? ''}
Saved Key : ${entry.savedKey ?? ''}`)
}
}