Unverified Commit 693293c2 authored by Daz DeBoer's avatar Daz DeBoer
Browse files

Consolidate logic for separate artifact file caching

Making this functionality more general will make it easier to add other
artifacts to the set that we cache separately.
parent ac5d8920
Loading
Loading
Loading
Loading
+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+51 −133
Changes for src/cache-gradle-user-home.ts: 51 added lines, 133 removed lines.
Original line number Diff line number Diff line
@@ -10,11 +10,16 @@ import {AbstractCache} from './cache-utils'

const CACHE_PATH = [
    '~/.gradle/caches/*', // All directories in 'caches'
    '!~/.gradle/caches/*/generated-gradle-jars', // Exclude generated-gradle-jars
    '~/.gradle/notifications/*', // Prevent the re-rendering of first-use message for version
    '~/.gradle/wrapper/dists/*/*/*.zip.txt' // Only wrapper zips are required : Gradle will expand these on demand
    '~/.gradle/wrapper/dists/*/*/*.zip.txt' // Only wrapper zips are required : We do not want to cache the exploded distributions
]

const DEDUPLCIATED_PATHS = [
    '~/.gradle/wrapper/dists/*/*/*.zip',
    '~/.gradle/caches/*/generated-gradle-jars/*.jar'
]
const MARKER_FILE_EXTENSION = '.txt'

export class GradleUserHomeCache extends AbstractCache {
    constructor() {
        super('gradle', 'Gradle User Home')
@@ -23,90 +28,48 @@ export class GradleUserHomeCache extends AbstractCache {
    async restore(): Promise<void> {
        await super.restore()
        await this.reportCacheEntrySize()
        await this.restoreWrapperZips()
        await this.restoreGeneratedJars()
        await this.restoreDeduplicatedPaths()
        await this.reportCacheEntrySize()
    }

    private async restoreWrapperZips(): Promise<void> {
        const globber = await glob.create(
            '~/.gradle/wrapper/dists/*/*/*.zip.txt'
        )
        const wrapperMarkers = await globber.glob()

        core.info('Found the following wrapper zips')
        for (const wrapperMarker of wrapperMarkers) {
            const wrapperZip = wrapperMarker.substring(
                0,
                wrapperMarker.length - '.txt'.length
            )
            core.info(
                `Wrapper marker: ${wrapperMarker}. Looking for zip ${wrapperZip}`
            )

            if (!fs.existsSync(wrapperZip)) {
                // Extract the wrapper URL hash
                const wrapperKey = path.basename(path.dirname(wrapperMarker))
                core.info(`Wrapper key: ${wrapperKey}`)
    private async restoreDeduplicatedPaths(): Promise<void> {
        const markerFilePatterns = DEDUPLCIATED_PATHS.map(targetPath => {
            return targetPath + MARKER_FILE_EXTENSION
        }).join('\n')

                const cacheKey = `gradle-wrapper-${wrapperKey}`
                core.info(`Cache key: ${cacheKey}. Cache path: ${wrapperZip}`)
        core.info(`Using marker file patterns: ${markerFilePatterns}`)
        const globber = await glob.create(markerFilePatterns)
        const markerFiles = await globber.glob()

                const restoreKey = await cache.restoreCache(
                    [wrapperZip],
                    cacheKey
                )
                if (restoreKey) {
                    core.info(
                        `Restored wrapper zip ${cacheKey} from cache to ${wrapperZip}`
                    )
                } else {
                    core.info(
                        `Did NOT restore wrapper zip from ${cacheKey} to ${wrapperZip}`
                    )
                }
            } else {
                core.info(`Wrapper zip file already exists: ${wrapperZip}`)
            }
        }
    }

    private async restoreGeneratedJars(): Promise<void> {
        const globber = await glob.create(
            '~/.gradle/caches/*/generated-gradle-jars/*.jar.txt'
        )
        const generatedJarMarkers = await globber.glob()

        core.info('Found the following generated jars')
        for (const jarMarker of generatedJarMarkers) {
            const generatedJar = jarMarker.substring(
        for (const markerFile of markerFiles) {
            const targetFile = markerFile.substring(
                0,
                jarMarker.length - '.txt'.length
                markerFile.length - MARKER_FILE_EXTENSION.length
            )
            core.info(
                `Jar marker: ${jarMarker}. Looking for jar ${generatedJar}`
                `Found marker file: ${markerFile}. Looking for ${targetFile}`
            )

            if (!fs.existsSync(generatedJar)) {
                // Extract the wrapper URL hash
                const jarKey = path.basename(generatedJar)
                const cacheKey = `gradle-generated-jar-${jarKey}`
                core.info(`Cache key: ${cacheKey}. Cache path: ${generatedJar}`)
            if (!fs.existsSync(targetFile)) {
                const key = path.relative(this.getGradleUserHome(), targetFile)
                const cacheKey = `gradle-dedup-${key}`
                core.info(`Cache key: ${cacheKey}. Cache path: ${targetFile}`)

                const restoreKey = await cache.restoreCache(
                    [generatedJar],
                    [targetFile],
                    cacheKey
                )
                if (restoreKey) {
                    core.info(
                        `Restored generated jar ${cacheKey} from cache to ${generatedJar}`
                        `Restored ${cacheKey} from cache to ${targetFile}`
                    )
                } else {
                    core.info(
                        `Did NOT restore generated jar from ${cacheKey} to ${generatedJar}`
                        `Did NOT restore from ${cacheKey} to ${targetFile}`
                    )
                }
            } else {
                core.info(`Generated jar file already exists: ${generatedJar}`)
                core.info(`Target file already exists: ${targetFile}`)
            }
        }
    }
@@ -125,75 +88,28 @@ export class GradleUserHomeCache extends AbstractCache {
    }

    async save(): Promise<void> {
        await this.cacheWrapperZips()
        await this.cacheGeneratedJars()
        await this.cacheDeduplicatedPaths()
        await super.save()
    }

    private async cacheWrapperZips(): Promise<void> {
        const globber = await glob.create('~/.gradle/wrapper/dists/*/*/*.zip')
        const wrapperZips = await globber.glob()

        for (const wrapperZip of wrapperZips) {
            core.info(`Wrapper zip: ${wrapperZip}`)

            const wrapperMarkerFile = `${wrapperZip}.txt`

            if (!fs.existsSync(wrapperMarkerFile)) {
                // Extract the wrapper URL hash
                const wrapperKey = path.basename(path.dirname(wrapperZip))
                core.info(`Wrapper key: ${wrapperKey}`)

                const cacheKey = `gradle-wrapper-${wrapperKey}`

                core.info(`Caching wrapper with cache key: ${cacheKey}`)
                try {
                    await cache.saveCache([wrapperZip], cacheKey)
                } catch (error) {
                    // Fail on validation errors or non-errors (the latter to keep Typescript happy)
                    if (
                        error instanceof cache.ValidationError ||
                        !(error instanceof Error)
                    ) {
                        throw error
                    }
                    // TODO : Avoid warning for reserve cache error: this is expected
                    core.warning(error.message)
                }

                // Write the marker file and delete the original
                fs.writeFileSync(wrapperMarkerFile, 'dummy')
            } else {
                core.info(
                    `Wrapper marker file already exists: ${wrapperMarkerFile}`
                )
            }
    private async cacheDeduplicatedPaths(): Promise<void> {
        const targetFilePatterns = DEDUPLCIATED_PATHS.join('\n')
        core.info(`Using target file patterns: ${targetFilePatterns}`)
        const globber = await glob.create(targetFilePatterns)
        const targetFiles = await globber.glob()

            // TODO : Should not need to delete. Just exclude from cache path.
            // Delete the wrapper
            fs.unlinkSync(wrapperZip)
        }
    }
        for (const targetFile of targetFiles) {
            core.info(`Deduplicate caching: ${targetFile}`)

    private async cacheGeneratedJars(): Promise<void> {
        const globber = await glob.create(
            '~/.gradle/caches/*/generated-gradle-jars/*.jar'
        )
        const generatedJars = await globber.glob()

        for (const generatedJar of generatedJars) {
            core.info(`Generated jar: ${generatedJar}`)
            const markerFile = `${targetFile}${MARKER_FILE_EXTENSION}`

            const generatedJarMarkerFile = `${generatedJar}.txt`
            if (!fs.existsSync(markerFile)) {
                const key = path.relative(this.getGradleUserHome(), targetFile)
                const cacheKey = `gradle-dedup-${key}`
                core.info(`Cache key: ${cacheKey}. Cache path: ${targetFile}`)

            if (!fs.existsSync(generatedJarMarkerFile)) {
                // Key by jar file name: this includes Gradle version
                const jarKey = path.basename(generatedJar)
                const cacheKey = `gradle-generated-jar-${jarKey}`

                core.info(`Caching generated jar with cache key: ${cacheKey}`)
                try {
                    await cache.saveCache([generatedJar], cacheKey)
                    await cache.saveCache([targetFile], cacheKey)
                } catch (error) {
                    // Fail on validation errors or non-errors (the latter to keep Typescript happy)
                    if (
@@ -207,26 +123,28 @@ export class GradleUserHomeCache extends AbstractCache {
                }

                // Write the marker file and delete the original
                fs.writeFileSync(generatedJarMarkerFile, 'dummy')
                fs.writeFileSync(markerFile, 'dummy')
            } else {
                core.info(
                    `Wrapper marker file already exists: ${generatedJarMarkerFile}`
                )
                core.info(`Marker file already exists: ${markerFile}`)
            }

            // TODO : Should not need to delete. Just exclude from cache path.
            // Delete the jar
            fs.unlinkSync(generatedJar)
            // Delete the target file
            fs.unlinkSync(targetFile)
        }
    }

    protected cacheOutputExists(): boolean {
        // Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
        const dir = path.resolve(os.homedir(), '.gradle/caches')
        const dir = path.resolve(this.getGradleUserHome(), 'caches')
        return fs.existsSync(dir)
    }

    protected getCachePath(): string[] {
        return CACHE_PATH
    }

    protected getGradleUserHome(): string {
        return path.resolve(os.homedir(), '.gradle')
    }
}