diff --git a/joj3_config_generator/loader.py b/joj3_config_generator/loader.py index 0fb74f1..6c8b877 100644 --- a/joj3_config_generator/loader.py +++ b/joj3_config_generator/loader.py @@ -1,3 +1,4 @@ +from importlib import resources from pathlib import Path from typing import Tuple @@ -13,6 +14,16 @@ def load_joj3_task_toml_answers() -> answer.Answers: language: answer.LanguageInterface = inquirer.list_input( "What's the language?", choices=answer.LANGUAGES ) + if inquirer.confirm("Load content from templates?", default=True): + answers = inquirer.prompt(language.get_template_questions()) + templates_dir = resources.files(f"joj3_config_generator.templates").joinpath( + language.__str__() + ) + template_file_path = answers["template_file"] + template_file_content = Path(templates_dir / template_file_path).read_text() + return answer.Answers( + name=name, language=language, template_file_content=template_file_content + ) stages = inquirer.checkbox( "What's the stages?", choices=[member.value for member in language.Stage], diff --git a/joj3_config_generator/models/answer.py b/joj3_config_generator/models/answer.py index b79aebf..706db22 100644 --- a/joj3_config_generator/models/answer.py +++ b/joj3_config_generator/models/answer.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod from enum import Enum +from importlib import resources from typing import Any, ClassVar, Dict, List import inquirer @@ -7,8 +8,9 @@ from pydantic import BaseModel, ConfigDict class LanguageInterface(ABC): + @classmethod @abstractmethod - def __str__(self) -> str: ... + def __str__(cls) -> str: ... @abstractmethod class Stage(str, Enum): ... @@ -31,9 +33,27 @@ class LanguageInterface(ABC): @abstractmethod def get_attribute_questions(cls) -> List[Any]: ... + @classmethod + def get_template_questions(cls) -> List[Any]: + templates_dir = resources.files(f"joj3_config_generator.templates").joinpath( + cls.__str__() + ) + choices = [] + for entry in templates_dir.iterdir(): + if entry.is_file() and entry.name.endswith(".toml"): + choices.append(entry.name) + return [ + inquirer.List( + "template_file", + message="Which template file do you want?", + choices=choices, + ), + ] + class Cpp(LanguageInterface): - def __str__(self) -> str: + @classmethod + def __str__(cls) -> str: return "C++" class Stage(str, Enum): @@ -67,7 +87,8 @@ class Cpp(LanguageInterface): class Python(LanguageInterface): - def __str__(self) -> str: + @classmethod + def __str__(cls) -> str: return "Python" class Stage(str, Enum): @@ -91,7 +112,8 @@ class Python(LanguageInterface): class Rust(LanguageInterface): - def __str__(self) -> str: + @classmethod + def __str__(cls) -> str: return "Rust" class Stage(str, Enum): @@ -120,5 +142,6 @@ LANGUAGES = [ class Answers(BaseModel): name: str language: LanguageInterface + template_file_content: str = "" model_config = ConfigDict(arbitrary_types_allowed=True) diff --git a/joj3_config_generator/templates/C++/clang-tidy.toml b/joj3_config_generator/templates/C++/clang-tidy.toml new file mode 100644 index 0000000..d7d29f6 --- /dev/null +++ b/joj3_config_generator/templates/C++/clang-tidy.toml @@ -0,0 +1,13 @@ +[[stages]] +name = "[cq] Clang-tidy" +command = "run-clang-tidy-18 -header-filter=.* -quiet -load=/usr/local/lib/libcodequality.so -p h7/build h7/ex2.cpp" +files.import = [ "tests/homework/h7/.clang-tidy", "h7/build/compile_commands.json" ] +limit.stdout = "65m" + +parsers = [ "clangtidy", "result-detail" ] +clangtidy.keyword = [ "codequality-unchecked-malloc-result", "codequality-no-global-variables", "codequality-no-header-guard", "codequality-no-fflush-stdin", "readability-function-size", "readability-duplicate-include", "readability-identifier-naming", "readability-redundant", "readability-misleading-indentation", "readability-misplaced-array-index", "cppcoreguidelines-init-variables", "bugprone-suspicious-string-compare", "google-global-names-in-headers", "clang-diagnostic", "clang-analyzer", "misc", "performance", "portability" ] +clangtidy.weight = [ 5, 20, 20, 20, 10, 5, 5, 5, 15, 5, 5, 5, 5, 5, 5, 5, 5, 5] +result-detail.exitstatus = true +result-detail.stdout = true +result-detail.time = false +result-detail.mem = false diff --git a/joj3_config_generator/templates/C++/cppcheck.toml b/joj3_config_generator/templates/C++/cppcheck.toml new file mode 100644 index 0000000..24d9c6b --- /dev/null +++ b/joj3_config_generator/templates/C++/cppcheck.toml @@ -0,0 +1,12 @@ +[[stages]] +name = "[cq] Cppcheck" +command = "cppcheck --template='{\"file\":\"{file}\",\"line\":{line}, \"column\":{column}, \"severity\":\"{severity}\", \"message\":\"{message}\", \"id\":\"{id}\"}' --force --enable=all --suppress=missingIncludeSystem --quiet h7/ex2.cpp" +limit.stderr = "65m" + +parsers = [ "cppcheck", "result-detail" ] +cppcheck.keyword = ["error", "warning", "portability", "performance", "style"] +cppcheck.weight = [15, 5, 5, 5, 5] +result-detail.exitstatus = true +result-detail.stderr = true +result-detail.time = false +result-detail.mem = false diff --git a/joj3_config_generator/templates/C++/cpplint.toml b/joj3_config_generator/templates/C++/cpplint.toml new file mode 100644 index 0000000..3740279 --- /dev/null +++ b/joj3_config_generator/templates/C++/cpplint.toml @@ -0,0 +1,12 @@ +[[stages]] +name = "[cq] Cpplint" +command = "cpplint --linelength=120 --filter=-legal,-readability/casting,-whitespace,-runtime/printf,-runtime/threadsafe_fn,-runtime/int,-readability/todo,-build/include_subdir,-build/header_guard,-build/include_what_you_use --recursive --exclude=build h7/ex2.cpp" +limit.stdout = "65m" + +parsers = ["cpplint", "result-detail"] +cpplint.keyword = ["runtime", "readability", "build"] +cpplint.weight = [5, 20, 10] +result-detail.exitstatus = true +result-detail.stderr = true +result-detail.time = false +result-detail.mem = false diff --git a/joj3_config_generator/templates/Python/basic.toml b/joj3_config_generator/templates/Python/basic.toml new file mode 100644 index 0000000..bdad5aa --- /dev/null +++ b/joj3_config_generator/templates/Python/basic.toml @@ -0,0 +1,3 @@ +[[stages]] +name = "Run" +command = "python3 main.py" diff --git a/joj3_config_generator/templates/Rust/basic.toml b/joj3_config_generator/templates/Rust/basic.toml new file mode 100644 index 0000000..03d2323 --- /dev/null +++ b/joj3_config_generator/templates/Rust/basic.toml @@ -0,0 +1,3 @@ +[[stages]] +name = "Run" +command = "cargo run" diff --git a/joj3_config_generator/transformers/answer.py b/joj3_config_generator/transformers/answer.py index 1b19bc2..b0a9917 100644 --- a/joj3_config_generator/transformers/answer.py +++ b/joj3_config_generator/transformers/answer.py @@ -1,9 +1,17 @@ from typing import Any, Callable, Dict, List, Type +import tomli + from joj3_config_generator.models import answer, task def get_task_conf_from_answers(answers: answer.Answers) -> task.Config: + if answers.template_file_content: + toml_dict = tomli.loads(answers.template_file_content) + return task.Config( + task=task.Task(name=answers.name), + stages=toml_dict["stages"], + ) language = answers.language transformer_dict = get_transformer_dict() transformer = transformer_dict[type(language)]