2021-12-07 16:52:53 -07:00
|
|
|
import * as core from '@actions/core'
|
2021-08-20 13:01:43 -06:00
|
|
|
import path from 'path'
|
2021-12-07 16:52:53 -07:00
|
|
|
import fs from 'fs'
|
|
|
|
import {AbstractCache, META_FILE_DIR, PROJECT_ROOTS_FILE} from './cache-base'
|
2021-08-20 13:01:43 -06:00
|
|
|
|
2021-11-28 10:19:56 -07:00
|
|
|
/**
|
|
|
|
* A simple cache that saves and restores the '.gradle/configuration-cache' directory in the project root.
|
|
|
|
*/
|
2021-09-06 11:16:08 -06:00
|
|
|
export class ProjectDotGradleCache extends AbstractCache {
|
2021-12-07 16:52:53 -07:00
|
|
|
constructor(gradleUserHome: string) {
|
|
|
|
super(gradleUserHome, 'project', 'Project configuration cache')
|
2021-08-20 13:01:43 -06:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:16:08 -06:00
|
|
|
protected getCachePath(): string[] {
|
2021-12-07 16:52:53 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
)
|
2021-08-20 13:01:43 -06:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:52:53 -07:00
|
|
|
/**
|
|
|
|
* 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')
|
2021-08-20 13:01:43 -06:00
|
|
|
}
|
|
|
|
}
|