gradle-build-action/src/cache-project-dot-gradle.ts
Daz DeBoer e1f84aa44d
Restore/save configuration-cache data in first action
Previously, the action was restoring/saving the configuration-cache data for each
step that applied the action. In order to support Gradle invocations that are _not_
managed by the action, the configuration-cache restore is now performed in the initial
action step, and save is performed in the final post-action step.

The build root directories are recorded for each invocation via an init script.
2021-12-08 15:09:27 -07:00

53 lines
2.2 KiB
TypeScript

import * as core from '@actions/core'
import path from 'path'
import fs from 'fs'
import {AbstractCache, META_FILE_DIR, PROJECT_ROOTS_FILE} from './cache-base'
/**
* A simple cache that saves and restores the '.gradle/configuration-cache' directory in the project root.
*/
export class ProjectDotGradleCache extends AbstractCache {
constructor(gradleUserHome: string) {
super(gradleUserHome, 'project', 'Project configuration cache')
}
protected getCachePath(): string[] {
return this.getProjectRoots().map(x => path.resolve(x, '.gradle/configuration-cache'))
}
protected initializeGradleUserHome(gradleUserHome: string, initScriptsDir: string): void {
const projectRootCapture = path.resolve(initScriptsDir, 'project-root-capture.init.gradle')
fs.writeFileSync(
projectRootCapture,
`
// Only run again root build. Do not run against included builds.
def isTopLevelBuild = gradle.getParent() == null
if (isTopLevelBuild) {
settingsEvaluated { settings ->
def projectRootEntry = settings.rootDir.absolutePath + "\\n"
def projectRootList = new File(settings.gradle.gradleUserHomeDir, "${META_FILE_DIR}/${PROJECT_ROOTS_FILE}")
println "Adding " + projectRootEntry + " to " + projectRootList
if (!projectRootList.exists() || !projectRootList.text.contains(projectRootEntry)) {
projectRootList << projectRootEntry
}
}
}`
)
}
/**
* For every Gradle invocation, we record the project root directory. This method returns the entire
* set of project roots, to allow saving of configuration-cache entries for each.
*/
private getProjectRoots(): string[] {
const projectList = path.resolve(this.gradleUserHome, META_FILE_DIR, PROJECT_ROOTS_FILE)
if (!fs.existsSync(projectList)) {
core.info(`Missing project list file ${projectList}`)
return []
}
const projectRoots = fs.readFileSync(projectList, 'utf-8')
core.info(`Found project roots '${projectRoots}' in ${projectList}`)
return projectRoots.trim().split('\n')
}
}