forked from mcxiaoke/packer-ng-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackerNgPlugin.groovy
164 lines (146 loc) · 5.44 KB
/
PackerNgPlugin.groovy
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.mcxiaoke.packer.ng
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.ProjectConfigurationException
// Android Multi Packer Plugin Source
class PackerNgPlugin implements Plugin<Project> {
static final String TAG = "PackerNg"
static final String PLUGIN_NAME = "packer"
static final String P_MARKET = "market"
Project project
PackerNgExtension modifierExtension
List<String> markets;
@Override
void apply(Project project) {
this.project = project
if (!project.plugins.hasPlugin("com.android.application")) {
throw new ProjectConfigurationException("the android plugin must be applied", null)
}
applyExtension()
parseMarkets()
applyPluginTasks()
}
void applyExtension() {
// setup plugin and extension
project.configurations.create(PLUGIN_NAME).extendsFrom(project.configurations.compile)
this.modifierExtension = project.extensions.create(PLUGIN_NAME, PackerNgExtension, project)
}
void applyPluginTasks() {
project.afterEvaluate {
checkCleanTask()
debug(":${project.name} flavors: ${project.android.productFlavors.collect { it.name }}")
//applySigningConfigs()
project.android.applicationVariants.all { BaseVariant variant ->
if (variant.buildType.name != "debug") {
checkPackerNgTask(variant)
}
}
}
}
/**
* parse markets file
* @param project Project
* @return found markets file
*/
boolean parseMarkets() {
markets = new ArrayList<String>();
if (!project.hasProperty(P_MARKET)) {
debug("parseMarkets() market property not found, ignore")
return false
}
// check markets file exists
def marketsFilePath = project.property(P_MARKET).toString()
if (!marketsFilePath) {
println(":${project.name} markets property not found, using default")
// if not set, use default ./markets.txt
marketsFilePath = "markets.txt"
}
File file = project.rootProject.file(marketsFilePath)
if (!file.exists() || !file.isFile() || !file.canRead()) {
throw new InvalidUserDataException(":${project.name} invalid market file: ${file.absolutePath}")
}
println(":${project.name} market: ${file.absolutePath}")
markets = readMarkets(file)
debug(":${project.name} found markets:$markets")
return true
}
List<String> readMarkets(File file) {
// add all markets
List<String> allMarkets = []
file.eachLine { line, number ->
String[] parts = line.split('#')
if (parts && parts[0]) {
def market = parts[0].trim()
if (market) {
allMarkets.add(market)
}
} else {
debug(":${project.name} skip invalid market line ${number}:'${line}'")
}
}
return allMarkets
}
/**
* add archiveApk tasks
* @param variant current Variant
*/
void checkPackerNgTask(BaseVariant variant) {
if (variant.buildType.signingConfig == null) {
println("WARNING:${project.name}:${variant.name}: signingConfig is null, " +
"task apk${variant.name.capitalize()} may fail.")
}
if (!variant.buildType.zipAlignEnabled) {
println("WARNING:${project.name}:${variant.name}: zipAlignEnabled is false, " +
"task apk${variant.name.capitalize()} may fail.")
}
debug("checkPackerNgTask() for ${variant.name}")
def File inputFile = variant.outputs[0].outputFile
def File tempDir = modifierExtension.tempOutput
def File outputDir = modifierExtension.archiveOutput
debug("checkPackerNgTask() input: ${inputFile}")
debug("checkPackerNgTask() temp: ${tempDir}")
debug("checkPackerNgTask() output: ${outputDir}")
def archiveTask = project.task("apk${variant.name.capitalize()}",
type: ArchiveAllApkTask) {
theVariant = variant
theExtension = modifierExtension
theMarkets = markets
dependsOn variant.assemble
}
debug("checkPackerNgTask() new variant task:${archiveTask.name}")
def buildTypeName = variant.buildType.name
if (variant.name != buildTypeName) {
def taskName = "apk${buildTypeName.capitalize()}"
def task = project.tasks.findByName(taskName)
if (task == null) {
debug("checkPackerNgTask() new build type task:${taskName}")
task = project.task(taskName, dependsOn: archiveTask)
}
}
}
/**
* add cleanArchives task if not added
* @return task
*/
void checkCleanTask() {
def output = modifierExtension.archiveOutput
debug("checkCleanTask() create clean archived apks task, path:${output}")
def task = project.task("cleanApks",
type: CleanArchivesTask) {
target = output
}
project.getTasksByName("clean", true)?.each {
it.dependsOn task
}
}
/**
* print debug messages
* @param msg msg
* @param vars vars
*/
void debug(String msg) {
project.logger.info(msg)
}
}