Skip to content

Commit c66b494

Browse files
fix back java compiler
1 parent 082c3b0 commit c66b494

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/auto_compiler/auto_compiler.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class AutoCompiler:
1212
def __init__(
1313
self,
1414
source_folder: Path,
15-
compile_folder: Path = Path('out'),
16-
result_folder: Path = Path('ais')
15+
compile_folder: Path = Path("out"),
16+
result_folder: Path = Path("ais")
1717
) -> None:
1818

1919
self.source_folder = source_folder
@@ -43,10 +43,9 @@ async def compile_file(self, name: Path) -> Path:
4343
symlink = self.result_folder.joinpath(executable.name)
4444
self.result_folder.mkdir(parents=True, exist_ok=True)
4545

46-
try:
47-
symlink.symlink_to(Path.cwd().joinpath(executable))
48-
except FileExistsError:
49-
pass
46+
if symlink.exists():
47+
symlink.unlink()
48+
symlink.symlink_to(Path.cwd().joinpath(executable))
5049

5150
return symlink
5251

src/auto_compiler/compilers/JavaCompiler.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,26 @@ def __init__(self) -> None:
1616
...
1717

1818
async def build_file(self, file: Path, compile_dir: Path) -> Path:
19-
compile_ai_dir = compile_dir / file.stem
20-
if compile_ai_dir.exists():
21-
for old_file in compile_ai_dir.glob("*.class"):
22-
old_file.unlink()
23-
else:
24-
compile_ai_dir.mkdir(parents=True)
19+
self.modify_class_name(file)
20+
executable = compile_dir.joinpath(Path(f"{file.stem}.class"))
2521
process = await create_subprocess_exec(
26-
"javac", "-d", str(compile_ai_dir), str(file), stdout=PIPE, stderr=PIPE
22+
"javac", "-d", str(compile_dir), str(file), stdout=PIPE, stderr=PIPE
2723
)
2824
stdout, stderr = await process.communicate()
2925
print(stdout.decode("utf-8"))
3026
if not process.returncode:
31-
class_files = list(compile_ai_dir.glob("*.class"))
32-
if len(class_files) > 0:
33-
executable = class_files[0]
34-
return executable.rename(compile_ai_dir / f"{file.stem}.class")
27+
if executable.exists():
28+
return executable
3529
else:
3630
raise CompilerException("File wasn't created")
3731
else:
3832
raise CompilerException(stderr.decode("utf-8"))
33+
34+
@staticmethod
35+
def modify_class_name(file):
36+
# Should probably use something more robust than regex
37+
with open(file, "r") as f:
38+
filedata = f.read()
39+
filedata = re.sub(r"(public\s+)?class\s.*\n?{", f"class {file.stem} {"{"}\n", filedata)
40+
with open(file, "w") as f:
41+
f.write(filedata)

0 commit comments

Comments
 (0)