From 37a2a1b18c1e857e1e2079b4328ec9a215aa2033 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 29 Apr 2021 13:48:07 -0700 Subject: [PATCH] merge of https://github.com/google/play-services-plugins/pull/131 originally by ChristianBecker --- .../googleservices/GoogleServicesTask.java | 71 +++++-------------- .../GoogleServicesPluginTest.java | 8 ++- 2 files changed, 23 insertions(+), 56 deletions(-) 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 f5b70e6a..59769705 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 @@ -55,21 +55,6 @@ /** */ public abstract class GoogleServicesTask extends DefaultTask { public final static String JSON_FILE_NAME = "google-services.json"; - // Some example of things that match this pattern are: - // "aBunchOfFlavors/release" - // "flavor/debug" - // "test" - // And here is an example with the capture groups in [square brackets] - // [a][BunchOfFlavors]/[release] - public final static Pattern VARIANT_PATTERN = Pattern.compile("(?:([^\\p{javaUpperCase}]+)((?:\\p{javaUpperCase}[^\\p{javaUpperCase}]*)*)\\/)?([^\\/]*)"); - // Some example of things that match this pattern are: - // "TestTheFlavor" - // "FlavorsOfTheRainbow" - // "Test" - // And here is an example with the capture groups in [square brackets] - // "[Flavors][Of][The][Rainbow]" - // Note: Pattern must be applied in a loop, not just once. - public final static Pattern FLAVOR_PATTERN = Pattern.compile("(\\p{javaUpperCase}[^\\p{javaUpperCase}]*)"); private static final String STATUS_DISABLED = "1"; private static final String STATUS_ENABLED = "2"; @@ -77,7 +62,8 @@ public abstract class GoogleServicesTask extends DefaultTask { private static final String OAUTH_CLIENT_TYPE_WEB = "3"; private File intermediateDir; - private String variantDir; + private String buildType; + private List productFlavors; private ObjectFactory objectFactory; @Inject @@ -91,16 +77,25 @@ public File getIntermediateDir() { } @Input - public String getVariantDir() { - return variantDir; + public String getBuildType() { + return buildType; + } + + @Input + public List getProductFlavors() { + return productFlavors; } public void setIntermediateDir(File intermediateDir) { this.intermediateDir = intermediateDir; } - public void setVariantDir(String variantDir){ - this.variantDir = variantDir; + public void setBuildType(String buildType) { + this.buildType = buildType; + } + + public void setProductFlavors(List productFlavors) { + this.productFlavors = productFlavors; } @Input @@ -109,7 +104,7 @@ public void setVariantDir(String variantDir){ @TaskAction public void action() throws IOException { File quickstartFile = null; - List fileLocations = getJsonLocations(variantDir); + List fileLocations = getJsonLocations(buildType, productFlavors); String searchedLocation = System.lineSeparator(); for (File jsonFile : objectFactory.fileCollection().from(fileLocations)) { searchedLocation = searchedLocation + jsonFile.getPath() + System.lineSeparator(); @@ -477,44 +472,14 @@ private static void deleteFolder(final File folder) { throw new GradleException("Failed to delete: " + folder); } } - - - private static List splitVariantNames(String variant) { - if (variant == null) { - return new ArrayList<>(); - } - List flavors = new ArrayList<>(); - Matcher flavorMatcher = FLAVOR_PATTERN.matcher(variant); - while (flavorMatcher.find()) { - String match = flavorMatcher.group(1); - if (match != null && !match.equals("null")) { - flavors.add(match.toLowerCase()); - } - } - return flavors; - } private static long countSlashes(String input) { return input.codePoints().filter(x -> x == '/').count(); } - static List getJsonLocations(String variantDirname) { - Matcher variantMatcher = VARIANT_PATTERN.matcher(variantDirname); + static List getJsonLocations(String buildType, List flavorNames) { List fileLocations = new ArrayList<>(); - fileLocations.add(""); - if (!variantMatcher.matches()) { - return fileLocations - .stream() - .map(location -> location + JSON_FILE_NAME) - .collect(toList()); - } - List flavorNames = new ArrayList<>(); - if (variantMatcher.group(1) != null && !variantMatcher.group(1).equals("null")) { - flavorNames.add(variantMatcher.group(1).toLowerCase()); - } - flavorNames.addAll(splitVariantNames(variantMatcher.group(2))); - String buildType = variantMatcher.group(3); - String flavorName = variantMatcher.group(1) + variantMatcher.group(2); + String flavorName = flavorNames.stream().reduce("", (a,b) -> a + (a.length() == 0 ? b : capitalize(b))); fileLocations.add("src/" + flavorName + "/" + buildType); fileLocations.add("src/" + buildType + "/" + flavorName); fileLocations.add("src/" + flavorName); 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 92ec9a86..f6697e5f 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 @@ -19,6 +19,8 @@ import org.junit.Test; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import static com.google.common.truth.Truth.assertThat; @@ -27,14 +29,14 @@ public class GoogleServicesPluginTest { @Test public void testNoFlavor() { - List output = toStringList(GoogleServicesTask.getJsonLocations("release")); + List output = toStringList(GoogleServicesTask.getJsonLocations("release", Collections.emptyList())); assertThat(output).contains("src/release/google-services.json"); } @Test public void testOneFlavor() { List output = - toStringList(GoogleServicesTask.getJsonLocations("flavor/release")); + toStringList(GoogleServicesTask.getJsonLocations("release", Collections.singletonList("flavor"))); assertThat(output) .containsAllOf( "src/release/google-services.json", @@ -47,7 +49,7 @@ public void testOneFlavor() { @Test public void testMultipleFlavors() { List output = - toStringList(GoogleServicesTask.getJsonLocations("flavorTest/release")); + toStringList(GoogleServicesTask.getJsonLocations("release", Arrays.asList("flavor", "test"))); assertThat(output) .containsAllOf( "src/release/google-services.json",