From f5a3242b2f3aa7e3abab193071f56a3c58111e4a Mon Sep 17 00:00:00 2001 From: Jonas Wilkens Date: Wed, 19 May 2021 16:09:20 +0200 Subject: [PATCH] Initial commit (seems to work) --- .vscode/launch.json | 16 ++++++++++++ main.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ make.bat | 8 ++++++ test/elo.rs | 3 +++ test/fakers | 1 + test/main.rs | 8 ++++++ 6 files changed, 100 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 main.py create mode 100644 make.bat create mode 100644 test/elo.rs create mode 100644 test/fakers create mode 100644 test/main.rs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..65a3115 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "main.py", + "console": "integratedTerminal", + "args": ["${workspaceRoot}//test", "main.rs"] + } + ] +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..42956f9 --- /dev/null +++ b/main.py @@ -0,0 +1,64 @@ +import pathlib +import sys +import os +import re + +def parse_file(path: pathlib.Path, main_file: str, content: list): + """Parses a given rust file into the content list. + + Args: + path (pathlib.Path): A path to the file + content (list): A list of all lines of content + """ + with open(path, "r") as file: + # Put file in mod section + if path.name != main_file: + content.append(f"mod {path.stem} {{\n") + + while line := file.readline(): + # Remove mod calls in file + if not re.match("mod\s+.*;", line) is None: + pass + else: + content.append(line) + + # Close file mod section + if path.name != main_file: + content.append(f"\n}}\n") + + # Add newline as delimiter + content.append("\n") + +def find_files(dir: pathlib.Path): + """Finds all files in the given directory recursively. + + Args: + dir (pathlib.Path): The root directory for finding files + + Yields: + pathlib.Path: The next file in the given directory + """ + for dirpath, _dirname, files in os.walk(dir): + for name in files: + if name.lower().endswith(".rs"): + yield pathlib.Path(dirpath) / name + +if __name__ == "__main__": + root_dir = pathlib.Path(sys.argv[1]) + main_file = sys.argv[2] + out_file = root_dir / "output.rs" + content = list() + + # Remove output file if it already exists + if out_file.exists(): + out_file.unlink() + + # Parse all files in current directory recursively + for file in find_files(root_dir): + parse_file(file, main_file, content) + + print(f"Writing file {out_file} ..."); + + # Write output file + with open(out_file, "w") as file: + file.writelines(content) \ No newline at end of file diff --git a/make.bat b/make.bat new file mode 100644 index 0000000..eeb9ee6 --- /dev/null +++ b/make.bat @@ -0,0 +1,8 @@ +@echo off +pyinstaller --noconfirm --name "cg_rust_merger" main.py --noconsole >nul 2>&1 +@RD /S /Q "__pycache__" +@RD /S /Q "build" >nul 2>&1 +if exist "build" rd /s /q "build" +DEL "cg_rust_merger.spec" + +echo Build complete \ No newline at end of file diff --git a/test/elo.rs b/test/elo.rs new file mode 100644 index 0000000..568e306 --- /dev/null +++ b/test/elo.rs @@ -0,0 +1,3 @@ +pub fn lebo() -> String { + String::from("ELO") +} \ No newline at end of file diff --git a/test/fakers b/test/fakers new file mode 100644 index 0000000..bdaeb66 --- /dev/null +++ b/test/fakers @@ -0,0 +1 @@ +i bims fake \ No newline at end of file diff --git a/test/main.rs b/test/main.rs new file mode 100644 index 0000000..b7803f5 --- /dev/null +++ b/test/main.rs @@ -0,0 +1,8 @@ +mod elo; + +use std::io; +use crate::elo; + +fn main() { + println!("{}", elo::lebo()); +} \ No newline at end of file