feat: support create from template files
All checks were successful
build / build (push) Successful in 2m1s

This commit is contained in:
张泊明518370910136 2025-03-19 01:53:48 -04:00
parent 4103b18673
commit 48c04d2830
GPG Key ID: D47306D7062CDA9D
8 changed files with 89 additions and 4 deletions

View File

@ -1,3 +1,4 @@
from importlib import resources
from pathlib import Path from pathlib import Path
from typing import Tuple from typing import Tuple
@ -13,6 +14,16 @@ def load_joj3_task_toml_answers() -> answer.Answers:
language: answer.LanguageInterface = inquirer.list_input( language: answer.LanguageInterface = inquirer.list_input(
"What's the language?", choices=answer.LANGUAGES "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( stages = inquirer.checkbox(
"What's the stages?", "What's the stages?",
choices=[member.value for member in language.Stage], choices=[member.value for member in language.Stage],

View File

@ -1,5 +1,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from enum import Enum from enum import Enum
from importlib import resources
from typing import Any, ClassVar, Dict, List from typing import Any, ClassVar, Dict, List
import inquirer import inquirer
@ -7,8 +8,9 @@ from pydantic import BaseModel, ConfigDict
class LanguageInterface(ABC): class LanguageInterface(ABC):
@classmethod
@abstractmethod @abstractmethod
def __str__(self) -> str: ... def __str__(cls) -> str: ...
@abstractmethod @abstractmethod
class Stage(str, Enum): ... class Stage(str, Enum): ...
@ -31,9 +33,27 @@ class LanguageInterface(ABC):
@abstractmethod @abstractmethod
def get_attribute_questions(cls) -> List[Any]: ... 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): class Cpp(LanguageInterface):
def __str__(self) -> str: @classmethod
def __str__(cls) -> str:
return "C++" return "C++"
class Stage(str, Enum): class Stage(str, Enum):
@ -67,7 +87,8 @@ class Cpp(LanguageInterface):
class Python(LanguageInterface): class Python(LanguageInterface):
def __str__(self) -> str: @classmethod
def __str__(cls) -> str:
return "Python" return "Python"
class Stage(str, Enum): class Stage(str, Enum):
@ -91,7 +112,8 @@ class Python(LanguageInterface):
class Rust(LanguageInterface): class Rust(LanguageInterface):
def __str__(self) -> str: @classmethod
def __str__(cls) -> str:
return "Rust" return "Rust"
class Stage(str, Enum): class Stage(str, Enum):
@ -120,5 +142,6 @@ LANGUAGES = [
class Answers(BaseModel): class Answers(BaseModel):
name: str name: str
language: LanguageInterface language: LanguageInterface
template_file_content: str = ""
model_config = ConfigDict(arbitrary_types_allowed=True) model_config = ConfigDict(arbitrary_types_allowed=True)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,3 @@
[[stages]]
name = "Run"
command = "python3 main.py"

View File

@ -0,0 +1,3 @@
[[stages]]
name = "Run"
command = "cargo run"

View File

@ -1,9 +1,17 @@
from typing import Any, Callable, Dict, List, Type from typing import Any, Callable, Dict, List, Type
import tomli
from joj3_config_generator.models import answer, task from joj3_config_generator.models import answer, task
def get_task_conf_from_answers(answers: answer.Answers) -> task.Config: 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 language = answers.language
transformer_dict = get_transformer_dict() transformer_dict = get_transformer_dict()
transformer = transformer_dict[type(language)] transformer = transformer_dict[type(language)]