Skip to content

Commit 2ce6262

Browse files
committed
Implement scenarios
1 parent c0c20a6 commit 2ce6262

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

app/build.gradle.kts

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import featureflags.installScenario
17+
import featureflags.loadScenarioFromLocal
18+
import featureflags.scenarios
19+
1620

1721
plugins {
1822
id("com.android.application")
@@ -58,15 +62,15 @@ android {
5862
}
5963

6064
dependencies {
65+
installScenario(scenarios, project.loadScenarioFromLocal())
66+
6167
implementation(project(Modules.corePublic))
6268
implementation(project(Modules.coreImpl))
6369
implementation(project(Modules.diPublic))
6470
implementation(project(Modules.singleActivityPublic))
6571
implementation(project(Modules.singleActivityImpl))
6672
implementation(project(Modules.guidomiaPublic))
67-
implementation(project(Modules.guidomiaImpl))
6873
implementation(project(Modules.databasePublic))
69-
implementation(project(Modules.databaseImpl))
7074
implementation(project(Modules.featureFlagsPublic))
7175
implementation(project(Modules.featureFlagsImpl))
7276

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright 2021 Hadi Lashkari Ghouchani
3+
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package featureflags
17+
18+
import org.gradle.api.Project
19+
import org.gradle.api.artifacts.Dependency
20+
import org.gradle.api.artifacts.ProjectDependency
21+
import org.gradle.api.artifacts.dsl.DependencyHandler
22+
import org.gradle.kotlin.dsl.DependencyHandlerScope
23+
import org.gradle.kotlin.dsl.add
24+
import java.util.*
25+
26+
fun DependencyHandlerScope.installScenario(
27+
scenarios: Map<String, List<String>>,
28+
scenario: String
29+
) {
30+
if (scenario.isNullOrBlank()) return
31+
println("Installing scenario: '$scenario'")
32+
val trimScenario = if (scenario.endsWith(SCENARIO_DELIMITER) && scenario != SCENARIO_DELIMITER) {
33+
scenario.substringBeforeLast(SCENARIO_DELIMITER)
34+
} else {
35+
scenario
36+
}
37+
installScenario(scenarios, trimScenario.substringBeforeLast(SCENARIO_DELIMITER))
38+
39+
scenarios[scenario]?.forEach { dependency ->
40+
if (dependency.startsWith(SCENARIO_DELIMITER)) {
41+
installScenario(scenarios, dependency)
42+
} else {
43+
implementation(dependency)
44+
}
45+
} ?: error("Cannot find the feature flag: '$scenario'")
46+
}
47+
48+
fun Project.loadScenarioFromLocal(): String {
49+
val props = Properties()
50+
val inputStream = file("${rootDir}/local.properties").inputStream()
51+
52+
inputStream.use { props.load(it) }
53+
return props["scenario"] as? String ?: DEFAULT_SCENARIO
54+
}
55+
56+
/**
57+
* Returns the runtime Java class of this object.
58+
*/
59+
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
60+
inline val <T : Any> T.javaClass: Class<T>
61+
@Suppress("UsePropertyAccessSyntax")
62+
get() = (this as java.lang.Object).getClass() as Class<T>
63+
64+
/**
65+
* Adds a dependency to the 'implementation' configuration.
66+
*
67+
* @param dependencyNotation notation for the dependency to be added.
68+
* @return The dependency.
69+
*
70+
* @see [DependencyHandler.add]
71+
*/
72+
private fun DependencyHandler.implementation(path: Any): Dependency? =
73+
add("implementation", project(mapOf("path" to path)) as ProjectDependency)
74+
75+
private const val SCENARIO_DELIMITER = ">"
76+
const val DEFAULT_SCENARIO = "${SCENARIO_DELIMITER}default"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2021 Hadi Lashkari Ghouchani
3+
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package featureflags
17+
18+
val scenarios = mapOf(
19+
Scenario.default to listOf(Scenario.guidomiaWithDatabase),
20+
Scenario.root to listOf(),
21+
Scenario.guidomia to listOf(Modules.guidomiaImpl),
22+
Scenario.guidomiaWithDatabase to listOf(Modules.databaseImpl)
23+
)
24+
25+
private object Scenario {
26+
const val default = DEFAULT_SCENARIO
27+
const val root = ">"
28+
const val guidomia = ">guidomia"
29+
const val guidomiaWithDatabase = ">guidomia>database"
30+
}

0 commit comments

Comments
 (0)