diff --git a/build-artifacts/project-template-gradle/build.gradle b/build-artifacts/project-template-gradle/build.gradle index 9f3b17cae..1e90f93a4 100644 --- a/build-artifacts/project-template-gradle/build.gradle +++ b/build-artifacts/project-template-gradle/build.gradle @@ -222,6 +222,60 @@ task pluginStructureCheck { } } +def updateProductFlavorsContent(flavor, dimensionName, oldContent) { + def endIndex = oldContent.length() - 1; + def index = 0; + def newContent = ""; + def level = -1; + def dimensionFound = false; + + while(index <= endIndex) { + if(level == 0 && (oldContent[index] == '"' || oldContent[index] == "'")) { + def closingQuotes = oldContent.indexOf('"', index + 1); + if(closingQuotes == -1) { + closingQuotes = oldContent.indexOf("'", index + 1); + } + + index = closingQuotes + 1; + newContent += "\"${flavor}\""; + continue; + } + + if(oldContent[index] == "{") { + level++; + } + + if(oldContent[index] == "}") { + level--; + } + + if(level > 0) { + if(!dimensionFound && oldContent.indexOf("dimension", index) == index) { + newContent += "dimension \"${dimensionName}\""; + dimensionFound = true; + index += "dimension ".length(); + def openingQuoutes = oldContent.indexOf('"', index); + if(openingQuoutes == -1) { + openingQuoutes = oldContent.indexOf("'", index); + } + + def closingQuotes = oldContent.indexOf('"', openingQuoutes + 1); + if(closingQuotes == -1) { + closingQuotes = closingQuotes.indexOf("'", openingQuoutes + 1); + } + + index = closingQuotes + 1; + } + } + + newContent += oldContent[index]; + + index++; + } + + return newContent; +} + def createProductFlavorsContent(flavor, dimensionName, includeAndroidContent = true) { if (includeAndroidContent) @@ -250,7 +304,6 @@ android { } } - def createIncludeFile (filePath, flavor, dimensionName) { println "\t + creating include.gradle file for ${filePath}" @@ -264,39 +317,38 @@ def sanatizeDimensionName(str) { def replaceProductFlavorInContent(content, dimension, flavor) { - def indexStart = content.indexOf("productFlavors"); - def index = indexStart + "productFlavors".length(); - def indexEnd = -1; - def nestedOpenBraketsCount = 0; - - while (index < content.length()) - { - print content[index] - if (content[index] == "}") - { - if (nestedOpenBraketsCount == 0) - { - indexEnd = index; - break; - } - else - { - nestedOpenBraketsCount--; - } - } - else if (content[index] == "{") - { - nestedOpenBraketsCount++; - } - - index++; - } - - if (indexEnd != -1) + def indexStart = content.indexOf("productFlavors"); + def index = indexStart + "productFlavors".length(); + def indexEnd = -1; + def nestedOpenBraketsCount = 0; + + while (index < content.length()) + { + // print content[index]; + if (content[index] == "}") + { + nestedOpenBraketsCount--; + + if (nestedOpenBraketsCount == 0) + { + indexEnd = index; + break; + } + } + else if (content[index] == "{") + { + nestedOpenBraketsCount++; + } + + index++; + } + + if (indexEnd != -1) { - def oldProductFlavorsText = content.substring(indexStart, indexEnd - 1); - - def newProductFlavorsContent = createProductFlavorsContent(flavor, dimension, false); + // full content of productFlavors { ... } -> the substring is parenthesis to parenthesis -> { ... } + def oldProductFlavorsText = content.substring(indexStart, indexEnd + 1); + + def newProductFlavorsContent = updateProductFlavorsContent(flavor, dimension, oldProductFlavorsText); return content.replace(oldProductFlavorsText, newProductFlavorsContent); }