Skip to content

Commit d9badf3

Browse files
authored
Merge pull request #487 from NativeScript/pete/update-include-gradle
Update include.gradle rewriting
2 parents 86c0542 + 7245384 commit d9badf3

File tree

1 file changed

+85
-33
lines changed

1 file changed

+85
-33
lines changed

build-artifacts/project-template-gradle/build.gradle

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,60 @@ task pluginStructureCheck {
222222
}
223223
}
224224

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+
225279
def createProductFlavorsContent(flavor, dimensionName, includeAndroidContent = true)
226280
{
227281
if (includeAndroidContent)
@@ -250,7 +304,6 @@ android {
250304
}
251305
}
252306

253-
254307
def createIncludeFile (filePath, flavor, dimensionName) {
255308
println "\t + creating include.gradle file for ${filePath}"
256309

@@ -264,39 +317,38 @@ def sanatizeDimensionName(str) {
264317

265318
def replaceProductFlavorInContent(content, dimension, flavor)
266319
{
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)
296347
{
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);
300352

301353
return content.replace(oldProductFlavorsText, newProductFlavorsContent);
302354
}

0 commit comments

Comments
 (0)