@@ -223,24 +223,63 @@ def sanatizeDimensionName(str) {
223223 return str. replaceAll(/ \W / , " " )
224224}
225225
226+ def replaceProductFlavorInContent (content , dimension , flavor )
227+ {
228+ def indexStart = content. indexOf(" productFlavors" );
229+ def index = indexStart + " productFlavors" . length();
230+ def indexEnd = -1 ;
231+ def nestedOpenBraketsCount = 0 ;
232+
233+ while (index < content. length())
234+ {
235+ print content[index]
236+ if (content[index] == " }" )
237+ {
238+ if (nestedOpenBraketsCount == 0 )
239+ {
240+ indexEnd = index;
241+ break ;
242+ }
243+ else
244+ {
245+ nestedOpenBraketsCount-- ;
246+ }
247+ }
248+ else if (content[index] == " {" )
249+ {
250+ nestedOpenBraketsCount++ ;
251+ }
252+
253+ index++ ;
254+ }
255+
256+ if (indexEnd != -1 )
257+ {
258+ def oldProductFlavorsText = content. substring(indexStart, indexEnd - 1 );
259+
260+ def newProductFlavorsText = """
261+ productFlavors {
262+ "${ flavor} " {
263+ dimension "${ dimension} "
264+ }
265+ }
266+ """ ;
267+
268+ return content. replace(oldProductFlavorsText, newProductFlavorsText);
269+ }
270+ else
271+ {
272+ return ' '
273+ }
274+ }
275+
226276// make sure the include.gradle file, produced by the user, has only allowed characters in dimension attribute and remove any invalid characters if necessary
227- def updateIncludeGradleFile (subFile , dimensionName , flavor ) {
228- def igFile = new File (subFile. getAbsolutePath())
229- def newContent = igFile. text. replaceAll(/ dimension\s +["'](.+?)["']/ ) { fullMatch , fDimension ->
230- def newFg = sanatizeDimensionName(fDimension)
231- dimensionName = newFg
232- return " dimension \" $newFg \" "
233- }
234-
235- // println "Before patch: " + newContent
236-
237- def finalContent = newContent. replace(dimensionName + " \" {" , flavor + " \" {" )
238-
239- // println "Dimension: " + dimensionName + " final content: " + finalContent
240-
241- igFile. text = finalContent
242-
243- return dimensionName
277+ def updateIncludeGradleFile (targetFile , dimensionName , flavor )
278+ {
279+ def fileEntry = new File (targetFile. getAbsolutePath());
280+ def content = fileEntry. text;
281+ def replacedContent = replaceProductFlavorInContent(content, dimensionName, flavor);
282+ fileEntry. text = replacedContent;
244283}
245284
246285def flavorNumber = 0
@@ -265,7 +304,7 @@ task createDefaultIncludeFiles {
265304
266305 if (subFile. name == " include.gradle" ) {
267306 foundIncludeFile = true
268- dimensionName = updateIncludeGradleFile(subFile, dimensionName, flavor)
307+ updateIncludeGradleFile(subFile, dimensionName, flavor)
269308 }
270309 }
271310
0 commit comments