2021-09-07 01:16:08 +08:00
|
|
|
import {GradleUserHomeCache} from './cache-gradle-user-home'
|
|
|
|
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
|
2021-08-21 03:01:43 +08:00
|
|
|
import * as core from '@actions/core'
|
2021-10-30 21:15:20 +08:00
|
|
|
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
2021-10-30 21:21:27 +08:00
|
|
|
import {CacheListener} from './cache-base'
|
2021-08-21 03:01:43 +08:00
|
|
|
|
|
|
|
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
|
2021-10-30 00:41:30 +08:00
|
|
|
const CACHING_REPORT = 'CACHING_REPORT'
|
2021-08-21 03:01:43 +08:00
|
|
|
|
|
|
|
export async function restore(buildRootDirectory: string): Promise<void> {
|
2021-09-13 04:26:38 +08:00
|
|
|
if (isCacheDisabled()) {
|
2021-10-29 21:34:44 +08:00
|
|
|
core.info('Cache is disabled: will not restore state from previous builds.')
|
2021-09-06 09:55:49 +08:00
|
|
|
return
|
|
|
|
}
|
2021-09-04 01:25:55 +08:00
|
|
|
|
2021-09-07 03:23:36 +08:00
|
|
|
await core.group('Restore Gradle state from cache', async () => {
|
|
|
|
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
|
2021-10-30 00:19:35 +08:00
|
|
|
|
2021-10-30 21:21:27 +08:00
|
|
|
const cacheListener = new CacheListener()
|
|
|
|
await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener)
|
2021-10-29 22:44:08 +08:00
|
|
|
|
|
|
|
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
|
2021-10-30 21:21:27 +08:00
|
|
|
if (cacheListener.fullyRestored) {
|
2021-10-29 22:44:08 +08:00
|
|
|
// Only restore the configuration-cache if the Gradle Home is fully restored
|
2021-10-30 21:21:27 +08:00
|
|
|
await projectDotGradleCache.restore(cacheListener)
|
2021-10-29 22:44:08 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, prepare the cache key for later save()
|
2021-10-30 00:41:30 +08:00
|
|
|
core.info('Gradle Home cache not fully restored: not restoring configuration-cache state')
|
2021-10-29 22:44:08 +08:00
|
|
|
projectDotGradleCache.prepareCacheKey()
|
|
|
|
}
|
2021-10-30 00:41:30 +08:00
|
|
|
|
2021-10-30 21:21:27 +08:00
|
|
|
core.saveState(CACHING_REPORT, cacheListener.stringify())
|
2021-09-07 03:23:36 +08:00
|
|
|
})
|
2021-08-21 03:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function save(): Promise<void> {
|
2021-09-13 04:26:38 +08:00
|
|
|
if (isCacheReadOnly()) {
|
2021-10-29 21:34:44 +08:00
|
|
|
core.info('Cache is read-only: will not save state for use in subsequent builds.')
|
2021-09-06 09:55:49 +08:00
|
|
|
return
|
|
|
|
}
|
2021-09-04 01:25:55 +08:00
|
|
|
|
2021-10-30 21:21:27 +08:00
|
|
|
const cachingReport: CacheListener = CacheListener.rehydrate(core.getState(CACHING_REPORT))
|
2021-10-30 00:41:30 +08:00
|
|
|
|
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([
|
2021-10-30 00:41:30 +08:00
|
|
|
new GradleUserHomeCache(buildRootDirectory).save(cachingReport),
|
|
|
|
new ProjectDotGradleCache(buildRootDirectory).save(cachingReport)
|
2021-09-07 03:23:36 +08:00
|
|
|
])
|
|
|
|
})
|
2021-10-30 00:41:30 +08:00
|
|
|
|
|
|
|
logCachingReport(cachingReport)
|
|
|
|
}
|
|
|
|
|
2021-10-30 21:21:27 +08:00
|
|
|
function logCachingReport(report: CacheListener): void {
|
2021-10-30 00:41:30 +08:00
|
|
|
core.info(JSON.stringify(report, null, 2))
|
2021-08-21 03:01:43 +08:00
|
|
|
}
|