mirror of
https://github.com/gradle/gradle-build-action.git
synced 2025-12-18 09:22:22 +08:00
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.
This commit is contained in:
@@ -1,27 +1,52 @@
|
||||
import * as core from '@actions/core'
|
||||
import path from 'path'
|
||||
import {AbstractCache} from './cache-base'
|
||||
|
||||
// TODO: Maybe allow the user to override / tweak this set
|
||||
const PATHS_TO_CACHE = [
|
||||
'configuration-cache' // Only configuration-cache is stored at present
|
||||
]
|
||||
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 {
|
||||
private rootDir: string
|
||||
constructor(rootDir: string) {
|
||||
super('project', 'Project configuration cache')
|
||||
this.rootDir = rootDir
|
||||
constructor(gradleUserHome: string) {
|
||||
super(gradleUserHome, 'project', 'Project configuration cache')
|
||||
}
|
||||
|
||||
protected getCachePath(): string[] {
|
||||
const dir = this.getProjectDotGradleDir()
|
||||
return PATHS_TO_CACHE.map(x => path.resolve(dir, x))
|
||||
return this.getProjectRoots().map(x => path.resolve(x, '.gradle/configuration-cache'))
|
||||
}
|
||||
|
||||
private getProjectDotGradleDir(): string {
|
||||
return path.resolve(this.rootDir, '.gradle')
|
||||
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')
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user