|
1 | 1 | from __future__ import annotations # This enables postponed evaluation of type annotations. Required for typing.TYPE_CHECKING. See https://peps.python.org/pep-0563/
|
2 |
| -from typing import TYPE_CHECKING, List, Union, cast, Dict, Any |
| 2 | +from typing import TYPE_CHECKING, List, Union, cast, Dict, Any, TypeVar, Callable, Sequence |
3 | 3 | import shutil
|
4 | 4 | from tempfile import TemporaryDirectory
|
5 | 5 | import subprocess
|
@@ -124,7 +124,11 @@ def make(self, directory: Path, workflow_runs: List[WorkflowRun.WorkflowRun]) ->
|
124 | 124 | elif action_type == "workflow-artifact":
|
125 | 125 | actions.append(WorkflowArtifactAction(workflow_runs, **cast(Dict[str, Any], action_args)))
|
126 | 126 | elif action_type == "shell":
|
127 |
| - actions.append(ShellAction(action_args)) |
| 127 | + modifiers : List[Callable[[str], str]] = [ |
| 128 | + lambda cmd: re.sub(pattern=r"\${{\s*coding-standards\.root\s*}}", repl=str(root_path), string=cmd), |
| 129 | + lambda cmd: re.sub(pattern=r"\${{\s*layout\.root\s*}}", repl=str(directory), string=cmd) |
| 130 | + ] |
| 131 | + actions.append(ShellAction(action_args, modifiers=modifiers)) |
128 | 132 | elif action_type == "file":
|
129 | 133 | actions.append(FileAction(action_args))
|
130 | 134 | else:
|
@@ -178,12 +182,24 @@ def run(self) -> List[Path]:
|
178 | 182 | return list(map(Path, Path(self.temp_workdir.name).glob("**/*")))
|
179 | 183 |
|
180 | 184 | class ShellAction():
|
181 |
| - def __init__(self, command: str) -> None: |
| 185 | + def __init__(self, command: str, **kwargs: Any) -> None: |
182 | 186 | self.command = command.strip()
|
183 | 187 | self.temp_workdir = TemporaryDirectory()
|
| 188 | + self.options = kwargs |
| 189 | + |
| 190 | + def _rewrite_command(self) -> str: |
| 191 | + E = TypeVar("E") |
| 192 | + R = TypeVar("R") |
| 193 | + def lfold(fn: Callable[[R, E], R], lst: Sequence[E], init: R) -> R: |
| 194 | + return lfold(fn, lst[1:], fn(init, lst[0])) if lst else init |
| 195 | + if 'modifiers' in self.options: |
| 196 | + return lfold(lambda acc, x: x(acc), self.options['modifiers'], self.command) |
| 197 | + else: |
| 198 | + return self.command |
184 | 199 |
|
185 | 200 | def run(self) -> List[Path]:
|
186 |
| - concrete_command = re.sub(pattern=r"\${{\s*coding-standards\.root\s*}}", repl=str(root_path), string=self.command) |
| 201 | + #concrete_command = re.sub(pattern=r"\${{\s*coding-standards\.root\s*}}", repl=str(root_path), string=self.command) |
| 202 | + concrete_command = self._rewrite_command() |
187 | 203 | subprocess.run(concrete_command, cwd=self.temp_workdir.name, check=True, shell=True)
|
188 | 204 | return list(map(Path, Path(self.temp_workdir.name).glob("**/*")))
|
189 | 205 |
|
|
0 commit comments