Skip to content

Commit

Permalink
Set package name eagerly when using Gradle plugin
Browse files Browse the repository at this point in the history
Having it provided lazily as a lambda from the extension captured the `project` property, which in turn prevented Gradle configuration cache from being enabled.
  • Loading branch information
MiSikora committed Jun 21, 2022
1 parent 4ce59ff commit a4e1b76
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 22 deletions.
23 changes: 23 additions & 0 deletions library/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeatureFlagInput>()
Expand All @@ -18,7 +18,7 @@ public class ChildFeatureFlagsInput internal constructor(
* Generates a new supervised feature flag.
*/
public fun feature(name: String, action: Action<FeatureFlagInput>) {
mutableFeatureInputs += FeatureFlagInput(name, packageNameProvider, supervisor).let { input ->
mutableFeatureInputs += FeatureFlagInput(name, parentPackageName, supervisor).let { input ->
action.execute(input)
return@let input
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,7 +24,7 @@ public class FeatureFactoryInput internal constructor(

internal fun toModel(features: List<FeatureFlagModel>, simpleName: String) = FeatureFactoryModel(
visibility = if (isPublic) Public else Internal,
className = ClassName(packageName ?: packageNameProvider(), simpleName),
className = ClassName(packageName ?: parentPackageName, simpleName),
features = features,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
/**
Expand Down Expand Up @@ -69,9 +69,8 @@ public class FeatureFlagInput internal constructor(
private fun withOption(name: String, isDefault: Boolean, action: Action<ChildFeatureFlagsInput>) {
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
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class LaboratoryExtension {
* Generates a new feature in this module.
*/
public fun feature(name: String, action: Action<FeatureFlagInput>) {
mutableFeatureInputs += FeatureFlagInput(name, { packageName }).let { input ->
mutableFeatureInputs += FeatureFlagInput(name, packageName).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -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<FeatureFactoryInput>) {
factoryInput = FeatureFactoryInput { packageName }.let { input ->
factoryInput = FeatureFactoryInput(packageName).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -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<SourcedFeatureStorageInput>) {
storageInput = SourcedFeatureStorageInput { packageName }.let { input ->
storageInput = SourcedFeatureStorageInput(packageName).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -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<FeatureFactoryInput>) {
featureSourcesFactory = FeatureFactoryInput { packageName }.let { input ->
featureSourcesFactory = FeatureFactoryInput(packageName).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -113,7 +113,7 @@ public abstract class LaboratoryExtension {
* be visible to it during compilation.
*/
public fun optionFactory(action: Action<OptionFactoryInput>) {
optionFactoryInput = OptionFactoryInput { packageName }.let { input ->
optionFactoryInput = OptionFactoryInput(packageName).let { input ->
action.execute(input)
return@let input
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,7 +24,7 @@ public class OptionFactoryInput internal constructor(

internal fun toModel(features: List<FeatureFlagModel>) = OptionFactoryModel(
visibility = if (isPublic) Public else Internal,
className = ClassName(packageName ?: packageNameProvider(), "GeneratedOptionFactory"),
className = ClassName(packageName ?: parentPackageName, "GeneratedOptionFactory"),
features = features,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,7 +23,7 @@ public class SourcedFeatureStorageInput internal constructor(

internal fun toModel(sourceNames: List<String>) = SourcedFeatureStorageModel(
visibility = if (isPublic) Public else Internal,
className = ClassName(packageName ?: packageNameProvider(), "SourcedGeneratedFeatureStorage"),
className = ClassName(packageName ?: parentPackageName, "SourcedGeneratedFeatureStorage"),
sourceNames = sourceNames,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ laboratory {

withDefaultOption("ParentOption") { parent ->
parent.feature("Child") {
packageName = "io.mehow.explicit"
it.packageName = "io.mehow.explicit"

it.withDefaultOption("ChildOption")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit a4e1b76

Please sign in to comment.