gradle-build-action/src/caches.ts

58 lines
2.1 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'
const CACHING_REPORT = 'CACHING_REPORT'
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 09:55:49 +08:00
return
}
2021-09-07 03:23:36 +08: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()
}
core.saveState(CACHING_REPORT, cacheListener.stringify())
2021-09-07 03:23:36 +08:00
})
}
export async function save(): Promise<void> {
if (isCacheReadOnly()) {
core.info('Cache is read-only: will not save state for use in subsequent builds.')
2021-09-06 09:55:49 +08:00
return
}
const cachingReport: CacheListener = CacheListener.rehydrate(core.getState(CACHING_REPORT))
2021-09-07 03:23:36 +08:00
await core.group('Caching Gradle state', async () => {
const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
return Promise.all([
new GradleUserHomeCache(buildRootDirectory).save(cachingReport),
new ProjectDotGradleCache(buildRootDirectory).save(cachingReport)
2021-09-07 03:23:36 +08:00
])
})
logCachingReport(cachingReport)
}
function logCachingReport(report: CacheListener): void {
core.info(JSON.stringify(report, null, 2))
}