2021-09-06 13:16:08 -04:00
|
|
|
import {GradleUserHomeCache} from './cache-gradle-user-home'
|
|
|
|
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
|
2021-08-20 15:01:43 -04:00
|
|
|
import * as core from '@actions/core'
|
2021-10-30 09:15:20 -04:00
|
|
|
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
2021-10-30 14:17:41 -04:00
|
|
|
import {CacheEntryListener, CacheListener} from './cache-base'
|
2021-08-20 15:01:43 -04:00
|
|
|
|
|
|
|
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
|
2021-10-30 09:39:21 -04:00
|
|
|
const CACHE_LISTENER = 'CACHE_LISTENER'
|
2021-08-20 15:01:43 -04:00
|
|
|
|
|
|
|
export async function restore(buildRootDirectory: string): Promise<void> {
|
2021-09-12 16:26:38 -04:00
|
|
|
if (isCacheDisabled()) {
|
2021-10-29 09:34:44 -04:00
|
|
|
core.info('Cache is disabled: will not restore state from previous builds.')
|
2021-09-05 21:55:49 -04:00
|
|
|
return
|
|
|
|
}
|
2021-09-03 13:25:55 -04:00
|
|
|
|
2021-09-06 15:23:36 -04:00
|
|
|
await core.group('Restore Gradle state from cache', async () => {
|
|
|
|
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
|
2021-10-29 12:19:35 -04:00
|
|
|
|
2021-10-30 09:21:27 -04:00
|
|
|
const cacheListener = new CacheListener()
|
|
|
|
await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener)
|
2021-10-29 10:44:08 -04:00
|
|
|
|
|
|
|
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
|
2021-10-30 09:21:27 -04:00
|
|
|
if (cacheListener.fullyRestored) {
|
2021-10-29 10:44:08 -04:00
|
|
|
// Only restore the configuration-cache if the Gradle Home is fully restored
|
2021-10-30 09:21:27 -04:00
|
|
|
await projectDotGradleCache.restore(cacheListener)
|
2021-10-29 10:44:08 -04:00
|
|
|
} else {
|
|
|
|
// Otherwise, prepare the cache key for later save()
|
2021-10-29 12:41:30 -04:00
|
|
|
core.info('Gradle Home cache not fully restored: not restoring configuration-cache state')
|
2021-10-29 10:44:08 -04:00
|
|
|
projectDotGradleCache.prepareCacheKey()
|
|
|
|
}
|
2021-10-29 12:41:30 -04:00
|
|
|
|
2021-10-30 09:39:21 -04:00
|
|
|
core.saveState(CACHE_LISTENER, cacheListener.stringify())
|
2021-09-06 15:23:36 -04:00
|
|
|
})
|
2021-08-20 15:01:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function save(): Promise<void> {
|
2021-10-30 09:39:21 -04:00
|
|
|
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
|
|
|
|
|
2021-09-12 16:26:38 -04:00
|
|
|
if (isCacheReadOnly()) {
|
2021-10-29 09:34:44 -04:00
|
|
|
core.info('Cache is read-only: will not save state for use in subsequent builds.')
|
2021-10-30 09:39:21 -04:00
|
|
|
logCachingReport(cacheListener)
|
2021-09-05 21:55:49 -04:00
|
|
|
return
|
|
|
|
}
|
2021-09-03 13:25:55 -04:00
|
|
|
|
2021-09-06 15:23:36 -04:00
|
|
|
await core.group('Caching Gradle state', async () => {
|
|
|
|
const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
|
|
|
|
return Promise.all([
|
2021-10-30 09:39:21 -04:00
|
|
|
new GradleUserHomeCache(buildRootDirectory).save(cacheListener),
|
|
|
|
new ProjectDotGradleCache(buildRootDirectory).save(cacheListener)
|
2021-09-06 15:23:36 -04:00
|
|
|
])
|
|
|
|
})
|
2021-10-29 12:41:30 -04:00
|
|
|
|
2021-10-30 09:39:21 -04:00
|
|
|
logCachingReport(cacheListener)
|
2021-10-29 12:41:30 -04:00
|
|
|
}
|
|
|
|
|
2021-10-30 09:39:21 -04:00
|
|
|
function logCachingReport(listener: CacheListener): void {
|
2021-10-30 14:17:41 -04:00
|
|
|
core.info(`---------- Caching Summary -------------
|
|
|
|
Restored Entries Count: ${getCount(listener.cacheEntries, e => e.restoredSize)}
|
|
|
|
Size: ${getSum(listener.cacheEntries, e => e.restoredSize)}
|
|
|
|
Saved Entries Count: ${getCount(listener.cacheEntries, e => e.savedSize)}
|
|
|
|
Size: ${getSum(listener.cacheEntries, e => e.savedSize)}`)
|
|
|
|
|
|
|
|
core.startGroup('Cache Entry details')
|
2021-10-30 09:39:21 -04:00
|
|
|
for (const entry of listener.cacheEntries) {
|
2021-10-30 14:17:41 -04:00
|
|
|
core.info(`Entry: ${entry.entryName}
|
|
|
|
Requested Key : ${entry.requestedKey ?? ''}
|
|
|
|
Restored Key : ${entry.restoredKey ?? ''}
|
|
|
|
Size: ${formatSize(entry.restoredSize)}
|
|
|
|
Saved Key : ${entry.savedKey ?? ''}
|
|
|
|
Size: ${formatSize(entry.savedSize)}`)
|
|
|
|
}
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCount(
|
|
|
|
cacheEntries: CacheEntryListener[],
|
|
|
|
predicate: (value: CacheEntryListener) => number | undefined
|
|
|
|
): number {
|
|
|
|
return cacheEntries.filter(e => predicate(e) !== undefined).length
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSum(
|
|
|
|
cacheEntries: CacheEntryListener[],
|
|
|
|
predicate: (value: CacheEntryListener) => number | undefined
|
|
|
|
): string {
|
|
|
|
return formatSize(cacheEntries.map(e => predicate(e) ?? 0).reduce((p, v) => p + v, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatSize(bytes: number | undefined): string {
|
|
|
|
if (bytes === undefined || bytes === 0) {
|
|
|
|
return ''
|
2021-10-30 09:39:21 -04:00
|
|
|
}
|
2021-10-30 14:17:41 -04:00
|
|
|
return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)`
|
2021-08-20 15:01:43 -04:00
|
|
|
}
|