ContriHUB / E-CP

A CLI tool with a variety of features to help in practicing competitive programming problems
0 stars 14 forks source link

Added support for java runtime #16

Closed DNA5769 closed 2 years ago

DNA5769 commented 2 years ago

Fixes #1

Firstly I made java a supported language by editing Config.py

# src/Config/Config.py
class Config():
    supported_lang = ['cpp', 'python', 'java']
    ...

I then wrote the code for creating the code file in ProblemManager.py. Had to do this separately since I wanted to capitalise the first letter to follow convention

# src/Problem/ProblemManager.py
        ...
        if lang == 'java':
            code_file_path = Path(dir, 'Code.java')
        else:
            code_file_path = Path(dir, f'code.{ext_map[lang]}')
        ...

Added mappers for java in mapper.py

# src/Runner/mapper.py
runner_map = {
    'cpp' : run_cpp_code,
    'python' : run_python_code,
    'java' : run_java_code
}

ext_map = {
    'cpp' : 'cpp', 
    'python' : 'py',
    'java' : 'java',
}

I wrote run_java_code function in runner.py. Tbh I just copied run_cpp_code 😋

I also wrote JavaRunner class in JavaRunner.py. This too is very similar too CppRunner.py 😋😋. However I did add a compile error check to JavaRunner (I think we should do this for CppRunner as well)

# src/Runner/runners/JavaRunner.py
...
    def __compile_code(self):
        compile_result = subprocess.run(['javac', self.code_file], capture_output=True, text=True)
        if compile_result.returncode != 0:
            raise CodeError(compile_result.stderr, 'compile error', compile_result.returncode)
...