aboutsummaryrefslogtreecommitdiffstats
path: root/task
diff options
context:
space:
mode:
authorBernardas AliĊĦauskas <bernardas.alisauskas@pm.me>2021-09-27 17:12:22 +0700
committerGitHub <noreply@github.com>2021-09-27 13:12:22 +0300
commitedde6029745a650696773c30247b97c151079487 (patch)
treee04a1580cf96e7d018d1a1ac4e63333d8cd7c4b9 /task
parentRelease version 1.8.2 (diff)
downloadtaskipy2-edde6029745a650696773c30247b97c151079487.zip
Add variable support (#38)
* add variable support * add use_vars task option and make command variables expand only when use_vars is True * add tests for variable feature * add setting use_vars as global use_vars for all commands
Diffstat (limited to '')
-rw-r--r--taskipy/pyproject.py14
-rw-r--r--taskipy/task.py19
-rw-r--r--taskipy/task_runner.py17
3 files changed, 39 insertions, 11 deletions
diff --git a/taskipy/pyproject.py b/taskipy/pyproject.py
index 8486cb6..702aad0 100644
--- a/taskipy/pyproject.py
+++ b/taskipy/pyproject.py
@@ -8,8 +8,7 @@ from taskipy.exceptions import (
InvalidRunnerTypeError,
MalformedPyProjectError,
MissingPyProjectFileError,
- MissingTaskipyTasksSectionError,
- MissingTaskipySettingsSectionError
+ MissingTaskipyTasksSectionError
)
@@ -28,11 +27,18 @@ class PyProject:
raise MissingTaskipyTasksSectionError()
@property
+ def variables(self) -> Dict[str, Task]:
+ try:
+ return self.__items['tool']['taskipy'].get('variables', {})
+ except KeyError:
+ return {}
+
+ @property
def settings(self) -> dict:
try:
return self.__items['tool']['taskipy']['settings']
except KeyError:
- raise MissingTaskipySettingsSectionError()
+ return {}
@property
def runner(self) -> Optional[str]:
@@ -43,7 +49,7 @@ class PyProject:
raise InvalidRunnerTypeError()
return runner.strip()
- except (KeyError, MissingTaskipySettingsSectionError):
+ except KeyError:
return None
@staticmethod
diff --git a/taskipy/task.py b/taskipy/task.py
index c357b98..c812c42 100644
--- a/taskipy/task.py
+++ b/taskipy/task.py
@@ -6,6 +6,7 @@ class Task:
self.__task_name = task_name
self.__task_command = self.__extract_task_command(task_toml_contents)
self.__task_description = self.__extract_task_description(task_toml_contents)
+ self.__task_use_vars = self.__extract_task_use_vars(task_toml_contents)
@property
def name(self):
@@ -19,6 +20,20 @@ class Task:
def description(self):
return self.__task_description
+ @property
+ def use_vars(self):
+ return self.__task_use_vars
+
+ def __extract_task_use_vars(self, task_toml_contents: object) -> bool:
+ if isinstance(task_toml_contents, str):
+ return False
+ if isinstance(task_toml_contents, dict):
+ value = task_toml_contents.get('use_vars', False)
+ if not isinstance(value, bool):
+ raise MalformedTaskError(self.__task_name, f'task\'s "use_vars" arg has to be bool type got {type(value)}')
+ return value
+ raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help, use_vars }')
+
def __extract_task_command(self, task_toml_contents: object) -> str:
if isinstance(task_toml_contents, str):
return task_toml_contents
@@ -29,7 +44,7 @@ class Task:
except KeyError:
raise MalformedTaskError(self.__task_name, 'the task item does not have the "cmd" property')
- raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help }')
+ raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help, use_vars }')
def __extract_task_description(self, task_toml_contents: object) -> str:
if isinstance(task_toml_contents, str):
@@ -41,4 +56,4 @@ class Task:
except KeyError:
return ''
- raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help }')
+ raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help, use_vars}')
diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py
index fb3f806..b510eaf 100644
--- a/taskipy/task_runner.py
+++ b/taskipy/task_runner.py
@@ -7,7 +7,7 @@ from pathlib import Path
from typing import List, Union, Optional
from taskipy.pyproject import PyProject
-from taskipy.exceptions import TaskNotFoundError
+from taskipy.exceptions import TaskNotFoundError, MalformedTaskError
from taskipy.task import Task
from taskipy.help import HelpFormatter
@@ -36,26 +36,33 @@ class TaskRunner:
pre_task = self.__pre_task(task_name)
if pre_task is not None:
- exit_code = self.__run_command_and_return_exit_code(pre_task.command)
+ exit_code = self.__run_command_and_return_exit_code(pre_task)
if exit_code != 0:
return exit_code
- exit_code = self.__run_command_and_return_exit_code(task.command, args)
+ exit_code = self.__run_command_and_return_exit_code(task, args)
if exit_code != 0:
return exit_code
post_task = self.__post_task(task_name)
if post_task is not None:
- exit_code = self.__run_command_and_return_exit_code(post_task.command)
+ exit_code = self.__run_command_and_return_exit_code(post_task)
if exit_code != 0:
return exit_code
return 0
- def __run_command_and_return_exit_code(self, command: str, args: List[str] = None) -> int:
+ def __run_command_and_return_exit_code(self, task: Task, args: List[str] = None) -> int:
if args is None:
args = []
+ command = task.command
+ if task.use_vars or self.__project.settings.get('use_vars', {}):
+ try:
+ command = command.format(**self.__project.variables)
+ except KeyError as e:
+ raise MalformedTaskError(task.name, f"{e} variable expected in [pyproject.taskipy.variables]")
+
if self.__project.runner is not None:
command = f'{self.__project.runner} {command}'