Skip to content

Instantly share code, notes, and snippets.

@shahrukhamd
Created July 23, 2019 03:44
Show Gist options
  • Save shahrukhamd/1a4b0f649f24b4c57e77fb3ee83b35ea to your computer and use it in GitHub Desktop.
Save shahrukhamd/1a4b0f649f24b4c57e77fb3ee83b35ea to your computer and use it in GitHub Desktop.
package com.project.sdk
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.util.regex.Matcher
import java.util.regex.Pattern
class ServicesPlugin implements Plugin<Project> {
public final static String JSON_FILE_NAME = "example.json"
public final static Pattern VARIANT_PATTERN = ~/(?:([^\p{javaUpperCase}]+)((?:\p{javaUpperCase}[^\p{javaUpperCase}]*)*)\/)?([^\/]*)/
public final static Pattern FLAVOR_PATTERN = ~/(\p{javaUpperCase}[^\p{javaUpperCase}]*)/
@Override
void apply(Project project) {
project.android.applicationVariants.all { variant ->
handleVariantOpenFile(project, variant)
}
}
static handleVariantOpenFile(Project project, def variant) {
File quickstartFile = null
List<String> fileLocations = getJsonLocations("$variant.dirName", project)
String searchedLocation = System.lineSeparator()
for (String location : fileLocations) {
File jsonFile = project.file(location + '/' + JSON_FILE_NAME)
searchedLocation = searchedLocation + jsonFile.getPath() + System.lineSeparator()
if (jsonFile.isFile()) {
quickstartFile = jsonFile
break
}
}
if (quickstartFile == null) {
quickstartFile = project.file(JSON_FILE_NAME)
}
File outputDir = project.file("$project.buildDir/generated/res/example-plugin/$variant.dirName")
ServicesTask task = project.tasks.create("${variant.name.capitalize()}YourPluginTaskName", ServicesTask)
task.jsonFile = quickstartFile
task.newFile = outputDir
}
static List<String> getJsonLocations(String variantDirname, Project project) {
Matcher variantMatcher = VARIANT_PATTERN.matcher(variantDirname)
List<String> fileLocations = new ArrayList<>()
if (!variantMatcher.matches()) {
project.getLogger().warn("$variantDirname failed to parse into flavors. Please start " +
"all flavors with a lowercase character")
fileLocations.add("src/$variantDirname")
return fileLocations
}
List<String> flavorNames = new ArrayList<>()
if (variantMatcher.group(1) != 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)}"
fileLocations.add("src/$flavorName/$buildType")
fileLocations.add("src/$buildType/$flavorName")
fileLocations.add("src/$flavorName")
fileLocations.add("src/$buildType")
fileLocations.add("src/$flavorName${buildType.capitalize()}")
fileLocations.add("src/$buildType")
String fileLocation = "src"
for(String flavor : flavorNames) {
fileLocation += "/$flavor"
fileLocations.add(fileLocation)
fileLocations.add("$fileLocation/$buildType")
fileLocations.add("$fileLocation${buildType.capitalize()}")
}
fileLocations.unique().sort{a,b -> countSlashes(b) <=> countSlashes(a)}
return fileLocations
}
private static List<String> splitVariantNames(String variant) {
if (variant == null) {
return Collections.emptyList()
}
List<String> flavors = new ArrayList<>()
Matcher flavorMatcher = FLAVOR_PATTERN.matcher(variant)
while (flavorMatcher.find()) {
String match = flavorMatcher.group(1)
if (match != null) {
flavors.add(match.toLowerCase())
}
}
return flavors
}
private static long countSlashes(String input) {
return input.codePoints().filter{x -> x == '/'}.count()
}
}
package com.project.sdk;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import java.io.*;
import java.util.Map;
import java.util.TreeMap;
public class ServicesTask extends DefaultTask {
private File jsonFile;
private File newFile;
@InputFile
@Optional
public File getJsonFile() {
return jsonFile;
}
public void setJsonFile(File jsonFile) {
this.jsonFile = jsonFile;
}
@OutputDirectory
public File getNewFile() {
return newFile;
}
public void setNewFile(File newFile) {
this.newFile = newFile;
}
@TaskAction
public void action() throws IOException {
JsonElement root = new JsonParser().parse(new BufferedReader(new InputStreamReader(
new FileInputStream(jsonFile), "UTF-8")));
JsonObject rootObject = root.getAsJsonObject();
Map<String, String> resValues = new TreeMap<>();
// set value map from config json
for (String key: rootObject.keySet()) {
resValues.put(key, rootObject.get(key).getAsString());
}
// write the values file.
File values = new File(newFile, "values");
if (!values.exists() && !values.mkdirs()) {
throw new GradleException("Failed to create folder: " + values);
}
PrintWriter fileWriter = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(new File(values, "values.xml")), "UTF-8"));
fileWriter.print(mapToString(resValues));
fileWriter.flush();
fileWriter.close();
}
/**
* This will get the map and convert it into Android String resource format string.
*/
private static String mapToString(Map<String, String> values) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n");
for (Map.Entry<String, String> entry : values.entrySet()) {
String name = entry.getKey();
sb.append(" <string name=\"").append(name).append("\" translatable=\"false\">")
.append(entry.getValue()).append("</string>\n");
}
sb.append("</resources>\n");
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment