Skip to content

Commit

Permalink
Bring back lazy package name in Gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSikora committed Jun 21, 2022
1 parent a4e1b76 commit 5166cdc
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 43 deletions.
20 changes: 0 additions & 20 deletions library/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### 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 parentPackageName: String,
private val packageNameProvider: () -> 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, parentPackageName, supervisor).let { input ->
mutableFeatureInputs += FeatureFlagInput(name, packageNameProvider, 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 parentPackageName: String,
private val packageNameProvider: () -> 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 ?: parentPackageName, simpleName),
className = ClassName(packageName ?: packageNameProvider(), 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 parentPackageName: String,
private val packageNameProvider: () -> String,
private val supervisor: (() -> Supervisor)? = null,
) {
/**
Expand Down Expand Up @@ -69,8 +69,9 @@ 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(packageName ?: parentPackageName, supervisorBuilder).let { input ->
childFeatureInputs += ChildFeatureFlagsInput(packageNameProvider, supervisorBuilder).let { input ->
action.execute(input)
return@let input
}
Expand Down Expand Up @@ -106,7 +107,7 @@ public class FeatureFlagInput internal constructor(

private fun toModel() = FeatureFlagModel(
visibility = if (isPublic) Public else Internal,
className = ClassName(packageName ?: parentPackageName, name),
className = ClassName(packageName ?: packageNameProvider(), name),
options = options,
sourceOptions = sources,
key = key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ public abstract class LaboratoryExtension {
* Sets package name for any factories or feature flags defined in this extension.
* Package names can be individually overwritten in each generating block
*/
public var packageName: String = ""
public var packageName: String
get() = packageNameProvider()
set(value) {
packageNameProvider.value = value
}

private val packageNameProvider = PackageNameProvider()

private val mutableFeatureInputs = mutableListOf<FeatureFlagInput>()

Expand All @@ -28,7 +34,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, packageNameProvider).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -49,7 +55,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(packageNameProvider).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -72,7 +78,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(packageNameProvider).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -93,7 +99,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(packageNameProvider).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -113,7 +119,7 @@ public abstract class LaboratoryExtension {
* be visible to it during compilation.
*/
public fun optionFactory(action: Action<OptionFactoryInput>) {
optionFactoryInput = OptionFactoryInput(packageName).let { input ->
optionFactoryInput = OptionFactoryInput(packageNameProvider).let { input ->
action.execute(input)
return@let input
}
Expand All @@ -125,9 +131,10 @@ public abstract class LaboratoryExtension {
*/
public fun dependency(project: Project) {
this.project.evaluationDependsOn(project.path)
val laboratoryExtension = requireNotNull(project.extensions.findByType(LaboratoryExtension::class.java)) {
"Cannot depend on a project without laboratory plugin"
}
val laboratoryExtension =
requireNotNull(project.extensions.findByType(LaboratoryExtension::class.java)) {
"Cannot depend on a project without laboratory plugin"
}
mutableDependencies += laboratoryExtension.featureInputs
}
}
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 parentPackageName: String,
private val packageNameProvider: () -> 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 ?: parentPackageName, "GeneratedOptionFactory"),
className = ClassName(packageName ?: packageNameProvider(), "GeneratedOptionFactory"),
features = features,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.mehow.laboratory.gradle

internal class PackageNameProvider : () -> String {
var value = ""

override fun invoke() = value
}
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 parentPackageName: String,
private val packageNameProvider: () -> 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 ?: parentPackageName, "SourcedGeneratedFeatureStorage"),
className = ClassName(packageName ?: packageNameProvider(), "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 to a new one" {
"uses last implicit package name for all features" {
val fixture = "feature-flag-package-name-implicit-switching".toFixture()

gradleRunner.withProjectDir(fixture).build()

val featureA = fixture.featureFile("io.mehow.implicit.FeatureA")
val featureA = fixture.featureFile("io.mehow.implicit.switch.FeatureA")
featureA.shouldExist()

featureA.readText() shouldContain "package io.mehow.implicit"
featureA.readText() shouldContain "package io.mehow.implicit.switch"

val featureB = fixture.featureFile("io.mehow.implicit.switch.FeatureB")
featureB.shouldExist()
Expand Down

0 comments on commit 5166cdc

Please sign in to comment.