diff --git a/constants.gradle b/constants.gradle index cf4de3ca..6588a5cb 100644 --- a/constants.gradle +++ b/constants.gradle @@ -16,7 +16,7 @@ // TODO migrate to version catalogs ext { groupId = "com.grab.grazel" - versionName = "0.4.0-alpha15" + versionName = "0.4.0-alpha16" kotlinVersion = "1.6.10" agpVersion = "7.1.2" diff --git a/grazel-gradle-plugin/build.gradle b/grazel-gradle-plugin/build.gradle index 968cfb6f..342604ca 100644 --- a/grazel-gradle-plugin/build.gradle +++ b/grazel-gradle-plugin/build.gradle @@ -26,7 +26,7 @@ buildscript { plugins { id "java-gradle-plugin" - id "org.gradle.kotlin.kotlin-dsl" version "2.1.4" + id "org.gradle.kotlin.kotlin-dsl" version "2.1.6" id "com.gradle.plugin-publish" version "0.14.0" id "maven-publish" id "idea" diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/BazelDependency.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/BazelDependency.kt index d9565571..2875bd01 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/BazelDependency.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/BazelDependency.kt @@ -17,20 +17,26 @@ package com.grab.grazel.bazel.starlark import com.grab.grazel.gradle.buildTargetName +import com.grab.grazel.gradle.dependencies.BuildGraphType import org.gradle.api.Project import org.gradle.api.artifacts.Dependency - sealed class BazelDependency { - data class ProjectDependency(val project: Project) : BazelDependency() { + data class ProjectDependency( + val dependencyProject: Project, + val suffix: String = "" + ) : BazelDependency() { override fun toString(): String { - val relativeProjectPath = project.rootProject.relativePath(project.projectDir) + val relativeProjectPath = + dependencyProject.rootProject.relativePath(dependencyProject.projectDir) return if (relativeProjectPath.contains("/")) { val path = relativeProjectPath.split("/").dropLast(1).joinToString("/") - "//" + path + "/" + project.buildTargetName() + "//" + path + "/" + dependencyProject.buildTargetName() + ":" + + dependencyProject.buildTargetName() + suffix } else { - "//" + project.buildTargetName() + "//" + dependencyProject.buildTargetName() + ":" + + dependencyProject.buildTargetName() + suffix } } } diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/Functions.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/Functions.kt index 3876da26..3306e4aa 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/Functions.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/starlark/Functions.kt @@ -20,7 +20,7 @@ import com.grab.grazel.bazel.rules.Visibility import com.grab.grazel.bazel.rules.rule import java.io.PrintWriter -class FunctionStatement( +data class FunctionStatement( val name: String, private val params: List, private val multilineParams: Boolean = false diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/BuildVariant.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/BuildVariant.kt index f283a6f4..02364b94 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/BuildVariant.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/BuildVariant.kt @@ -21,6 +21,7 @@ import com.android.build.gradle.LibraryExtension import com.android.build.gradle.api.BaseVariant import com.android.build.gradle.api.TestVariant import com.android.build.gradle.api.UnitTestVariant +import com.android.builder.model.BuildType import com.android.builder.model.ProductFlavor import com.grab.grazel.extension.DefaultVariantFilter import com.grab.grazel.extension.VariantFilter @@ -51,6 +52,14 @@ internal interface AndroidVariantDataSource { * @return The list of variants that can be migrated. */ fun getMigratableVariants(project: Project): List + + /** + * return all variants minus the ones that declared in filtered variants + */ + fun getMigratableVariants( + project: Project, + configurationScope: ConfigurationScope? + ): Set } internal class DefaultAndroidVariantDataSource( @@ -58,6 +67,23 @@ internal class DefaultAndroidVariantDataSource( override val variantFilter: Action? = null ) : AndroidVariantDataSource { + override fun getMigratableVariants( + project: Project, + configurationScope: ConfigurationScope? + ): Set { + return when (configurationScope) { + ConfigurationScope.TEST -> { + androidVariantsExtractor.getUnitTestVariants(project) + } + ConfigurationScope.ANDROID_TEST -> { + androidVariantsExtractor.getTestVariants(project) + } + else -> { + androidVariantsExtractor.getVariants(project) + } + }.filterNot(::ignoredVariantFilter).toSet() + } + override fun getIgnoredFlavors(project: Project): List { val supportFlavors = getMigratableVariants(project).flatMap(BaseVariant::getProductFlavors) return androidVariantsExtractor.getFlavors(project) @@ -88,6 +114,7 @@ internal interface AndroidVariantsExtractor { fun getTestVariants(project: Project): Set fun getVariants(project: Project): Set fun getFlavors(project: Project): Set + fun getBuildTypes(project: Project): Set } @@ -127,6 +154,14 @@ internal class DefaultAndroidVariantsExtractor @Inject constructor() : AndroidVa else -> emptySet() } } + + override fun getBuildTypes(project: Project): Set { + return when { + project.isAndroidAppOrDynFeature -> project.the().buildTypes + project.isAndroidLibrary -> project.the().buildTypes + else -> emptySet() + } + } } internal fun AndroidVariantDataSource.getMigratableBuildVariants(project: Project): List = diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/Configuration.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/Configuration.kt index 12338776..7f55959e 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/Configuration.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/Configuration.kt @@ -16,13 +16,16 @@ package com.grab.grazel.gradle +import com.android.build.gradle.api.BaseVariant +import com.android.build.gradle.internal.variant.BaseVariantFactory import com.grab.grazel.GrazelExtension +import com.grab.grazel.gradle.dependencies.BuildGraphType import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import javax.inject.Inject import javax.inject.Singleton -internal enum class ConfigurationScope { +enum class ConfigurationScope { BUILD, TEST, ANDROID_TEST; } @@ -41,14 +44,22 @@ internal interface ConfigurationDataSource { */ fun resolvedConfigurations( project: Project, - vararg scopes: ConfigurationScope + vararg buildGraphTypes: BuildGraphType ): Sequence /** * Return a sequence of the configurations filtered out by the ignore flavors, build variants and the configuration scopes * If the scopes is empty, the build scope will be used by default. */ - fun configurations(project: Project, vararg scopes: ConfigurationScope): Sequence + fun configurations( + project: Project, + vararg scope: ConfigurationScope + ): Sequence + + fun isThisConfigurationBelongsToThisVariants( + vararg variants: BaseVariant?, + configuration: Configuration + ): Boolean } @Singleton @@ -69,14 +80,14 @@ internal class DefaultConfigurationDataSource @Inject constructor( .filter { !it.name.contains("_internal_aapt2_binary") } .filter { !it.name.contains("archives") } .filter { !it.isDynamicConfiguration() } // Remove when Grazel support dynamic-feature plugin - .filter { + .filter { configuration -> when { - scopes.isEmpty() -> it.isNotTest() // If the scopes is empty, the build scope will be used by default. + scopes.isEmpty() -> configuration.isNotTest() // If the scopes is empty, the build scope will be used by default. else -> scopes.any { scope -> when (scope) { - ConfigurationScope.TEST -> !it.isAndroidTest() && it.isUnitTest() - ConfigurationScope.ANDROID_TEST -> !it.isUnitTest() - ConfigurationScope.BUILD -> it.isNotTest() + ConfigurationScope.TEST -> !configuration.isAndroidTest() && configuration.isUnitTest() + ConfigurationScope.ANDROID_TEST -> !configuration.isUnitTest() + ConfigurationScope.BUILD -> configuration.isNotTest() } } } @@ -90,11 +101,24 @@ internal class DefaultConfigurationDataSource @Inject constructor( } } + override fun isThisConfigurationBelongsToThisVariants( + vararg variants: BaseVariant?, + configuration: Configuration + ) = variants.any { variant -> + variant == null || + variant.compileConfiguration.hierarchy.contains(configuration) || + variant.runtimeConfiguration.hierarchy.contains(configuration) || + variant.annotationProcessorConfiguration.hierarchy.contains(configuration) + } + override fun resolvedConfigurations( project: Project, - vararg scopes: ConfigurationScope + vararg buildGraphTypes: BuildGraphType ): Sequence { - return configurations(project, *scopes).filter { it.isCanBeResolved } + return configurations( + project, + *buildGraphTypes.map { it.configurationScope }.toTypedArray() + ).filter { it.isCanBeResolved } } } diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/BuildGraphType.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/BuildGraphType.kt new file mode 100644 index 00000000..fd383083 --- /dev/null +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/BuildGraphType.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2022 Grabtaxi Holdings PTE LTD (GRAB) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.grab.grazel.gradle.dependencies + +import com.android.build.gradle.api.BaseVariant +import com.grab.grazel.gradle.ConfigurationScope + +data class BuildGraphType( + val configurationScope: ConfigurationScope, + val variant: BaseVariant? = null +) + +private val HUMPS = "(?<=.)(?=\\p{Upper})".toRegex() +fun String.variantNameSuffix() = "-${replace(HUMPS, "-").toLowerCase()}" diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/Dependencies.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/Dependencies.kt index 9f710964..584ca528 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/Dependencies.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/Dependencies.kt @@ -20,6 +20,7 @@ import com.grab.grazel.GrazelExtension import com.grab.grazel.bazel.rules.MavenInstallArtifact import com.grab.grazel.bazel.rules.MavenInstallArtifact.Exclusion.SimpleExclusion import com.grab.grazel.di.qualifiers.RootProject +import com.grab.grazel.gradle.AndroidVariantsExtractor import com.grab.grazel.gradle.ConfigurationDataSource import com.grab.grazel.gradle.ConfigurationScope import com.grab.grazel.gradle.RepositoryDataSource @@ -83,7 +84,10 @@ internal interface DependenciesDataSource { /** * Return the project's maven dependencies before the resolution strategy and any other custom substitution by Gradle */ - fun mavenDependencies(project: Project, vararg scopes: ConfigurationScope): Sequence + fun mavenDependencies( + project: Project, + vararg buildGraphTypes: BuildGraphType + ): Sequence /** * Return the project's project (module) dependencies before the resolution strategy and any other custom @@ -142,7 +146,8 @@ internal class DefaultDependenciesDataSource @Inject constructor( private val configurationDataSource: ConfigurationDataSource, private val artifactsConfig: ArtifactsConfig, private val repositoryDataSource: RepositoryDataSource, - private val dependencyResolutionService: GradleProvider + private val dependencyResolutionService: GradleProvider, + private val androidVariantsExtractor: AndroidVariantsExtractor ) : DependenciesDataSource { private val configurationScopes by lazy { grazelExtension.configurationScopes() } @@ -174,10 +179,12 @@ internal class DefaultDependenciesDataSource @Inject constructor( .toMap() } - private fun Project.resolvableConfigurations(): Sequence { - return configurationDataSource - .resolvedConfigurations(this, *configurationScopes) - } + private fun Project.buildGraphTypes() = + configurationScopes.flatMap { configurationScope -> + androidVariantsExtractor.getVariants(this).map { variant -> + BuildGraphType(configurationScope, variant) + } + } /** * Given a group, name and version will update version with following properties @@ -228,7 +235,12 @@ internal class DefaultDependenciesDataSource @Inject constructor( // Filter out configurations we are interested in. val configurations = projects .asSequence() - .flatMap { configurationDataSource.configurations(it, *configurationScopes) } + .flatMap { project -> + configurationDataSource.configurations( + project, + *configurationScopes + ) + } .toList() // Calculate all the external artifacts @@ -243,7 +255,12 @@ internal class DefaultDependenciesDataSource @Inject constructor( // (Perf fix) - collecting all projects' forced modules is costly, hence take the first sub project // TODO Provide option to consider all forced versions backed by a flag. val forcedVersions = sequenceOf(rootProject.subprojects.first()) - .flatMap { configurationDataSource.configurations(it, *configurationScopes) } + .flatMap { project -> + configurationDataSource.configurations( + project, + *configurationScopes + ) + } .let(::collectForcedVersions) return (DEFAULT_MAVEN_ARTIFACTS + externalArtifacts + forcedVersions) @@ -290,14 +307,25 @@ internal class DefaultDependenciesDataSource @Inject constructor( override fun mavenDependencies( project: Project, - vararg scopes: ConfigurationScope - ): Sequence = declaredDependencies(project, *scopes) - .map { it.second } - .filter { it.group != null && !DEP_GROUP_EMBEDDED_BY_RULES.contains(it.group) } - .filter { - val artifact = MavenArtifact(it.group, it.name) - !artifact.isExcluded && !artifact.isIgnored - }.filter { it !is ProjectDependency } + vararg buildGraphTypes: BuildGraphType + ): Sequence = + declaredDependencies(project, *buildGraphTypes.map { it.configurationScope }.toTypedArray()) + .filter { (configuration, _) -> + if (buildGraphTypes.isEmpty()) { + true + } else { + configurationDataSource.isThisConfigurationBelongsToThisVariants( + *buildGraphTypes.map { it.variant }.toTypedArray(), + configuration = configuration + ) + } + } + .map { it.second } + .filter { it.group != null && !DEP_GROUP_EMBEDDED_BY_RULES.contains(it.group) } + .filter { + val artifact = MavenArtifact(it.group, it.name) + !artifact.isExcluded && !artifact.isIgnored + }.filter { it !is ProjectDependency } override fun projectDependencies( project: Project, vararg scopes: ConfigurationScope @@ -343,7 +371,10 @@ internal class DefaultDependenciesDataSource @Inject constructor( private fun Project.externalResolvedDependencies() = dependencyResolutionService.get() .resolve( project = this, - configurations = resolvableConfigurations() + configurations = configurationDataSource.resolvedConfigurations( + this, + *buildGraphTypes().toTypedArray() + ) ) /** @@ -362,7 +393,10 @@ internal class DefaultDependenciesDataSource @Inject constructor( * @return Sequence of [DefaultResolvedDependency] in the first level */ private fun Project.firstLevelModuleDependencies(): Sequence { - return resolvableConfigurations() + return configurationDataSource.resolvedConfigurations( + this, + *buildGraphTypes().toTypedArray() + ) .map { it.resolvedConfiguration.lenientConfiguration } .flatMap { try { diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependenciesGraphsBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependenciesGraphsBuilder.kt index a819baac..5d4d70a0 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependenciesGraphsBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependenciesGraphsBuilder.kt @@ -20,54 +20,135 @@ import com.google.common.graph.ImmutableValueGraph import com.google.common.graph.MutableValueGraph import com.google.common.graph.ValueGraphBuilder import com.grab.grazel.di.qualifiers.RootProject +import com.grab.grazel.extension.TestExtension +import com.grab.grazel.gradle.AndroidVariantDataSource +import com.grab.grazel.gradle.ConfigurationDataSource import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.isAndroid import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import javax.inject.Inject internal class DependenciesGraphsBuilder @Inject constructor( @param:RootProject private val rootProject: Project, - private val dependenciesDataSource: DependenciesDataSource + private val dependenciesDataSource: DependenciesDataSource, + private val configurationDataSource: ConfigurationDataSource, + private val androidVariantDataSource: AndroidVariantDataSource, + private val testExtension: TestExtension ) { - fun build(): DependencyGraphs { - val buildGraph: MutableValueGraph = - ValueGraphBuilder - .directed() - .allowsSelfLoops(false) - .expectedNodeCount(rootProject.subprojects.size) - .build() - val testGraph: MutableValueGraph = - ValueGraphBuilder - .directed() - .allowsSelfLoops(false) - .expectedNodeCount(rootProject.subprojects.size) - .build() - rootProject.subprojects - .forEach { sourceProject -> - buildGraph.addNode(sourceProject) - testGraph.addNode(sourceProject) - dependenciesDataSource.projectDependencies(sourceProject, ConfigurationScope.BUILD) + fun build(): DependencyGraphs { + val buildGraphs: MutableMap> = + mutableMapOf() + buildList { + add(ConfigurationScope.BUILD) + if (testExtension.enableTestMigration) { + add(ConfigurationScope.TEST) + } + }.forEach { configurationScope -> + rootProject.subprojects.forEach { sourceProject -> + addProjectAsNodeToAllOfItsVariantsGraphs( + sourceProject, + configurationScope, + buildGraphs + ) + addEdges(sourceProject, configurationScope, buildGraphs) + dependenciesDataSource.projectDependencies(sourceProject, configurationScope) .forEach { (configuration, projectDependency) -> - buildGraph.putEdgeValue( + androidVariantDataSource.getMigratableVariants( sourceProject, - projectDependency.dependencyProject, - configuration + configurationScope ) + .forEach { variant -> + if (configurationDataSource.isThisConfigurationBelongsToThisVariants( + variant, + configuration = configuration + ) + ) { + buildGraphs.putEdgeValue( + BuildGraphType(configurationScope, variant), + sourceProject, + projectDependency.dependencyProject, + configuration + ) + } + } } + } + } - dependenciesDataSource.projectDependencies(sourceProject, ConfigurationScope.TEST) - .forEach { (configuration, projectDependency) -> - testGraph.putEdgeValue( - sourceProject, - projectDependency.dependencyProject, - configuration - ) + val immutableBuildGraphs = buildGraphs + .withDefault { buildGraph(0) } + .mapValues { (_, graph) -> + ImmutableValueGraph.copyOf(graph) + } + + return DefaultDependencyGraphs(buildGraphs = immutableBuildGraphs) + } + + private fun addEdges( + project: Project, + configurationScope: ConfigurationScope, + graph: MutableMap>, + ) { + dependenciesDataSource.projectDependencies(project, configurationScope) + .forEach { (configuration, projectDependency) -> + androidVariantDataSource.getMigratableVariants(project, configurationScope) + .forEach { variant -> + if (variant.compileConfiguration.hierarchy.contains(configuration)) { + graph.putEdgeValue( + BuildGraphType(configurationScope, variant), + project, + projectDependency.dependencyProject, + configuration + ) + } } } - return DefaultDependencyGraphs( - buildGraph = ImmutableValueGraph.copyOf(buildGraph), - testGraph = ImmutableValueGraph.copyOf(testGraph) - ) } -} \ No newline at end of file + + private fun addProjectAsNodeToAllOfItsVariantsGraphs( + sourceProject: Project, + configurationScope: ConfigurationScope, + buildGraphs: MutableMap> + ) { + if (sourceProject.isAndroid) { + androidVariantDataSource.getMigratableVariants(sourceProject, configurationScope) + .forEach { variant -> + buildGraphs.addNode(BuildGraphType(configurationScope, variant), sourceProject) + } + } else { + buildGraphs.addNode(BuildGraphType(configurationScope, null), sourceProject) + } + } +} + +private fun MutableMap>.putEdgeValue( + buildGraphType: BuildGraphType, + sourceProject: Project, + dependencyProject: Project, + configuration: Configuration +) { + computeIfAbsent(buildGraphType) { + buildGraph(sourceProject.subprojects.size) + } + get(buildGraphType)!!.putEdgeValue(sourceProject, dependencyProject, configuration) +} + +private fun MutableMap>.addNode( + buildGraphType: BuildGraphType, + sourceProject: Project +) { + computeIfAbsent(buildGraphType) { + buildGraph(sourceProject.subprojects.size) + } + get(buildGraphType)!!.addNode(sourceProject) +} + +fun buildGraph(size: Int): MutableValueGraph { + return ValueGraphBuilder + .directed() + .allowsSelfLoops(false) + .expectedNodeCount(size) + .build() +} diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependencyGraphs.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependencyGraphs.kt index cad76b54..1e48f1fa 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependencyGraphs.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/DependencyGraphs.kt @@ -3,43 +3,32 @@ package com.grab.grazel.gradle.dependencies import com.google.common.graph.Graphs import com.google.common.graph.ImmutableValueGraph import com.grab.grazel.bazel.starlark.BazelDependency -import com.grab.grazel.gradle.ConfigurationScope import org.gradle.api.Project import org.gradle.api.artifacts.Configuration internal interface DependencyGraphs { - fun nodes(vararg scopes: ConfigurationScope): Set + fun nodes(vararg buildGraphType: BuildGraphType): Set fun dependenciesSubGraph( project: Project, - scopes: Array = ConfigurationScope.values() + vararg buildGraphTypes: BuildGraphType ): Set fun directDependencies( project: Project, - scopes: Array = ConfigurationScope.values() + buildGraphType: BuildGraphType ): Set } -internal fun DependencyGraphs.directProjectDependencies( - project: Project, - vararg scopes: ConfigurationScope -) = directDependencies(project, *scopes).map { BazelDependency.ProjectDependency(it) } - internal class DefaultDependencyGraphs( - private val buildGraph: ImmutableValueGraph, - private val testGraph: ImmutableValueGraph + private val buildGraphs: Map> ) : DependencyGraphs { - override fun nodes(vararg scopes: ConfigurationScope): Set { + override fun nodes(vararg buildGraphType: BuildGraphType): Set { return when { - scopes.isEmpty() -> buildGraph.nodes() + testGraph.nodes() + buildGraphType.isEmpty() -> buildGraphs.values.flatMap { it.nodes() }.toSet() else -> { - scopes.flatMap { - when (it) { - ConfigurationScope.BUILD -> buildGraph.nodes() - ConfigurationScope.TEST -> testGraph.nodes() - else -> emptySet() - } + buildGraphType.flatMap { + buildGraphs.getValue(it).nodes() }.toSet() } } @@ -47,39 +36,20 @@ internal class DefaultDependencyGraphs( override fun dependenciesSubGraph( project: Project, - scopes: Array + vararg buildGraphTypes: BuildGraphType ): Set = - scopes.flatMap { - when (it) { - ConfigurationScope.BUILD -> Graphs.reachableNodes(buildGraph.asGraph(), project) - ConfigurationScope.TEST -> Graphs.reachableNodes(testGraph.asGraph(), project) - else -> emptySet() + if (buildGraphTypes.isEmpty()) { + buildGraphs.values.flatMap { + Graphs.reachableNodes(it.asGraph(), project) + } + } else { + buildGraphTypes.flatMap { buildGraphType -> + Graphs.reachableNodes(buildGraphs.getValue(buildGraphType).asGraph(), project) } }.toSet() override fun directDependencies( project: Project, - scopes: Array - ): Set = - scopes.flatMap { - when (it) { - ConfigurationScope.BUILD -> buildGraph.successors(project) - ConfigurationScope.TEST -> testGraph.successors(project) - else -> emptySet() - } - }.toSet() -} - -internal fun DependencyGraphs.dependenciesSubGraph( - project: Project, - vararg scopes: ConfigurationScope -): Set { - return dependenciesSubGraph(project, arrayOf(*scopes)) -} - -internal fun DependencyGraphs.directDependencies( - project: Project, - vararg scopes: ConfigurationScope -): Set { - return directDependencies(project, arrayOf(*scopes)) + buildGraphType: BuildGraphType + ): Set = buildGraphs.getValue(buildGraphType).successors(project).toSet() } \ No newline at end of file diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/GradleDependencyToBazelDependency.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/GradleDependencyToBazelDependency.kt new file mode 100644 index 00000000..efac898f --- /dev/null +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/gradle/dependencies/GradleDependencyToBazelDependency.kt @@ -0,0 +1,91 @@ +/* + * Copyright 2022 Grabtaxi Holdings PTE LTD (GRAB) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.grab.grazel.gradle.dependencies + +import com.android.build.gradle.api.BaseVariant +import com.grab.grazel.bazel.starlark.BazelDependency +import com.grab.grazel.gradle.AndroidVariantsExtractor +import com.grab.grazel.gradle.isAndroid +import org.gradle.api.Project +import java.lang.IllegalStateException +import javax.inject.Inject + +internal class GradleDependencyToBazelDependency @Inject constructor( + private val androidVariantsExtractor: AndroidVariantsExtractor +) { + + /** + * [variant] can only be null if and only if the [project] is a Java/Kotlin project + */ + fun map( + project: Project, + dependency: Project, + variant: BaseVariant? = null + ): BazelDependency.ProjectDependency { + + return if (project.isAndroid) { + if (variant == null) { + throw IllegalStateException( + "please provide the variant for the android project=${project.name}" + ) + } + if (dependency.isAndroid) {// project is an android project, dependent is also + val dependentVariants = androidVariantsExtractor.getVariants(dependency) + + if (dependentVariants.firstOrNull { dependentVariant -> + dependentVariant.name == variant.name + } != null + ) { + BazelDependency.ProjectDependency(dependency, variant.name.variantNameSuffix()) + } else { + if (isThereAnyCompatibleDependentBuildType(dependentVariants, variant)) { + BazelDependency.ProjectDependency( + dependency, + variant.buildType.name.variantNameSuffix() + ) + } else { + throw IllegalStateException( + "${project.name} with variant=${variant.name}" + + " depends on ${dependency.name} which doesn't have that variant " + + "${dependentVariants.map { it.name }}" + ) + } + } + } else {// project is an android project, dependent is NOT + BazelDependency.ProjectDependency(dependency) + } + } else {// Kotlin/Java Library + if (dependency.isAndroid) { + throw IllegalStateException( + "${project.name} is not android project but it " + + "depends on ${dependency.name} which is an android project this is not a " + + "valid dependency" + ) + } else { + BazelDependency.ProjectDependency(dependency) + } + } + } + + + private fun isThereAnyCompatibleDependentBuildType( + dependentVariants: Set, + variant: BaseVariant + ) = dependentVariants.firstOrNull { dependentVariant -> + dependentVariant.name == variant.buildType.name + } != null +} \ No newline at end of file diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryDataExtractor.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryDataExtractor.kt index 7f14b74e..625d4760 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryDataExtractor.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryDataExtractor.kt @@ -17,6 +17,9 @@ package com.grab.grazel.migrate.android import com.android.build.gradle.BaseExtension +import com.android.build.gradle.api.BaseVariant +import com.android.build.gradle.internal.variant.BaseVariantFactory +import com.android.builder.model.Variant import com.grab.grazel.GrazelExtension import com.grab.grazel.bazel.rules.DATABINDING_ARTIFACTS import com.grab.grazel.bazel.rules.Multidex @@ -34,7 +37,7 @@ import javax.inject.Singleton internal interface AndroidBinaryDataExtractor { - fun extract(project: Project, androidLibraryData: AndroidLibraryData): AndroidBinaryData + fun extract(project: Project, variant: BaseVariant, androidLibraryData: AndroidLibraryData): AndroidBinaryData } @Singleton @@ -47,11 +50,13 @@ internal class DefaultAndroidBinaryDataExtractor @Inject constructor( override fun extract( project: Project, + variant: BaseVariant, androidLibraryData: AndroidLibraryData ): AndroidBinaryData { val extension = project.extensions.getByType() val manifestValues = manifestValuesBuilder.build( project, + variant, extension.defaultConfig, androidLibraryData.packageName ) diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryTarget.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryTarget.kt index 0ac240b1..db3dbc78 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryTarget.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidBinaryTarget.kt @@ -16,15 +16,14 @@ package com.grab.grazel.migrate.android +import com.grab.grazel.bazel.rules.GOOGLE_SERVICES_XML import com.grab.grazel.bazel.rules.Multidex import com.grab.grazel.bazel.rules.Visibility import com.grab.grazel.bazel.rules.androidBinary -import com.grab.grazel.bazel.rules.crashlyticsAndroidLibrary -import com.grab.grazel.bazel.rules.googleServicesXml import com.grab.grazel.bazel.starlark.BazelDependency import com.grab.grazel.bazel.starlark.Statement -import com.grab.grazel.bazel.starlark.asString import com.grab.grazel.bazel.starlark.statements +import com.grab.grazel.bazel.starlark.toStatement import com.grab.grazel.migrate.BazelBuildTarget internal data class AndroidBinaryTarget( @@ -54,18 +53,7 @@ internal data class AndroidBinaryTarget( var resourceFiles = buildResources(res, ResValues(), customResourceSets, name) var finalDeps = deps if (googleServicesJson != null) { - val googleServicesXmlRes = googleServicesXml( - packageName = packageName, - googleServicesJson = googleServicesJson - ) - resourceFiles += googleServicesXmlRes - if (hasCrashlytics && buildId != null) { - finalDeps += crashlyticsAndroidLibrary( - packageName = packageName, - buildId = buildId, - resourceFiles = googleServicesXmlRes.asString() - ) - } + resourceFiles += GOOGLE_SERVICES_XML.toStatement() } androidBinary( diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryDataExtractor.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryDataExtractor.kt index 239ee3e1..a17a6927 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryDataExtractor.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryDataExtractor.kt @@ -18,6 +18,7 @@ package com.grab.grazel.migrate.android import com.android.build.gradle.BaseExtension import com.android.build.gradle.api.AndroidSourceSet +import com.android.build.gradle.api.BaseVariant import com.grab.grazel.GrazelExtension import com.grab.grazel.bazel.rules.ANDROIDX_GROUP import com.grab.grazel.bazel.rules.ANNOTATION_ARTIFACT @@ -26,10 +27,10 @@ import com.grab.grazel.bazel.rules.DATABINDING_GROUP import com.grab.grazel.bazel.starlark.BazelDependency import com.grab.grazel.gradle.AndroidVariantDataSource import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.BuildGraphType import com.grab.grazel.gradle.dependencies.DependenciesDataSource import com.grab.grazel.gradle.dependencies.DependencyGraphs -import com.grab.grazel.gradle.dependencies.directProjectDependencies -import com.grab.grazel.gradle.getMigratableBuildVariants +import com.grab.grazel.gradle.dependencies.GradleDependencyToBazelDependency import com.grab.grazel.gradle.hasDatabinding import com.grab.grazel.gradle.isAndroid import com.grab.grazel.migrate.dependencies.calculateDirectDependencyTags @@ -44,6 +45,7 @@ import javax.inject.Singleton internal interface AndroidLibraryDataExtractor { fun extract( project: Project, + variant: BaseVariant, sourceSetType: SourceSetType = SourceSetType.JAVA ): AndroidLibraryData } @@ -55,33 +57,41 @@ internal class DefaultAndroidLibraryDataExtractor @Inject constructor( private val dependencyGraphsProvider: Lazy, private val androidManifestParser: AndroidManifestParser, private val grazelExtension: GrazelExtension, + private val gradleDependencyToBazelDependency: GradleDependencyToBazelDependency ) : AndroidLibraryDataExtractor { private val projectDependencyGraphs get() = dependencyGraphsProvider.get() - override fun extract(project: Project, sourceSetType: SourceSetType): AndroidLibraryData { + override fun extract( + project: Project, + variant: BaseVariant, + sourceSetType: SourceSetType + ): AndroidLibraryData { if (project.isAndroid) { val extension = project.extensions.getByType() val deps = projectDependencyGraphs - .directProjectDependencies(project, ConfigurationScope.BUILD) + + .directDependencies( + project, + BuildGraphType(ConfigurationScope.BUILD, variant) + ).map { dependent -> + gradleDependencyToBazelDependency.map(project, dependent, variant) + } + dependenciesDataSource.collectMavenDeps(project) + project.kotlinParcelizeDeps() - return project.extract(extension, sourceSetType, deps) + return project.extract(variant, extension, sourceSetType, deps) } else { throw IllegalArgumentException("${project.name} is not an Android project") } } private fun Project.extract( + variant: BaseVariant, extension: BaseExtension, sourceSetType: SourceSetType = SourceSetType.JAVA, deps: List ): AndroidLibraryData { // Only consider source sets from migratable variants - val migratableSourceSets = variantDataSource - .getMigratableBuildVariants(this) - .asSequence() - .flatMap { it.sourceSets.asSequence() } + val migratableSourceSets = variant.sourceSets .filterIsInstance() .toList() @@ -197,8 +207,8 @@ internal class DefaultAndroidLibraryDataExtractor @Inject constructor( } internal fun DependenciesDataSource.collectMavenDeps( - project: Project, vararg scopes: ConfigurationScope -): List = mavenDependencies(project, *scopes) + project: Project, vararg buildGraphTypes: BuildGraphType +): List = mavenDependencies(project, *buildGraphTypes) .filter { if (project.hasDatabinding) { it.group != DATABINDING_GROUP && (it.group != ANDROIDX_GROUP && it.name != ANNOTATION_ARTIFACT) diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestData.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestData.kt index 6fc02be9..7abc052e 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestData.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestData.kt @@ -1,5 +1,6 @@ package com.grab.grazel.migrate.android +import com.android.build.gradle.api.BaseVariant import com.grab.grazel.bazel.starlark.BazelDependency data class AndroidUnitTestData( diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestDataExtractor.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestDataExtractor.kt index e480a8de..276f249b 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestDataExtractor.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidUnitTestDataExtractor.kt @@ -16,14 +16,17 @@ package com.grab.grazel.migrate.android import com.android.build.gradle.api.AndroidSourceSet +import com.android.build.gradle.api.BaseVariant import com.grab.grazel.GrazelExtension import com.grab.grazel.bazel.starlark.BazelDependency import com.grab.grazel.extension.KotlinExtension import com.grab.grazel.gradle.AndroidVariantDataSource import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.BuildGraphType import com.grab.grazel.gradle.dependencies.DependenciesDataSource import com.grab.grazel.gradle.dependencies.DependencyGraphs -import com.grab.grazel.gradle.dependencies.directProjectDependencies +import com.grab.grazel.gradle.dependencies.GradleDependencyToBazelDependency +import com.grab.grazel.gradle.dependencies.variantNameSuffix import com.grab.grazel.gradle.getMigratableBuildVariants import com.grab.grazel.gradle.getMigratableUnitTestVariants import com.grab.grazel.migrate.common.calculateTestAssociate @@ -39,7 +42,7 @@ import javax.inject.Singleton internal const val FORMAT_UNIT_TEST_NAME = "%s-test" internal interface AndroidUnitTestDataExtractor { - fun extract(project: Project): AndroidUnitTestData + fun extract(project: Project, variant: BaseVariant): AndroidUnitTestData } @Singleton @@ -49,19 +52,18 @@ internal class DefaultAndroidUnitTestDataExtractor @Inject constructor( private val dependencyGraphsProvider: Lazy, private val androidManifestParser: AndroidManifestParser, private val grazelExtension: GrazelExtension, + private val gradleDependencyToBazelDependency: GradleDependencyToBazelDependency ) : AndroidUnitTestDataExtractor { private val projectDependencyGraphs get() = dependencyGraphsProvider.get() private val kotlinExtension: KotlinExtension get() = grazelExtension.rules.kotlin - override fun extract(project: Project): AndroidUnitTestData { + override fun extract(project: Project, variant: BaseVariant): AndroidUnitTestData { val name = FORMAT_UNIT_TEST_NAME.format(project.name) - val migratableSourceSets = variantDataSource - .getMigratableUnitTestVariants(project) + val migratableSourceSets = variant.sourceSets .asSequence() - .flatMap { it.sourceSets.asSequence() } .filterIsInstance() val srcs = project.unitTestSources(migratableSourceSets).toList() @@ -69,20 +71,31 @@ internal class DefaultAndroidUnitTestDataExtractor @Inject constructor( val resources = project.unitTestResources(migratableSourceSets).toList() - val associate = calculateTestAssociate(project) + val associate = calculateTestAssociate(project, targetVariantSuffix(variant)) val deps = projectDependencyGraphs - .directProjectDependencies(project, ConfigurationScope.TEST) + - dependenciesDataSource.collectMavenDeps(project, ConfigurationScope.TEST) + + .directDependencies( + project, + BuildGraphType(ConfigurationScope.TEST, variant) + ).map { dependent -> + gradleDependencyToBazelDependency.map(project, dependent, variant) + } + + dependenciesDataSource.collectMavenDeps( + project, + BuildGraphType(ConfigurationScope.TEST, variant) + ) + project.kotlinParcelizeDeps() + - BazelDependency.ProjectDependency(project) + BazelDependency.ProjectDependency( + project, + targetVariantSuffix(variant) + ) val tags = if (kotlinExtension.enabledTransitiveReduction) { deps.calculateDirectDependencyTags(name) } else emptyList() return AndroidUnitTestData( - name = name, + name = "$name${variant.name.variantNameSuffix()}", srcs = srcs, additionalSrcSets = additionalSrcSets, deps = deps, @@ -93,6 +106,9 @@ internal class DefaultAndroidUnitTestDataExtractor @Inject constructor( ) } + private fun targetVariantSuffix(variant: BaseVariant) = + ("${variant.flavorName}${variant.buildType.name.capitalize()}".variantNameSuffix()) + private fun Project.unitTestSources( sourceSets: Sequence, sourceSetType: SourceSetType = SourceSetType.JAVA_KOTLIN diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/CrashlyticsTarget.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/CrashlyticsTarget.kt new file mode 100644 index 00000000..bd20bfab --- /dev/null +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/CrashlyticsTarget.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2022 Grabtaxi Holdings PTE LTD (GRAB) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.grab.grazel.migrate.android + +import com.grab.grazel.bazel.rules.crashlyticsAndroidLibrary +import com.grab.grazel.bazel.rules.googleServicesXml +import com.grab.grazel.bazel.starlark.Statement +import com.grab.grazel.bazel.starlark.asString +import com.grab.grazel.migrate.BazelTarget + +class CrashlyticsTarget( + override val name: String = "crashlytics_lib", + val packageName: String, + private val buildId: String, + private val googleServicesJson: String +) : BazelTarget { + + override fun statements(): List = com.grab.grazel.bazel.starlark.statements { + val googleServicesXmlRes = googleServicesXml( + packageName = packageName, + googleServicesJson = googleServicesJson + ) + crashlyticsAndroidLibrary( + packageName = packageName, + buildId = buildId, + resourceFiles = googleServicesXmlRes.asString() + ) + } +} \ No newline at end of file diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/ManifestValuesBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/ManifestValuesBuilder.kt index 18606ae5..eb2c01be 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/ManifestValuesBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/ManifestValuesBuilder.kt @@ -17,11 +17,12 @@ package com.grab.grazel.migrate.android import com.android.build.gradle.BaseExtension +import com.android.build.gradle.api.BaseVariant import com.android.build.gradle.internal.dsl.DefaultConfig import com.grab.grazel.gradle.AndroidVariantDataSource import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.BuildGraphType import com.grab.grazel.gradle.dependencies.DependencyGraphs -import com.grab.grazel.gradle.dependencies.dependenciesSubGraph import com.grab.grazel.gradle.getMigratableBuildVariants import com.grab.grazel.gradle.isAndroid import dagger.Lazy @@ -32,6 +33,7 @@ import javax.inject.Inject internal interface ManifestValuesBuilder { fun build( project: Project, + variant: BaseVariant, defaultConfig: DefaultConfig, packageName: String ): Map @@ -44,12 +46,16 @@ internal class DefaultManifestValuesBuilder @Inject constructor( private val projectDependencyGraphs get() = dependencyGraphsProvider.get() override fun build( project: Project, + variant: BaseVariant, defaultConfig: DefaultConfig, packageName: String ): Map { // Collect manifest values for all dependant projects val libraryFlavorManifestPlaceHolders = - projectDependencyGraphs.dependenciesSubGraph(project, ConfigurationScope.BUILD) + projectDependencyGraphs.dependenciesSubGraph( + project, + BuildGraphType(ConfigurationScope.BUILD, variant) + ) .asSequence() .filter(Project::isAndroid) .flatMap { depProject -> diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidBinaryTargetBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidBinaryTargetBuilder.kt index da83467a..cac0e770 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidBinaryTargetBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidBinaryTargetBuilder.kt @@ -16,6 +16,9 @@ package com.grab.grazel.migrate.builder +import com.grab.grazel.gradle.AndroidVariantDataSource +import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.variantNameSuffix import com.grab.grazel.gradle.isAndroidApplication import com.grab.grazel.gradle.isKotlin import com.grab.grazel.migrate.BazelTarget @@ -23,6 +26,7 @@ import com.grab.grazel.migrate.TargetBuilder import com.grab.grazel.migrate.android.AndroidBinaryDataExtractor import com.grab.grazel.migrate.android.AndroidBinaryTarget import com.grab.grazel.migrate.android.AndroidLibraryDataExtractor +import com.grab.grazel.migrate.android.CrashlyticsTarget import com.grab.grazel.migrate.android.DefaultAndroidBinaryDataExtractor import com.grab.grazel.migrate.android.DefaultKeyStoreExtractor import com.grab.grazel.migrate.android.DefaultManifestValuesBuilder @@ -37,7 +41,6 @@ import org.gradle.api.Project import javax.inject.Inject import javax.inject.Singleton - @Module internal interface AndroidBinaryTargetBuilderModule { @Binds @@ -57,7 +60,8 @@ internal interface AndroidBinaryTargetBuilderModule { @Singleton internal class AndroidBinaryTargetBuilder @Inject constructor( private val androidLibDataExtractor: AndroidLibraryDataExtractor, - private val androidBinDataExtractor: AndroidBinaryDataExtractor + private val androidBinDataExtractor: AndroidBinaryDataExtractor, + private val androidVariantDataSource: AndroidVariantDataSource ) : TargetBuilder { override fun build(project: Project): List { @@ -69,62 +73,110 @@ internal class AndroidBinaryTargetBuilder @Inject constructor( project: Project, intermediateTargets: List ): List { - var androidLibData = androidLibDataExtractor.extract(project) - val deps = if (project.isKotlin) { - // For kotlin project, don't duplicate Maven dependencies - intermediateTargets.map { it.toBazelDependency() } - } else { - intermediateTargets.map { it.toBazelDependency() } + androidLibData.deps - } - androidLibData = androidLibData.copy(deps = deps) - val binaryData = androidBinDataExtractor.extract(project, androidLibData) - - return intermediateTargets + AndroidBinaryTarget( - name = binaryData.name, - deps = androidLibData.deps + binaryData.deps, - srcs = androidLibData.srcs, - multidex = binaryData.multidex, - debugKey = binaryData.debugKey, - dexShards = binaryData.dexShards, - incrementalDexing = binaryData.incrementalDexing, - enableDataBinding = binaryData.hasDatabinding, - packageName = androidLibData.packageName, - manifest = androidLibData.manifestFile, - manifestValues = binaryData.manifestValues, - res = androidLibData.res, - resValues = androidLibData.resValues, - customResourceSets = androidLibData.extraRes, - assetsGlob = androidLibData.assets, - assetsDir = androidLibData.assetsDir, - buildId = binaryData.buildId, - googleServicesJson = binaryData.googleServicesJson, - hasCrashlytics = binaryData.hasCrashlytics - ) + var targets = + androidVariantDataSource.getMigratableVariants(project, ConfigurationScope.BUILD) + .flatMap { variant -> + var androidLibData = androidLibDataExtractor.extract(project, variant) + val deps = if (project.isKotlin) { + // For kotlin project, don't duplicate Maven dependencies + intermediateTargets.filter { it.name.endsWith(variant.name.variantNameSuffix()) } + .map { it.toBazelDependency() } + } else { + intermediateTargets.filter { it.name.endsWith(variant.name.variantNameSuffix()) } + .map { it.toBazelDependency() } + androidLibData.deps + } + + androidLibData = androidLibData.copy(deps = deps) + val binaryData = + androidBinDataExtractor.extract(project, variant, androidLibData) + + listOf( + AndroidBinaryTarget( + name = "${binaryData.name}${variant.name.variantNameSuffix()}", + deps = androidLibData.deps + binaryData.deps, + srcs = androidLibData.srcs, + multidex = binaryData.multidex, + debugKey = binaryData.debugKey, + dexShards = binaryData.dexShards, + incrementalDexing = binaryData.incrementalDexing, + enableDataBinding = binaryData.hasDatabinding, + packageName = androidLibData.packageName, + manifest = androidLibData.manifestFile, + manifestValues = binaryData.manifestValues, + res = androidLibData.res, + resValues = androidLibData.resValues, + customResourceSets = androidLibData.extraRes, + assetsGlob = androidLibData.assets, + assetsDir = androidLibData.assetsDir, + buildId = binaryData.buildId, + googleServicesJson = binaryData.googleServicesJson, + hasCrashlytics = binaryData.hasCrashlytics + ) + ) + } + intermediateTargets + + targets = addCrashlyticsTarget(targets) + + return targets + } + + private fun addCrashlyticsTarget(targets: List): List { + var resultTargets = targets + val androidTargetWithCrashlytics = resultTargets.firstOrNull { bazelTarget -> + bazelTarget is AndroidBinaryTarget && + bazelTarget.hasCrashlytics && + bazelTarget.buildId != null && + bazelTarget.googleServicesJson != null + } as AndroidBinaryTarget? + if (androidTargetWithCrashlytics != null) { + val crashlyticsTarget = with(androidTargetWithCrashlytics) { + CrashlyticsTarget( + packageName = packageName, + buildId = buildId!!, + googleServicesJson = googleServicesJson!! + ) + } + + resultTargets = resultTargets.map { bazelTarget -> + if (bazelTarget is AndroidBinaryTarget) { + bazelTarget.copy(deps = bazelTarget.deps + crashlyticsTarget.toBazelDependency()) + } else { + bazelTarget + } + }.toMutableList() + resultTargets.add(0, crashlyticsTarget) + } + return resultTargets } private fun buildKtAndroidTargets(project: Project): List { return buildList { - val androidProjectData = androidLibDataExtractor.extract( - project = project, - sourceSetType = SourceSetType.JAVA_KOTLIN - ).copy(name = "${project.name}_lib", hasDatabinding = false) - var deps = androidProjectData.deps - - with(androidProjectData) { - toBuildConfigTarget().also { - deps += it.toBazelDependency() - add(it) + androidVariantDataSource.getMigratableVariants(project, ConfigurationScope.BUILD) + .forEach { variant -> + val androidProjectData = androidLibDataExtractor.extract( + project = project, + sourceSetType = SourceSetType.JAVA_KOTLIN, + variant = variant + ).copy(name = "${project.name}_lib", hasDatabinding = false) + var deps = androidProjectData.deps + + with(androidProjectData) { + toBuildConfigTarget(variant.name.variantNameSuffix()).also { + deps += it.toBazelDependency() + add(it) + } + } + + androidProjectData + .copy( + name = "${androidProjectData.name}${variant.name.variantNameSuffix()}", + deps = deps, + tags = emptyList() // Don't generate classpath reduction tags for final binary target + ) + .toKtLibraryTarget() + ?.also { add(it) } } - } - - androidProjectData - .copy( - deps = deps, - tags = emptyList() // Don't generate classpath reduction tags for final binary target - ) - .toKtLibraryTarget() - ?.also { add(it) } } } diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidLibTargetBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidLibTargetBuilder.kt index 0f17db05..2d71a699 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidLibTargetBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/AndroidLibTargetBuilder.kt @@ -17,6 +17,8 @@ package com.grab.grazel.migrate.builder import com.grab.grazel.extension.TestExtension +import com.grab.grazel.gradle.AndroidVariantDataSource +import com.grab.grazel.gradle.ConfigurationScope import com.grab.grazel.gradle.isAndroid import com.grab.grazel.gradle.isAndroidApplication import com.grab.grazel.gradle.isKotlin @@ -45,19 +47,30 @@ internal interface AndroidLibTargetBuilderModule { internal class AndroidLibTargetBuilder @Inject constructor( private val projectDataExtractor: AndroidLibraryDataExtractor, private val unitTestDataExtractor: AndroidUnitTestDataExtractor, - private val testExtension: TestExtension + private val testExtension: TestExtension, + private val androidVariantDataSource: AndroidVariantDataSource ) : TargetBuilder { override fun build(project: Project): List { - return if (testExtension.enableTestMigration) { - listOf( - projectDataExtractor.extract(project).toAndroidLibTarget(), - unitTestDataExtractor.extract(project).toUnitTestTarget() - ) + return androidVariantDataSource.getMigratableVariants( + project, + ConfigurationScope.BUILD + ).map { variant -> + projectDataExtractor.extract(project, variant).toAndroidLibTarget() + } + unitTestsTargets(project) + } + + private fun unitTestsTargets(project: Project) = + if (testExtension.enableTestMigration) { + androidVariantDataSource.getMigratableVariants( + project, + ConfigurationScope.TEST + ).map { variant -> + unitTestDataExtractor.extract(project, variant).toUnitTestTarget() + } } else { - listOf(projectDataExtractor.extract(project).toAndroidLibTarget()) + emptyList() } - } override fun canHandle(project: Project): Boolean = with(project) { isAndroid && !isKotlin && !isAndroidApplication diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtAndroidLibTargetBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtAndroidLibTargetBuilder.kt index 4685e753..a621345a 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtAndroidLibTargetBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtAndroidLibTargetBuilder.kt @@ -18,8 +18,10 @@ package com.grab.grazel.migrate.builder import com.grab.grazel.bazel.rules.KotlinProjectType import com.grab.grazel.bazel.rules.Visibility -import com.grab.grazel.extension.KotlinExtension import com.grab.grazel.extension.TestExtension +import com.grab.grazel.gradle.AndroidVariantDataSource +import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.variantNameSuffix import com.grab.grazel.gradle.isAndroid import com.grab.grazel.gradle.isAndroidApplication import com.grab.grazel.gradle.isKotlin @@ -45,7 +47,6 @@ import org.gradle.api.Project import javax.inject.Inject import javax.inject.Singleton - @Module internal interface KtAndroidLibTargetBuilderModule { @Binds @@ -67,34 +68,43 @@ internal interface KtAndroidLibTargetBuilderModule { internal class KtAndroidLibTargetBuilder @Inject constructor( private val projectDataExtractor: AndroidLibraryDataExtractor, private val unitTestDataExtractor: AndroidUnitTestDataExtractor, - private val kotlinExtension: KotlinExtension, + private val androidVariantDataSource: AndroidVariantDataSource, private val testExtension: TestExtension ) : TargetBuilder { override fun build(project: Project): List { return mutableListOf().apply { - val projectData = projectDataExtractor.extract( - project, sourceSetType = SourceSetType.JAVA_KOTLIN - ) - var deps = projectData.deps - with(projectData) { - toAarResTarget()?.also { add(it) } - toBuildConfigTarget().also { - deps += it.toBazelDependency() - add(it) + androidVariantDataSource.getMigratableVariants(project, ConfigurationScope.BUILD) + .forEach { variant -> + val projectData = projectDataExtractor.extract( + project, + sourceSetType = SourceSetType.JAVA_KOTLIN, + variant = variant + ) + var deps = projectData.deps + with(projectData) { + toAarResTarget(variant.name.variantNameSuffix())?.also { add(it) } + toBuildConfigTarget(variant.name.variantNameSuffix()).also { + deps += it.toBazelDependency() + add(it) + } + } + projectData + .copy( + name = projectData.name + variant.name.variantNameSuffix(), + deps = deps + ) + .toKtLibraryTarget() + ?.also { add(it) } + + } + if (testExtension.enableTestMigration) { + androidVariantDataSource.getMigratableVariants(project, ConfigurationScope.TEST) + .forEach { variant -> + add(unitTestDataExtractor.extract(project, variant).toUnitTestTarget()) + } } - projectData - .copy(deps = deps) - .toKtLibraryTarget() - ?.also { add(it) } - - if (testExtension.enableTestMigration) - add( - unitTestDataExtractor - .extract(project) - .toUnitTestTarget() - ) } } @@ -123,11 +133,11 @@ internal fun AndroidLibraryData.toKtLibraryTarget(): KtLibraryTarget? = ) } else null -internal fun AndroidLibraryData.toAarResTarget(): AndroidLibraryTarget? { +internal fun AndroidLibraryData.toAarResTarget(variantName: String): AndroidLibraryTarget? { return if (res.isNotEmpty() && !hasDatabinding) { // For hybrid builds we need separate AAR for resources // When it is a pure resource module, keep the res target as the main target - val targetName = if (srcs.isEmpty()) name else "${name}-res" + val targetName = if (srcs.isEmpty()) name else "${name}-res$variantName" AndroidLibraryTarget( name = targetName, packageName = packageName, @@ -143,9 +153,9 @@ internal fun AndroidLibraryData.toAarResTarget(): AndroidLibraryTarget? { } else null } -internal fun AndroidLibraryData.toBuildConfigTarget(): BuildConfigTarget { +internal fun AndroidLibraryData.toBuildConfigTarget(variantName: String): BuildConfigTarget { return BuildConfigTarget( - name = "$name-build-config", + name = "$name$variantName-build-config", packageName = packageName, strings = buildConfigData.strings, booleans = buildConfigData.booleans, diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtLibTargetBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtLibTargetBuilder.kt index 1f6ea96c..b9c4973c 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtLibTargetBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/builder/KtLibTargetBuilder.kt @@ -57,17 +57,16 @@ internal class KtLibTargetBuilder @Inject constructor( private val kotlinExtension: KotlinExtension, private val testExtension: TestExtension ) : TargetBuilder { + override fun build(project: Project): List { val projectData = projectDataExtractor.extract(project) - val unitTestData = kotlinUnitTestDataExtractor.extract(project) + val ktLibTargets = projectData.toKtLibraryTarget(kotlinExtension.enabledTransitiveReduction) return if (testExtension.enableTestMigration) { - listOf( - projectData.toKtLibraryTarget(kotlinExtension.enabledTransitiveReduction), - unitTestData.toUnitTestTarget() - ) + val unitTestsTargets = kotlinUnitTestDataExtractor.extract(project).toUnitTestTarget() + listOf(ktLibTargets, unitTestsTargets) } else { - listOf(projectData.toKtLibraryTarget(kotlinExtension.enabledTransitiveReduction)) + listOf(ktLibTargets) } } diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/common/TestAssociates.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/common/TestAssociates.kt index ebcb6b9b..f5c11f2a 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/common/TestAssociates.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/common/TestAssociates.kt @@ -32,17 +32,17 @@ import org.gradle.api.Project * @param project The project for which associate needs to be calculated * @return The associate that was calculated, null otherwise */ -internal fun calculateTestAssociate(project: Project): BazelDependency? { +internal fun calculateTestAssociate(project: Project, suffix: String = ""): BazelDependency? { return when { project.isKotlin && project.hasDatabinding -> { return BazelDependency.StringDependency( - """${ProjectDependency(project)}:${project.buildTargetName()}-kotlin""" + """${ProjectDependency(project, suffix)}-kotlin""" ) } project.isKotlin && project.isAndroid -> return BazelDependency.StringDependency( - """${ProjectDependency(project)}:${project.buildTargetName()}_kt""" + """${ProjectDependency(project, suffix)}_kt""" ) - project.isKotlin -> return ProjectDependency(project) + project.isKotlin -> return ProjectDependency(project, suffix) else -> null } } \ No newline at end of file diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinProjectDataExtractor.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinProjectDataExtractor.kt index 954f8d5b..e7acddeb 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinProjectDataExtractor.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinProjectDataExtractor.kt @@ -21,9 +21,10 @@ import com.grab.grazel.bazel.rules.KOTLIN_PARCELIZE_TARGET import com.grab.grazel.bazel.starlark.BazelDependency import com.grab.grazel.extension.KotlinExtension import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.BuildGraphType import com.grab.grazel.gradle.dependencies.DependenciesDataSource import com.grab.grazel.gradle.dependencies.DependencyGraphs -import com.grab.grazel.gradle.dependencies.directProjectDependencies +import com.grab.grazel.gradle.dependencies.GradleDependencyToBazelDependency import com.grab.grazel.gradle.hasKotlinAndroidExtensions import com.grab.grazel.migrate.android.SourceSetType import com.grab.grazel.migrate.android.collectMavenDeps @@ -48,6 +49,7 @@ internal class DefaultKotlinProjectDataExtractor @Inject constructor( private val dependenciesDataSource: DependenciesDataSource, private val dependencyGraphsProvider: Lazy, private val grazelExtension: GrazelExtension, + private val gradleDependencyToBazelDependency: GradleDependencyToBazelDependency ) : KotlinProjectDataExtractor { private val kotlinExtension: KotlinExtension get() = grazelExtension.rules.kotlin @@ -61,7 +63,12 @@ internal class DefaultKotlinProjectDataExtractor @Inject constructor( val resources = project.kotlinSources(sourceSets, SourceSetType.RESOURCES).toList() val deps = projectDependencyGraphs - .directProjectDependencies(project, ConfigurationScope.BUILD) + + .directDependencies( + project, + BuildGraphType(ConfigurationScope.BUILD) + ).map { dependent -> + gradleDependencyToBazelDependency.map(project, dependent, null) + } + dependenciesDataSource.collectMavenDeps(project) + project.androidJarDeps() + project.kotlinParcelizeDeps() diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinUnitTestDataExtractor.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinUnitTestDataExtractor.kt index dbf052fd..a0fe4f3d 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinUnitTestDataExtractor.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/kotlin/KotlinUnitTestDataExtractor.kt @@ -20,9 +20,10 @@ import com.grab.grazel.GrazelExtension import com.grab.grazel.bazel.starlark.BazelDependency import com.grab.grazel.extension.KotlinExtension import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.BuildGraphType import com.grab.grazel.gradle.dependencies.DependenciesDataSource import com.grab.grazel.gradle.dependencies.DependencyGraphs -import com.grab.grazel.gradle.dependencies.directProjectDependencies +import com.grab.grazel.gradle.dependencies.GradleDependencyToBazelDependency import com.grab.grazel.migrate.android.FORMAT_UNIT_TEST_NAME import com.grab.grazel.migrate.android.SourceSetType import com.grab.grazel.migrate.android.collectMavenDeps @@ -48,7 +49,8 @@ internal interface KotlinUnitTestDataExtractor { internal class DefaultKotlinUnitTestDataExtractor @Inject constructor( private val dependenciesDataSource: DependenciesDataSource, private val dependencyGraphsProvider: Lazy, - private val grazelExtension: GrazelExtension + private val grazelExtension: GrazelExtension, + private val gradleDependencyToBazelDependency: GradleDependencyToBazelDependency ) : KotlinUnitTestDataExtractor { private val kotlinExtension: KotlinExtension get() = grazelExtension.rules.kotlin @@ -67,12 +69,19 @@ internal class DefaultKotlinUnitTestDataExtractor @Inject constructor( val deps: List = buildList { addAll( - projectDependencyGraphs.directProjectDependencies( + projectDependencyGraphs.directDependencies( project, - ConfigurationScope.TEST + BuildGraphType(ConfigurationScope.TEST) + ).map { dependent -> + gradleDependencyToBazelDependency.map(project, dependent) + } + ) + addAll( + dependenciesDataSource.collectMavenDeps( + project, + BuildGraphType(ConfigurationScope.TEST) ) ) - addAll(dependenciesDataSource.collectMavenDeps(project, ConfigurationScope.TEST)) addAll(project.kotlinParcelizeDeps()) if (projectDependency.toString() != associate.toString()) { add(projectDependency) diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeDependencyGraphs.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeDependencyGraphs.kt index caa80f71..19a55335 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeDependencyGraphs.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeDependencyGraphs.kt @@ -1,6 +1,6 @@ package com.grab.grazel.fake -import com.grab.grazel.gradle.ConfigurationScope +import com.grab.grazel.gradle.dependencies.BuildGraphType import com.grab.grazel.gradle.dependencies.DependencyGraphs import org.gradle.api.Project @@ -9,16 +9,16 @@ internal class FakeDependencyGraphs( private val dependenciesSubGraph: Set = emptySet(), private val nodes: Set = emptySet() ) : DependencyGraphs { - override fun nodes(vararg scopes: ConfigurationScope): Set = nodes + override fun nodes(vararg buildGraphType: BuildGraphType): Set = nodes override fun dependenciesSubGraph( project: Project, - scopes: Array + vararg buildGraphTypes: BuildGraphType ): Set = dependenciesSubGraph override fun directDependencies( project: Project, - scopes: Array + buildGraphTypes: BuildGraphType ): Set = directDeps } \ No newline at end of file diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeProductFlavor.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeProductFlavor.kt index d3d3745f..3b29fd1d 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeProductFlavor.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeProductFlavor.kt @@ -23,6 +23,7 @@ import com.android.builder.model.ProductFlavor import com.android.builder.model.VectorDrawablesOptions import com.grab.grazel.extension.VariantFilter import com.grab.grazel.gradle.AndroidVariantDataSource +import com.grab.grazel.gradle.ConfigurationScope import org.gradle.api.Action import org.gradle.api.Project import java.io.File @@ -41,6 +42,13 @@ class FakeAndroidVariantDataSource( override fun getMigratableVariants(project: Project): List { return emptyList() } + + override fun getMigratableVariants( + project: Project, + configurationScope: ConfigurationScope? + ): Set { + return emptySet() + } } class FakeProductFlavor(private val name: String) : ProductFlavor { diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeVariant.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeVariant.kt index 049cb409..478821c2 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeVariant.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/fake/FakeVariant.kt @@ -52,7 +52,8 @@ internal const val RELEASE_FLAVOR2 = "releaseFlavor2" class FakeVariant( private val name: String, - private val flavor: String? = null + private val flavor: String? = null, + private val sourceSets: List = emptyList() ) : BaseVariant { override fun getProductFlavors(): MutableList = if (flavor != null) mutableListOf(FakeProductFlavor(flavor)) @@ -88,7 +89,7 @@ class FakeVariant( } override fun getSourceSets(): MutableList { - TODO("Not yet implemented") + return sourceSets.toMutableList() } override fun getJavaCompileOptions(): JavaCompileOptions { @@ -314,6 +315,6 @@ class FakeBuildType : BuildType { get() = TODO("Not yet implemented") override fun getName(): String { - TODO("Not yet implemented") + return "debug" } } \ No newline at end of file diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultAndroidVariantDataSourceTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultAndroidVariantDataSourceTest.kt index 6c82437d..d9291786 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultAndroidVariantDataSourceTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultAndroidVariantDataSourceTest.kt @@ -17,6 +17,7 @@ package com.grab.grazel.gradle import com.android.build.gradle.api.BaseVariant +import com.android.builder.model.BuildType import com.android.builder.model.ProductFlavor import com.grab.grazel.GrazelExtension import com.grab.grazel.GrazelPluginTest @@ -82,8 +83,10 @@ class DefaultAndroidVariantDataSourceTest : GrazelPluginTest() { } } -private class FakeAndroidVariantsExtractor : AndroidVariantsExtractor { - override fun getVariants(project: Project): Set = setOf( +class FakeAndroidVariantsExtractor : AndroidVariantsExtractor { + override fun getVariants( + project: Project + ): Set = setOf( FakeVariant(DEBUG_FLAVOR1, FLAVOR1), FakeVariant(DEBUG_FLAVOR2, FLAVOR2), FakeVariant(RELEASE_FLAVOR1, FLAVOR1), @@ -93,6 +96,10 @@ private class FakeAndroidVariantsExtractor : AndroidVariantsExtractor { override fun getFlavors(project: Project): Set = listOf(FLAVOR1, FLAVOR2).map { FakeProductFlavor(it) }.toSet() + override fun getBuildTypes(project: Project): Set { + return getVariants(project).map { it.buildType }.toSet() + } + override fun getUnitTestVariants(project: Project): Set = emptySet() override fun getTestVariants(project: Project): Set = emptySet() diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultDependenciesDataSourceTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultDependenciesDataSourceTest.kt index ffda4578..1e438ad7 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultDependenciesDataSourceTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/DefaultDependenciesDataSourceTest.kt @@ -69,6 +69,7 @@ class DefaultDependenciesDataSourceTest : GrazelPluginTest() { private lateinit var repositoryDataSource: RepositoryDataSource private lateinit var fakeVariantDataSource: FakeAndroidVariantDataSource private lateinit var configurationDataSource: DefaultConfigurationDataSource + private lateinit var androidVariantsExtractor: AndroidVariantsExtractor @Before fun setUp() { @@ -81,6 +82,7 @@ class DefaultDependenciesDataSourceTest : GrazelPluginTest() { fakeVariantDataSource = FakeAndroidVariantDataSource() configurationDataSource = DefaultConfigurationDataSource(fakeVariantDataSource) repositoryDataSource = DefaultRepositoryDataSource(rootProject) + androidVariantsExtractor = FakeAndroidVariantsExtractor() dependenciesDataSource = DefaultDependenciesDataSource( rootProject = rootProject, @@ -88,7 +90,8 @@ class DefaultDependenciesDataSourceTest : GrazelPluginTest() { artifactsConfig = ArtifactsConfig(ignoredList = listOf(KOTLIN_STDLIB)), repositoryDataSource = repositoryDataSource, dependencyResolutionService = DefaultDependencyResolutionService.register(rootProject), - grazelExtension = GrazelExtension(rootProject) + grazelExtension = GrazelExtension(rootProject), + androidVariantsExtractor = androidVariantsExtractor ) } @@ -217,7 +220,8 @@ class DefaultDependenciesDataSourceTest : GrazelPluginTest() { artifactsConfig = ArtifactsConfig(ignoredList = listOf(DAGGER.split(":%s").first())), repositoryDataSource = repositoryDataSource, dependencyResolutionService = DefaultDependencyResolutionService.register(rootProject), - grazelExtension = GrazelExtension(rootProject) + grazelExtension = GrazelExtension(rootProject), + androidVariantsExtractor = androidVariantsExtractor ) assertTrue( "hasIgnoredArtifacts returns true when project contains any ignored artifacts", @@ -246,7 +250,8 @@ class DefaultDependenciesDataSourceTest : GrazelPluginTest() { artifactsConfig = ArtifactsConfig(excludedList = listOf(excludedDagger)), repositoryDataSource = repositoryDataSource, dependencyResolutionService = DefaultDependencyResolutionService.register(rootProject), - grazelExtension = GrazelExtension(rootProject) + grazelExtension = GrazelExtension(rootProject), + androidVariantsExtractor = androidVariantsExtractor ) val resolvedArtifacts = dependenciesDataSource .resolvedArtifactsFor(listOf(rootProject, subProject)) @@ -285,7 +290,8 @@ class DefaultDependenciesDataSourceTest : GrazelPluginTest() { artifactsConfig = ArtifactsConfig(excludedList = listOf()), repositoryDataSource = repositoryDataSource, dependencyResolutionService = DefaultDependencyResolutionService.register(rootProject), - grazelExtension = GrazelExtension(rootProject) + grazelExtension = GrazelExtension(rootProject), + androidVariantsExtractor = DefaultAndroidVariantsExtractor() ) val dependencyArtifactMap = dependenciesDataSource.dependencyArtifactMap( rootProject, diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/dependencies/BuildGraphTypeKtTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/dependencies/BuildGraphTypeKtTest.kt new file mode 100644 index 00000000..9ca4b6f4 --- /dev/null +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/dependencies/BuildGraphTypeKtTest.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2022 Grabtaxi Holdings PTE LTD (GRAB) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.grab.grazel.gradle.dependencies + +import org.junit.Assert.* +import org.junit.Test + +class BuildGraphTypeKtTest{ + + @Test + fun test(){ + assertEquals("-app-gps-debug","appGpsDebug".variantNameSuffix()) + assertEquals("-app-gps-debug","AppGpsDebug".variantNameSuffix()) + } + +} \ No newline at end of file diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/dependencies/DefaultDependencyGraphsTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/dependencies/DefaultDependencyGraphsTest.kt index f94ab34f..67ee79cb 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/dependencies/DefaultDependencyGraphsTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/gradle/dependencies/DefaultDependencyGraphsTest.kt @@ -19,8 +19,10 @@ class DefaultDependencyGraphsTest { private val projectE = FakeProject("E") private val dependenciesGraphs = DefaultDependencyGraphs( - testGraph = buildTestGraphs(), - buildGraph = buildBuildGraphs() + buildGraphs = mapOf( + BuildGraphType(ConfigurationScope.BUILD) to buildBuildGraphs(), + BuildGraphType(ConfigurationScope.TEST) to buildTestGraphs() + ) ) @Test @@ -36,11 +38,14 @@ class DefaultDependencyGraphsTest { fun nodesShouldReturnTheCorrectItemsBaseOnScopes() { val buildNodes = setOf(projectA, projectB, projectC) val testNodes = setOf(projectA, projectB, projectC, projectD, projectE) - assertEquals(buildNodes, dependenciesGraphs.nodes(ConfigurationScope.BUILD)) - assertEquals(testNodes, dependenciesGraphs.nodes(ConfigurationScope.TEST)) + assertEquals(buildNodes, dependenciesGraphs.nodes(BuildGraphType(ConfigurationScope.BUILD))) + assertEquals(testNodes, dependenciesGraphs.nodes(BuildGraphType(ConfigurationScope.TEST))) assertEquals( testNodes + buildNodes, - dependenciesGraphs.nodes(ConfigurationScope.TEST, ConfigurationScope.BUILD) + dependenciesGraphs.nodes( + BuildGraphType(ConfigurationScope.TEST), + BuildGraphType(ConfigurationScope.BUILD) + ) ) } @@ -49,20 +54,7 @@ class DefaultDependencyGraphsTest { val directDepsFromAWithBuildScope = setOf(projectB, projectC) assertEquals( directDepsFromAWithBuildScope, - dependenciesGraphs.directDependencies(projectA, ConfigurationScope.BUILD) - ) - } - - @Test - fun directDependenciesShouldReturnDirectDepsFromBuildAndTestScope() { - val directDepsFromAWithBuildAndTestScope = setOf(projectB, projectC, projectE) - assertEquals( - directDepsFromAWithBuildAndTestScope, - dependenciesGraphs.directDependencies( - projectA, - ConfigurationScope.BUILD, - ConfigurationScope.TEST - ) + dependenciesGraphs.directDependencies(projectA, BuildGraphType(ConfigurationScope.BUILD)) ) } @@ -80,7 +72,10 @@ class DefaultDependencyGraphsTest { val expectDeps = setOf(projectB, projectC) assertEquals( expectDeps, - dependenciesGraphs.dependenciesSubGraph(projectB, ConfigurationScope.BUILD) + dependenciesGraphs.dependenciesSubGraph( + projectB, + BuildGraphType(ConfigurationScope.BUILD) + ) ) } @@ -91,8 +86,8 @@ class DefaultDependencyGraphsTest { expectDeps, dependenciesGraphs.dependenciesSubGraph( projectB, - ConfigurationScope.BUILD, - ConfigurationScope.TEST + BuildGraphType(ConfigurationScope.BUILD), + BuildGraphType(ConfigurationScope.TEST) ) ) } diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/DaggerWorkspaceRuleTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/DaggerWorkspaceRuleTest.kt index 09679c28..029d7641 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/DaggerWorkspaceRuleTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/DaggerWorkspaceRuleTest.kt @@ -1,5 +1,6 @@ package com.grab.grazel.migrate +import com.android.build.gradle.LibraryExtension import com.google.common.truth.Truth import com.grab.grazel.GrazelExtension import com.grab.grazel.bazel.starlark.asString @@ -8,6 +9,7 @@ import com.grab.grazel.di.DaggerGrazelComponent import com.grab.grazel.gradle.ANDROID_LIBRARY_PLUGIN import com.grab.grazel.gradle.KOTLIN_ANDROID_PLUGIN import com.grab.grazel.migrate.internal.WorkspaceBuilder +import com.grab.grazel.util.doEvaluate import org.gradle.api.Project import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies @@ -34,6 +36,11 @@ class DaggerWorkspaceRuleTest { apply(ANDROID_LIBRARY_PLUGIN) apply(KOTLIN_ANDROID_PLUGIN) } + extensions.configure { + defaultConfig { + compileSdkVersion(30) + } + } repositories { mavenCentral() google() @@ -49,6 +56,8 @@ class DaggerWorkspaceRuleTest { subProject.dependencies { add("implementation", "com.google.dagger:dagger:$daggerTag") } + subProject.doEvaluate() + rootProject.configure { rules { dagger { diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/KotlinTargetsTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/KotlinTargetsTest.kt index c7258533..3dcef644 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/KotlinTargetsTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/KotlinTargetsTest.kt @@ -115,7 +115,7 @@ class KotlinTargetsTest { val generatedStatements = projectBazelFileBuilder.build().asString() Truth.assertThat(generatedStatements).apply { contains("kt_db_android_library") - contains("""name = "android-library"""") + contains("""name = "android-library-debug"""") contains(""""src/main/res/activity_main.xml",""") contains("""custom_package = "com.example.androidlibrary"""") contains("""manifest = "src/main/AndroidManifest.xml"""") diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidUnitTestDataExtractorTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidUnitTestDataExtractorTest.kt index a693cfd8..2742b566 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidUnitTestDataExtractorTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidUnitTestDataExtractorTest.kt @@ -26,13 +26,16 @@ import com.grab.grazel.gradle.ANDROID_LIBRARY_PLUGIN import com.grab.grazel.gradle.DefaultAndroidVariantDataSource import com.grab.grazel.gradle.DefaultConfigurationDataSource import com.grab.grazel.gradle.DefaultRepositoryDataSource +import com.grab.grazel.gradle.FakeAndroidVariantsExtractor import com.grab.grazel.gradle.KOTLIN_ANDROID_PLUGIN import com.grab.grazel.gradle.dependencies.ArtifactsConfig import com.grab.grazel.gradle.dependencies.DefaultDependenciesDataSource import com.grab.grazel.gradle.dependencies.DefaultDependencyResolutionService +import com.grab.grazel.gradle.dependencies.GradleDependencyToBazelDependency import com.grab.grazel.util.doEvaluate import org.gradle.api.Project import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.the import org.junit.Before import org.junit.Rule import org.junit.Test @@ -47,6 +50,8 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() { private lateinit var subProjectDir: File private lateinit var defaultAndroidUnitTestDataExtractor: DefaultAndroidUnitTestDataExtractor + private lateinit var androidVariantsExtractor: FakeAndroidVariantsExtractor + private lateinit var gradleDependencyToBazelDependency: GradleDependencyToBazelDependency @get:Rule val temporaryFolder = TemporaryFolder() @@ -71,7 +76,9 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() { } } } - + androidVariantsExtractor = FakeAndroidVariantsExtractor() + gradleDependencyToBazelDependency = + GradleDependencyToBazelDependency(androidVariantsExtractor) File(subProjectDir, "src/main/AndroidManifest.xml").apply { parentFile.mkdirs() createNewFile() @@ -95,6 +102,7 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() { repositoryDataSource = repositoryDataSource, dependencyResolutionService = DefaultDependencyResolutionService.register(rootProject), grazelExtension = GrazelExtension(rootProject), + androidVariantsExtractor = androidVariantsExtractor ) val dependencyGraphs = FakeDependencyGraphs() @@ -105,7 +113,8 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() { variantDataSource = variantDataSource, dependencyGraphsProvider = { dependencyGraphs }, androidManifestParser = androidManifestParser, - grazelExtension = GrazelExtension(rootProject) + grazelExtension = GrazelExtension(rootProject), + gradleDependencyToBazelDependency = gradleDependencyToBazelDependency ) } @@ -117,7 +126,11 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() { } subProject.doEvaluate() - val androidUnitTestData = defaultAndroidUnitTestDataExtractor.extract(subProject) + + val androidUnitTestData = defaultAndroidUnitTestDataExtractor.extract( + subProject, + debugUnitTestVariant(subProject) + ) Truth.assertThat(androidUnitTestData.resources).apply { contains("src/test/resources/test.json") } @@ -136,7 +149,10 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() { } subProject.doEvaluate() - val androidUnitTestData = defaultAndroidUnitTestDataExtractor.extract(subProject) + val androidUnitTestData = defaultAndroidUnitTestDataExtractor.extract( + subProject, + debugUnitTestVariant(subProject) + ) Truth.assertThat(androidUnitTestData.resources).apply { contains("src/test/resources/**") } @@ -155,11 +171,19 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() { } subProject.doEvaluate() - val androidUnitTestData = defaultAndroidUnitTestDataExtractor.extract(subProject) + val androidUnitTestData = defaultAndroidUnitTestDataExtractor.extract( + subProject, + debugUnitTestVariant(subProject) + ) Truth.assertThat(androidUnitTestData.additionalSrcSets).apply { containsExactlyElementsIn(additionalTestSourceSets) containsNoneIn(defaultTestSourceSets) } } + + private fun debugUnitTestVariant(project: Project) = + project.project.the().unitTestVariants.first { + it.name == "debugUnitTest" + } } diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultManifestValuesBuilderTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultManifestValuesBuilderTest.kt index cefc9664..dc49df0b 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultManifestValuesBuilderTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultManifestValuesBuilderTest.kt @@ -24,7 +24,10 @@ import com.grab.grazel.GrazelExtension import com.grab.grazel.GrazelExtension.Companion.GRAZEL_EXTENSION import com.grab.grazel.GrazelPluginTest import com.grab.grazel.buildProject +import com.grab.grazel.fake.DEBUG_FLAVOR1 +import com.grab.grazel.fake.FLAVOR1 import com.grab.grazel.fake.FakeDependencyGraphs +import com.grab.grazel.fake.FakeVariant import com.grab.grazel.gradle.ANDROID_APPLICATION_PLUGIN import com.grab.grazel.gradle.ANDROID_LIBRARY_PLUGIN import com.grab.grazel.gradle.AndroidVariantDataSource @@ -43,6 +46,7 @@ class DefaultManifestValuesBuilderTest : GrazelPluginTest() { private lateinit var androidBinary: Project private lateinit var androidLibrary: Project private lateinit var defaultManifestValuesBuilder: DefaultManifestValuesBuilder + private val fakeVariant = FakeVariant(DEBUG_FLAVOR1, FLAVOR1) @Before fun setUp() { @@ -100,6 +104,7 @@ class DefaultManifestValuesBuilderTest : GrazelPluginTest() { val defaultConfig = androidBinary.the().defaultConfig val androidBinaryManifestValues = defaultManifestValuesBuilder.build( androidBinary, + fakeVariant, defaultConfig, "test.packageName" ) diff --git a/sample-android-flavor/BUILD.bazel b/sample-android-flavor/BUILD.bazel index a1500b01..babb5744 100644 --- a/sample-android-flavor/BUILD.bazel +++ b/sample-android-flavor/BUILD.bazel @@ -1,14 +1,14 @@ load("@grab_bazel_common//tools/build_config:build_config.bzl", "build_config") build_config( - name = "sample-android-flavor-build-config", + name = "sample-android-flavor-flavor1-debug-build-config", package_name = "com.grab.grazel.android.flavor", ) load("@grab_bazel_common//tools/databinding:databinding.bzl", "kt_db_android_library") kt_db_android_library( - name = "sample-android-flavor", + name = "sample-android-flavor-flavor1-debug", srcs = glob([ "src/main/java/com/grab/grazel/android/flavor/**/*.kt", "src/flavor1/java/com/grab/grazel/android/flavor/HelloFlavorMessage.kt", @@ -24,9 +24,9 @@ kt_db_android_library( "//visibility:public", ], deps = [ - ":sample-android-flavor-build-config", + ":sample-android-flavor-flavor1-debug-build-config", "//:parcelize", - "//sample-lib-flavor1", + "//sample-library-flavor1", "@maven//:androidx_appcompat_appcompat", "@maven//:androidx_constraintlayout_constraintlayout", "@maven//:androidx_constraintlayout_constraintlayout_core", diff --git a/sample-android-flavor/build.gradle b/sample-android-flavor/build.gradle index 906907bc..c5adda9a 100644 --- a/sample-android-flavor/build.gradle +++ b/sample-android-flavor/build.gradle @@ -47,8 +47,8 @@ android { } dependencies { - flavor1Implementation project(path: ":sample-lib-flavor1") - flavor2Implementation project(path: ":sample-lib-flavor2") + flavor1Implementation project(path: ':sample-library-flavor1') + flavor2Implementation project(path: ":sample-library-flavor2") implementation "androidx.appcompat:appcompat:1.3.1" implementation "androidx.core:core:1.5.0" diff --git a/sample-android-lib/BUILD.bazel b/sample-android-library/BUILD.bazel similarity index 76% rename from sample-android-lib/BUILD.bazel rename to sample-android-library/BUILD.bazel index 28059425..43acf287 100644 --- a/sample-android-lib/BUILD.bazel +++ b/sample-android-library/BUILD.bazel @@ -1,14 +1,14 @@ load("@grab_bazel_common//tools/build_config:build_config.bzl", "build_config") build_config( - name = "sample-android-lib-build-config", + name = "sample-android-library-debug-build-config", package_name = "com.grab.grazel.android.sample.lib", ) load("@grab_bazel_common//tools/kotlin:android.bzl", "kt_android_library") kt_android_library( - name = "sample-android-lib", + name = "sample-android-library-debug", srcs = glob([ "src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt", ]), @@ -18,7 +18,7 @@ kt_android_library( "//visibility:public", ], deps = [ - ":sample-android-lib-build-config", + ":sample-android-library-debug-build-config", "//:parcelize", "@maven//:org_jacoco_org_jacoco_ant", ], @@ -27,12 +27,12 @@ kt_android_library( load("@grab_bazel_common//tools/test:test.bzl", "grab_android_local_test") grab_android_local_test( - name = "sample-android-lib-test", + name = "sample-android-library-test-debug-unit-test", srcs = glob([ "src/test/java/com/grab/grazel/android/sample/SampleViewModelTest.kt", ]), associates = [ - "//sample-android-lib:sample-android-lib_kt", + "//sample-android-library:sample-android-library-debug_kt", ], custom_package = "com.grab.grazel.android.sample.lib", resources = glob([ @@ -43,7 +43,7 @@ grab_android_local_test( ], deps = [ "//:parcelize", - "//sample-android-lib", + "//sample-android-library:sample-android-library-debug", "@maven//:junit_junit", ], ) diff --git a/sample-android-lib/build.gradle b/sample-android-library/build.gradle similarity index 100% rename from sample-android-lib/build.gradle rename to sample-android-library/build.gradle diff --git a/sample-android-lib/src/main/AndroidManifest.xml b/sample-android-library/src/main/AndroidManifest.xml similarity index 100% rename from sample-android-lib/src/main/AndroidManifest.xml rename to sample-android-library/src/main/AndroidManifest.xml diff --git a/sample-android-lib/src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt b/sample-android-library/src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt similarity index 100% rename from sample-android-lib/src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt rename to sample-android-library/src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt diff --git a/sample-android-lib/src/test/java/com/grab/grazel/android/sample/SampleViewModelTest.kt b/sample-android-library/src/test/java/com/grab/grazel/android/sample/SampleViewModelTest.kt similarity index 100% rename from sample-android-lib/src/test/java/com/grab/grazel/android/sample/SampleViewModelTest.kt rename to sample-android-library/src/test/java/com/grab/grazel/android/sample/SampleViewModelTest.kt diff --git a/sample-android-lib/src/test/resources/mock-response.json b/sample-android-library/src/test/resources/mock-response.json similarity index 100% rename from sample-android-lib/src/test/resources/mock-response.json rename to sample-android-library/src/test/resources/mock-response.json diff --git a/sample-android-lib/src/test/resources/test.json b/sample-android-library/src/test/resources/test.json similarity index 100% rename from sample-android-lib/src/test/resources/test.json rename to sample-android-library/src/test/resources/test.json diff --git a/sample-android/BUILD.bazel b/sample-android/BUILD.bazel index 77e35421..a612e4d6 100644 --- a/sample-android/BUILD.bazel +++ b/sample-android/BUILD.bazel @@ -1,70 +1,3 @@ -load("@grab_bazel_common//tools/build_config:build_config.bzl", "build_config") - -build_config( - name = "sample-android_lib-build-config", - package_name = "com.grab.grazel.android.sample", - booleans = { - "SOME_BOOLEAN": "false", - }, - ints = { - "SOME_INT": 0, - "VERSION_CODE": 1, - }, - longs = { - "SOME_LONG": 0, - }, - strings = { - "SOME_STRING": "Something", - "VERSION_NAME": "1.0", - }, -) - -load("@grab_bazel_common//tools/custom_res:custom_res.bzl", "custom_res") -load("@grab_bazel_common//tools/res_value:res_value.bzl", "res_value") -load("@grab_bazel_common//tools/kotlin:android.bzl", "kt_android_library") - -kt_android_library( - name = "sample-android_lib", - srcs = glob([ - "src/main/java/com/grab/grazel/android/sample/MainActivity.kt", - ]), - assets = glob([ - "src/main/assets/assert-file.png", - ]), - assets_dir = "src/main/assets", - custom_package = "com.grab.grazel.android.sample", - manifest = "src/debug/AndroidManifest.xml", - resource_files = glob([ - "src/main/res/**", - ]) + custom_res( - dir_name = "res-debug", - resource_files = glob([ - "src/main/res-debug/values/strings.xml", - ]), - target = "sample-android_lib", - ) + res_value( - name = "sample-android_lib-res-value", - strings = { - "generated_value": "important-key", - }, - ), - visibility = [ - "//visibility:public", - ], - deps = [ - ":sample-android_lib-build-config", - "//:dagger", - "//:parcelize", - "//sample-android-flavor", - "//sample-android-lib", - "//sample-kotlin-lib", - "@maven//:androidx_appcompat_appcompat", - "@maven//:androidx_constraintlayout_constraintlayout", - "@maven//:org_jacoco_org_jacoco_ant", - ], -) - -load("@grab_bazel_common//tools/custom_res:custom_res.bzl", "custom_res") load("@tools_android//tools/googleservices:defs.bzl", "google_services_xml") GOOGLE_SERVICES_XML = google_services_xml( @@ -81,8 +14,10 @@ crashlytics_android_library( resource_files = GOOGLE_SERVICES_XML, ) +load("@grab_bazel_common//tools/custom_res:custom_res.bzl", "custom_res") + android_binary( - name = "sample-android", + name = "sample-android-flavor1-debug", assets = glob([ "src/main/assets/assert-file.png", ]), @@ -111,15 +46,14 @@ android_binary( resource_files = glob([ "src/main/res-debug/values/strings.xml", ]), - target = "sample-android", + target = "sample-android-flavor1-debug", ) + GOOGLE_SERVICES_XML, visibility = [ "//visibility:public", ], deps = [ ":crashlytics_lib", - ":sample-android_lib", - ":sample-android_lib-build-config", + ":sample-android_lib-flavor1-debug", "@maven//:androidx_annotation_annotation", "@maven//:androidx_databinding_databinding_adapters", "@maven//:androidx_databinding_databinding_common", @@ -127,3 +61,69 @@ android_binary( "@maven//:androidx_databinding_viewbinding", ], ) + +load("@grab_bazel_common//tools/build_config:build_config.bzl", "build_config") + +build_config( + name = "sample-android_lib-flavor1-debug-build-config", + package_name = "com.grab.grazel.android.sample", + booleans = { + "SOME_BOOLEAN": "false", + }, + ints = { + "SOME_INT": 0, + "VERSION_CODE": 1, + }, + longs = { + "SOME_LONG": 0, + }, + strings = { + "SOME_STRING": "Something", + "VERSION_NAME": "1.0", + }, +) + +load("@grab_bazel_common//tools/custom_res:custom_res.bzl", "custom_res") +load("@grab_bazel_common//tools/res_value:res_value.bzl", "res_value") +load("@grab_bazel_common//tools/kotlin:android.bzl", "kt_android_library") + +kt_android_library( + name = "sample-android_lib-flavor1-debug", + srcs = glob([ + "src/main/java/com/grab/grazel/android/sample/MainActivity.kt", + ]), + assets = glob([ + "src/main/assets/assert-file.png", + ]), + assets_dir = "src/main/assets", + custom_package = "com.grab.grazel.android.sample", + manifest = "src/debug/AndroidManifest.xml", + resource_files = glob([ + "src/main/res/**", + ]) + custom_res( + dir_name = "res-debug", + resource_files = glob([ + "src/main/res-debug/values/strings.xml", + ]), + target = "sample-android_lib-flavor1-debug", + ) + res_value( + name = "sample-android_lib-flavor1-debug-res-value", + strings = { + "generated_value": "important-key", + }, + ), + visibility = [ + "//visibility:public", + ], + deps = [ + ":sample-android_lib-flavor1-debug-build-config", + "//:dagger", + "//:parcelize", + "//sample-android-flavor:sample-android-flavor-flavor1-debug", + "//sample-android-library:sample-android-library-debug", + "//sample-kotlin-library", + "@maven//:androidx_appcompat_appcompat", + "@maven//:androidx_constraintlayout_constraintlayout", + "@maven//:org_jacoco_org_jacoco_ant", + ], +) diff --git a/sample-android/build.gradle b/sample-android/build.gradle index 892c3ef8..0f04cb25 100644 --- a/sample-android/build.gradle +++ b/sample-android/build.gradle @@ -81,8 +81,8 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) - implementation project(path: ":sample-kotlin-lib") - implementation project(path: ":sample-android-lib") + implementation project(path: ':sample-kotlin-library') + implementation project(path: ':sample-android-library') implementation project(path: ":sample-android-flavor") implementation "androidx.appcompat:appcompat:1.3.1" diff --git a/sample-kotlin-lib/.gitignore b/sample-kotlin-library/.gitignore similarity index 100% rename from sample-kotlin-lib/.gitignore rename to sample-kotlin-library/.gitignore diff --git a/sample-kotlin-lib/BUILD.bazel b/sample-kotlin-library/BUILD.bazel similarity index 80% rename from sample-kotlin-lib/BUILD.bazel rename to sample-kotlin-library/BUILD.bazel index 36126738..576bdd21 100644 --- a/sample-kotlin-lib/BUILD.bazel +++ b/sample-kotlin-library/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") kt_jvm_library( - name = "sample-kotlin-lib", + name = "sample-kotlin-library", srcs = glob([ "src/main/java/com/grab/grazel/sample/HelloWorld.kt", ]), @@ -13,12 +13,12 @@ kt_jvm_library( load("@grab_bazel_common//tools/test:test.bzl", "grab_kt_jvm_test") grab_kt_jvm_test( - name = "sample-kotlin-lib-test", + name = "sample-kotlin-library-test", srcs = glob([ "src/test/java/com/grab/grazel/sample/TestClass1.kt", ]), associates = [ - "//sample-kotlin-lib", + "//sample-kotlin-library:sample-kotlin-library", ], visibility = [ "//visibility:public", diff --git a/sample-kotlin-lib/build.gradle b/sample-kotlin-library/build.gradle similarity index 100% rename from sample-kotlin-lib/build.gradle rename to sample-kotlin-library/build.gradle diff --git a/sample-kotlin-lib/src/main/java/com/grab/grazel/sample/HelloWorld.kt b/sample-kotlin-library/src/main/java/com/grab/grazel/sample/HelloWorld.kt similarity index 100% rename from sample-kotlin-lib/src/main/java/com/grab/grazel/sample/HelloWorld.kt rename to sample-kotlin-library/src/main/java/com/grab/grazel/sample/HelloWorld.kt diff --git a/sample-kotlin-lib/src/test/java/com/grab/grazel/sample/TestClass1.kt b/sample-kotlin-library/src/test/java/com/grab/grazel/sample/TestClass1.kt similarity index 100% rename from sample-kotlin-lib/src/test/java/com/grab/grazel/sample/TestClass1.kt rename to sample-kotlin-library/src/test/java/com/grab/grazel/sample/TestClass1.kt diff --git a/sample-lib-flavor1/.gitignore b/sample-library-flavor1/.gitignore similarity index 100% rename from sample-lib-flavor1/.gitignore rename to sample-library-flavor1/.gitignore diff --git a/sample-lib-flavor1/BUILD.bazel b/sample-library-flavor1/BUILD.bazel similarity index 80% rename from sample-lib-flavor1/BUILD.bazel rename to sample-library-flavor1/BUILD.bazel index e8602b41..152ae42e 100644 --- a/sample-lib-flavor1/BUILD.bazel +++ b/sample-library-flavor1/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") kt_jvm_library( - name = "sample-lib-flavor1", + name = "sample-library-flavor1", srcs = glob([ "src/main/java/com/grab/grazel/android/flavor/ModuleName.kt", ]), @@ -13,12 +13,12 @@ kt_jvm_library( load("@grab_bazel_common//tools/test:test.bzl", "grab_kt_jvm_test") grab_kt_jvm_test( - name = "sample-lib-flavor1-test", + name = "sample-library-flavor1-test", srcs = glob([ "src/test/java/com/grab/grazel/flavor1/ModuleNameTest.kt", ]), associates = [ - "//sample-lib-flavor1", + "//sample-library-flavor1:sample-library-flavor1", ], visibility = [ "//visibility:public", diff --git a/sample-lib-flavor1/build.gradle b/sample-library-flavor1/build.gradle similarity index 100% rename from sample-lib-flavor1/build.gradle rename to sample-library-flavor1/build.gradle diff --git a/sample-lib-flavor1/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt b/sample-library-flavor1/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt similarity index 100% rename from sample-lib-flavor1/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt rename to sample-library-flavor1/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt diff --git a/sample-lib-flavor1/src/test/java/com/grab/grazel/flavor1/ModuleNameTest.kt b/sample-library-flavor1/src/test/java/com/grab/grazel/flavor1/ModuleNameTest.kt similarity index 100% rename from sample-lib-flavor1/src/test/java/com/grab/grazel/flavor1/ModuleNameTest.kt rename to sample-library-flavor1/src/test/java/com/grab/grazel/flavor1/ModuleNameTest.kt diff --git a/sample-lib-flavor2/.gitignore b/sample-library-flavor2/.gitignore similarity index 100% rename from sample-lib-flavor2/.gitignore rename to sample-library-flavor2/.gitignore diff --git a/sample-lib-flavor2/BUILD.bazel b/sample-library-flavor2/BUILD.bazel similarity index 80% rename from sample-lib-flavor2/BUILD.bazel rename to sample-library-flavor2/BUILD.bazel index 99c7af19..18e26768 100644 --- a/sample-lib-flavor2/BUILD.bazel +++ b/sample-library-flavor2/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") kt_jvm_library( - name = "sample-lib-flavor2", + name = "sample-library-flavor2", srcs = glob([ "src/main/java/com/grab/grazel/android/flavor/ModuleName.kt", ]), @@ -13,12 +13,12 @@ kt_jvm_library( load("@grab_bazel_common//tools/test:test.bzl", "grab_kt_jvm_test") grab_kt_jvm_test( - name = "sample-lib-flavor2-test", + name = "sample-library-flavor2-test", srcs = glob([ "src/test/java/com/grab/grazel/flavor2/ModuleNameTest.kt", ]), associates = [ - "//sample-lib-flavor2", + "//sample-library-flavor2:sample-library-flavor2", ], visibility = [ "//visibility:public", diff --git a/sample-lib-flavor2/build.gradle b/sample-library-flavor2/build.gradle similarity index 100% rename from sample-lib-flavor2/build.gradle rename to sample-library-flavor2/build.gradle diff --git a/sample-lib-flavor2/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt b/sample-library-flavor2/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt similarity index 100% rename from sample-lib-flavor2/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt rename to sample-library-flavor2/src/main/java/com/grab/grazel/android/flavor/ModuleName.kt diff --git a/sample-lib-flavor2/src/test/java/com/grab/grazel/flavor2/ModuleNameTest.kt b/sample-library-flavor2/src/test/java/com/grab/grazel/flavor2/ModuleNameTest.kt similarity index 100% rename from sample-lib-flavor2/src/test/java/com/grab/grazel/flavor2/ModuleNameTest.kt rename to sample-library-flavor2/src/test/java/com/grab/grazel/flavor2/ModuleNameTest.kt diff --git a/settings.gradle b/settings.gradle index 22ea983b..f7d7dc59 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,11 +17,11 @@ rootProject.name = "grazel" include(":sample-android") -include(":sample-android-lib") +include(':sample-android-library') include(":sample-android-flavor") -include(":sample-lib-flavor1") -include(":sample-lib-flavor2") -include(":sample-kotlin-lib") +include(':sample-library-flavor1') +include(":sample-library-flavor2") +include(':sample-kotlin-library') includeBuild("grazel-gradle-plugin") {