@@ -222,6 +222,60 @@ task pluginStructureCheck {
222
222
}
223
223
}
224
224
225
+ def updateProductFlavorsContent (flavor , dimensionName , oldContent ) {
226
+ def endIndex = oldContent. length() - 1 ;
227
+ def index = 0 ;
228
+ def newContent = " " ;
229
+ def level = -1 ;
230
+ def dimensionFound = false ;
231
+
232
+ while (index <= endIndex) {
233
+ if (level == 0 && (oldContent[index] == ' "' || oldContent[index] == " '" )) {
234
+ def closingQuotes = oldContent. indexOf(' "' , index + 1 );
235
+ if (closingQuotes == -1 ) {
236
+ closingQuotes = oldContent. indexOf(" '" , index + 1 );
237
+ }
238
+
239
+ index = closingQuotes + 1 ;
240
+ newContent + = " \" ${ flavor} \" " ;
241
+ continue ;
242
+ }
243
+
244
+ if (oldContent[index] == " {" ) {
245
+ level++ ;
246
+ }
247
+
248
+ if (oldContent[index] == " }" ) {
249
+ level-- ;
250
+ }
251
+
252
+ if (level > 0 ) {
253
+ if (! dimensionFound && oldContent. indexOf(" dimension" , index) == index) {
254
+ newContent + = " dimension \" ${ dimensionName} \" " ;
255
+ dimensionFound = true ;
256
+ index + = " dimension " . length();
257
+ def openingQuoutes = oldContent. indexOf(' "' , index);
258
+ if (openingQuoutes == -1 ) {
259
+ openingQuoutes = oldContent. indexOf(" '" , index);
260
+ }
261
+
262
+ def closingQuotes = oldContent. indexOf(' "' , openingQuoutes + 1 );
263
+ if (closingQuotes == -1 ) {
264
+ closingQuotes = closingQuotes. indexOf(" '" , openingQuoutes + 1 );
265
+ }
266
+
267
+ index = closingQuotes + 1 ;
268
+ }
269
+ }
270
+
271
+ newContent + = oldContent[index];
272
+
273
+ index++ ;
274
+ }
275
+
276
+ return newContent;
277
+ }
278
+
225
279
def createProductFlavorsContent (flavor , dimensionName , includeAndroidContent = true )
226
280
{
227
281
if (includeAndroidContent)
@@ -250,7 +304,6 @@ android {
250
304
}
251
305
}
252
306
253
-
254
307
def createIncludeFile (filePath , flavor , dimensionName ) {
255
308
println " \t + creating include.gradle file for ${ filePath} "
256
309
@@ -264,39 +317,38 @@ def sanatizeDimensionName(str) {
264
317
265
318
def replaceProductFlavorInContent (content , dimension , flavor )
266
319
{
267
- def indexStart = content. indexOf(" productFlavors" );
268
- def index = indexStart + " productFlavors" . length();
269
- def indexEnd = -1 ;
270
- def nestedOpenBraketsCount = 0 ;
271
-
272
- while (index < content. length())
273
- {
274
- print content[index]
275
- if (content[index] == " }" )
276
- {
277
- if (nestedOpenBraketsCount == 0 )
278
- {
279
- indexEnd = index;
280
- break ;
281
- }
282
- else
283
- {
284
- nestedOpenBraketsCount-- ;
285
- }
286
- }
287
- else if (content[index] == " {" )
288
- {
289
- nestedOpenBraketsCount++ ;
290
- }
291
-
292
- index++ ;
293
- }
294
-
295
- if (indexEnd != -1 )
320
+ def indexStart = content. indexOf(" productFlavors" );
321
+ def index = indexStart + " productFlavors" . length();
322
+ def indexEnd = -1 ;
323
+ def nestedOpenBraketsCount = 0 ;
324
+
325
+ while (index < content. length())
326
+ {
327
+ // print content[index];
328
+ if (content[index] == " }" )
329
+ {
330
+ nestedOpenBraketsCount-- ;
331
+
332
+ if (nestedOpenBraketsCount == 0 )
333
+ {
334
+ indexEnd = index;
335
+ break ;
336
+ }
337
+ }
338
+ else if (content[index] == " {" )
339
+ {
340
+ nestedOpenBraketsCount++ ;
341
+ }
342
+
343
+ index++ ;
344
+ }
345
+
346
+ if (indexEnd != -1 )
296
347
{
297
- def oldProductFlavorsText = content. substring(indexStart, indexEnd - 1 );
298
-
299
- def newProductFlavorsContent = createProductFlavorsContent(flavor, dimension, false );
348
+ // full content of productFlavors { ... } -> the substring is parenthesis to parenthesis -> { ... }
349
+ def oldProductFlavorsText = content. substring(indexStart, indexEnd + 1 );
350
+
351
+ def newProductFlavorsContent = updateProductFlavorsContent(flavor, dimension, oldProductFlavorsText);
300
352
301
353
return content. replace(oldProductFlavorsText, newProductFlavorsContent);
302
354
}
0 commit comments