Skip to content

Commit

Permalink
Fix resource sets not handling manifests correctly (#142)
Browse files Browse the repository at this point in the history
Fixes #142
  • Loading branch information
arunkumar9t2 committed Nov 20, 2024
1 parent 8d14323 commit 0f9124e
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 42 deletions.
4 changes: 2 additions & 2 deletions 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 = "93a0875d12a7e223221390505b04deff76ea3845",
commit = "9b0439127d50741c2f19d8df9932d6338f763dab",
remote = "https://github.com/grab/grab-bazel-common.git",
)

Expand Down Expand Up @@ -400,6 +400,7 @@ maven_install(
"android.arch.lifecycle:livedata-core",
"android.arch.lifecycle:runtime",
"android.arch.lifecycle:viewmodel",
"androidx.fragment:fragment",
"com.android.databinding:baseLibrary",
"com.android.support:animated-vector-drawable",
"com.android.support:cardview-v7",
Expand All @@ -413,7 +414,6 @@ maven_install(
"com.android.support:support-compat",
"com.android.support:support-core-ui",
"com.android.support:support-core-utils",
"com.android.support:support-fragment",
"com.android.support:support-vector-drawable",
"com.android.support:versionedparcelable",
"com.android.support:viewpager",
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ grazel {
rules {
bazelCommon {
gitRepository {
commit = "93a0875d12a7e223221390505b04deff76ea3845"
commit = "9b0439127d50741c2f19d8df9932d6338f763dab"
remote = "https://github.com/grab/grab-bazel-common.git"
}
toolchains {
Expand Down
2 changes: 1 addition & 1 deletion constants.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
ext {
groupId = "com.grab.grazel"
versionName = project.hasProperty("versionName") ? versionName : "0.4.2-alpha.02"
versionName = project.hasProperty("versionName") ? versionName : "0.4.2-alpha.03"

website = "https://grab.github.io/Grazel/"
}
4 changes: 0 additions & 4 deletions flavors/sample-android-flavor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ android_library(
},
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
Expand Down Expand Up @@ -71,7 +70,6 @@ android_library(
},
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
Expand Down Expand Up @@ -117,7 +115,6 @@ android_library(
},
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
Expand Down Expand Up @@ -160,7 +157,6 @@ android_library(
},
"main": {
"res": "src/main/res",
"assets": "src/main/assets",
"manifest": "src/main/AndroidManifest.xml",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ internal data class BazelSourceSet(
val res: String?,
val assets: String?,
val manifest: String?,
)
) {
val isEmpty: Boolean = res == null && assets == null && manifest == null
val hasResources: Boolean = res != null || assets != null
}

internal interface AndroidData {
val name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,14 @@ constructor(
val srcs = androidSources(migratableSourceSets, JAVA_KOTLIN).toList()

val resourceSets = migratableSourceSets.flatMap { it.toResourceSet(project) }
.filterNot(BazelSourceSet::isEmpty)
.reversed()
.toSet()
.let { resourcesSets ->
if (resourcesSets.size == 1 && resourcesSets.all { !it.hasResources }) {
emptySet()
} else resourcesSets
}

val manifestFile = androidManifestParser
.androidManifestFile(migratableSourceSets)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,18 @@ enum class PathResolveMode {
internal fun AndroidSourceSet.toResourceSet(
project: Project
): Set<BazelSourceSet> {
val manifestPath = manifest.srcFile.takeIf { it.exists() }?.let(project::relativePath)

fun File.isValid() = exists() && walk().drop(1).any()
val manifestPath = manifest.srcFile.takeIf { it.exists() }?.let(project::relativePath)
val resources = res.srcDirs.filter(File::isValid)
val assets = assets.srcDirs.filter(File::isValid)

return if (resources.size == 1 && assets.size == 1) {
return if (resources.size <= 1 && assets.size <= 1) {
// Happy path, most modules would be like this with one single res and assets dir.
setOf(
BazelSourceSet(
name = name,
res = project.relativePath(resources.first()),
assets = project.relativePath(assets.first()),
res = resources.firstOrNull()?.let(project::relativePath),
assets = assets.firstOrNull()?.let(project::relativePath),
manifest = manifestPath
)
)
Expand All @@ -82,6 +81,8 @@ internal fun AndroidSourceSet.toResourceSet(
// set dir for Bazel.
return LinkedHashSet<BazelSourceSet>().apply {
resources.mapIndexedTo(this) { index, resDir ->
// No need to duplicate manifest across res directories for same source set, assign to
// first entry alone
val sourceSetManifest = if (index == 0) manifestPath else null
BazelSourceSet(
name = name,
Expand All @@ -91,6 +92,8 @@ internal fun AndroidSourceSet.toResourceSet(
)
}
assets.mapIndexedTo(this) { index, assets ->
// No need to duplicate manifest across res directories for same source set, assign to
// first entry alone
val sourceSetManifest = if (index == 0) manifestPath else null
BazelSourceSet(
name = name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.the
import org.junit.Test
import java.nio.file.Files
import kotlin.io.path.writeText
import kotlin.test.assertEquals

class DefaultAndroidLibraryDataExtractorTest {
Expand Down Expand Up @@ -53,6 +54,9 @@ class DefaultAndroidLibraryDataExtractorTest {
compileSdkVersion(32)
}
app(this)
sourceSets.named("main").configure {
res.srcDirs("src/main/res-extra")
}
}
dependencies {
add("implementation", libraryProject)
Expand Down Expand Up @@ -93,13 +97,21 @@ class DefaultAndroidLibraryDataExtractorTest {
}

private fun createSources() {
val resMain = appProject.file("src/main/res")
resMain.toPath().let {
Files.createDirectories(it)
val values = it.resolve("values")
Files.createDirectories(values)
values.resolve("values.xml").toFile().writeText("")
}
appProject.file("src/main/res/values")
.toPath()
.also(Files::createDirectories)
.resolve("values.xml")
.writeText("")
appProject.file("src/debug")
.toPath()
.also(Files::createDirectories)
.resolve("AndroidManifest.xml")
.writeText("<manifest package=\"grazel\" />")
appProject.file("src/main/res-extra/values")
.toPath()
.also(Files::createDirectories)
.resolve("values.xml")
.writeText("")
}

private fun debugVariant(): MatchedVariant {
Expand Down Expand Up @@ -142,13 +154,25 @@ class DefaultAndroidLibraryDataExtractorTest {
}

@Test
fun `assert resource sets are calulcated for android binary only when file exists`() {
fun `assert resource sets are calculated correctly for variants`() {
configure()
val resourceSets = androidLibraryDataExtractor
.extract(appProject, debugVariant())
.resourceSets
resourceSets.truth {
containsExactly(
BazelSourceSet(
name = "debug",
res = null,
assets = null,
manifest = "src/debug/AndroidManifest.xml"
),
BazelSourceSet(
name = "main",
res = "src/main/res-extra",
assets = null,
manifest = null,
),
BazelSourceSet(
name = "main",
res = "src/main/res",
Expand All @@ -157,7 +181,7 @@ class DefaultAndroidLibraryDataExtractorTest {
)
)
containsNoDuplicates()
hasSize(1)
hasSize(3)
}
}
}
44 changes: 40 additions & 4 deletions sample-android-library/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ android_library(
name = "sample-android-library-demo-free-debug",
srcs = glob([
"src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt",
"src/debug/kotlin/Empty.kt",
]),
custom_package = "com.grab.grazel.android.sample.lib",
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
manifest = "src/debug/AndroidManifest.xml",
resource_sets = {
"debug": {
"manifest": "src/debug/AndroidManifest.xml",
},
"main": {
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
"//visibility:public",
],
Expand All @@ -24,14 +33,23 @@ android_library(
name = "sample-android-library-demo-paid-debug",
srcs = glob([
"src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt",
"src/debug/kotlin/Empty.kt",
]),
custom_package = "com.grab.grazel.android.sample.lib",
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
manifest = "src/debug/AndroidManifest.xml",
resource_sets = {
"debug": {
"manifest": "src/debug/AndroidManifest.xml",
},
"main": {
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
"//visibility:public",
],
Expand All @@ -44,14 +62,23 @@ android_library(
name = "sample-android-library-full-free-debug",
srcs = glob([
"src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt",
"src/debug/kotlin/Empty.kt",
]),
custom_package = "com.grab.grazel.android.sample.lib",
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
manifest = "src/debug/AndroidManifest.xml",
resource_sets = {
"debug": {
"manifest": "src/debug/AndroidManifest.xml",
},
"main": {
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
"//visibility:public",
],
Expand All @@ -64,14 +91,23 @@ android_library(
name = "sample-android-library-full-paid-debug",
srcs = glob([
"src/main/java/com/grab/grazel/android/sample/SampleViewModel.kt",
"src/debug/kotlin/Empty.kt",
]),
custom_package = "com.grab.grazel.android.sample.lib",
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
manifest = "src/debug/AndroidManifest.xml",
resource_sets = {
"debug": {
"manifest": "src/debug/AndroidManifest.xml",
},
"main": {
"manifest": "src/main/AndroidManifest.xml",
},
},
visibility = [
"//visibility:public",
],
Expand Down
17 changes: 17 additions & 0 deletions sample-android-library/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ 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.
-->

<manifest package="com.grab.grazel.android.sample.lib" />
16 changes: 16 additions & 0 deletions sample-android-library/src/debug/kotlin/Empty.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2024 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.
*/

Loading

0 comments on commit 0f9124e

Please sign in to comment.