Created
August 10, 2020 20:28
-
-
Save ethanquix/96ff4a440e89a183a14a6c04e01b5e90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How tu use: | |
# Create a from folder with the launch.json | |
# Create a output folder | |
import json | |
from xml.etree.ElementTree import Element, SubElement, Comment, tostring | |
from xml.etree import ElementTree | |
from xml.dom import minidom | |
PROJECT = 'platform' | |
# PROJECT = 'platform-stream' | |
def prettify(elem): | |
"""Return a pretty-printed XML string for the Element. | |
""" | |
rough_string = ElementTree.tostring(elem, 'utf-8') | |
reparsed = minidom.parseString(rough_string) | |
return reparsed.toprettyxml(indent=" ") | |
from jsoncomment import JsonComment | |
def parseConfNameToFileName(name): | |
toReplace = '()-+=,.:?/ ' | |
out = "" | |
for l in name: | |
if l in toReplace: | |
out += "_" | |
else: | |
out += l | |
return out | |
def getFolderName(name: str): | |
if "ads/prebid" in name: | |
return "Prebid" | |
if "console/server" in name: | |
return "Console" | |
def convertOneConf(conf, dirTo): | |
name = conf['name'] | |
fileName = parseConfNameToFileName(name) + ".xml" | |
file_handle = open(dirTo + "/" + fileName, "wb") | |
workingDir = conf['program'] | |
workingDir = workingDir.replace('${workspaceFolder}', '$PROJECT_DIR$') | |
workingDir = workingDir.replace('/main.go', '') | |
parameters = [] | |
if "args" in conf: | |
parameters = conf["args"] | |
outParameters = [] | |
for p in parameters: | |
outParameters.append(p.replace("${workspaceFolder}", "$PROJECT_DIR$")) | |
parameters = " ".join(outParameters) | |
try: | |
envs = conf['env'] | |
except: | |
envs = [] | |
# XML | |
root = Element('component') | |
root.set('name', 'ProjectRunConfigurationManager') | |
xml_conf = SubElement(root, "configuration") | |
xml_conf.set('default', 'false') | |
xml_conf.set('type', 'GoApplicationRunConfiguration') | |
xml_conf.set('factoryName', "Go Application") | |
# folderName = getFolderName(name) | |
# if folderName is not None: | |
# xml_conf.set("folderName", folderName) | |
betterName = name.replace("//ads/prebid", "prebid") | |
betterName = betterName.replace("//console/server", "console") | |
xml_conf.set('name', betterName) | |
# else: | |
# xml_conf.set('name', name) | |
#MAYBE REMOVE THIS LINE??? | |
xml_module = SubElement(xml_conf, "module") | |
xml_module.set('name', PROJECT) | |
xml_working_directory = SubElement(xml_conf, "working_directory") | |
xml_working_directory.set('value', workingDir) | |
xml_go_parameters = SubElement(xml_conf, "go_parameters") | |
xml_go_parameters.set('value', "-i") | |
xml_parameters = SubElement(xml_conf, "parameters") | |
xml_parameters.set('value', parameters) | |
xml_envs = SubElement(xml_conf, "envs") | |
for env in envs: | |
xml_env = SubElement(xml_envs, "env") | |
xml_env.set('name', env) | |
tmpValue = envs[env] | |
tmpValue = tmpValue.replace("${workspaceFolder}", "$PROJECT_DIR$") | |
xml_env.set('value', envs[env]) | |
xml_kind = SubElement(xml_conf, "kind") | |
xml_kind.set('value', "PACKAGE") | |
xml_filePath = SubElement(xml_conf, "filePath") | |
xml_filePath.set('value', "$PROJECT_DIR$/|" + workingDir + "/main.go") | |
xml_package = SubElement(xml_conf, "package") | |
xml_package.set('value', "github.com/publica-project/" + PROJECT + workingDir.replace("$PROJECT_DIR$", "")) | |
xml_directory = SubElement(xml_conf, "directory") | |
xml_directory.set('value', "$PROJECT_DIR$/") | |
xml_method = SubElement(xml_conf, "method") | |
xml_method.set('v', "2") | |
#print(prettify(root)) | |
ElementTree.ElementTree(root).write(file_handle) | |
file_handle.close() | |
def convert(fileFrom, dirTo): | |
with open(fileFrom) as data_file: | |
parser = JsonComment(json) | |
data = parser.load(data_file) | |
for conf in data['configurations']: | |
convertOneConf(conf, dirTo) | |
def main(): | |
convert("from/launch.json", "output") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment