From 95f04187cf0cf1e0825af34f477c5db7b4a8037e Mon Sep 17 00:00:00 2001 From: Andrey Mukamolov <4471821+fobo66@users.noreply.github.com> Date: Fri, 23 Sep 2022 20:57:55 +0300 Subject: [PATCH] Replaced incorrect capitalize() method to mitigate issue with camelCase flavors --- google-services-plugin/build.gradle | 1 + .../GoogleServicesPlugin.groovy | 3 ++- .../googleservices/GoogleServicesTask.java | 12 ++++-------- .../GoogleServicesPluginTest.java | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/google-services-plugin/build.gradle b/google-services-plugin/build.gradle index 69478128..f1bb21bb 100644 --- a/google-services-plugin/build.gradle +++ b/google-services-plugin/build.gradle @@ -23,6 +23,7 @@ dependencies { implementation 'com.google.android.gms:strict-version-matcher-plugin:1.2.4' implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.google.guava:guava:27.0.1-jre' + implementation 'org.apache.commons:commons-lang3:3.12.0' testImplementation 'junit:junit:4.12' testImplementation 'com.google.truth:truth:0.42' } diff --git a/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesPlugin.groovy b/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesPlugin.groovy index c382b1b9..1a0d8a32 100644 --- a/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesPlugin.groovy +++ b/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesPlugin.groovy @@ -164,7 +164,8 @@ class GoogleServicesPlugin implements Plugin { "com.android.model.application" ]), MODEL_LIBRARY(["com.android.model.library"]) - public PluginType(Collection plugins) { + + PluginType(Collection plugins) { this.plugins = plugins } private final Collection plugins diff --git a/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesTask.java b/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesTask.java index aede4107..58ac1f39 100644 --- a/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesTask.java +++ b/google-services-plugin/src/main/groovy/com/google/gms/googleservices/GoogleServicesTask.java @@ -24,6 +24,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; +import org.apache.commons.lang3.StringUtils; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.file.DirectoryProperty; @@ -458,20 +459,20 @@ private static long countSlashes(String input) { static List getJsonLocations(String buildType, List flavorNames) { List fileLocations = new ArrayList<>(); - String flavorName = flavorNames.stream().reduce("", (a,b) -> a + (a.length() == 0 ? b : capitalize(b))); + String flavorName = flavorNames.stream().reduce("", (a, b) -> a + (a.length() == 0 ? b : StringUtils.capitalize(b))); fileLocations.add(""); fileLocations.add("src/" + flavorName + "/" + buildType); fileLocations.add("src/" + buildType + "/" + flavorName); fileLocations.add("src/" + flavorName); fileLocations.add("src/" + buildType); - fileLocations.add("src/" + flavorName + capitalize(buildType)); + fileLocations.add("src/" + flavorName + StringUtils.capitalize(buildType)); fileLocations.add("src/" + buildType); String fileLocation = "src"; for(String flavor : flavorNames) { fileLocation += "/" + flavor; fileLocations.add(fileLocation); fileLocations.add(fileLocation + "/" + buildType); - fileLocations.add(fileLocation + capitalize(buildType)); + fileLocations.add(fileLocation + StringUtils.capitalize(buildType)); } fileLocations = fileLocations .stream() @@ -481,9 +482,4 @@ static List getJsonLocations(String buildType, List flavorNames) .collect(toList()); return fileLocations; } - - public static String capitalize(String s) { - if (s.length() == 0) return s; - return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); - } } diff --git a/google-services-plugin/src/test/java/com/google/gms/googleservices/GoogleServicesPluginTest.java b/google-services-plugin/src/test/java/com/google/gms/googleservices/GoogleServicesPluginTest.java index 728a5492..02404a49 100644 --- a/google-services-plugin/src/test/java/com/google/gms/googleservices/GoogleServicesPluginTest.java +++ b/google-services-plugin/src/test/java/com/google/gms/googleservices/GoogleServicesPluginTest.java @@ -65,6 +65,25 @@ public void testMultipleFlavors() { "src/flavor/testRelease/google-services.json"); } + @Test + public void testMultipleFlavorsWithCamelCase() { + List output = + toStringList(GoogleServicesTask.getJsonLocations("release", Arrays.asList("flavor", "testTest"))); + assertThat(output) + .containsAllOf( + "google-services.json", + "src/release/google-services.json", + "src/flavorRelease/google-services.json", + "src/flavor/google-services.json", + "src/flavor/release/google-services.json", + "src/release/flavorTestTest/google-services.json", + "src/flavorTestTest/google-services.json", + "src/flavorTestTestRelease/google-services.json", + "src/flavor/testTest/release/google-services.json", + "src/flavor/testTestRelease/google-services.json"); + } + + // This is necessary because some of the strings are actually groovy string implementations // which fail equality tests with java strings during testing private static List toStringList(List input) {