From 0051cfd04e01b7d03d5d5c8598f33dcc89cb8d98 Mon Sep 17 00:00:00 2001 From: Adrien Leravat Date: Fri, 7 Jul 2017 18:35:17 +0200 Subject: [PATCH] Reorganized source generation --- gen-src/combine-qml.py | 142 --------------------- gen-src/gen-src.pro | 13 -- gen-src/tests/main-base.qml | 16 --- gen-src/tests/main.qml | 16 --- responsive-helper.pro | 4 +- {gen-src => src}/Button.qml | 0 {gen-src => src}/ResponsiveHelper-base.qml | 0 {gen-src => src}/TextField.qml | 0 src/src.pro | 13 ++ 9 files changed, 15 insertions(+), 189 deletions(-) delete mode 100644 gen-src/combine-qml.py delete mode 100644 gen-src/gen-src.pro delete mode 100644 gen-src/tests/main-base.qml delete mode 100644 gen-src/tests/main.qml rename {gen-src => src}/Button.qml (100%) rename {gen-src => src}/ResponsiveHelper-base.qml (100%) rename {gen-src => src}/TextField.qml (100%) create mode 100644 src/src.pro diff --git a/gen-src/combine-qml.py b/gen-src/combine-qml.py deleted file mode 100644 index a9fc9cc..0000000 --- a/gen-src/combine-qml.py +++ /dev/null @@ -1,142 +0,0 @@ -import re -import os -from shutil import copyfile - -def trimComponent(componentLines): - linesToDelete = [] - for i in range(len(componentLines)): - if componentLines[i].startswith('import'): - linesToDelete.append(i) - - for lineIndex in reversed(linesToDelete): - del componentLines[lineIndex] - -# Removes components original properties when redefined in final file -def filterRedefinedProperties(componentLines, contentLines): - linesToAdd = componentLines[:] - propertyRegexp = re.compile(r'^(\s*)(property\s+\w+\s+)?(\w+)\s*:(.*$)') - - for contentIndex, contentLine in enumerate(contentLines): - matchInRedefinition = propertyRegexp.search(contentLine) - - if matchInRedefinition: - deleteLineIndex = -1 - propertyIndentation = matchInRedefinition.group(1) - propertyName = matchInRedefinition.group(3) - propertyValue = matchInRedefinition.group(4) - openedBraces = 0 - for lineIndex in range(len(linesToAdd)): - - if linesToAdd[lineIndex].find('{') >= 0: - openedBraces += 1 - if linesToAdd[lineIndex].find('}') >= 0: - openedBraces -= 1 - - if openedBraces == 1: - matchInOriginal = re.match(r'^\s+((property\s+\w+\s+)?' + propertyName + '\s*:)', linesToAdd[lineIndex]) - if matchInOriginal: - propertyDeclaration = matchInOriginal.group(1) - - # Combine original declaration, and value redefinition - newLine = propertyDeclaration + propertyValue + '\n' - newLine = propertyIndentation + newLine - - contentLines[contentIndex] = newLine - deleteLineIndex = lineIndex - print 'merging line ' + linesToAdd[lineIndex] - break - - if deleteLineIndex >= 0: - del linesToAdd[deleteLineIndex] - - return linesToAdd - -# Returns the component merged with its local content -def mergedComponent(componentLines, contentLines, indentation): - comp = '' - filteredComponentLines = filterRedefinedProperties(componentLines=componentLines, contentLines=contentLines) - - # Insert component code - for line in filteredComponentLines: - if line.find('}') == 0: - break - else: - comp += indentation + line - - if len(contentLines) > 0: - comp += indentation + ' //---- Redefinitions ----\n' - - # Insert code form its content - for contentLine in contentLines: - comp += contentLine - - return comp - -# Replaces component tokens by content of a file -def mergeComponentInDocument(originalFilePath, componentFilePath, componentName): - print 'Merging @' + componentName + ' (' + componentFilePath + ') into ' + originalFilePath - - # Read component - try: - componentFile = open(componentFilePath) - componentLines = componentFile.readlines() - except: - print 'Failed to open file ' + componentFilePath - return - - trimComponent(componentLines) - - # Open destination file - try: - originalFile = open(originalFilePath, 'r+') - except: - print 'Failed to open file ' + originalFilePath - return - - # Insert component in file - finalFileContent = '' - componentRegexp = re.compile(r'^(\s*)@'+componentName+' ?{') - insertingComponent = False - indentation = '' - - for line in originalFile: - if insertingComponent == False: - res = componentRegexp.search(line) - if res: - indentation = res.group(1) - contentLines = [] - insertingComponent = True - openedBraces = 1 - else: - finalFileContent += line - else: - if line.find('{') >= 0: - openedBraces += 1 - if line.find('}') >= 0: - openedBraces -= 1 - - if openedBraces > 0: - contentLines.append(line) - else: - insertingComponent = False - comp = mergedComponent(componentLines=componentLines, contentLines=contentLines, indentation=indentation) - finalFileContent += indentation + '//---------------\n' - finalFileContent += indentation + '// @' + componentName + '\n' - finalFileContent += comp - finalFileContent += line - - originalFile.seek(0) - originalFile.write(finalFileContent) - originalFile.truncate() - -copyfile('tests/main-base.qml', 'tests/main.temp.qml') -mergeComponentInDocument('tests/main.temp.qml', 'Button.qml', 'Button') - -print open('tests/main.qml').read() -os.remove('tests/main.temp.qml') - -copyfile('ResponsiveHelper-base.qml', 'ResponsiveHelper.temp.qml') -mergeComponentInDocument('ResponsiveHelper.temp.qml', 'Button.qml', 'Button') -mergeComponentInDocument('ResponsiveHelper.temp.qml', 'TextField.qml', 'TextField') -copyfile('ResponsiveHelper.temp.qml', '../ResponsiveHelper.qml') -os.remove('ResponsiveHelper.temp.qml') diff --git a/gen-src/gen-src.pro b/gen-src/gen-src.pro deleted file mode 100644 index 6bfa607..0000000 --- a/gen-src/gen-src.pro +++ /dev/null @@ -1,13 +0,0 @@ -TEMPLATE = aux -OBJECTS_DIR = ./ -DESTDIR = ./ - -system(python combine-qml.py) - -OTHER_FILES = \ - combine-qml.py \ - Button.qml \ - TextField.qml - -DISTFILES += \ - ResponsiveHelper-base.qml diff --git a/gen-src/tests/main-base.qml b/gen-src/tests/main-base.qml deleted file mode 100644 index 7a6275a..0000000 --- a/gen-src/tests/main-base.qml +++ /dev/null @@ -1,16 +0,0 @@ -Item { - @Button { - text: "toto" - } - - Toto { - color: "blue" - - @Button { - text: "toto" - Text { - - } - } - } -} diff --git a/gen-src/tests/main.qml b/gen-src/tests/main.qml deleted file mode 100644 index 7a6275a..0000000 --- a/gen-src/tests/main.qml +++ /dev/null @@ -1,16 +0,0 @@ -Item { - @Button { - text: "toto" - } - - Toto { - color: "blue" - - @Button { - text: "toto" - Text { - - } - } - } -} diff --git a/responsive-helper.pro b/responsive-helper.pro index 315acd6..dbe4d28 100644 --- a/responsive-helper.pro +++ b/responsive-helper.pro @@ -2,7 +2,7 @@ TEMPLATE = subdirs SUBDIRS = \ examples \ - gen-src + src OTHER_FILES = \ ResponsiveHelper.qml @@ -10,4 +10,4 @@ OTHER_FILES = \ OTHER_FILES += \ gen-src/ResponsiveHelper.original.qml -examples.depends = gen-src +#examples.depends = gen-src diff --git a/gen-src/Button.qml b/src/Button.qml similarity index 100% rename from gen-src/Button.qml rename to src/Button.qml diff --git a/gen-src/ResponsiveHelper-base.qml b/src/ResponsiveHelper-base.qml similarity index 100% rename from gen-src/ResponsiveHelper-base.qml rename to src/ResponsiveHelper-base.qml diff --git a/gen-src/TextField.qml b/src/TextField.qml similarity index 100% rename from gen-src/TextField.qml rename to src/TextField.qml diff --git a/src/src.pro b/src/src.pro new file mode 100644 index 0000000..83c8ce3 --- /dev/null +++ b/src/src.pro @@ -0,0 +1,13 @@ +TEMPLATE = aux +OBJECTS_DIR = ./ +DESTDIR = ./ + +system(python combine-qml/combine-qml.py ResponsiveHelper-base.qml ../ResponsiveHelper.qml -c Button.qml Button -c TextField.qml TextField) + +OTHER_FILES = \ + generate.py \ + Button.qml \ + TextField.qml + +DISTFILES += \ + ResponsiveHelper-base.qml