From a4e1b76f1a7ad4c028cc62f61ca7c0158b1a8868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sikora?= Date: Fri, 17 Jun 2022 16:55:36 +0200 Subject: [PATCH] Set package name eagerly when using Gradle plugin Having it provided lazily as a lambda from the extension captured the `project` property, which in turn prevented Gradle configuration cache from being enabled. --- library/docs/changelog.md | 23 +++++++++++++++++++ .../gradle/ChildFeatureFlagsInput.kt | 4 ++-- .../laboratory/gradle/FeatureFactoryInput.kt | 4 ++-- .../laboratory/gradle/FeatureFlagInput.kt | 7 +++--- .../laboratory/gradle/LaboratoryExtension.kt | 10 ++++---- .../laboratory/gradle/OptionFactoryInput.kt | 4 ++-- .../gradle/SourcedFeatureStorageInput.kt | 4 ++-- .../gradle/GenerateFeatureFlagsTaskSpec.kt | 6 ++--- .../build.gradle | 2 +- .../build.gradle | 2 +- 10 files changed, 44 insertions(+), 22 deletions(-) diff --git a/library/docs/changelog.md b/library/docs/changelog.md index 7dad1563f..9c715cf34 100644 --- a/library/docs/changelog.md +++ b/library/docs/changelog.md @@ -6,6 +6,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Gradle [configuration cache](https://docs.gradle.org/current/userguide/configuration_cache.html) support. + +### Changed +- Package name in the `laboratory` block no longer applies to all features retroactively. Before, the following configuration would generate `second.FeatureA` and `second.FeatureB`. Now, it will generate `first.FeatureA` and `second.FeatureB`. + ```groovy + laboratory { + packageName = "first" + + feature("FeatureA") { + withDefaultOption("A") + withOption("B") + } + + packageName = "second" + + feature("FeatureB") { + withDefaultOption("A") + withOption("B") + } + } + ``` + ### Removed - CoreKtx dependency. diff --git a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/ChildFeatureFlagsInput.kt b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/ChildFeatureFlagsInput.kt index 1c06635a6..75d90d2f0 100644 --- a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/ChildFeatureFlagsInput.kt +++ b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/ChildFeatureFlagsInput.kt @@ -7,7 +7,7 @@ import org.gradle.api.Action * An entry point for configuration of supervised feature flags code generation. */ public class ChildFeatureFlagsInput internal constructor( - private val packageNameProvider: () -> String, + private val parentPackageName: String, private val supervisor: () -> Supervisor, ) { private val mutableFeatureInputs = mutableListOf() @@ -18,7 +18,7 @@ public class ChildFeatureFlagsInput internal constructor( * Generates a new supervised feature flag. */ public fun feature(name: String, action: Action) { - mutableFeatureInputs += FeatureFlagInput(name, packageNameProvider, supervisor).let { input -> + mutableFeatureInputs += FeatureFlagInput(name, parentPackageName, supervisor).let { input -> action.execute(input) return@let input } diff --git a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFactoryInput.kt b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFactoryInput.kt index d5b737b06..9d836faf9 100644 --- a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFactoryInput.kt +++ b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFactoryInput.kt @@ -10,7 +10,7 @@ import io.mehow.laboratory.generator.Visibility.Public * Representation of a generated feature factory class. */ public class FeatureFactoryInput internal constructor( - private val packageNameProvider: () -> String, + private val parentPackageName: String, ) { /** * Sets whether the generated feature factory should be public or internal. @@ -24,7 +24,7 @@ public class FeatureFactoryInput internal constructor( internal fun toModel(features: List, simpleName: String) = FeatureFactoryModel( visibility = if (isPublic) Public else Internal, - className = ClassName(packageName ?: packageNameProvider(), simpleName), + className = ClassName(packageName ?: parentPackageName, simpleName), features = features, ) } diff --git a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFlagInput.kt b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFlagInput.kt index 74a56124f..cf9cd3474 100644 --- a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFlagInput.kt +++ b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/FeatureFlagInput.kt @@ -15,7 +15,7 @@ import org.gradle.api.Action */ public class FeatureFlagInput internal constructor( private val name: String, - private val packageNameProvider: () -> String, + private val parentPackageName: String, private val supervisor: (() -> Supervisor)? = null, ) { /** @@ -69,9 +69,8 @@ public class FeatureFlagInput internal constructor( private fun withOption(name: String, isDefault: Boolean, action: Action) { val option = FeatureFlagOption(name, isDefault) options += option - val packageNameProvider = { packageName ?: packageNameProvider() } val supervisorBuilder = { Supervisor(toModel(), option) } - childFeatureInputs += ChildFeatureFlagsInput(packageNameProvider, supervisorBuilder).let { input -> + childFeatureInputs += ChildFeatureFlagsInput(packageName ?: parentPackageName, supervisorBuilder).let { input -> action.execute(input) return@let input } @@ -107,7 +106,7 @@ public class FeatureFlagInput internal constructor( private fun toModel() = FeatureFlagModel( visibility = if (isPublic) Public else Internal, - className = ClassName(packageName ?: packageNameProvider(), name), + className = ClassName(packageName ?: parentPackageName, name), options = options, sourceOptions = sources, key = key, diff --git a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/LaboratoryExtension.kt b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/LaboratoryExtension.kt index 3543f2f36..2ea434d05 100644 --- a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/LaboratoryExtension.kt +++ b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/LaboratoryExtension.kt @@ -28,7 +28,7 @@ public abstract class LaboratoryExtension { * Generates a new feature in this module. */ public fun feature(name: String, action: Action) { - mutableFeatureInputs += FeatureFlagInput(name, { packageName }).let { input -> + mutableFeatureInputs += FeatureFlagInput(name, packageName).let { input -> action.execute(input) return@let input } @@ -49,7 +49,7 @@ public abstract class LaboratoryExtension { * that needs to have information about all feature flags for QA purposes. */ public fun featureFactory(action: Action) { - factoryInput = FeatureFactoryInput { packageName }.let { input -> + factoryInput = FeatureFactoryInput(packageName).let { input -> action.execute(input) return@let input } @@ -72,7 +72,7 @@ public abstract class LaboratoryExtension { * Feature storage generated by this method should be then used in the application. */ public fun sourcedStorage(action: Action) { - storageInput = SourcedFeatureStorageInput { packageName }.let { input -> + storageInput = SourcedFeatureStorageInput(packageName).let { input -> action.execute(input) return@let input } @@ -93,7 +93,7 @@ public abstract class LaboratoryExtension { * that needs to have information about all feature flags for QA purposes. */ public fun featureSourceFactory(action: Action) { - featureSourcesFactory = FeatureFactoryInput { packageName }.let { input -> + featureSourcesFactory = FeatureFactoryInput(packageName).let { input -> action.execute(input) return@let input } @@ -113,7 +113,7 @@ public abstract class LaboratoryExtension { * be visible to it during compilation. */ public fun optionFactory(action: Action) { - optionFactoryInput = OptionFactoryInput { packageName }.let { input -> + optionFactoryInput = OptionFactoryInput(packageName).let { input -> action.execute(input) return@let input } diff --git a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/OptionFactoryInput.kt b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/OptionFactoryInput.kt index 3366aedb1..8fab76480 100644 --- a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/OptionFactoryInput.kt +++ b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/OptionFactoryInput.kt @@ -10,7 +10,7 @@ import io.mehow.laboratory.generator.Visibility.Public * Representation of a generated option factory that is aware of feature flags. */ public class OptionFactoryInput internal constructor( - private val packageNameProvider: () -> String, + private val parentPackageName: String, ) { /** * Sets whether the generated option factory should be public or internal. @@ -24,7 +24,7 @@ public class OptionFactoryInput internal constructor( internal fun toModel(features: List) = OptionFactoryModel( visibility = if (isPublic) Public else Internal, - className = ClassName(packageName ?: packageNameProvider(), "GeneratedOptionFactory"), + className = ClassName(packageName ?: parentPackageName, "GeneratedOptionFactory"), features = features, ) } diff --git a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/SourcedFeatureStorageInput.kt b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/SourcedFeatureStorageInput.kt index 54937529a..e5b12b934 100644 --- a/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/SourcedFeatureStorageInput.kt +++ b/library/gradle-plugin/src/main/java/io/mehow/laboratory/gradle/SourcedFeatureStorageInput.kt @@ -9,7 +9,7 @@ import io.mehow.laboratory.generator.Visibility.Public * Representation of a generated feature storage that is aware of feature flags sources. */ public class SourcedFeatureStorageInput internal constructor( - private val packageNameProvider: () -> String, + private val parentPackageName: String, ) { /** * Sets whether the generated feature storage should be public or internal. @@ -23,7 +23,7 @@ public class SourcedFeatureStorageInput internal constructor( internal fun toModel(sourceNames: List) = SourcedFeatureStorageModel( visibility = if (isPublic) Public else Internal, - className = ClassName(packageName ?: packageNameProvider(), "SourcedGeneratedFeatureStorage"), + className = ClassName(packageName ?: parentPackageName, "SourcedGeneratedFeatureStorage"), sourceNames = sourceNames, ) } diff --git a/library/gradle-plugin/src/test/java/io/mehow/laboratory/gradle/GenerateFeatureFlagsTaskSpec.kt b/library/gradle-plugin/src/test/java/io/mehow/laboratory/gradle/GenerateFeatureFlagsTaskSpec.kt index f87f20e6d..1377dd7e5 100644 --- a/library/gradle-plugin/src/test/java/io/mehow/laboratory/gradle/GenerateFeatureFlagsTaskSpec.kt +++ b/library/gradle-plugin/src/test/java/io/mehow/laboratory/gradle/GenerateFeatureFlagsTaskSpec.kt @@ -277,15 +277,15 @@ internal class GenerateFeatureFlagsTaskSpec : StringSpec({ featureB.shouldExist() } - "switches implicit package name" { + "switches implicit package name to a new one" { val fixture = "feature-flag-package-name-implicit-switching".toFixture() gradleRunner.withProjectDir(fixture).build() - val featureA = fixture.featureFile("io.mehow.implicit.switch.FeatureA") + val featureA = fixture.featureFile("io.mehow.implicit.FeatureA") featureA.shouldExist() - featureA.readText() shouldContain "package io.mehow.implicit.switch" + featureA.readText() shouldContain "package io.mehow.implicit" val featureB = fixture.featureFile("io.mehow.implicit.switch.FeatureB") featureB.shouldExist() diff --git a/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit-override/build.gradle b/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit-override/build.gradle index ed1cd8d13..b6bbb4427 100644 --- a/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit-override/build.gradle +++ b/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit-override/build.gradle @@ -9,7 +9,7 @@ laboratory { withDefaultOption("ParentOption") { parent -> parent.feature("Child") { - packageName = "io.mehow.explicit" + it.packageName = "io.mehow.explicit" it.withDefaultOption("ChildOption") } diff --git a/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit/build.gradle b/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit/build.gradle index 9c277eda4..1c0e2224b 100644 --- a/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit/build.gradle +++ b/library/gradle-plugin/src/test/projects/feature-flag-supervisor-package-name-explicit/build.gradle @@ -7,7 +7,7 @@ laboratory { feature("Parent") { withDefaultOption("ParentOption") { parent -> parent.feature("Child") { - packageName = "io.mehow.explicit" + it.packageName = "io.mehow.explicit" it.withDefaultOption("ChildOption") }