Compare commits
6
Commits
1a3217115f
...
0.6.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c70d561c53 | ||
|
|
cc2d62c22b | ||
|
|
e54d8dd23f | ||
|
|
dc566087fb | ||
|
|
6c15ab4d0f | ||
|
|
58e3c2446a |
@@ -53,6 +53,7 @@ class Task(NamedTuple):
|
||||
|
||||
|
||||
class Entry:
|
||||
DEFAULT_EXECUTOR: type[CommandExecutor] = CommandExecutor
|
||||
_logger = logger
|
||||
|
||||
def __init__(
|
||||
@@ -61,11 +62,13 @@ class Entry:
|
||||
variables: Variables,
|
||||
basepath: Path,
|
||||
rootpath: Path,
|
||||
executor: type[CommandExecutor] | None = None,
|
||||
):
|
||||
self._tasks = tasks
|
||||
self._variables = variables
|
||||
self._basepath = basepath
|
||||
self._roopath = rootpath
|
||||
self._executor = executor or self.DEFAULT_EXECUTOR
|
||||
|
||||
@property
|
||||
def sensentive_data(self):
|
||||
@@ -103,7 +106,7 @@ class Entry:
|
||||
if key == task.key:
|
||||
self._logger.info("🔵 %s %s", task.key, self)
|
||||
for command in task.commands:
|
||||
CommandExecutor(command).run(
|
||||
self._executor(command).run(
|
||||
(task.cwd and Path(task.cwd)) or self._basepath
|
||||
)
|
||||
c += 1
|
||||
|
||||
@@ -52,6 +52,8 @@ class ProjectManager:
|
||||
except:
|
||||
self._logger.exception(entry)
|
||||
counter[ResultType.FAILED] += 1
|
||||
if not counter:
|
||||
raise RuntimeError(f'Task not found "{key}"')
|
||||
self._logger.info(
|
||||
"%s", "\n".join(f"{RESULT_ICONS[k]} {v}" for k, v in counter.items())
|
||||
)
|
||||
|
||||
@@ -7,7 +7,7 @@ variables.docker_group = "{{ 'DOCKER_GROUP' | env }}"
|
||||
variables.docker_image = "{% if docker_group | bool %}{{ docker_group }}/{% endif %}{{ docker_name }}"
|
||||
variables.docker_tag = "{{ 'DOCKER_TAG' | env('latest') }}"
|
||||
variables.docker_target = "{{ 'DOCKER_TARGET' | env }}"
|
||||
variables.docker_target_str = "{{ ('--target ' + docker_target) if docker_target else '' }}"
|
||||
variables.docker_target_str = "{{ ('--target ' + docker_target) if docker_target | bool else '' }}"
|
||||
variables.docker_build_args = {}
|
||||
variables.docker_build_args_str = "{% for item in docker_build_args.items() %}--build-arg \"{{ item[0] }}={{ item[1] }}\" {% endfor %}"
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
conditions = ["{{ 'package.json' | file }}"]
|
||||
variables.use_nvm = "{{ (((('home' | env) + '/.nvm/nvm.sh') | file) and ('.nvmrc' | file)) | bool }}"
|
||||
variables.nvm = "{{ '. ~/.nvm/nvm.sh && nvm use && ' if use_nvm | bool else '' }}"
|
||||
variables.npm_scripts = "{{ 'package.json' | read | get('scripts') | tojson }}"
|
||||
|
||||
# install
|
||||
[presets.nodejs.tasks.setup.npm]
|
||||
@@ -13,20 +14,9 @@ commands = ["{{ nvm }}npm install-clean"]
|
||||
conditions = ["{{ not ('package-lock.json' | file) }}"]
|
||||
commands = ["{{ nvm }}npm install"]
|
||||
|
||||
# test
|
||||
[presets.nodejs.tasks.test.jest]
|
||||
conditions = ["{{ 'node_modules/jest' | file }}"]
|
||||
commands = ["{{ nvm }}npx jest"]
|
||||
|
||||
# build
|
||||
[presets.nodejs.tasks.build.npm]
|
||||
conditions = ["{{ 'package.json' | read | get('scripts.build') }}"]
|
||||
commands = ["{{ nvm }}npm run build"]
|
||||
|
||||
# publish
|
||||
[presets.nodejs.tasks.publish.npm]
|
||||
conditions = ["{{ 'package.json' | read | get('scripts.publish') }}"]
|
||||
commands = ["{{ nvm }}npm run publish"]
|
||||
# scripts
|
||||
[presets.nodejs.tasks."{{ npm_scripts | tojson }}".npm]
|
||||
commands = ["{{ nvm }}npm run {{ task }}"]
|
||||
|
||||
# clean
|
||||
[presets.nodejs.tasks.clean.node_modules]
|
||||
@@ -36,4 +26,6 @@ commands = ["rm -rf node_modules"]
|
||||
# version
|
||||
[presets.nodejs.tasks.version.npm]
|
||||
conditions = ["{{ version }}"]
|
||||
commands = ["{{ nvm }}npm version {{ version }} --allow-same-version --commit-hooks false --git-tag-version false"]
|
||||
commands = [
|
||||
"{{ nvm }}npm version {{ version }} --allow-same-version --commit-hooks false --git-tag-version false",
|
||||
]
|
||||
|
||||
@@ -5,7 +5,7 @@ variables.python_poetry = "{{ 'pyproject.toml' | read | get('build-system.build-
|
||||
# setup
|
||||
[presets.python.tasks.setup.poetry]
|
||||
conditions = ["{{ python_poetry }}"]
|
||||
variables.python_version = "{{ 'pyproject.toml' | read | get('project.requires-python') | search('(\\d+.\\d+)') }}"
|
||||
variables.python_version = "{{ 'pyproject.toml' | read | get('project.requires-python') | search('(\\\\d+.\\\\d+)') }}"
|
||||
commands = ["poetry env use {{ python_version }}", "poetry install --all-extras"]
|
||||
|
||||
# playwright
|
||||
|
||||
@@ -15,6 +15,21 @@ class PresetEntryDetector(EntryDetector):
|
||||
def __init__(self, preset_loader_config: PresetLoaderConfig | None = None):
|
||||
self._preset_loader_config = preset_loader_config
|
||||
|
||||
def _expand_section(
|
||||
self,
|
||||
section: str,
|
||||
template: PathTemplate,
|
||||
variables: Variables,
|
||||
) -> dict[str, str | None]:
|
||||
data = template.fill_template(section, variables)
|
||||
try:
|
||||
sections: dict[str, str | None] = json.loads(data)
|
||||
if isinstance(sections, str):
|
||||
sections = json.loads(sections)
|
||||
except ValueError:
|
||||
sections = {data: None}
|
||||
return sections
|
||||
|
||||
# pylint:disable=too-many-locals,too-many-nested-blocks,too-many-branches
|
||||
def detect_entry(self, basepath: Path, rootpath: Path) -> Entry:
|
||||
template = PathTemplate(basepath)
|
||||
@@ -46,33 +61,37 @@ class PresetEntryDetector(EntryDetector):
|
||||
):
|
||||
continue
|
||||
for task_name, task_pack in preset.tasks.items():
|
||||
for sub_tag, task_preset in task_pack.items():
|
||||
sub_tags = template.fill_template(sub_tag, preset_variables)
|
||||
try:
|
||||
sub_tags = json.loads(sub_tags)
|
||||
except ValueError:
|
||||
sub_tags = {sub_tags: None}
|
||||
for i_sub_tag, i_sub_tag_value in sub_tags.items():
|
||||
if i_sub_tag_value:
|
||||
preset_variables["item"] = i_sub_tag_value
|
||||
if not task_preset.conditions or template.check_conditions(
|
||||
task_preset.conditions, preset_variables
|
||||
):
|
||||
variables = preset_variables.copy()
|
||||
if task_preset.variables:
|
||||
template.update_variables(
|
||||
task_preset.variables, variables
|
||||
task_names = self._expand_section(task_name, template, preset_variables)
|
||||
for i_task_name, i_task_value in task_names.items():
|
||||
if i_task_value:
|
||||
preset_variables["task"] = i_task_name
|
||||
preset_variables["item"] = i_task_value
|
||||
for sub_tag, task_preset in task_pack.items():
|
||||
sub_tags = self._expand_section(
|
||||
sub_tag, template, preset_variables
|
||||
)
|
||||
for i_sub_tag, i_sub_tag_value in sub_tags.items():
|
||||
if i_sub_tag_value:
|
||||
preset_variables["sub_tag"] = i_sub_tag
|
||||
preset_variables["item"] = i_sub_tag_value
|
||||
if not task_preset.conditions or template.check_conditions(
|
||||
task_preset.conditions, preset_variables
|
||||
):
|
||||
variables = preset_variables.copy()
|
||||
if task_preset.variables:
|
||||
template.update_variables(
|
||||
task_preset.variables, variables
|
||||
)
|
||||
task = Task(
|
||||
key=TaskKey(i_task_name, tag, i_sub_tag),
|
||||
commands=[
|
||||
template.fill_template(command, variables)
|
||||
for command in task_preset.commands
|
||||
],
|
||||
priority=task_preset.priority,
|
||||
cwd=task_preset.cwd,
|
||||
)
|
||||
task = Task(
|
||||
key=TaskKey(task_name, tag, i_sub_tag),
|
||||
commands=[
|
||||
template.fill_template(command, variables)
|
||||
for command in task_preset.commands
|
||||
],
|
||||
priority=task_preset.priority,
|
||||
cwd=task_preset.cwd,
|
||||
)
|
||||
tasks.append(task)
|
||||
tasks.append(task)
|
||||
if tasks:
|
||||
tasks = sorted(tasks, key=operator.attrgetter("priority"), reverse=True)
|
||||
return Entry(tasks, entry_variables, basepath, rootpath)
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[project]
|
||||
name = "project-manager"
|
||||
description = ""
|
||||
version = "0.5.0"
|
||||
version = "0.6.0"
|
||||
readme = "README.md"
|
||||
authors = [{ name = "shmyga", email = "shmyga.z@gmail.com" }]
|
||||
requires-python = ">=3.14,<4.0"
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
"version": "0.1.0",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"build": "echo build"
|
||||
"build": "echo build",
|
||||
"test": "echo test",
|
||||
"format": "echo format",
|
||||
"lint": "echo lint"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^29.2.2"
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
[tool.poetry]
|
||||
name = "example"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = []
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.8"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
black = "^23.1.0"
|
||||
pytest = "^7.2.2"
|
||||
isort = "^5.12.0"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
+23
-2
@@ -4,6 +4,7 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from project_manager.common.command import CommandExecutor
|
||||
from project_manager.entry.detector import EntryDetector
|
||||
from project_manager.entry.entry import Entry, PartialTaskKey
|
||||
from project_manager.preset.detector import PresetEntryDetector
|
||||
@@ -29,6 +30,24 @@ def entry(entry_detector: EntryDetector, entry_path: Path, tmp_path: Path):
|
||||
return entry_detector.detect_entry(destination, destination)
|
||||
|
||||
|
||||
class FakeCommandExecutor(CommandExecutor):
|
||||
def run(self, basepath: Path) -> dict | None:
|
||||
if self._command == "npm install":
|
||||
(basepath / "node_modules").mkdir(exist_ok=True)
|
||||
elif self._command == "poetry install --all-extras":
|
||||
bin_dir = basepath / ".venv/bin"
|
||||
bin_dir.mkdir(parents=True, exist_ok=True)
|
||||
(bin_dir / "pytest").write_text("")
|
||||
(bin_dir / "pylint").write_text("")
|
||||
(bin_dir / "isort").write_text("")
|
||||
(bin_dir / "black").write_text("")
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def entry_command_executor():
|
||||
Entry.DEFAULT_EXECUTOR = FakeCommandExecutor
|
||||
|
||||
|
||||
@pytest.mark.parametrize("entry_path", [Path(__file__).parent / "python"])
|
||||
def test_python_entry(entry: Entry | None, entry_detector: EntryDetector):
|
||||
assert entry is not None
|
||||
@@ -44,10 +63,12 @@ def test_python_entry(entry: Entry | None, entry_detector: EntryDetector):
|
||||
@pytest.mark.parametrize("entry_path", [Path(__file__).parent / "nodejs"])
|
||||
def test_nodejs_entry(entry: Entry | None, entry_detector: EntryDetector):
|
||||
assert entry is not None
|
||||
assert {"setup", "build"} == {task.key.name for task in entry._tasks}
|
||||
assert {"setup", "build", "lint", "format", "test"} == {
|
||||
task.key.name for task in entry._tasks
|
||||
}
|
||||
entry.run_task(PartialTaskKey.from_str("setup"))
|
||||
entry = entry_detector.detect_entry(entry._basepath, entry._basepath)
|
||||
assert entry is not None
|
||||
assert {"setup", "build", "test", "clean"} == {
|
||||
assert {"setup", "build", "lint", "format", "test", "clean"} == {
|
||||
task.key.name for task in entry._tasks
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user