1
+
1
2
import components.*
2
- import org.gradle.api.NamedDomainObjectContainer
3
3
import org.gradle.api.Plugin
4
4
import org.gradle.api.Project
5
- import org.gradle.api.internal.plugins.DslObject
6
5
7
6
open class StringCare : Plugin <Project > {
8
7
@@ -23,16 +22,11 @@ open class StringCare : Plugin<Project> {
23
22
}
24
23
25
24
@JvmStatic
26
- internal val moduleMap: MutableMap <String , Configuration > = mutableMapOf ()
27
-
28
- @JvmStatic
29
- internal val variantMap: MutableMap <String , VariantApplicationId > = mutableMapOf ()
25
+ internal var configuration: Configuration = defaultConfig()
30
26
31
27
@JvmStatic
32
- internal var mainModule : String = defaultMainModule
28
+ internal var variantMap = mutableMapOf< String , String >()
33
29
34
- @JvmStatic
35
- internal var debug: Boolean = defaultDebug
36
30
}
37
31
38
32
private lateinit var project: Project
@@ -45,194 +39,125 @@ open class StringCare : Plugin<Project> {
45
39
absoluteProjectPath = project.absolutePath()
46
40
47
41
this .project.afterEvaluate {
48
- extension.modules.forEach { module ->
49
- moduleMap[module.name] = Configuration (module.name).apply {
50
- debug = extension.debug
51
- }
52
- if (module.srcFolders.isNotEmpty()) {
53
- moduleMap[module.name]!! .srcFolders.addAll(module.srcFolders)
54
- }
55
- if (module.stringFiles.isNotEmpty()) {
56
- moduleMap[module.name]!! .stringFiles.addAll(module.stringFiles)
57
- }
58
- if (module.assetsFiles.isNotEmpty()) {
59
- moduleMap[module.name]!! .assetsFiles.addAll(module.assetsFiles)
60
- }
61
-
62
- if (moduleMap[module.name]!! .srcFolders.isEmpty()) {
63
- moduleMap[module.name]!! .srcFolders.addAll(defaultConfig().srcFolders)
64
- }
65
- if (moduleMap[module.name]!! .stringFiles.isEmpty()) {
66
- moduleMap[module.name]!! .stringFiles.addAll(defaultConfig().stringFiles)
67
- }
42
+ this .project.applicationVariants()?.forEach { variant ->
43
+ variantMap[variant.name] = variant.applicationId
68
44
}
69
- extension.variants.forEach { variant ->
70
- variantMap[variant.name] = VariantApplicationId (variant.name).apply {
71
- applicationId = variant.applicationId
72
- mockedFingerprint = variant.mockedFingerprint
73
- skip = variant.skip
74
- }
45
+ configuration = this .project.config(extension)
46
+
47
+ if (configuration.debug) {
48
+ PrintUtils .print (" PATH" , absoluteProjectPath)
75
49
}
76
- this .project.registerTask()
50
+ this .project.registerTask(configuration )
77
51
}
78
- this .project.gradle.addBuildListener(ExecutionListener (
79
- debug = extension.debug,
80
- dataFound = { _, _ ->
81
- // nothing to do here
82
- },
83
- mergeResourcesStart = { module, variant ->
84
- fingerPrint(variantMap, module, variant, extension.debug) { key ->
85
- when {
86
- moduleMap.containsKey(module) -> {
87
- val variantOrFlavor = extension.variants.find {
88
- variant.toLowerCase().contains(it.name.toLowerCase())
89
- }
90
- if (variantOrFlavor != null && variantOrFlavor.skip) {
91
- PrintUtils .print (module, " Skipping $variant " )
92
- return @fingerPrint
93
- }
52
+ this .project.gradle.addBuildListener(
53
+ ExecutionListener (
54
+ debug = configuration.debug,
55
+ dataFound = { _, _ ->
56
+ // nothing to do here
57
+ },
58
+ mergeResourcesStart = { module, variant ->
59
+ configuration.name = module
60
+ if (variantMap.containsKey(variant)) {
61
+ configuration.applicationId = variantMap[variant] ? : " "
62
+ }
63
+ PrintUtils .print (" " , " ApplicationId: ${configuration.applicationId} " , tab = true )
64
+ fingerPrint(module, variant, configuration) { key ->
65
+ if (configuration.skip) {
66
+ PrintUtils .print (module, " Skipping $variant " )
67
+ return @fingerPrint
68
+ }
94
69
95
- if (" none" == key || key.trim().isEmpty()) {
96
- PrintUtils .print (" No SHA1 key found for :$module :$variant " )
97
- return @fingerPrint
98
- }
70
+ if (" none" == key || key.trim().isEmpty()) {
71
+ PrintUtils .print (" No SHA1 key found for :$module :$variant " )
72
+ return @fingerPrint
73
+ }
99
74
100
- PrintUtils .print (module, " $variant :$key " )
101
- PrintUtils .print (module, backupStringRes)
102
- moduleMap[module]?.let { configuration ->
103
- backupResourceFiles(absoluteProjectPath, configuration)
104
- }
75
+ PrintUtils .print (module, " $variant :$key " )
76
+ PrintUtils .print (module, backupStringRes)
77
+ backupResourceFiles(absoluteProjectPath, configuration)
105
78
106
- moduleMap[module]?.let { configuration ->
107
- val files = locateResourceFiles(absoluteProjectPath, configuration)
108
- files.forEach { file ->
109
- modifyXML(
110
- file.file, extension.main_module, key, extension.debug,
111
- variantOrFlavor?.applicationId ? : " "
112
- )
113
- }
114
- }
115
- PrintUtils .print (module, obfuscateStringRes)
116
- }
117
- else -> {
118
- val defaultConfiguration = defaultConfig().apply {
119
- name = module
120
- }
121
- if (" none" == key || key.trim().isEmpty()) {
122
- PrintUtils .print (" No SHA1 key found for :$module :$variant " )
123
- return @fingerPrint
124
- }
125
- PrintUtils .print (module, " $variant :$key " )
126
- PrintUtils .print (module, backupStringRes)
127
- backupResourceFiles(absoluteProjectPath, defaultConfiguration)
128
- PrintUtils .print (module, obfuscateStringRes)
129
- val files = locateResourceFiles(absoluteProjectPath, defaultConfiguration)
130
- files.forEach { file ->
131
- modifyXML(file.file, extension.main_module, key, extension.debug)
132
- }
79
+
80
+ val files = locateResourceFiles(absoluteProjectPath, configuration)
81
+ files.forEach { file ->
82
+ modifyXML(file.file, key, configuration)
133
83
}
134
84
}
135
- }
85
+ PrintUtils .print (module, obfuscateStringRes)
86
+ },
87
+ mergeResourcesFinish = { module, _ ->
88
+ if (configuration.skip) {
89
+ return @ExecutionListener
90
+ }
91
+ PrintUtils .print (module, restoreStringRes)
92
+ restoreResourceFiles(absoluteProjectPath, module)
93
+ },
94
+ mergeAssetsStart = { module, variant ->
95
+ configuration.name = module
96
+ if (variantMap.containsKey(variant)) {
97
+ configuration.applicationId = variantMap[variant] ? : " "
98
+ }
99
+ PrintUtils .print (" " , " ApplicationId: ${configuration.applicationId} " , tab = true )
100
+ fingerPrint(module, variant, configuration) { key ->
101
+ if (configuration.skip) {
102
+ PrintUtils .print (module, " Skipping $variant " )
103
+ return @fingerPrint
104
+ }
136
105
137
- },
138
- mergeResourcesFinish = { module, variant ->
139
- PrintUtils .print (module, restoreStringRes)
140
- val variantOrFlavor = extension.variants.find {
141
- variant.toLowerCase().contains(it.name.toLowerCase())
142
- }
143
- if (variantOrFlavor != null && variantOrFlavor.skip) {
144
- return @ExecutionListener
145
- }
146
- restoreResourceFiles(absoluteProjectPath, module)
147
- },
148
- mergeAssetsStart = { module, variant ->
149
- fingerPrint(variantMap, module, variant, extension.debug) { key ->
150
- when {
151
- moduleMap.containsKey(module) -> {
152
- val variantOrFlavor = extension.variants.find {
153
- variant.toLowerCase().contains(it.name.toLowerCase())
154
- }
155
- if (variantOrFlavor != null && variantOrFlavor.skip) {
156
- PrintUtils .print (module, " Skipping $variant " )
157
- return @fingerPrint
158
- }
106
+ if (" none" == key || key.trim().isEmpty()) {
107
+ PrintUtils .print (" No SHA1 key found for :$module :$variant " )
108
+ return @fingerPrint
109
+ }
159
110
160
- if (" none" == key || key.trim().isEmpty()) {
161
- PrintUtils .print (" No SHA1 key found for :$module :$variant " )
162
- return @fingerPrint
163
- }
111
+ PrintUtils .print (module, " $variant :$key " )
112
+ PrintUtils .print (module, backupAssets)
113
+ backupAssetsFiles(absoluteProjectPath, configuration)
164
114
165
- PrintUtils .print (module, " $variant :$key " )
166
- PrintUtils .print (module, backupAssets)
167
- moduleMap[module]?.let { configuration ->
168
- backupAssetsFiles(absoluteProjectPath, configuration)
169
- }
170
115
171
- moduleMap[module]?.let { configuration ->
172
- val files = locateAssetsFiles(absoluteProjectPath, configuration)
173
- files.forEach { file ->
174
- if (extension.debug) {
175
- PrintUtils .print (null , file.file.getContent())
176
- }
177
- obfuscateFile(
178
- extension.main_module,
179
- key,
180
- file.file,
181
- variantOrFlavor?.applicationId ? : " "
182
- )
183
- if (extension.debug) {
184
- PrintUtils .print (null , file.file.getContent())
185
- }
186
- }
116
+ val files = locateAssetsFiles(absoluteProjectPath, configuration)
117
+ files.forEach { file ->
118
+ if (configuration.debug) {
119
+ PrintUtils .print (null , file.file.getContent())
120
+ }
121
+ obfuscateFile(
122
+ key,
123
+ file.file,
124
+ configuration.applicationId
125
+ )
126
+ if (configuration.debug) {
127
+ PrintUtils .print (null , file.file.getContent())
187
128
}
188
- PrintUtils .print (module, obfuscateAssets)
189
129
}
130
+ PrintUtils .print (module, obfuscateAssets)
131
+ }
132
+ },
133
+ mergeAssetsFinish = { module, _ ->
134
+ if (configuration.skip) {
135
+ return @ExecutionListener
190
136
}
137
+ PrintUtils .print (module, restoreAssets)
138
+ restoreAssetsFiles(absoluteProjectPath, module)
191
139
}
192
140
193
- },
194
- mergeAssetsFinish = { module, variant ->
195
- PrintUtils .print (module, restoreAssets)
196
- val variantOrFlavor = extension.variants.find {
197
- variant.toLowerCase().contains(it.name.toLowerCase())
198
- }
199
- if (variantOrFlavor != null && variantOrFlavor.skip) {
200
- return @ExecutionListener
201
- }
202
- restoreAssetsFiles(absoluteProjectPath, module)
203
- }
204
-
205
- ))
141
+ ))
206
142
}
207
143
208
144
open class Extension {
209
- var debug: Boolean = false
210
- var main_module: String = " app"
211
- var modules: NamedDomainObjectContainer <Configuration >
212
- @Suppress(" UNCHECKED_CAST" )
213
- get() = DslObject (this ).extensions.getByName(" modules" ) as NamedDomainObjectContainer <Configuration >
214
- internal set(value) {
215
- DslObject (this ).extensions.add(" modules" , value)
216
- }
217
- var variants: NamedDomainObjectContainer <VariantApplicationId >
218
- @Suppress(" UNCHECKED_CAST" )
219
- get() = DslObject (this ).extensions.getByName(" variants" ) as NamedDomainObjectContainer <VariantApplicationId >
220
- internal set(value) {
221
- DslObject (this ).extensions.add(" variants" , value)
222
- }
145
+ var assetsFiles = mutableListOf<String >()
146
+ var stringFiles = mutableListOf<String >()
147
+ var srcFolders = mutableListOf<String >()
148
+ var debug = false
149
+ var skip = false
150
+ var mockedFingerprint = " "
223
151
}
224
152
225
153
open class Configuration (var name : String ) {
226
154
var assetsFiles = mutableListOf<String >()
227
155
var stringFiles = mutableListOf<String >()
228
156
var srcFolders = mutableListOf<String >()
229
157
var debug = false
230
- }
231
-
232
- open class VariantApplicationId (var name : String ) {
158
+ var skip = false
233
159
var applicationId = " "
234
160
var mockedFingerprint = " "
235
- var skip = false
236
161
}
237
162
238
163
}
0 commit comments