Skip to content

Commit

Permalink
merge of #131 originally by ChristianBecker
Browse files Browse the repository at this point in the history
  • Loading branch information
David Motsonashvili committed Apr 29, 2021
1 parent c988dd1 commit 37a2a1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 56 deletions.
Expand Up @@ -55,29 +55,15 @@
/** */
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";

private static final String OAUTH_CLIENT_TYPE_WEB = "3";

private File intermediateDir;
private String variantDir;
private String buildType;
private List<String> productFlavors;
private ObjectFactory objectFactory;

@Inject
Expand All @@ -91,16 +77,25 @@ public File getIntermediateDir() {
}

@Input
public String getVariantDir() {
return variantDir;
public String getBuildType() {
return buildType;
}

@Input
public List<String> 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<String> productFlavors) {
this.productFlavors = productFlavors;
}

@Input
Expand All @@ -109,7 +104,7 @@ public void setVariantDir(String variantDir){
@TaskAction
public void action() throws IOException {
File quickstartFile = null;
List<String> fileLocations = getJsonLocations(variantDir);
List<String> fileLocations = getJsonLocations(buildType, productFlavors);
String searchedLocation = System.lineSeparator();
for (File jsonFile : objectFactory.fileCollection().from(fileLocations)) {
searchedLocation = searchedLocation + jsonFile.getPath() + System.lineSeparator();
Expand Down Expand Up @@ -477,44 +472,14 @@ private static void deleteFolder(final File folder) {
throw new GradleException("Failed to delete: " + folder);
}
}


private static List<String> splitVariantNames(String variant) {
if (variant == null) {
return new ArrayList<>();
}
List<String> 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<String> getJsonLocations(String variantDirname) {
Matcher variantMatcher = VARIANT_PATTERN.matcher(variantDirname);
static List<String> getJsonLocations(String buildType, List<String> flavorNames) {
List<String> fileLocations = new ArrayList<>();
fileLocations.add("");
if (!variantMatcher.matches()) {
return fileLocations
.stream()
.map(location -> location + JSON_FILE_NAME)
.collect(toList());
}
List<String> 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);
Expand Down
Expand Up @@ -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;
Expand All @@ -27,14 +29,14 @@ public class GoogleServicesPluginTest {

@Test
public void testNoFlavor() {
List<String> output = toStringList(GoogleServicesTask.getJsonLocations("release"));
List<String> output = toStringList(GoogleServicesTask.getJsonLocations("release", Collections.emptyList()));
assertThat(output).contains("src/release/google-services.json");
}

@Test
public void testOneFlavor() {
List<String> output =
toStringList(GoogleServicesTask.getJsonLocations("flavor/release"));
toStringList(GoogleServicesTask.getJsonLocations("release", Collections.singletonList("flavor")));
assertThat(output)
.containsAllOf(
"src/release/google-services.json",
Expand All @@ -47,7 +49,7 @@ public void testOneFlavor() {
@Test
public void testMultipleFlavors() {
List<String> output =
toStringList(GoogleServicesTask.getJsonLocations("flavorTest/release"));
toStringList(GoogleServicesTask.getJsonLocations("release", Arrays.asList("flavor", "test")));
assertThat(output)
.containsAllOf(
"src/release/google-services.json",
Expand Down

0 comments on commit 37a2a1b

Please sign in to comment.