|
| 1 | +import io |
| 2 | +import os |
| 3 | + |
| 4 | +for root, dirs, files in os.walk(os.getcwd()): |
| 5 | + for file in files: |
| 6 | + if (".lua" in file): |
| 7 | + enc = "utf8" |
| 8 | + fullpath = os.path.join(root, file) |
| 9 | + try: |
| 10 | + hasCmd = "cmd" in io.open(fullpath, encoding=enc).read() |
| 11 | + except UnicodeDecodeError: |
| 12 | + enc = "windows-1252" |
| 13 | + hasCmd = "cmd" in io.open(fullpath, encoding=enc).read() |
| 14 | + if (hasCmd): |
| 15 | + print("cmd found:", fullpath) |
| 16 | + with io.open(fullpath, "r", encoding=enc) as f: |
| 17 | + lines = f.readlines() |
| 18 | + for i, line in enumerate(lines): |
| 19 | + if not line.lstrip().startswith("-"): |
| 20 | + if "cmd" in line.split("--")[0]: |
| 21 | + indent = line[:len(line) - len(line.lstrip())] |
| 22 | + cmt = " " + line[line.rfind("--"):].strip() if len(line.split("--")) > 1 else "" |
| 23 | + line = line.split("--")[0] |
| 24 | + oneLiner = "{" in line and "}" in line |
| 25 | + ending = line.strip()[line.strip().index("}"):] if oneLiner else line.strip()[-1] |
| 26 | + if ending == ')': |
| 27 | + ending = "" |
| 28 | + init = line.split("cmd")[0] |
| 29 | + args = line.split("cmd")[1] |
| 30 | + idx = args[:args.rfind(')')].rfind(')') if "RunCommandsOnChildren" in init else args[:args[:args.rfind(')')].rfind(')')].rfind(')') if init[-1] == '(' else args.rfind(')') |
| 31 | + args = args[1:idx].split(";") if idx - 1 > 0 else "" |
| 32 | + newArg = "" |
| 33 | + if len(args) > 0: |
| 34 | + for arg in args: |
| 35 | + if (arg != ""): |
| 36 | + if (len(arg.split(",", 1)) == 1): |
| 37 | + newArg += ":" + arg + "()" |
| 38 | + else: |
| 39 | + newArg += ":" + arg.split(",", 1)[0].strip() + "(" + arg.split(",", 1)[1].strip() + ")" |
| 40 | + if oneLiner: |
| 41 | + init = ("{\n" + indent + " ").join(init.split("{")) |
| 42 | + lines[i] = indent + cmt + "\n" + init + "function(self)\n" + indent + " self" + newArg + "\n" + indent + " end\n" + indent + ending + "\n" |
| 43 | + elif "RunCommandsOnChildren" in init: |
| 44 | + lines[i] = init + "function(self) self" + newArg + " end" + ")" + ending + cmt + "\n" |
| 45 | + elif init[-1] == '(': |
| 46 | + lines[i] = indent + line[line.rfind('(')+1:line.rfind(')')] + newArg + ending + cmt + "\n" |
| 47 | + else: |
| 48 | + lines[i] = init + "function(self)\n" + indent + " self" + newArg + cmt + "\n" + indent + "end" + ending + "\n" |
| 49 | + else: |
| 50 | + lines[i] = "" |
| 51 | + with io.open(fullpath, "w", encoding=enc) as f: |
| 52 | + lines = "".join(lines) |
| 53 | + f.write(lines) |
0 commit comments