forked from brunodles/anime-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amp.gradle
100 lines (82 loc) · 3.47 KB
/
amp.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class AndroidManifestPreprocessorExtension {
HashMap<String, String> placeHolders = new HashMap<>()
void placeholder(String key, String value) {
placeHolders.put('${' + key + '}', value)
}
}
class AndroidManifestPreprocessorTask extends DefaultTask {
String group = "Android Manifest Preprocessor"
String description = "Replace placeholders for configured strings."
List<TaskConfiguration> configurations
AndroidManifestPreprocessorTask() {
outputs.upToDateWhen { false }
}
@TaskAction
def execute(IncrementalTaskInputs inputs) {
AndroidManifestPreprocessorExtension extension = project.extensions.getByType(AndroidManifestPreprocessorExtension.class)
configurations.each { config ->
def input = config.inputFile
def workingText = input.text
extension.placeHolders.each { placeholder ->
workingText = workingText.replace(placeholder.key, placeholder.value)
}
config.outputFile.write(workingText)
}
}
static class TaskConfiguration {
File inputFile
File outputFile
TaskConfiguration(File inputFile, File outputFile) {
this.inputFile = inputFile
this.outputFile = outputFile
}
}
}
class AndroidManifestPreprocessorPlugin implements Plugin<Project> {
@SuppressWarnings("GroovyUnusedDeclaration")
public static final String VERSION = "0.1"
private AndroidManifestPreprocessorExtension extension
@Override
void apply(Project project) {
project.afterEvaluate {
if (!containsAndroid(project))
throw GradleException("Project does not contains Android Plugin")
project.android.applicationVariants.all { variant ->
def manifestFiles = findManifestFiles(variant)
def task = project.tasks.create("preProcessAndroidManifest${variant.name.capitalize()}", AndroidManifestPreprocessorTask.class) {
configurations = manifestFiles
}
Task pluginTask = findPluginTask(project, variant)
pluginTask?.dependsOn?.add(task)
}
}
extension = project.extensions.create("amp", AndroidManifestPreprocessorExtension)
}
private static boolean containsAndroid(Project project) {
def plugins = project.plugins
if (plugins == null || plugins.isEmpty())
return false
return plugins.findPlugin("com.android.application") != null ||
plugins.findPlugin("com.android.library") != null
}
private static List<AndroidManifestPreprocessorTask.TaskConfiguration> findManifestFiles(variant) {
variant.sourceSets.collect { sourceSet ->
new AndroidManifestPreprocessorTask.TaskConfiguration(
new File(sourceSet.manifestFile.parentFile, "manifest/AndroidManifest.xml"),
sourceSet.manifestFile
)
}.findAll {
it.inputFile.exists()
}.toList()
}
private static Task findPluginTask(project, variant) {
def pluginTaskName = "check${variant.name.capitalize()}Manifest"
def pluginTask = project.tasks.findByName(pluginTaskName)
if (pluginTask == null) {
project.logger.log(LogLevel.ERROR, "The task \"${pluginTaskName}\" does not exists!")
return null
}
return pluginTask
}
}
plugins.apply(AndroidManifestPreprocessorPlugin.class)