Repository navigation
Expand file tree
/
Copy pathDevTools.php
More file actions
295 lines (252 loc) · 10.6 KB
/
Copy pathDevTools.php
File metadata and controls
295 lines (252 loc) · 10.6 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Console;
use FastForward\DevTools\Console\Command\SelfUpdateCommand;
use FastForward\DevTools\Environment\EnvironmentInterface;
use FastForward\DevTools\Environment\RuntimeEnvironmentInterface;
use FastForward\DevTools\Path\ManagedWorkspace;
use FastForward\DevTools\SelfUpdate\SelfUpdateRunnerInterface;
use FastForward\DevTools\SelfUpdate\SelfUpdateScopeResolverInterface;
use FastForward\DevTools\SelfUpdate\VersionCheckNotifierInterface;
use FastForward\DevTools\SelfUpdate\WorkingDirectorySwitcherInterface;
use Composer\InstalledVersions;
use Override;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use function Safe\putenv;
/**
* Wraps the fast-forward console tooling suite conceptually as an isolated application instance.
* Extending the base application, it MUST provide default command injections safely.
*/
final class DevTools extends Application
{
private const string PACKAGE = 'fast-forward/dev-tools';
private const string VERSION_UNKNOWN = '0.0.0';
private const string LOGO = <<<'LOGO'
____ _____ _
| _ \ _____ _|_ _|__ ___ | |___
| | | |/ _ \ \ / / | |/ _ \ / _ \| / __|
| |_| | __/\ V / | | (_) | (_) | \__ \
|____/ \___| \_/ |_|\___/ \___/|_|___/
========================================
LOGO;
/**
* Commands that require raw output and therefore must not render the logo.
*
* @var list<string>
*/
private const array RAW_OUTPUT_COMMANDS = ['changelog:next-version', 'changelog:show'];
/**
* Initializes the DevTools global context and dependency graph.
*
* The method MUST define default configurations and MAY accept an explicit command provider.
* It SHALL instruct the runner to treat the `standards` command generically as its default endpoint.
*
* @param CommandLoaderInterface $commandLoader the command loader responsible for providing command instances
* @param WorkingDirectorySwitcherInterface $workingDirectorySwitcher switches the process working directory
* @param VersionCheckNotifierInterface $versionCheckNotifier emits non-blocking version freshness warnings
* @param SelfUpdateRunnerInterface $selfUpdateRunner runs explicit or automatic self-update flows
* @param SelfUpdateScopeResolverInterface $selfUpdateScopeResolver resolves whether the active binary is global
* @param EnvironmentInterface $environment reads environment flags for optional auto-update behavior
* @param RuntimeEnvironmentInterface $runtimeEnvironment resolves runtime environment capabilities
*/
public function __construct(
CommandLoaderInterface $commandLoader,
private readonly WorkingDirectorySwitcherInterface $workingDirectorySwitcher,
private readonly VersionCheckNotifierInterface $versionCheckNotifier,
private readonly SelfUpdateRunnerInterface $selfUpdateRunner,
private readonly SelfUpdateScopeResolverInterface $selfUpdateScopeResolver,
private readonly EnvironmentInterface $environment,
private readonly RuntimeEnvironmentInterface $runtimeEnvironment,
) {
parent::__construct('Fast Forward Dev Tools', $this->resolveVersion());
$this->setDefaultCommand('dev-tools:standards');
$this->setCommandLoader($commandLoader);
}
/**
* Resolves the running DevTools version for command metadata.
*
* @return string the current package version or a safe fallback
*/
private function resolveVersion(): string
{
return InstalledVersions::getPrettyVersion(self::PACKAGE)
?? InstalledVersions::getVersion(self::PACKAGE)
?? self::VERSION_UNKNOWN;
}
/**
* Returns the application-level input definition with DevTools runtime options.
*
* @return InputDefinition the global application input definition
*/
#[Override]
protected function getDefaultInputDefinition(): InputDefinition
{
$definition = parent::getDefaultInputDefinition();
$definition->addOption(new InputOption(
name: 'working-dir',
shortcut: 'd',
mode: InputOption::VALUE_REQUIRED,
description: 'Run DevTools as if it was started in the given directory.',
));
$definition->addOption(new InputOption(
name: 'auto-update',
mode: InputOption::VALUE_NONE,
description: 'Update fast-forward/dev-tools before running the requested command.',
));
$definition->addOption(new InputOption(
name: 'workspace-dir',
shortcut: 'w',
mode: InputOption::VALUE_REQUIRED,
description: 'Store generated DevTools artifacts in the given directory.',
));
$definition->addOption(new InputOption(
name: 'no-logo',
mode: InputOption::VALUE_NONE,
description: 'Hide the startup ASCII logo.',
));
return $definition;
}
/**
* Runs the application after applying global runtime options.
*
* @param InputInterface $input the application input
* @param OutputInterface $output the application output
*
* @return int the application status code
*/
#[Override]
public function doRun(InputInterface $input, OutputInterface $output): int
{
if ($this->shouldRenderLogo($input)) {
$output->writeln(self::LOGO);
}
try {
$this->workingDirectorySwitcher->switchTo($this->getWorkingDirectoryOption($input));
$this->configureWorkspaceDirectory($input);
} catch (Throwable $throwable) {
$output->writeln(\sprintf('<error>%s</error>', $throwable->getMessage()));
return Command::FAILURE;
}
if ($this->shouldRenderLogo($input) && ! $this->isSelfUpdateCommand($input)) {
$this->runAutoUpdateWhenRequested($input, $output);
$this->versionCheckNotifier->notify($output);
}
return parent::doRun($input, $output);
}
/**
* Resolves the raw working-directory option before command parsing.
*
* @param InputInterface $input the application input
*/
private function getWorkingDirectoryOption(InputInterface $input): ?string
{
$workingDirectory = $input->getParameterOption(['--working-dir', '-d'], null, true);
return \is_string($workingDirectory) ? $workingDirectory : null;
}
/**
* Applies the configured workspace directory before resolving command defaults.
*
* @param InputInterface $input the application input
*/
private function configureWorkspaceDirectory(InputInterface $input): void
{
$workspaceDirectory = $input->getParameterOption('--workspace-dir', null, true);
if (! \is_string($workspaceDirectory) || '' === $workspaceDirectory) {
return;
}
putenv(ManagedWorkspace::ENV_WORKSPACE_DIR . '=' . $workspaceDirectory);
}
/**
* Runs an explicit automatic update without letting failures block the requested command.
*
* @param InputInterface $input the application input
* @param OutputInterface $output the application output
*/
private function runAutoUpdateWhenRequested(InputInterface $input, OutputInterface $output): void
{
$autoUpdateMode = $this->environment->get('FAST_FORWARD_AUTO_UPDATE', '');
if (! $input->hasParameterOption('--auto-update', true) && ! $this->isTruthyAutoUpdateMode($autoUpdateMode)) {
return;
}
try {
$global = $this->selfUpdateScopeResolver->isGlobalInstallation();
$statusCode = $this->selfUpdateRunner->update($global, $output);
} catch (Throwable) {
$output->writeln('<comment>DevTools auto-update failed; continuing with the requested command.</comment>');
return;
}
if (Command::SUCCESS !== $statusCode) {
$output->writeln('<comment>DevTools auto-update failed; continuing with the requested command.</comment>');
}
}
/**
* Determines whether the startup logo should be rendered for this invocation.
*
* @param InputInterface $input the application input
*/
private function shouldRenderLogo(InputInterface $input): bool
{
if ($this->runtimeEnvironment->isAgentPresent()) {
return false;
}
if ((bool) $input->getParameterOption('--no-logo', null, true)) {
return false;
}
if ($input->hasParameterOption('--json', true) || $input->hasParameterOption('--pretty-json', true)) {
return false;
}
return ! $this->isRawOutputCommand($input);
}
/**
* Checks whether the current command is designed for raw output mode.
*
* @param InputInterface $input the application input
*/
private function isRawOutputCommand(InputInterface $input): bool
{
$commandName = $input->getFirstArgument();
if (! \is_string($commandName)) {
return false;
}
return \in_array($commandName, self::RAW_OUTPUT_COMMANDS, true);
}
/**
* Detects whether the current invocation targets the self-update command.
*
* @param InputInterface $input the application input
*/
private function isSelfUpdateCommand(InputInterface $input): bool
{
return \in_array($input->getFirstArgument(), SelfUpdateCommand::getCommandNames(), true);
}
/**
* Interprets environment values that enable auto-update.
*
* @param string|null $mode the FAST_FORWARD_AUTO_UPDATE value
*/
private function isTruthyAutoUpdateMode(?string $mode): bool
{
return null !== $mode && \in_array(strtolower($mode), ['1', 'true', 'yes', 'on'], true);
}
}