Skip to content

Commit

Permalink
Add option to disable compose resources generation (#4526)
Browse files Browse the repository at this point in the history
# Changes

* Add `Never` to `enum class ResourceClassGeneration` to disable the
generation of Res class in the gradle plugin

# Motivation

As the [comment in issue
4229](#4229 (comment))
said, my team is not in the ecosystem of gradle, but organize the build
steps in bazel. We want to follow the files layout but just disable the
generation task of gradle and handle it by outself.
  • Loading branch information
xiaozhikang0916 committed Mar 25, 2024
1 parent 5f67523 commit 7c0dcfb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ abstract class ResourcesExtension {
*/
var packageOfResClass: String = ""

enum class ResourceClassGeneration { Auto, Always }
enum class ResourceClassGeneration { Auto, Always, Never }

//to support groovy DSL
val auto = ResourceClassGeneration.Auto
val always = ResourceClassGeneration.Always
val never = ResourceClassGeneration.Never

/**
* The mode of resource class generation.
*
* - `auto`: The Res class will be generated if the current project has an explicit "implementation" or "api" dependency on the resource's library.
* - `always`: Unconditionally generate the Res class. This may be useful when the resources library is available transitively.
* - `never`: Never generate the Res class.
*/
var generateResClass: ResourceClassGeneration = auto
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ private fun Project.configureResourceGenerator(
ResourcesExtension.ResourceClassGeneration.Always -> {
true
}
ResourcesExtension.ResourceClassGeneration.Never -> {
false
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,20 @@ class ResourcesTest : GradlePluginTestBase() {
.map { it.toPath().relativeTo(actualPath) }.sorted().joinToString("\n")
assertEquals(expectedFilesCount, actualFilesCount)
}

@Test
fun testResourcesTaskDisabled() = with(testProject("misc/commonResources")) {
file("build.gradle.kts").appendText(
"""
compose {
resources {
generateResClass = never
}
}
""".trimIndent()
)
gradle("generateComposeResClass").checks {
check.logContains("Generation Res class is disabled")
}
}
}

0 comments on commit 7c0dcfb

Please sign in to comment.