|
1 | 1 | import argparse |
| 2 | +import shlex |
2 | 3 | import subprocess |
| 4 | +from dataclasses import dataclass |
3 | 5 | from pathlib import Path |
4 | | -from typing import Callable, Dict, List |
| 6 | +from typing import Dict, List |
| 7 | + |
| 8 | +from glotter.source import Source |
| 9 | + |
| 10 | +TEST_INFO_DIR: Dict[str, str] = { |
| 11 | + "c": "c", |
| 12 | + "cpp": "c-plus-plus", |
| 13 | + "c#": "c-sharp", |
| 14 | + "java": "java", |
| 15 | + "kotlin": "kotlin", |
| 16 | + "swift": "swift", |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class TestInfoStruct: |
| 22 | + path: Path |
| 23 | + language: str |
| 24 | + test_info_str: str |
5 | 25 |
|
6 | 26 |
|
7 | 27 | def main(): |
8 | 28 | parser = argparse.ArgumentParser() |
9 | 29 | parser.add_argument("language", help="language to build") |
10 | 30 | parser.add_argument("files_changed", nargs="*", help="files that have changed") |
11 | 31 | parsed_args = parser.parse_args() |
12 | | - command_func = COMMANDS[parsed_args.language] |
| 32 | + testinfo_struct = get_test_info_struct(parsed_args.language) |
13 | 33 | for changed_file in parsed_args.files_changed: |
14 | 34 | path = Path(changed_file) |
15 | 35 | print(f"Building {path}") |
16 | | - command = command_func(path) |
| 36 | + command = get_build_command(testinfo_struct, path) |
17 | 37 | subprocess.run(command, cwd=path.parent, check=True) |
18 | 38 |
|
19 | 39 |
|
20 | | -def build_c(path: Path) -> List[str]: |
21 | | - return ["gcc", "-o", path.stem, path.name, "-lm"] |
22 | | - |
23 | | - |
24 | | -def build_cpp(path: Path) -> List[str]: |
25 | | - return ["g++", "-o", path.stem, path.name] |
26 | | - |
| 40 | +def get_test_info_struct(language: str) -> TestInfoStruct: |
| 41 | + path = Path("archive") / language[0] / TEST_INFO_DIR[language] / "testinfo.yml" |
| 42 | + testinfo_str = path.read_text(encoding="utf-8") |
| 43 | + return TestInfoStruct(path=path, language=language, test_info_str=testinfo_str) |
27 | 44 |
|
28 | | -def build_c_sharp(path: Path) -> List[str]: |
29 | | - return ["mcs", "-reference:System.Numerics", path.name] |
30 | 45 |
|
| 46 | +def get_build_command(testinfo_struct: TestInfoStruct, path: Path) -> List[str]: |
| 47 | + source = Source( |
| 48 | + name=path.name, |
| 49 | + language=testinfo_struct.language, |
| 50 | + path=str(path), |
| 51 | + test_info_string=testinfo_struct.test_info_str, |
| 52 | + ) |
| 53 | + build: str = source.test_info.container_info.build |
| 54 | + return shlex.split(build) |
31 | 55 |
|
32 | | -def build_java(path: Path) -> List[str]: |
33 | | - return ["javac", path.name] |
34 | | - |
35 | | - |
36 | | -def build_kotlin(path: Path) -> List[str]: |
37 | | - return ["kotlinc", path.name, "-include-runtime", "-d", f"{path.stem}.jar"] |
38 | | - |
39 | | - |
40 | | -def build_swift(path: Path) -> List[str]: |
41 | | - return ["swiftc", "-o", path.stem, path.name] |
42 | | - |
43 | | - |
44 | | -COMMANDS: Dict[str, Callable[[Path], List[str]]] = { |
45 | | - "c": build_c, |
46 | | - "cpp": build_cpp, |
47 | | - "c#": build_c_sharp, |
48 | | - "java": build_java, |
49 | | - "kotlin": build_kotlin, |
50 | | - "swift": build_swift, |
51 | | -} |
52 | 56 |
|
53 | 57 | if __name__ == "__main__": |
54 | 58 | main() |
0 commit comments