Skip to content

Commit

Permalink
Fix configuration cache for focus task and add a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Mexator committed Aug 27, 2024
1 parent cb0d476 commit 23a8b15
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
package com.dropbox.focus

import java.io.Serializable
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.work.DisableCachingByDefault

@DisableCachingByDefault(because = "Not worth caching")
public abstract class CreateFocusSettingsTask : DefaultTask() {

@get:Input
protected abstract val projectDeps: SetProperty<DependentProjectInfo>

@get:Input
protected abstract val projectPath: Property<String>

@get:OutputFile
public abstract val settingsFile: RegularFileProperty

@get:OutputFile
public abstract val modulesToDirMapFile: RegularFileProperty

/**
* Project info required for focus.settings.gradle generation.
*/
protected data class DependentProjectInfo(
public val path: String,
public val projectDir: String,
): Serializable

init {
outputs.upToDateWhen { false }
}

@TaskAction
public fun createFocusSettings() {
val dependencies = project.collectDependencies().sortedBy { it.path }
val dependencies = projectDeps.get().sortedBy { it.path }

// generate CSV mapping from module name to its absolute path
modulesToDirMapFile.get().asFile.writer().use { writer ->
Expand All @@ -37,7 +55,7 @@ public abstract class CreateFocusSettingsTask : DefaultTask() {
}

settingsFile.get().asFile.writer().use { writer ->
writer.write("// ${project.path} specific settings\n")
writer.write("// ${projectPath.get()} specific settings\n")
writer.appendLine("//")
writer.appendLine("// This file is autogenerated by the focus task. Changes will be overwritten.")
writer.appendLine()
Expand All @@ -60,11 +78,12 @@ public abstract class CreateFocusSettingsTask : DefaultTask() {
}
}

private fun Project.collectDependencies(): Set<Project> {
val result = mutableSetOf<Project>()
private fun Project.collectDependencies(): Set<DependentProjectInfo> {
val result = mutableSetOf<DependentProjectInfo>()
fun addDependent(project: Project) {
val configuredProject = this.evaluationDependsOn(project.path)
if (result.add(configuredProject)) {
val dep = DependentProjectInfo(project.path, project.projectDir.absolutePath)
if (result.add(dep)) {
configuredProject.configurations.forEach { config ->
config.dependencies
.filterIsInstance<ProjectDependency>()
Expand All @@ -83,7 +102,10 @@ public abstract class CreateFocusSettingsTask : DefaultTask() {
group = FOCUS_TASK_GROUP
settingsFile.set(subExtension.focusSettingsFile)
modulesToDirMapFile.set(subExtension.moduleToDirMapFile)
notCompatibleWithConfigurationCache("This reads configurations from the project at action-time.")
projectPath.set(project.path)
// Collecting dependencies info in the configuration phase.
// Gradle disallows accessing project in execution phase.
projectDeps.set(project.collectDependencies())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@ class FocusPluginTest {
}

@Test
fun configurationCache() {
fun configurationCache_focus() {
val fixtureRoot = File("src/test/projects/configuration-cache-compatible")

gradleRunner
.withArguments("--configuration-cache", "focus")
.withProjectDir(fixtureRoot)
.build()

val result = gradleRunner
.withArguments("--configuration-cache", "focus")
.withProjectDir(fixtureRoot)
.build()

assertThat(result.output).contains("Reusing configuration cache.")
}

@Test
fun configurationCache_clear() {
val fixtureRoot = File("src/test/projects/configuration-cache-compatible")

gradleRunner
Expand Down

0 comments on commit 23a8b15

Please sign in to comment.