-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
271 lines (228 loc) · 11.9 KB
/
commands.py
File metadata and controls
271 lines (228 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from __future__ import annotations
from sys import exit, stderr
from uvtask.colors import color_service, preference_manager
from uvtask.executor import CommandExecutor
from uvtask.formatters import CommandMatcher, CustomArgumentParser
class CommandValidator:
def __init__(self, command_matcher: CommandMatcher | None = None):
self._matcher = command_matcher or CommandMatcher()
def validate_exists(self, command_name: str, scripts: dict[str, str | list[str]]) -> None:
if command_name not in scripts:
error_text = color_service.bold_red("error")
usage_text = color_service.bold_green("Usage:")
prog_text = color_service.bold_teal("uvtask")
options_text = color_service.teal("[OPTIONS]")
command_text = color_service.teal("<COMMAND>")
help_text = color_service.bold_teal("--help")
print(f"{error_text}: unrecognized subcommand '{color_service.yellow(command_name)}'", file=stderr)
available_commands = list(scripts.keys())
similar = self._matcher.find_similar(command_name, available_commands)
if similar:
tip_text = color_service.yellow("tip")
similar_cmd = color_service.yellow(f"'{similar}'")
print(f"\n {tip_text}: a similar subcommand exists: {similar_cmd}", file=stderr)
print(f"\n{usage_text} {prog_text} {options_text} {command_text}", file=stderr)
print(f"\nFor more information, try '{help_text}'.", file=stderr)
exit(1)
class CommandResolver:
@staticmethod
def resolve_command_references(command: str, all_scripts: dict[str, str | list[str]], visited: set[str] | None = None) -> list[str]:
if visited is None:
visited = set()
if command in visited:
raise ValueError(f"Circular reference detected: {' -> '.join(visited)} -> {command}")
if command not in all_scripts:
return [command]
visited.add(command)
referenced_script = all_scripts[command]
if isinstance(referenced_script, str):
result = CommandResolver.resolve_command_references(referenced_script, all_scripts, visited.copy())
elif isinstance(referenced_script, list):
result = []
for cmd in referenced_script:
if cmd in all_scripts:
result.extend(CommandResolver.resolve_command_references(cmd, all_scripts, visited.copy()))
else:
result.append(cmd)
else:
result = [str(referenced_script)]
visited.remove(command)
return result
@staticmethod
def resolve_list_references(commands: list[str], all_scripts: dict[str, str | list[str]]) -> list[str]:
resolved = []
for cmd in commands:
if cmd in all_scripts:
resolved.extend(CommandResolver.resolve_command_references(cmd, all_scripts))
else:
resolved.append(cmd)
return resolved
class CommandBuilder:
def __init__(self, resolver: CommandResolver | None = None):
self._resolver = resolver or CommandResolver()
def build_commands(self, script: str | list[str], script_args: list[str], all_scripts: dict[str, str | list[str]] | None = None) -> list[str]:
script_args_str = " ".join(script_args) if script_args else ""
if isinstance(script, str):
if all_scripts and script in all_scripts:
resolved = self._resolver.resolve_command_references(script, all_scripts)
return [f"{cmd} {script_args_str}".strip() for cmd in resolved]
return [f"{script} {script_args_str}".strip()]
elif isinstance(script, list):
if all_scripts:
resolved = self._resolver.resolve_list_references(script, all_scripts)
return [f"{cmd} {script_args_str}".strip() for cmd in resolved]
return [f"{cmd} {script_args_str}".strip() for cmd in script]
else:
raise ValueError(f"Invalid script format: {script}")
class HelpCommandHandler:
def __init__(self, command_matcher: CommandMatcher | None = None):
self._matcher = command_matcher or CommandMatcher()
def handle_help(
self,
help_command_name: str | None,
scripts: dict[str, str | list[str]],
script_descriptions: dict[str, str],
parser: "CustomArgumentParser", # type: ignore
) -> None:
if help_command_name:
self._show_command_help(help_command_name, scripts, script_descriptions)
else:
self._show_general_help(parser)
def _show_command_help(self, command_name: str, scripts: dict[str, str | list[str]], script_descriptions: dict[str, str]) -> None:
if command_name not in scripts:
error_text = color_service.bold_red("error")
print(f"{error_text}: unknown command '{color_service.yellow(command_name)}'", file=stderr)
available_commands = list(scripts.keys())
similar = self._matcher.find_similar(command_name, available_commands)
if similar:
tip_text = color_service.yellow("tip")
similar_cmd = color_service.yellow(f"'{similar}'")
print(f"\n {tip_text}: a similar command exists: {similar_cmd}", file=stderr)
exit(1)
description = script_descriptions.get(command_name, "")
command_cmd = color_service.bold_teal(command_name) if preference_manager.supports_color() else command_name
if description:
print(description)
else:
no_desc_text = color_service.yellow("No description provided") if preference_manager.supports_color() else "No description provided"
print(no_desc_text)
print()
print("To add a description, update your pyproject.toml:")
print()
actual_command = scripts.get(command_name, "...")
# Handle both str and list[str] cases
if isinstance(actual_command, list):
actual_command = actual_command[0] if actual_command else "..."
escaped_command = actual_command.replace('"', '\\"').replace('\n', '\\n')
if len(actual_command) > 50 or '\n' in actual_command:
example_cmd = (
color_service.bold_teal(f' {command_name} = {{ command = "...", description = "Your description here" }}')
if preference_manager.supports_color()
else f' {command_name} = {{ command = "...", description = "Your description here" }}'
)
else:
example_cmd = (
color_service.bold_teal(f' {command_name} = {{ command = "{escaped_command}", description = "Your description here" }}')
if preference_manager.supports_color()
else f' {command_name} = {{ command = "{escaped_command}", description = "Your description here" }}'
)
print(example_cmd)
print()
usage_text = color_service.bold_green("Usage:") if preference_manager.supports_color() else "Usage:"
prog_text = color_service.bold_teal("uvtask") if preference_manager.supports_color() else "uvtask"
options_text = color_service.teal("[OPTIONS]") if preference_manager.supports_color() else "[OPTIONS]"
command_arg_text = color_service.teal("[COMMAND]") if preference_manager.supports_color() else "[COMMAND]"
print(f"{usage_text} {prog_text} {command_cmd} {options_text} {command_arg_text}")
print()
help_cmd_text = color_service.bold("uvtask help <command>") if preference_manager.supports_color() else "uvtask help <command>"
print(f"Use `{help_cmd_text}` for more information on a specific command.")
print()
exit(0)
def _show_general_help(self, parser: "CustomArgumentParser") -> None:
original_epilog = parser.epilog
help_cmd_text = color_service.bold("uvtask help <command>")
parser.epilog = f"\n\nUse `{help_cmd_text}` for more information on a specific command."
parser.print_help()
print()
parser.epilog = original_epilog
exit(0)
class CommandExecutorOrchestrator:
def __init__(
self,
executor: CommandExecutor,
verbose_output: "VerboseOutputHandler" | None = None,
):
self._executor = executor
self._verbose = verbose_output or VerboseOutputHandler()
def execute(
self,
command_name: str,
main_commands: list[str],
pre_hooks: list[str],
post_hooks: list[str],
quiet_count: int,
verbose_count: int,
) -> None:
try:
self._verbose.show_execution_info(command_name, main_commands, pre_hooks, post_hooks, verbose_count)
for hook_command in pre_hooks:
exit_code = self._executor.execute(hook_command, quiet_count, verbose_count)
if exit_code != 0:
self._verbose.show_hook_failure("Pre-hook", exit_code, verbose_count)
exit(exit_code)
main_exit_code = 0
for i, main_command in enumerate(main_commands):
if verbose_count > 0 and len(main_commands) > 1:
print(
color_service.bold_green(f"Executing command {i + 1}/{len(main_commands)}"),
file=stderr,
)
exit_code = self._executor.execute(main_command, quiet_count, verbose_count)
if exit_code != 0:
main_exit_code = exit_code
self._verbose.show_command_failure(exit_code, verbose_count)
break
for hook_command in post_hooks:
hook_exit_code = self._executor.execute(hook_command, quiet_count, verbose_count)
if main_exit_code == 0 and hook_exit_code != 0:
main_exit_code = hook_exit_code
self._verbose.show_hook_failure("Post-hook", hook_exit_code, verbose_count)
self._verbose.show_final_exit_code(main_exit_code, verbose_count)
exit(main_exit_code)
except KeyboardInterrupt:
if verbose_count > 0:
print(color_service.bold_red("Interrupted by user"), file=stderr)
exit(130)
class VerboseOutputHandler:
@staticmethod
def show_execution_info(
command_name: str,
main_commands: list[str],
pre_hooks: list[str],
post_hooks: list[str],
verbose_count: int,
) -> None:
if verbose_count > 0:
print(color_service.bold_green(f"Command: {command_name}"), file=stderr)
if pre_hooks:
print(color_service.bold_green(f"Pre-hooks: {len(pre_hooks)}"), file=stderr)
if len(main_commands) > 1:
print(color_service.bold_green(f"Commands to execute: {len(main_commands)}"), file=stderr)
if post_hooks:
print(color_service.bold_green(f"Post-hooks: {len(post_hooks)}"), file=stderr)
print("", file=stderr)
@staticmethod
def show_hook_failure(hook_type: str, exit_code: int, verbose_count: int) -> None:
if verbose_count > 0:
print(color_service.bold_red(f"{hook_type} failed with exit code {exit_code}"), file=stderr)
@staticmethod
def show_command_failure(exit_code: int, verbose_count: int) -> None:
if verbose_count > 0:
print(color_service.bold_red(f"Command failed with exit code {exit_code}"), file=stderr)
@staticmethod
def show_final_exit_code(exit_code: int, verbose_count: int) -> None:
if verbose_count > 0:
final_exit_text = (
color_service.bold_green(f"Final exit code: {exit_code}") if exit_code == 0 else color_service.bold_red(f"Final exit code: {exit_code}")
)
print(final_exit_text, file=stderr)