aboutsummaryrefslogtreecommitdiffstats
path: root/task
diff options
context:
space:
mode:
authorDiogo <rossi.diogo@gmail.com>2024-08-23 17:59:31 -0300
committerGitHub <noreply@github.com>2024-08-23 23:59:31 +0300
commit7cfecafdbd84c0f25b8a6c09a4cef72616968917 (patch)
treea6ebb99681950915a503366cd4be1fc3c2e2ff0c /task
parentUpdate README.md (diff)
downloadtaskipy2-7cfecafdbd84c0f25b8a6c09a4cef72616968917.zip
Add option `cwd` to running tasks (#83)
* 🎨 chore: removing settings that are deprecated Reference: https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions#existing-python-tools-extensions * 👷 build: add `.venv` folder to `.gitignore` * ✨ feat: new property 'workdir' for tasks * 🎨 chore: add "cwd" to error messages * ✨ feat: new property `dirpath` of `PyProject` * ✨ feat: new method to get workdir in `TaskRunner` * 🐞 fix: allow for absolute paths * 🐞 fix: the task cwd should takes precedence * 📄 docs: update `README.md` * Update README.md Co-authored-by: Roy Sommer <illBeRoy@users.noreply.github.com> * rename variable `working_dir` into `cwd_as_path` to emphasize what it's really used for suggestion: https://github.com/taskipy/taskipy/pull/83/files#r1676391148 * assert that `cwd` is a string, and throw if it's not * 👷 build: add settings to test in VSCode * 🐞 fix: add task exec when testing on windows * ✅ test: add new `SetCWDTestCase` * ✅ test: fix error message in test case * ✏️ typo: in fixture name * 💄 style: format code * Update settings.json --------- Co-authored-by: Roy Sommer <illBeRoy@users.noreply.github.com>
Diffstat (limited to '')
-rw-r--r--task.bat1
-rw-r--r--taskipy/pyproject.py8
-rw-r--r--taskipy/task.py23
-rw-r--r--taskipy/task_runner.py20
4 files changed, 44 insertions, 8 deletions
diff --git a/task.bat b/task.bat
new file mode 100644
index 0000000..3be1261
--- /dev/null
+++ b/task.bat
@@ -0,0 +1 @@
+python %~dp0taskipy\cli.py %*
diff --git a/taskipy/pyproject.py b/taskipy/pyproject.py
index 1ffcb3e..e7fd74e 100644
--- a/taskipy/pyproject.py
+++ b/taskipy/pyproject.py
@@ -16,8 +16,8 @@ from taskipy.exceptions import (
class PyProject:
def __init__(self, base_dir: Path):
- pyproject_path = PyProject.__find_pyproject_path(base_dir)
- self.__items = PyProject.__load_toml_file(pyproject_path)
+ self.__pyproject_path = PyProject.__find_pyproject_path(base_dir)
+ self.__items = PyProject.__load_toml_file(self.__pyproject_path)
@property
def tasks(self) -> Dict[str, Task]:
@@ -33,6 +33,10 @@ class PyProject:
return tasks
@property
+ def dirpath(self) -> Path:
+ return self.__pyproject_path.parent
+
+ @property
def variables(self) -> Dict[str, Variable]:
try:
toml_vars = self.__items['tool']['taskipy'].get('variables', {})
diff --git a/taskipy/task.py b/taskipy/task.py
index 61fd718..a065fd8 100644
--- a/taskipy/task.py
+++ b/taskipy/task.py
@@ -9,6 +9,7 @@ class Task:
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)
+ self.__task_workdir = self.__extract_task_workdir(task_toml_contents)
@property
def name(self) -> str:
@@ -19,6 +20,10 @@ class Task:
return self.__task_command
@property
+ def workdir(self) -> Optional[str]:
+ return self.__task_workdir
+
+ @property
def description(self) -> str:
return self.__task_description
@@ -36,7 +41,7 @@ class Task:
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 }')
+ raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')
def __extract_task_command(self, task_toml_contents: object) -> str:
if isinstance(task_toml_contents, str):
@@ -48,7 +53,19 @@ 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, use_vars }')
+ raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')
+
+ def __extract_task_workdir(self, task_toml_contents: object) -> Optional[str]:
+ if isinstance(task_toml_contents, str):
+ return None
+
+ if isinstance(task_toml_contents, dict):
+ value = task_toml_contents.get('cwd')
+ if value is not None and not isinstance(value, str):
+ raise MalformedTaskError(self.__task_name, f'task\'s "cwd" arg has to be str type got {type(value)}')
+ return value
+
+ raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')
def __extract_task_description(self, task_toml_contents: object) -> str:
if isinstance(task_toml_contents, str):
@@ -60,4 +77,4 @@ class Task:
except KeyError:
return ''
- raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help, use_vars}')
+ raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')
diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py
index ab81012..d9cd6f4 100644
--- a/taskipy/task_runner.py
+++ b/taskipy/task_runner.py
@@ -23,9 +23,9 @@ else:
class TaskRunner:
def __init__(self, cwd: Union[str, Path]):
- working_dir = cwd if isinstance(cwd, Path) else Path(cwd)
- self.__working_dir = working_dir
- self.__project = PyProject(working_dir)
+ cwd_as_path = cwd if isinstance(cwd, Path) else Path(cwd)
+ self.__project = PyProject(cwd_as_path)
+ self.__working_dir = self.__get_working_dir() or cwd_as_path
def list(self):
"""lists tasks to stdout"""
@@ -34,6 +34,7 @@ class TaskRunner:
def run(self, task_name: str, args: List[str]) -> int:
pre_command, command, post_command = self.__get_formatted_commands(task_name)
+ self.__working_dir = self.__get_working_dir(task_name) or self.__working_dir
if pre_command is not None:
exit_code = self.__run_command_and_return_exit_code(pre_command)
@@ -194,3 +195,16 @@ class TaskRunner:
process.send_signal(signum)
return signal_handler
+
+ def __get_working_dir(self, task_name: Optional[str] = None) -> Optional[Path]:
+ cwd: Optional[str] = self.__project.settings.get("cwd", None)
+
+ if task_name is not None:
+ cwd = self.__project.tasks[task_name].workdir or cwd
+
+ if cwd is not None:
+ path = self.__project.dirpath / cwd
+ if path.exists():
+ return path
+
+ return None