|
4 | 4 | import re
|
5 | 5 |
|
6 | 6 | from contextlib import suppress
|
7 |
| -from functools import cached_property |
8 | 7 | from importlib import import_module
|
9 | 8 | from pathlib import Path
|
10 | 9 | from typing import TYPE_CHECKING
|
|
25 | 24 |
|
26 | 25 | if TYPE_CHECKING:
|
27 | 26 | from collections.abc import Callable
|
| 27 | + from typing import Any |
28 | 28 |
|
29 | 29 | from cleo.events.event import Event
|
30 | 30 | from cleo.io.inputs.argv_input import ArgvInput
|
@@ -103,6 +103,8 @@ def __init__(self) -> None:
|
103 | 103 | self._disable_plugins = False
|
104 | 104 | self._disable_cache = False
|
105 | 105 | self._plugins_loaded = False
|
| 106 | + self._working_directory = Path.cwd() |
| 107 | + self._project_directory = self._working_directory |
106 | 108 |
|
107 | 109 | dispatcher = EventDispatcher()
|
108 | 110 | dispatcher.add_listener(COMMAND, self.register_command_loggers)
|
@@ -156,21 +158,6 @@ def _default_definition(self) -> Definition:
|
156 | 158 |
|
157 | 159 | return definition
|
158 | 160 |
|
159 |
| - @cached_property |
160 |
| - def _project_directory(self) -> Path: |
161 |
| - if self._io and self._io.input.option("project"): |
162 |
| - with directory(self._working_directory): |
163 |
| - return Path(self._io.input.option("project")).absolute() |
164 |
| - |
165 |
| - return self._working_directory |
166 |
| - |
167 |
| - @cached_property |
168 |
| - def _working_directory(self) -> Path: |
169 |
| - if self._io and self._io.input.option("directory"): |
170 |
| - return Path(self._io.input.option("directory")).absolute() |
171 |
| - |
172 |
| - return Path.cwd() |
173 |
| - |
174 | 161 | @property
|
175 | 162 | def poetry(self) -> Poetry:
|
176 | 163 | from poetry.factory import Factory
|
@@ -227,16 +214,55 @@ def create_io(
|
227 | 214 | return io
|
228 | 215 |
|
229 | 216 | def _run(self, io: IO) -> int:
|
230 |
| - self._disable_plugins = io.input.parameter_option("--no-plugins") |
231 |
| - self._disable_cache = io.input.has_parameter_option("--no-cache") |
232 |
| - |
233 | 217 | self._load_plugins(io)
|
234 | 218 |
|
235 | 219 | with directory(self._working_directory):
|
236 | 220 | exit_code: int = super()._run(io)
|
237 | 221 |
|
238 | 222 | return exit_code
|
239 | 223 |
|
| 224 | + def _option_get_value(self, io: IO, name: str, default: Any) -> Any: |
| 225 | + option = self.definition.option(name) |
| 226 | + |
| 227 | + if option is None: |
| 228 | + return default |
| 229 | + |
| 230 | + values = [f"--{option.name}"] |
| 231 | + |
| 232 | + if option.shortcut: |
| 233 | + values.append(f"-{option.shortcut}") |
| 234 | + |
| 235 | + if not io.input.has_parameter_option(values): |
| 236 | + return default |
| 237 | + |
| 238 | + if option.is_flag(): |
| 239 | + return True |
| 240 | + |
| 241 | + return io.input.parameter_option(values=values, default=default) |
| 242 | + |
| 243 | + def _configure_custom_application_options(self, io: IO) -> None: |
| 244 | + if io is None: |
| 245 | + # nothing to do here then |
| 246 | + return |
| 247 | + |
| 248 | + self._disable_plugins = self._option_get_value( |
| 249 | + io, "no-plugins", self._disable_plugins |
| 250 | + ) |
| 251 | + self._disable_cache = self._option_get_value( |
| 252 | + io, "no-cache", self._disable_cache |
| 253 | + ) |
| 254 | + self._working_directory = self._project_directory = Path( |
| 255 | + self._option_get_value(io, "directory", Path.cwd()) |
| 256 | + ) |
| 257 | + |
| 258 | + self._project_directory = Path( |
| 259 | + self._option_get_value(io, "project", self._working_directory) |
| 260 | + ) |
| 261 | + |
| 262 | + if self._project_directory != self._working_directory: |
| 263 | + with directory(self._working_directory): |
| 264 | + self._project_directory = self._project_directory.absolute() |
| 265 | + |
240 | 266 | def _configure_io(self, io: IO) -> None:
|
241 | 267 | # We need to check if the command being run
|
242 | 268 | # is the "run" command.
|
@@ -273,6 +299,8 @@ def _configure_io(self, io: IO) -> None:
|
273 | 299 |
|
274 | 300 | super()._configure_io(io)
|
275 | 301 |
|
| 302 | + self._configure_custom_application_options(io) |
| 303 | + |
276 | 304 | def register_command_loggers(
|
277 | 305 | self, event: Event, event_name: str, _: EventDispatcher
|
278 | 306 | ) -> None:
|
|
0 commit comments