Unverified Commit ae74c014 authored by Daz DeBoer's avatar Daz DeBoer
Browse files

Use a BuildService to always collect project root

Using `settingsEvaluated` meant that the project root was not recorded
when the build was run with a config-cache hit. This meant that the subsequent
build would not restore the config-cache, resulting in a cache miss.

In order to avoid issues running the init script on older versions of Gradle
the project-collection is extracted into a separate groovy file that is only
applied conditionally on Gradle 7 or higher.
parent cde06327
Loading
Loading
Loading
Loading
+1 −1
Changes for README.md: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -195,7 +195,7 @@ By default, this action aims to cache any and all reusable state that may be spe
The state that is cached includes:
- Any distributions downloaded to satisfy a `gradle-version` parameter ;
- A subset of the Gradle User Home directory, including downloaded dependencies, wrapper distributions, and the local build cache ;
- Any [configuration-cache](https://docs.gradle.org/nightly/userguide/configuration_cache.html) data stored in the project `.gradle` directory.
- Any [configuration-cache](https://docs.gradle.org/nightly/userguide/configuration_cache.html) data stored in the project `.gradle` directory. (Only supported for Gradle 7 or higher.)

To reduce the space required for caching, this action makes a best effort to reduce duplication in cache entries.

+5 −1
Changes for src/cache-base.ts: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -170,7 +170,11 @@ export class GradleStateCache {
        const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
        fs.appendFileSync(propertiesFile, 'org.gradle.daemon=false')

        const initScriptFilenames = ['build-result-capture.init.gradle', 'project-root-capture.init.gradle']
        const initScriptFilenames = [
            'build-result-capture.init.gradle',
            'project-root-capture.init.gradle',
            'project-root-capture.plugin.groovy'
        ]
        for (const initScriptFilename of initScriptFilenames) {
            const initScriptContent = this.readResourceAsString(initScriptFilename)
            const initScriptPath = path.resolve(initScriptsDir, initScriptFilename)
+7 −9
Changes for src/resources/project-root-capture.init.gradle: 7 added lines, 9 removed lines.
Original line number Diff line number Diff line
// Capture the build root directory for each executed Gradle build.
import org.gradle.util.GradleVersion

// Only run against 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, "project-roots.txt")
        if (!projectRootList.exists() || !projectRootList.text.contains(projectRootEntry)) {
            projectRootList << projectRootEntry
        }
    }
// Only record configuration-cache entries for Gradle 7+
def isAtLeastGradle7 = GradleVersion.current() >= GradleVersion.version('7.0')

if (isTopLevelBuild && isAtLeastGradle7) {   
    apply from: 'project-root-capture.plugin.groovy'
}
+44
Changes for src/resources/project-root-capture.plugin.groovy: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line

/*
 * Capture the build root directory for each executed Gradle build.
 * This is used to save/restore configuration-cache files, so:
 * - The implementation only makes sense if it's config-cache compatible
 * - We only need to support Gradle 7+
 */

import org.gradle.tooling.events.*

println "Applying Project tracker plugin"

settingsEvaluated { settings ->
    def rootDir = settings.rootDir.absolutePath
    def rootListLocation = new File(settings.gradle.gradleUserHomeDir, "project-roots.txt").absolutePath

    def projectTracker = gradle.sharedServices.registerIfAbsent("gradle-build-action-projectRootTracker", ProjectTracker, { spec ->
        spec.getParameters().getRootDir().set(rootDir);
        spec.getParameters().getRootListLocation().set(rootListLocation);
    })

    gradle.services.get(BuildEventsListenerRegistry).onTaskCompletion(projectTracker)
}

abstract class ProjectTracker implements BuildService<ProjectTracker.Params>, OperationCompletionListener, AutoCloseable {
    interface Params extends BuildServiceParameters {
        Property<String> getRootDir();
        Property<String> getRootListLocation();
    }

    public void onFinish(FinishEvent finishEvent) {}

    @Override
    public void close() {
        print "Closing ProjectTracker"
        def rootDir = getParameters().getRootDir().get()
        def rootDirEntry = rootDir + '\n'
        def rootListFile = new File(getParameters().getRootListLocation().get())
        if (!rootListFile.exists() || !rootListFile.text.contains(rootDirEntry)) {
            println "Added rootDir entry to list: ${rootDir}"
            rootListFile << rootDirEntry
        }
    }
}
 No newline at end of file