|
| 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" |
0 commit comments