diff --git a/focus-gradle-plugin/api/focus-gradle-plugin.api b/focus-gradle-plugin/api/focus-gradle-plugin.api index 2bb1baa..ef7d56a 100644 --- a/focus-gradle-plugin/api/focus-gradle-plugin.api +++ b/focus-gradle-plugin/api/focus-gradle-plugin.api @@ -13,6 +13,7 @@ public abstract class com/dropbox/focus/CreateFocusSettingsTask : org/gradle/api public static final field Companion Lcom/dropbox/focus/CreateFocusSettingsTask$Companion; public fun ()V public final fun createFocusSettings ()V + public abstract fun getModulesToDirMapFile ()Lorg/gradle/api/file/RegularFileProperty; public abstract fun getSettingsFile ()Lorg/gradle/api/file/RegularFileProperty; } @@ -45,6 +46,7 @@ public final class com/dropbox/focus/FocusPlugin$apply$1$1$1$inlined$sam$i$org_g public abstract class com/dropbox/focus/FocusSubExtension { public fun (Lorg/gradle/api/file/ProjectLayout;Lorg/gradle/api/model/ObjectFactory;)V public final fun getFocusSettingsFile ()Lorg/gradle/api/file/RegularFileProperty; + public final fun getModuleToDirMapFile ()Lorg/gradle/api/file/RegularFileProperty; } public abstract class com/dropbox/focus/FocusTask : org/gradle/api/DefaultTask { diff --git a/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/CreateFocusSettingsTask.kt b/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/CreateFocusSettingsTask.kt index 474fae5..47d49c7 100644 --- a/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/CreateFocusSettingsTask.kt +++ b/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/CreateFocusSettingsTask.kt @@ -3,7 +3,6 @@ package com.dropbox.focus import org.gradle.api.DefaultTask import org.gradle.api.Project import org.gradle.api.artifacts.ProjectDependency -import org.gradle.api.artifacts.SelfResolvingDependency import org.gradle.api.file.RegularFileProperty import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction @@ -15,6 +14,9 @@ public abstract class CreateFocusSettingsTask : DefaultTask() { @get:OutputFile public abstract val settingsFile: RegularFileProperty + @get:OutputFile + public abstract val modulesToDirMapFile: RegularFileProperty + init { outputs.upToDateWhen { false } } @@ -23,29 +25,38 @@ public abstract class CreateFocusSettingsTask : DefaultTask() { public fun createFocusSettings() { val dependencies = project.collectDependencies().sortedBy { it.path } + // generate CSV mapping from module name to its absolute path + modulesToDirMapFile.get().asFile.writer().use { writer -> + + dependencies + .forEach { dep -> + val projectPath = dep.path + val projectDir = dep.projectDir + writer.appendLine("$projectPath,$projectDir") + } + } + settingsFile.get().asFile.writer().use { writer -> writer.write("// ${project.path} specific settings\n") writer.appendLine("//") writer.appendLine("// This file is autogenerated by the focus task. Changes will be overwritten.") writer.appendLine() - // Add the includes statements - dependencies.forEach { dep -> - writer.appendLine("include(\"${dep.path}\")") - } - - writer.appendLine() - - // Add overrides for projects with a root that's different from the gradle path - dependencies - .forEach { dep -> - val gradleProjectPath = dep.path.substring(1).replace(":", "/") - if (project.rootDir.resolve(gradleProjectPath) != dep.projectDir) { - val gradleProjectDir = dep.projectDir.path.replace("\\", "\\\\") - // language=groovy - writer.appendLine("""project("${dep.path}").projectDir = new File('$gradleProjectDir')""") + // language=groovy + writer.append( + """ + File f = new File('${modulesToDirMapFile.get().asFile.absolutePath}') + if (f.exists()) { + f.eachLine { line -> + var values = line.split(",") + var module = values[0] + var path = values[1] + include(module) + project(module).projectDir = new File(path) } } + """.trimIndent() + ) } } @@ -71,6 +82,7 @@ public abstract class CreateFocusSettingsTask : DefaultTask() { public operator fun invoke(subExtension: FocusSubExtension): CreateFocusSettingsTask.() -> Unit = { group = FOCUS_TASK_GROUP settingsFile.set(subExtension.focusSettingsFile) + modulesToDirMapFile.set(subExtension.moduleToDirMapFile) notCompatibleWithConfigurationCache("This reads configurations from the project at action-time.") } } diff --git a/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/FocusSubExtension.kt b/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/FocusSubExtension.kt index 0ed319f..3e49fc7 100644 --- a/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/FocusSubExtension.kt +++ b/focus-gradle-plugin/src/main/kotlin/com/dropbox/focus/FocusSubExtension.kt @@ -16,4 +16,7 @@ public abstract class FocusSubExtension @Inject constructor( layout.buildDirectory.file("focus.settings.gradle") ) + public val moduleToDirMapFile: RegularFileProperty = objects.fileProperty().convention( + layout.buildDirectory.file("moduleToDirMap.csv") + ) } diff --git a/focus-gradle-plugin/src/test/kotlin/com/dropbox/focus/FocusPluginTest.kt b/focus-gradle-plugin/src/test/kotlin/com/dropbox/focus/FocusPluginTest.kt index 4762fd0..1c4fc48 100644 --- a/focus-gradle-plugin/src/test/kotlin/com/dropbox/focus/FocusPluginTest.kt +++ b/focus-gradle-plugin/src/test/kotlin/com/dropbox/focus/FocusPluginTest.kt @@ -2,7 +2,7 @@ package com.dropbox.focus import com.google.common.truth.Truth.assertThat import java.io.File -import java.time.temporal.Temporal +import java.util.regex.Pattern import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner import org.junit.Before @@ -51,17 +51,46 @@ class FocusPluginTest { } @Test - fun singleQuotePath() { - val fixtureRoot = File("src/test/projects/single-quote-path") + fun allPathLiteralsAreSingleQuote() { + val fixtureRoot = File("src/test/projects/happy-path") + gradleRunner + .withArguments(":module:focus") + .runFixture(fixtureRoot) { build() } + + val focusFileContent = + File("src/test/projects/happy-path/build/notnowhere/build/focus.settings.gradle").readText() + // has at least one single-quote path literal + assertThat(focusFileContent).matches(Pattern.compile(""".*new File\('.*'\).*""", Pattern.DOTALL)) + // no double-quote path literals + assertThat(focusFileContent).doesNotMatch(Pattern.compile(""".*new File\(".*"\).*""", Pattern.DOTALL)) + } + + @Test + fun happyPath_CsvCreated() { + val fixtureRoot = File("src/test/projects/happy-path") gradleRunner .withArguments(":module:focus") .runFixture(fixtureRoot) { build() } - val focusFileContent = File("src/test/projects/single-quote-path/build/notnowhere/build/focus.settings.gradle").readText() + val csvFileContents = File("src/test/projects/happy-path/build/notnowhere/build/moduleToDirMap.csv").readText() val absoluteFilePath = fixtureRoot.resolve("build/notnowhere").absolutePath.replace("\\", "\\\\") + // language=csv + assertThat(csvFileContents).contains(""":module,$absoluteFilePath""") + } + + @Test + fun happyPath_CsvRead() { + val fixtureRoot = File("src/test/projects/happy-path") + + gradleRunner + .withArguments(":module:focus") + .runFixture(fixtureRoot) { build() } + + val csvFilePath = File("src/test/projects/happy-path/build/notnowhere/build/moduleToDirMap.csv").absolutePath + val focusFileContent = File("src/test/projects/happy-path/build/notnowhere/build/focus.settings.gradle").readText() // language=groovy - assertThat(focusFileContent).contains("""project(":module").projectDir = new File('$absoluteFilePath')""") + assertThat(focusFileContent).contains("""File f = new File('$csvFilePath')""") } private fun GradleRunner.runFixture( diff --git a/focus-gradle-plugin/src/test/projects/single-quote-path/build.gradle b/focus-gradle-plugin/src/test/projects/happy-path/build.gradle similarity index 100% rename from focus-gradle-plugin/src/test/projects/single-quote-path/build.gradle rename to focus-gradle-plugin/src/test/projects/happy-path/build.gradle diff --git a/focus-gradle-plugin/src/test/projects/single-quote-path/module/build.gradle b/focus-gradle-plugin/src/test/projects/happy-path/module/build.gradle similarity index 100% rename from focus-gradle-plugin/src/test/projects/single-quote-path/module/build.gradle rename to focus-gradle-plugin/src/test/projects/happy-path/module/build.gradle diff --git a/focus-gradle-plugin/src/test/projects/single-quote-path/settings-all.gradle b/focus-gradle-plugin/src/test/projects/happy-path/settings-all.gradle similarity index 100% rename from focus-gradle-plugin/src/test/projects/single-quote-path/settings-all.gradle rename to focus-gradle-plugin/src/test/projects/happy-path/settings-all.gradle diff --git a/focus-gradle-plugin/src/test/projects/single-quote-path/settings.gradle b/focus-gradle-plugin/src/test/projects/happy-path/settings.gradle similarity index 100% rename from focus-gradle-plugin/src/test/projects/single-quote-path/settings.gradle rename to focus-gradle-plugin/src/test/projects/happy-path/settings.gradle