Skip to content

Commit

Permalink
Input res folders to react when something changes
Browse files Browse the repository at this point in the history
  • Loading branch information
extmkv committed Feb 5, 2024
1 parent e2be440 commit c2b6641
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
application = { id = "com.android.application", version.ref = "androidGradle" }
library = { id = "com.android.library", version.ref = "androidGradle" }
androidGradleX = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
resourcePlaceholders = "pt.jcosta.resourceplaceholders:0.10.0"
resourcePlaceholders = "pt.jcosta.resourceplaceholders:0.11.0"
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "pt.jcosta.resourceplaceholders"
version = "0.10.0"
version = "0.11.0"

// Use java-gradle-plugin to generate plugin descriptors and specify plugin ids
gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.api.AndroidBasePlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.File

private const val EXTENSION_NAME = "resourcePlaceholders"
internal const val BUILD_FOLDER = "resourcePlaceholders/"
Expand Down Expand Up @@ -42,8 +43,11 @@ class ResourcePlaceholdersPlugin : Plugin<Project> {
"resourcePlaceholdersFor${variant.name.replaceFirstChar { char -> char.uppercase() }}"
val outputDir = project.layout.buildDirectory.dir(BUILD_FOLDER + variant.name)

val files = project.getResDirs()

val task =
project.tasks.register(taskName, ResourcePlaceholdersTask::class.java) { task ->
task.inputs.files(files)
task.group = "resource-placeholders"
task.variantName = variant.name
task.source = variant.sources.res?.all
Expand All @@ -63,6 +67,29 @@ class ResourcePlaceholdersPlugin : Plugin<Project> {
return plugin is AndroidBasePlugin
}

/**
* Search for all the directories with .xml files and check if they have a parent called res
* then returns the res folders
*/
private fun Project.getResDirs(): List<File> {
return project.layout.projectDirectory.asFileTree.filter {
!it.absolutePath.contains(project.layout.buildDirectory.get().asFile.absolutePath) && it.extension == "xml"
}.mapNotNull { file ->
var currentDirectory = file.parentFile
var isInsideResDirectory = false

while (currentDirectory != null) {
if (currentDirectory.name == "res") {
isInsideResDirectory = true
break
}
currentDirectory = currentDirectory.parentFile
}

if (isInsideResDirectory) currentDirectory else null
}.distinct()
}

private fun Project.getAndroidPluginOrNull(): AndroidBasePlugin? {
return project.plugins.findPlugin(AndroidBasePlugin::class.java)
}
Expand Down
4 changes: 4 additions & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ android {
}
}

testOptions {
unitTests.isIncludeAndroidResources = true
}

flavorDimensions += listOf("env")
productFlavors {
create("staging") {
Expand Down

0 comments on commit c2b6641

Please sign in to comment.