d
This commit is contained in:
15
Utilities/platform/3/Mode.py
Normal file
15
Utilities/platform/3/Mode.py
Normal file
@@ -0,0 +1,15 @@
|
||||
class Mode:
|
||||
def __init__(self):
|
||||
self.reset()
|
||||
|
||||
def parseLine(self, line):
|
||||
if line.startswith("src:"):
|
||||
self.reset()
|
||||
self.isSource = True
|
||||
elif line.startswith("replace:"):
|
||||
self.reset()
|
||||
self.isReplacement = True
|
||||
|
||||
def reset(self):
|
||||
self.isSource = False
|
||||
self.isReplacement = False
|
||||
12
Utilities/platform/3/Replace.py
Normal file
12
Utilities/platform/3/Replace.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from readKeyValue import *
|
||||
|
||||
class Replace:
|
||||
def __init__(self):
|
||||
self.items = { }
|
||||
|
||||
def parseLine(self, line):
|
||||
kv = readKeyValue(line)
|
||||
# Игнорируем всё, что не является ключом со значением.
|
||||
if kv is None:
|
||||
return
|
||||
self.items[kv[0]] = kv[1]
|
||||
5
Utilities/platform/3/Result.py
Normal file
5
Utilities/platform/3/Result.py
Normal file
@@ -0,0 +1,5 @@
|
||||
class Result:
|
||||
def __init__(self, structure):
|
||||
self.structure = structure
|
||||
|
||||
self.file = ""
|
||||
13
Utilities/platform/3/Source.py
Normal file
13
Utilities/platform/3/Source.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from readKeyValue import *
|
||||
|
||||
class Source:
|
||||
def __init__(self):
|
||||
self.name = ""
|
||||
|
||||
def parseLine(self, line):
|
||||
kv = readKeyValue(line)
|
||||
# Игнорируем всё, что не является ключом со значением.
|
||||
if kv is None:
|
||||
return
|
||||
|
||||
self.name = kv[1]
|
||||
8
Utilities/platform/3/Structure.py
Normal file
8
Utilities/platform/3/Structure.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from Replace import *
|
||||
from Source import *
|
||||
|
||||
class Structure:
|
||||
def __init__(self):
|
||||
self.orig = ""
|
||||
self.replace = Replace()
|
||||
self.src = Source()
|
||||
48
Utilities/platform/3/generate
Executable file
48
Utilities/platform/3/generate
Executable file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
from argparse import ArgumentParser
|
||||
from generateStructure import *
|
||||
from parseLines import *
|
||||
from readModuleSrc import *
|
||||
from Structure import *
|
||||
|
||||
DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
|
||||
|
||||
# Импорт из общей для всех генераторов директории.
|
||||
sys.path.append(f"{DIR}/../common")
|
||||
from modulePaths import *
|
||||
from readFile import *
|
||||
|
||||
parser = ArgumentParser(prog='generate v3')
|
||||
parser.add_argument('module', type=str,
|
||||
help='the name of the module to generate')
|
||||
parser.add_argument('-i', '--input', type=str,
|
||||
help='The path and name of the input file')
|
||||
parser.add_argument('-o', '--output', type=str,
|
||||
help='The path of the output files')
|
||||
parser.add_argument('-s', '--source', type=str,
|
||||
help='The path of the source files')
|
||||
|
||||
args = parser.parse_args()
|
||||
(PATH, MODULE) = modulePaths(args.module)
|
||||
|
||||
print(f"Generating platform for module '{PATH}'...")
|
||||
|
||||
FILE_IN = args.input or f"{DIR}/../../../Modules/{PATH}/{MODULE}.yml"
|
||||
DIR_OUT = args.output or f"{DIR}/../../../Modules/{PATH}/src/"
|
||||
FILE_OUT = os.path.join(DIR_OUT, f"{MODULE}.Generated.swift")
|
||||
|
||||
# Читаем файл и разбираем его на ключи-значения.
|
||||
lines = readFile(FILE_IN)
|
||||
structure = Structure()
|
||||
parseLines(lines, structure)
|
||||
ORIG_SRC = args.source or f"{DIR}/../../../Modules/{structure.src.name}/src"
|
||||
structure.orig = readModuleSrc(ORIG_SRC, readFile)
|
||||
|
||||
# Генерируем код.
|
||||
output = generateStructure(structure)
|
||||
|
||||
# Сохраняем файл.
|
||||
with open(FILE_OUT, "w") as file:
|
||||
file.write(output)
|
||||
9
Utilities/platform/3/generateStructure.py
Normal file
9
Utilities/platform/3/generateStructure.py
Normal file
@@ -0,0 +1,9 @@
|
||||
def generateStructure(structure):
|
||||
output = f"""// ВНИМАНИЕ Сгенерировано автоматом из файла {structure.src.name}.yml
|
||||
// ВНИМАНИЕ Не менять руками!
|
||||
"""
|
||||
output += structure.orig
|
||||
for key in structure.replace.items:
|
||||
value = structure.replace.items[key]
|
||||
output = output.replace(key, value)
|
||||
return output
|
||||
23
Utilities/platform/3/parseLines.py
Normal file
23
Utilities/platform/3/parseLines.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from Mode import *
|
||||
|
||||
def parseLines(lines, structure):
|
||||
mode = Mode()
|
||||
for line in lines:
|
||||
# Определяем режим строки.
|
||||
mode.parseLine(line)
|
||||
ln = line.strip()
|
||||
|
||||
# Игнорируем пустую строку.
|
||||
if len(ln) == 0:
|
||||
continue
|
||||
|
||||
# Игнорируем комментарий.
|
||||
if ln.startswith("#"):
|
||||
continue
|
||||
|
||||
# replace.
|
||||
if mode.isReplacement:
|
||||
structure.replace.parseLine(ln)
|
||||
# src.
|
||||
elif mode.isSource:
|
||||
structure.src.parseLine(ln)
|
||||
8
Utilities/platform/3/readKeyValue.py
Normal file
8
Utilities/platform/3/readKeyValue.py
Normal file
@@ -0,0 +1,8 @@
|
||||
def readKeyValue(line):
|
||||
parts = line.split(": ")
|
||||
# Ключ со значением.
|
||||
if len(parts) == 2:
|
||||
return (parts[0], parts[1])
|
||||
|
||||
# Не является ключом со значением.
|
||||
return None
|
||||
12
Utilities/platform/3/readModuleSrc.py
Normal file
12
Utilities/platform/3/readModuleSrc.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import os
|
||||
|
||||
def readModuleSrc(dir, readFile):
|
||||
fileNames = os.listdir(dir)
|
||||
contents = []
|
||||
|
||||
for fileName in sorted(fileNames):
|
||||
path = dir + "/" + fileName
|
||||
lines = readFile(path)
|
||||
contents.extend(lines)
|
||||
|
||||
return "\n".join(contents)
|
||||
Reference in New Issue
Block a user