mirror of
				https://github.com/gradle/gradle-build-action.git
				synced 2025-11-04 18:08:57 +08:00 
			
		
		
		
	Rename 'report' classes to 'listener'
- `CachingReport` -> `CacheListener` - `CacheEntryReport` -> `CacheEntryListener`
This commit is contained in:
		@@ -40,41 +40,41 @@ class CacheKey {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class CachingReport {
 | 
			
		||||
    cacheEntryReports: CacheEntryReport[] = []
 | 
			
		||||
export class CacheListener {
 | 
			
		||||
    cacheEntries: CacheEntryListener[] = []
 | 
			
		||||
 | 
			
		||||
    get fullyRestored(): boolean {
 | 
			
		||||
        return this.cacheEntryReports.every(x => !x.wasRequestedButNotRestored())
 | 
			
		||||
        return this.cacheEntries.every(x => !x.wasRequestedButNotRestored())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    entryReport(name: string): CacheEntryReport {
 | 
			
		||||
        for (const report of this.cacheEntryReports) {
 | 
			
		||||
            if (report.entryName === name) {
 | 
			
		||||
                return report
 | 
			
		||||
    entry(name: string): CacheEntryListener {
 | 
			
		||||
        for (const entry of this.cacheEntries) {
 | 
			
		||||
            if (entry.entryName === name) {
 | 
			
		||||
                return entry
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const newReport = new CacheEntryReport(name)
 | 
			
		||||
        this.cacheEntryReports.push(newReport)
 | 
			
		||||
        return newReport
 | 
			
		||||
        const newEntry = new CacheEntryListener(name)
 | 
			
		||||
        this.cacheEntries.push(newEntry)
 | 
			
		||||
        return newEntry
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    stringify(): string {
 | 
			
		||||
        return JSON.stringify(this)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static rehydrate(stringRep: string): CachingReport {
 | 
			
		||||
        const rehydrated: CachingReport = Object.assign(new CachingReport(), JSON.parse(stringRep))
 | 
			
		||||
        const entryReports = rehydrated.cacheEntryReports
 | 
			
		||||
        for (let index = 0; index < entryReports.length; index++) {
 | 
			
		||||
            const rawEntryReport = entryReports[index]
 | 
			
		||||
            entryReports[index] = Object.assign(new CacheEntryReport(rawEntryReport.entryName), rawEntryReport)
 | 
			
		||||
    static rehydrate(stringRep: string): CacheListener {
 | 
			
		||||
        const rehydrated: CacheListener = Object.assign(new CacheListener(), JSON.parse(stringRep))
 | 
			
		||||
        const entries = rehydrated.cacheEntries
 | 
			
		||||
        for (let index = 0; index < entries.length; index++) {
 | 
			
		||||
            const rawEntry = entries[index]
 | 
			
		||||
            entries[index] = Object.assign(new CacheEntryListener(rawEntry.entryName), rawEntry)
 | 
			
		||||
        }
 | 
			
		||||
        return rehydrated
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class CacheEntryReport {
 | 
			
		||||
export class CacheEntryListener {
 | 
			
		||||
    entryName: string
 | 
			
		||||
    requestedKey: string | undefined
 | 
			
		||||
    requestedRestoreKeys: string[] | undefined
 | 
			
		||||
@@ -92,18 +92,18 @@ export class CacheEntryReport {
 | 
			
		||||
        return this.requestedKey !== undefined && this.restoredKey === undefined
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    markRequested(key: string, restoreKeys: string[] = []): CacheEntryReport {
 | 
			
		||||
    markRequested(key: string, restoreKeys: string[] = []): CacheEntryListener {
 | 
			
		||||
        this.requestedKey = key
 | 
			
		||||
        this.requestedRestoreKeys = restoreKeys
 | 
			
		||||
        return this
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    markRestored(key: string): CacheEntryReport {
 | 
			
		||||
    markRestored(key: string): CacheEntryListener {
 | 
			
		||||
        this.restoredKey = key
 | 
			
		||||
        return this
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    markSaved(key: string): CacheEntryReport {
 | 
			
		||||
    markSaved(key: string): CacheEntryListener {
 | 
			
		||||
        this.savedKey = key
 | 
			
		||||
        return this
 | 
			
		||||
    }
 | 
			
		||||
@@ -125,14 +125,14 @@ export abstract class AbstractCache {
 | 
			
		||||
        this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async restore(report: CachingReport): Promise<void> {
 | 
			
		||||
    async restore(listener: CacheListener): Promise<void> {
 | 
			
		||||
        if (this.cacheOutputExists()) {
 | 
			
		||||
            core.info(`${this.cacheDescription} already exists. Not restoring from cache.`)
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const cacheKey = this.prepareCacheKey()
 | 
			
		||||
        const entryReport = report.entryReport(this.cacheName)
 | 
			
		||||
        const entryReport = listener.entry(this.cacheName)
 | 
			
		||||
        entryReport.markRequested(cacheKey.key, cacheKey.restoreKeys)
 | 
			
		||||
 | 
			
		||||
        this.debug(
 | 
			
		||||
@@ -153,7 +153,7 @@ export abstract class AbstractCache {
 | 
			
		||||
        core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult}`)
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            await this.afterRestore(report)
 | 
			
		||||
            await this.afterRestore(listener)
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`)
 | 
			
		||||
        }
 | 
			
		||||
@@ -184,9 +184,9 @@ export abstract class AbstractCache {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected async afterRestore(_report: CachingReport): Promise<void> {}
 | 
			
		||||
    protected async afterRestore(_listener: CacheListener): Promise<void> {}
 | 
			
		||||
 | 
			
		||||
    async save(report: CachingReport): Promise<void> {
 | 
			
		||||
    async save(listener: CacheListener): Promise<void> {
 | 
			
		||||
        if (!this.cacheOutputExists()) {
 | 
			
		||||
            this.debug(`No ${this.cacheDescription} to cache.`)
 | 
			
		||||
            return
 | 
			
		||||
@@ -206,7 +206,7 @@ export abstract class AbstractCache {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            await this.beforeSave(report)
 | 
			
		||||
            await this.beforeSave(listener)
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`)
 | 
			
		||||
            return
 | 
			
		||||
@@ -216,12 +216,12 @@ export abstract class AbstractCache {
 | 
			
		||||
        const cachePath = this.getCachePath()
 | 
			
		||||
        await this.saveCache(cachePath, cacheKey)
 | 
			
		||||
 | 
			
		||||
        report.entryReport(this.cacheName).markSaved(cacheKey)
 | 
			
		||||
        listener.entry(this.cacheName).markSaved(cacheKey)
 | 
			
		||||
 | 
			
		||||
        return
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected async beforeSave(_report: CachingReport): Promise<void> {}
 | 
			
		||||
    protected async beforeSave(_listener: CacheListener): Promise<void> {}
 | 
			
		||||
 | 
			
		||||
    protected async saveCache(cachePath: string[], cacheKey: string): Promise<void> {
 | 
			
		||||
        try {
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import * as core from '@actions/core'
 | 
			
		||||
import * as glob from '@actions/glob'
 | 
			
		||||
import * as exec from '@actions/exec'
 | 
			
		||||
 | 
			
		||||
import {AbstractCache, CacheEntryReport, CachingReport} from './cache-base'
 | 
			
		||||
import {AbstractCache, CacheEntryListener, CacheListener} from './cache-base'
 | 
			
		||||
import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils'
 | 
			
		||||
 | 
			
		||||
const META_FILE_DIR = '.gradle-build-action'
 | 
			
		||||
@@ -22,13 +22,13 @@ export class GradleUserHomeCache extends AbstractCache {
 | 
			
		||||
        this.gradleUserHome = this.determineGradleUserHome(rootDir)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async afterRestore(report: CachingReport): Promise<void> {
 | 
			
		||||
    async afterRestore(listener: CacheListener): Promise<void> {
 | 
			
		||||
        await this.reportGradleUserHomeSize('as restored from cache')
 | 
			
		||||
        await this.restoreArtifactBundles(report)
 | 
			
		||||
        await this.restoreArtifactBundles(listener)
 | 
			
		||||
        await this.reportGradleUserHomeSize('after restoring common artifacts')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async restoreArtifactBundles(report: CachingReport): Promise<void> {
 | 
			
		||||
    private async restoreArtifactBundles(listener: CacheListener): Promise<void> {
 | 
			
		||||
        const processes: Promise<void>[] = []
 | 
			
		||||
 | 
			
		||||
        const bundleMetaFiles = await this.getBundleMetaFiles()
 | 
			
		||||
@@ -37,16 +37,16 @@ export class GradleUserHomeCache extends AbstractCache {
 | 
			
		||||
        // Iterate over all bundle meta files and try to restore
 | 
			
		||||
        for (const bundleMetaFile of bundleMetaFiles) {
 | 
			
		||||
            const bundle = path.basename(bundleMetaFile, '.cache')
 | 
			
		||||
            const bundleEntryReport = report.entryReport(bundle)
 | 
			
		||||
            const entryListener = listener.entry(bundle)
 | 
			
		||||
            const bundlePattern = bundlePatterns.get(bundle)
 | 
			
		||||
 | 
			
		||||
            // Handle case where the 'artifactBundlePatterns' have been changed
 | 
			
		||||
            if (bundlePattern === undefined) {
 | 
			
		||||
                core.info(`Found bundle metafile for ${bundle} but no such bundle defined`)
 | 
			
		||||
                bundleEntryReport.markRequested('BUNDLE_NOT_CONFIGURED')
 | 
			
		||||
                entryListener.markRequested('BUNDLE_NOT_CONFIGURED')
 | 
			
		||||
                tryDelete(bundleMetaFile)
 | 
			
		||||
            } else {
 | 
			
		||||
                const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, bundleEntryReport)
 | 
			
		||||
                const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, entryListener)
 | 
			
		||||
                // Run sequentially when debugging enabled
 | 
			
		||||
                if (this.cacheDebuggingEnabled) {
 | 
			
		||||
                    await p
 | 
			
		||||
@@ -62,15 +62,15 @@ export class GradleUserHomeCache extends AbstractCache {
 | 
			
		||||
        bundle: string,
 | 
			
		||||
        bundlePattern: string,
 | 
			
		||||
        bundleMetaFile: string,
 | 
			
		||||
        report: CacheEntryReport
 | 
			
		||||
        listener: CacheEntryListener
 | 
			
		||||
    ): Promise<void> {
 | 
			
		||||
        const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
 | 
			
		||||
        report.markRequested(cacheKey)
 | 
			
		||||
        listener.markRequested(cacheKey)
 | 
			
		||||
 | 
			
		||||
        const restoredKey = await this.restoreCache([bundlePattern], cacheKey)
 | 
			
		||||
        if (restoredKey) {
 | 
			
		||||
            core.info(`Restored ${bundle} with key ${cacheKey} to ${bundlePattern}`)
 | 
			
		||||
            report.markRestored(restoredKey)
 | 
			
		||||
            listener.markRestored(restoredKey)
 | 
			
		||||
        } else {
 | 
			
		||||
            core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`)
 | 
			
		||||
            tryDelete(bundleMetaFile)
 | 
			
		||||
@@ -88,10 +88,10 @@ export class GradleUserHomeCache extends AbstractCache {
 | 
			
		||||
        return bundleFiles
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async beforeSave(report: CachingReport): Promise<void> {
 | 
			
		||||
    async beforeSave(listener: CacheListener): Promise<void> {
 | 
			
		||||
        await this.reportGradleUserHomeSize('before saving common artifacts')
 | 
			
		||||
        this.removeExcludedPaths()
 | 
			
		||||
        await this.saveArtifactBundles(report)
 | 
			
		||||
        await this.saveArtifactBundles(listener)
 | 
			
		||||
        await this.reportGradleUserHomeSize(
 | 
			
		||||
            "after saving common artifacts (only 'caches' and 'notifications' will be stored)"
 | 
			
		||||
        )
 | 
			
		||||
@@ -107,12 +107,12 @@ export class GradleUserHomeCache extends AbstractCache {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async saveArtifactBundles(report: CachingReport): Promise<void> {
 | 
			
		||||
    private async saveArtifactBundles(listener: CacheListener): Promise<void> {
 | 
			
		||||
        const processes: Promise<void>[] = []
 | 
			
		||||
        for (const [bundle, pattern] of this.getArtifactBundles()) {
 | 
			
		||||
            const bundleEntryReport = report.entryReport(bundle)
 | 
			
		||||
            const entryListener = listener.entry(bundle)
 | 
			
		||||
 | 
			
		||||
            const p = this.saveArtifactBundle(bundle, pattern, bundleEntryReport)
 | 
			
		||||
            const p = this.saveArtifactBundle(bundle, pattern, entryListener)
 | 
			
		||||
            // Run sequentially when debugging enabled
 | 
			
		||||
            if (this.cacheDebuggingEnabled) {
 | 
			
		||||
                await p
 | 
			
		||||
@@ -123,7 +123,11 @@ export class GradleUserHomeCache extends AbstractCache {
 | 
			
		||||
        await Promise.all(processes)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async saveArtifactBundle(bundle: string, artifactPath: string, report: CacheEntryReport): Promise<void> {
 | 
			
		||||
    private async saveArtifactBundle(
 | 
			
		||||
        bundle: string,
 | 
			
		||||
        artifactPath: string,
 | 
			
		||||
        listener: CacheEntryListener
 | 
			
		||||
    ): Promise<void> {
 | 
			
		||||
        const bundleMetaFile = this.getBundleMetaFile(bundle)
 | 
			
		||||
 | 
			
		||||
        const globber = await glob.create(artifactPath, {
 | 
			
		||||
@@ -152,7 +156,7 @@ export class GradleUserHomeCache extends AbstractCache {
 | 
			
		||||
            core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
 | 
			
		||||
            await this.saveCache([artifactPath], cacheKey)
 | 
			
		||||
            this.writeBundleMetaFile(bundleMetaFile, cacheKey)
 | 
			
		||||
            report.markSaved(cacheKey)
 | 
			
		||||
            listener.markSaved(cacheKey)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (const file of bundleFiles) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ 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 {CachingReport} from './cache-base'
 | 
			
		||||
import {CacheListener} from './cache-base'
 | 
			
		||||
 | 
			
		||||
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
 | 
			
		||||
const CACHING_REPORT = 'CACHING_REPORT'
 | 
			
		||||
@@ -16,20 +16,20 @@ export async function restore(buildRootDirectory: string): Promise<void> {
 | 
			
		||||
    await core.group('Restore Gradle state from cache', async () => {
 | 
			
		||||
        core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
 | 
			
		||||
 | 
			
		||||
        const cachingReport = new CachingReport()
 | 
			
		||||
        await new GradleUserHomeCache(buildRootDirectory).restore(cachingReport)
 | 
			
		||||
        const cacheListener = new CacheListener()
 | 
			
		||||
        await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener)
 | 
			
		||||
 | 
			
		||||
        const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
 | 
			
		||||
        if (cachingReport.fullyRestored) {
 | 
			
		||||
        if (cacheListener.fullyRestored) {
 | 
			
		||||
            // Only restore the configuration-cache if the Gradle Home is fully restored
 | 
			
		||||
            await projectDotGradleCache.restore(cachingReport)
 | 
			
		||||
            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, cachingReport.stringify())
 | 
			
		||||
        core.saveState(CACHING_REPORT, cacheListener.stringify())
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -39,7 +39,7 @@ export async function save(): Promise<void> {
 | 
			
		||||
        return
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const cachingReport: CachingReport = CachingReport.rehydrate(core.getState(CACHING_REPORT))
 | 
			
		||||
    const cachingReport: CacheListener = CacheListener.rehydrate(core.getState(CACHING_REPORT))
 | 
			
		||||
 | 
			
		||||
    await core.group('Caching Gradle state', async () => {
 | 
			
		||||
        const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
 | 
			
		||||
@@ -52,6 +52,6 @@ export async function save(): Promise<void> {
 | 
			
		||||
    logCachingReport(cachingReport)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function logCachingReport(report: CachingReport): void {
 | 
			
		||||
function logCachingReport(report: CacheListener): void {
 | 
			
		||||
    core.info(JSON.stringify(report, null, 2))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user