Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for resource_sets API in bazel-common #132

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "grab_bazel_common",
commit = "e68539fe1df4eff9c613bd78a6003c6dad9a0018",
commit = "77f924900d1931ef531131132bdfe4842695a030",
remote = "https://github.com/grab/grab-bazel-common.git",
)

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ grazel {
rules {
bazelCommon {
gitRepository {
commit = "e68539fe1df4eff9c613bd78a6003c6dad9a0018"
commit = "77f924900d1931ef531131132bdfe4842695a030"
remote = "https://github.com/grab/grab-bazel-common.git"
}
toolchains {
Expand Down Expand Up @@ -169,7 +169,7 @@ idea {
module {
excludeDirs += [
file("bazel-bin"),
file("bazel-grazel"),
file("bazel-" + projectDir.name),
file("bazel-cache"),
file("bazel-out"),
file("bazel-testlogs"),
Expand Down
46 changes: 32 additions & 14 deletions flavors/sample-android-flavor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ android_library(
"generated_value": "generated",
},
},
resources = {
"src/demo/res": {
resource_sets = {
"demo": {
"res": "src/demo/res",
},
"src/free/res": {
"free": {
"res": "src/free/res",
},
"src/main/res": {
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
Expand Down Expand Up @@ -58,10 +63,14 @@ android_library(
"generated_value": "generated",
},
},
resources = {
"src/demo/res": {
resource_sets = {
"demo": {
"res": "src/demo/res",
},
"src/main/res": {
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
Expand Down Expand Up @@ -96,12 +105,17 @@ android_library(
"generated_value": "generated",
},
},
resources = {
"src/full/res": {
resource_sets = {
"full": {
"res": "src/full/res",
},
"src/free/res": {
"free": {
"res": "src/free/res",
},
"src/main/res": {
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
Expand Down Expand Up @@ -136,10 +150,14 @@ android_library(
"generated_value": "generated",
},
},
resources = {
"src/full/res": {
resource_sets = {
"full": {
"res": "src/full/res",
},
"src/main/res": {
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal fun StatementsBuilder.androidBinary(
enableCompose: Boolean = false,
visibility: Visibility = Visibility.Public,
resourceFiles: List<Assignee> = emptyList(),
resources: Assignee? = null,
resourceSets: Assignee? = null,
resValuesData: ResValuesData,
deps: List<BazelDependency>,
assetsGlob: List<String> = emptyList(),
Expand Down Expand Up @@ -172,7 +172,7 @@ internal fun StatementsBuilder.androidBinary(
resConfigFilters.notEmpty {
"resource_configuration_filters" `=` resConfigFilters.quote
}
resources?.let { "resources" `=` resources }
resourceSets?.let { "resource_sets" `=` resourceSets }
deps.notEmpty {
"deps" `=` array(deps.map(BazelDependency::toString).quote)
}
Expand All @@ -199,7 +199,7 @@ internal fun StatementsBuilder.androidLibrary(
manifest: String? = null,
srcsGlob: List<String> = emptyList(),
visibility: Visibility = Visibility.Public,
resources: Assignee? = null,
resorceSets: Assignee? = null,
resourceFiles: List<Assignee> = emptyList(),
enableDataBinding: Boolean = false,
enableCompose: Boolean = false,
Expand All @@ -226,7 +226,7 @@ internal fun StatementsBuilder.androidLibrary(
transform = Assignee::asString
)
}
resources?.let { "resources" `=` resources }
resorceSets?.let { "resource_sets" `=` resorceSets }
deps.notEmpty {
"deps" `=` array(deps.map(BazelDependency::toString).map(String::quote))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ import com.grab.grazel.bazel.rules.Multidex
import com.grab.grazel.bazel.starlark.BazelDependency
import com.grab.grazel.bazel.starlark.LintConfigs

internal data class BazelSourceSet(
val name: String,
val res: String?,
val assets: String?,
val manifest: String?,
)

internal interface AndroidData {
val name: String
val srcs: List<String>
val res: List<String>
val resourceSets: Set<BazelSourceSet>
val resValuesData: ResValuesData
val assets: List<String>
val assetsDir: String?
val manifestFile: String?

// Custom package used for detecting Java/Kotlin sources root
Expand All @@ -46,10 +51,8 @@ internal interface AndroidData {
internal data class AndroidLibraryData(
override val name: String,
override val srcs: List<String> = emptyList(),
override val res: List<String> = emptyList(),
override val resourceSets: Set<BazelSourceSet> = emptySet(),
override val resValuesData: ResValuesData = ResValuesData(),
override val assets: List<String> = emptyList(),
override val assetsDir: String? = null,
override val manifestFile: String? = null,
override val customPackage: String,
override val packageName: String,
Expand All @@ -65,10 +68,8 @@ internal data class AndroidLibraryData(
internal data class AndroidBinaryData(
override val name: String,
override val srcs: List<String> = emptyList(),
override val res: List<String> = emptyList(),
override val resourceSets: Set<BazelSourceSet> = emptySet(),
override val resValuesData: ResValuesData = ResValuesData(),
override val assets: List<String> = emptyList(),
override val assetsDir: String? = null,
override val manifestFile: String? = null,
override val customPackage: String,
override val packageName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package com.grab.grazel.migrate.android

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import com.android.build.gradle.internal.dsl.LintOptions
import com.grab.grazel.GrazelExtension
import com.grab.grazel.bazel.rules.Multidex
import com.grab.grazel.bazel.starlark.BazelDependency
import com.grab.grazel.bazel.starlark.LintConfigs
import com.grab.grazel.gradle.ConfigurationScope.BUILD
import com.grab.grazel.gradle.dependencies.BuildGraphType
import com.grab.grazel.gradle.dependencies.DependenciesDataSource
Expand All @@ -36,10 +34,7 @@ import com.grab.grazel.gradle.variant.AndroidVariantDataSource
import com.grab.grazel.gradle.variant.MatchedVariant
import com.grab.grazel.gradle.variant.getMigratableBuildVariants
import com.grab.grazel.gradle.variant.nameSuffix
import com.grab.grazel.migrate.android.PathResolveMode.DIRECTORY
import com.grab.grazel.migrate.android.SourceSetType.ASSETS
import com.grab.grazel.migrate.android.SourceSetType.JAVA_KOTLIN
import com.grab.grazel.migrate.android.SourceSetType.RESOURCES
import com.grab.grazel.migrate.dependencies.calculateDirectDependencyTags
import com.grab.grazel.migrate.kotlin.kotlinParcelizeDeps
import dagger.Lazy
Expand Down Expand Up @@ -105,7 +100,6 @@ constructor(
matchedVariant: MatchedVariant,
extension: BaseExtension,
deps: List<BazelDependency>,
sourceSetType: SourceSetType = JAVA_KOTLIN
): AndroidLibraryData {
// Only consider source sets from migratable variants
val migratableSourceSets = matchedVariant.variant.sourceSets
Expand All @@ -115,13 +109,14 @@ constructor(
extension,
migratableSourceSets
) ?: ""
val srcs = androidSources(migratableSourceSets, sourceSetType).toList()
val res = androidSources(migratableSourceSets, RESOURCES, DIRECTORY).toList().reversed()
val srcs = androidSources(migratableSourceSets, JAVA_KOTLIN).toList()

val assets = androidSources(migratableSourceSets, ASSETS).toList()
val assetsDir = assetsDirectory(migratableSourceSets, assets)
val resourceSets = migratableSourceSets.flatMap { it.toResourceSet(project) }
.reversed()
.toSet()

val manifestFile = androidManifestParser.androidManifestFile(migratableSourceSets)
val manifestFile = androidManifestParser
.androidManifestFile(migratableSourceSets)
?.let(::relativePath)

val tags = if (grazelExtension.rules.kotlin.enabledTransitiveReduction) {
Expand All @@ -133,9 +128,7 @@ constructor(
return AndroidLibraryData(
name = name + matchedVariant.nameSuffix,
srcs = srcs,
res = res,
assets = assets,
assetsDir = assetsDir,
resourceSets = resourceSets,
manifestFile = manifestFile,
customPackage = packageName,
packageName = packageName,
Expand All @@ -148,19 +141,6 @@ constructor(
lintConfigs = lintConfigs
)
}

private fun Project.assetsDirectory(
sourceSets: List<AndroidSourceSet>,
assets: List<String>
): String? {
return if (assets.isNotEmpty()) {
val assetItem = assets.first()
sourceSets
.flatMap { it.assets.srcDirs }
.map { relativePath(it) }
.first { assetItem.contains(it) }
} else null
}
}

internal interface AndroidBinaryDataExtractor : AndroidExtractor<AndroidBinaryData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,25 @@ internal fun StatementsBuilder.buildResFiles(
}

internal fun buildResources(
resDirs: List<String>,
) = if (resDirs.isEmpty()) null else
resourceSets: Set<BazelSourceSet>,
) = if (resourceSets.isEmpty()) null else
Assignee {
add(
statement = resDirs
.groupBy { it }
.mapValues { emptyMap<String, String>() }
.toObject(quoteKeys = true, quoteValues = true, allowEmpty = true)
statement = resourceSets.groupBy(
keySelector = { it.name },
valueTransform = {
mapOf(
"res" to it.res,
"assets" to it.assets,
"manifest" to it.manifest
)
})
.mapValues { it.value.first() }
.toObject(
quoteKeys = true,
quoteValues = true,
allowEmpty = false
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal interface AndroidTarget : BazelBuildTarget {
val enableDataBinding: Boolean
val enableCompose: Boolean
val projectName: String
val resDirs: List<String>
val resourceSets: Set<BazelSourceSet>
val resValuesData: ResValuesData
val buildConfigData: BuildConfigData
val packageName: String
Expand All @@ -48,7 +48,7 @@ internal data class AndroidLibraryTarget(
override val enableDataBinding: Boolean = false,
override val enableCompose: Boolean = false,
override val projectName: String = name,
override val resDirs: List<String>,
override val resourceSets: Set<BazelSourceSet> = emptySet(),
override val resValuesData: ResValuesData = ResValuesData(),
override val buildConfigData: BuildConfigData = BuildConfigData(),
override val packageName: String,
Expand All @@ -66,7 +66,7 @@ internal data class AndroidLibraryTarget(
enableDataBinding = enableDataBinding,
enableCompose = enableCompose,
srcsGlob = srcs,
resources = buildResources(resDirs),
resorceSets = buildResources(resourceSets),
visibility = visibility,
deps = deps,
tags = tags,
Expand All @@ -88,7 +88,7 @@ internal data class AndroidBinaryTarget(
override val enableDataBinding: Boolean = false,
override val enableCompose: Boolean = false,
override val projectName: String = name,
override val resDirs: List<String>,
override val resourceSets: Set<BazelSourceSet> = emptySet(),
override val resValuesData: ResValuesData = ResValuesData(),
override val buildConfigData: BuildConfigData = BuildConfigData(),
override val packageName: String,
Expand Down Expand Up @@ -123,7 +123,7 @@ internal data class AndroidBinaryTarget(
manifest = manifest,
manifestValues = manifestValues,
resConfigFilters = resConfigFilters,
resources = buildResources(resDirs),
resourceSets = buildResources(resourceSets),
resValuesData = resValuesData,
deps = deps,
assetsGlob = assetsGlob,
Expand Down
Loading
Loading