feat: add version task

This commit is contained in:
2026-09-14 22:53:35 +03:00
parent 5401f04485
commit 2df0dc762f
9 changed files with 56 additions and 5 deletions
+2 -1
View File
@@ -49,10 +49,11 @@ class CommandExecutor:
err_logger = CommandLogger(logging.TRACE) err_logger = CommandLogger(logging.TRACE)
try: try:
result = subprocess.run( result = subprocess.run(
self._command.split(" "), self._command,
cwd=basepath, cwd=basepath,
stdout=out_logger, # type: ignore stdout=out_logger, # type: ignore
stderr=err_logger, # type: ignore stderr=err_logger, # type: ignore
shell=True,
) )
finally: finally:
out_logger.close() out_logger.close()
+6
View File
@@ -1,4 +1,5 @@
import json import json
import os
import re import re
import tomllib import tomllib
from pathlib import Path from pathlib import Path
@@ -13,6 +14,10 @@ def do_bool(data: str | None) -> bool:
return not (data is None or data.lower() in ["false", "null", "none", "0", ""]) return not (data is None or data.lower() in ["false", "null", "none", "0", ""])
def do_env(key: str, default: str | None = None) -> str | None:
return os.environ.get(key=key.upper(), default=default)
def _build_env(basepath: Path) -> Environment: def _build_env(basepath: Path) -> Environment:
env = Environment() env = Environment()
@@ -65,6 +70,7 @@ def _build_env(basepath: Path) -> Environment:
env.filters["search"] = do_search env.filters["search"] = do_search
env.filters["key"] = do_key env.filters["key"] = do_key
env.filters["bool"] = do_bool env.filters["bool"] = do_bool
env.filters["env"] = do_env
return env return env
+6 -2
View File
@@ -14,7 +14,9 @@ class EntryResolver:
self._ignore = ignore self._ignore = ignore
self._max_depth = max_depth self._max_depth = max_depth
def _resolve(self, rootpath: Path, basepath: Path, depth: int = 0) -> Iterable[Entry]: def _resolve(
self, rootpath: Path, basepath: Path, depth: int = 0
) -> Iterable[Entry]:
if basepath.is_dir(): if basepath.is_dir():
if basepath.name.startswith(".") or self._ignore.match_file(basepath): if basepath.name.startswith(".") or self._ignore.match_file(basepath):
return return
@@ -30,4 +32,6 @@ class EntryResolver:
yield from self._resolve(rootpath, path, depth + 1) yield from self._resolve(rootpath, path, depth + 1)
def resolve(self, basepath: Path) -> Iterable[Entry]: def resolve(self, basepath: Path) -> Iterable[Entry]:
return self._resolve(basepath, basepath) result = list(self._resolve(basepath, basepath))
result.reverse()
return result
+11
View File
@@ -0,0 +1,11 @@
[presets.git]
conditions = ["{{ '.git/config' | file }}"]
[presets.git.tasks.version.git]
priority = -1
conditions = ["{{ version }}"]
commands = [
"git add .",
"git commit -m 'ci(version): {{ version }}'",
"git tag '{{ version }}'",
]
@@ -0,0 +1,5 @@
[presets.'*']
priority = 1000
conditions = ["{{ true }}"]
variables.version = "{{ 'version' | env }}"
tasks = {}
@@ -1,26 +1,37 @@
[presets.nodejs] [presets.nodejs]
conditions = ["{{ 'package.json' | file }}"] conditions = ["{{ 'package.json' | file }}"]
# install
[presets.nodejs.tasks.setup.npm] [presets.nodejs.tasks.setup.npm]
conditions = ["{{ 'package-lock.json' | file }}"] conditions = ["{{ 'package-lock.json' | file }}"]
commands = ["npm install-clean"] commands = ["npm install-clean"]
# install legacy
[presets.nodejs.tasks.setup.npm-legacy] [presets.nodejs.tasks.setup.npm-legacy]
conditions = ["{{ not ('package-lock.json' | file) }}"] conditions = ["{{ not ('package-lock.json' | file) }}"]
commands = ["npm install"] commands = ["npm install"]
# test
[presets.nodejs.tasks.test.jest] [presets.nodejs.tasks.test.jest]
conditions = ["{{ 'node_modules/jest' | file }}"] conditions = ["{{ 'node_modules/jest' | file }}"]
commands = ["npx jest"] commands = ["npx jest"]
# build
[presets.nodejs.tasks.build.npm] [presets.nodejs.tasks.build.npm]
conditions = ["{{ 'package.json' | read | get('scripts.build') }}"] conditions = ["{{ 'package.json' | read | get('scripts.build') }}"]
commands = ["npm run build"] commands = ["npm run build"]
# publish
[presets.nodejs.tasks.publish.npm] [presets.nodejs.tasks.publish.npm]
conditions = ["{{ 'package.json' | read | get('scripts.publish') }}"] conditions = ["{{ 'package.json' | read | get('scripts.publish') }}"]
commands = ["npm run publish"] commands = ["npm run publish"]
# clean
[presets.nodejs.tasks.clean.node_modules] [presets.nodejs.tasks.clean.node_modules]
conditions = ["{{ 'node_modules' | file }}"] conditions = ["{{ 'node_modules' | file }}"]
commands = ["rm -rf node_modules"] commands = ["rm -rf node_modules"]
# version
[presets.nodejs.tasks.version.npm]
conditions = ["{{ version }}"]
commands = ["npm version {{ version }} --allow-same-version"]
@@ -53,3 +53,8 @@ commands = [".venv/bin/black ./"]
[presets.python.tasks.clean.venv] [presets.python.tasks.clean.venv]
conditions = ["{{ '.venv' | file }}"] conditions = ["{{ '.venv' | file }}"]
commands = ["rm -rf .venv"] commands = ["rm -rf .venv"]
# version
[presets.python.tasks.version.poetry]
conditions = ["{{ python_poetry and version }}"]
commands = ["poetry version {{ version }}"]
+9 -2
View File
@@ -1,4 +1,5 @@
import operator import operator
from os import environ
from pathlib import Path from pathlib import Path
from ..common.template import PathTemplate from ..common.template import PathTemplate
@@ -19,9 +20,15 @@ class PresetEntryDetector(EntryDetector):
if preset.skip: if preset.skip:
return ENTRY_SKIP return ENTRY_SKIP
tasks = [] tasks = []
for tag, preset in preset.presets.items(): global_variables = {f"env_{k.lower()}": v for k, v in environ.items()}
preset_variables: Variables = {"entry_path": basepath} presets = sorted(
preset.presets.items(), key=lambda item: item[1].priority, reverse=True
)
for tag, preset in presets:
preset_variables: Variables = {**global_variables, "entry_path": basepath}
if preset.variables: if preset.variables:
if tag == "*":
template.update_variables(preset.variables, global_variables)
template.update_variables(preset.variables, preset_variables) template.update_variables(preset.variables, preset_variables)
if preset.conditions and not template.check_conditions( if preset.conditions and not template.check_conditions(
preset.conditions, preset_variables preset.conditions, preset_variables
+1
View File
@@ -12,6 +12,7 @@ class TaskPreset(BaseModel):
class EntryPreset(BaseModel): class EntryPreset(BaseModel):
priority: int = 0
conditions: list[str] | None = None conditions: list[str] | None = None
variables: Variables | None = None variables: Variables | None = None
tasks: dict[str, dict[str, TaskPreset]] tasks: dict[str, dict[str, TaskPreset]]