forked from coenjacobs/mozart
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathReplaceCommand.php
247 lines (191 loc) · 7.4 KB
/
ReplaceCommand.php
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
<?php
/**
* Rename a namespace in files. (in-place renaming)
*
* strauss replace --from "YourCompany\\Project" --to "BrianHenryIE\\MyProject" --paths "includes,my-plugin.php"
*/
namespace BrianHenryIE\Strauss\Console\Commands;
use BrianHenryIE\Strauss\Composer\ComposerPackage;
use BrianHenryIE\Strauss\Composer\Extra\ReplaceConfigInterface;
use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
use BrianHenryIE\Strauss\Files\DiscoveredFiles;
use BrianHenryIE\Strauss\Helpers\FileSystem;
use BrianHenryIE\Strauss\Pipeline\ChangeEnumerator;
use BrianHenryIE\Strauss\Pipeline\FileEnumerator;
use BrianHenryIE\Strauss\Pipeline\FileSymbolScanner;
use BrianHenryIE\Strauss\Pipeline\Licenser;
use BrianHenryIE\Strauss\Pipeline\Prefixer;
use BrianHenryIE\Strauss\Types\DiscoveredSymbols;
use Exception;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
class ReplaceCommand extends Command
{
use LoggerAwareTrait;
/** @var string */
protected string $workingDir;
protected StraussConfig $config;
/** @var Prefixer */
protected Prefixer $replacer;
/** @var ComposerPackage[] */
protected array $flatDependencyTree = [];
/**
* ArrayAccess of \BrianHenryIE\Strauss\File objects indexed by their path relative to the output target directory.
*
* Each object contains the file's relative and absolute paths, the package and autoloaders it came from,
* and flags indicating should it / has it been copied / deleted etc.
*
*/
protected DiscoveredFiles $discoveredFiles;
protected DiscoveredSymbols $discoveredSymbols;
protected Filesystem $filesystem;
/**
* @return void
*/
protected function configure()
{
$this->setName('replace');
$this->setDescription("Rename a namespace in files.");
$this->setHelp('');
$this->addOption(
'from',
null,
InputArgument::OPTIONAL,
'Original namespace'
);
$this->addOption(
'to',
null,
InputArgument::OPTIONAL,
'New namespace'
);
$this->addOption(
'paths',
null,
InputArgument::OPTIONAL,
'Comma separated list of files and directories to update. Default is the current working directory.',
getcwd()
);
// TODO: permissions?
$this->filesystem = new Filesystem(
new \League\Flysystem\Filesystem(new LocalFilesystemAdapter('/'))
);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @see Command::execute()
*
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->setLogger(
new ConsoleLogger(
$output,
[ LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL ]
)
);
$workingDir = getcwd() . DIRECTORY_SEPARATOR;
$this->workingDir = $workingDir;
try {
$config = $this->createConfig($input);
// Pipeline
$this->enumerateFiles($config);
$this->determineChanges($config);
$this->performReplacements($config);
$this->performReplacementsInProjectFiles($config);
$this->addLicenses($config);
} catch (Exception $e) {
$this->logger->error($e->getMessage());
return 1;
}
return Command::SUCCESS;
}
protected function createConfig(InputInterface $input): ReplaceConfigInterface
{
$config = new StraussConfig();
$from = $input->getOption('from');
$to = $input->getOption('to');
// TODO:
$config->setNamespaceReplacementPatterns([$from => $to]);
$paths = explode(',', $input->getOption('paths'));
$config->setUpdateCallSites($paths);
return $config;
}
protected function enumerateFiles(ReplaceConfigInterface $config): void
{
$this->logger->info('Enumerating files...');
$this->discoveredFiles = (new FileEnumerator($this->workingDir, $config, $this->filesystem))->compileFileListForPaths($config->getUpdateCallSites());
}
// 4. Determine namespace and classname changes
protected function determineChanges(ReplaceConfigInterface $config): void
{
$this->logger->info('Determining changes...');
$fileScanner = new FileSymbolScanner(
$config,
$this->filesystem
);
$this->discoveredSymbols = $fileScanner->findInFiles($this->discoveredFiles);
$changeEnumerator = new ChangeEnumerator(
$config,
$this->workingDir,
$this->filesystem
);
$changeEnumerator->determineReplacements($this->discoveredSymbols);
}
// 5. Update namespaces and class names.
// Replace references to updated namespaces and classnames throughout the dependencies.
protected function performReplacements(ReplaceConfigInterface $config): void
{
$this->logger->info('Performing replacements...');
$this->replacer = new Prefixer($config, $this->workingDir, $this->filesystem);
$this->replacer->replaceInFiles($this->discoveredSymbols, $this->discoveredFiles->getFiles());
}
protected function performReplacementsInProjectFiles(ReplaceConfigInterface $config): void
{
$callSitePaths = $config->getUpdateCallSites();
if (empty($callSitePaths)) {
return;
}
$projectReplace = new Prefixer($config, $this->workingDir, $this->filesystem);
$fileEnumerator = new FileEnumerator(
$this->workingDir,
$config,
$this->filesystem
);
$phpFilePaths = $fileEnumerator->compileFileListForPaths($callSitePaths);
// TODO: Warn when a file that was specified is not found (during config validation).
// $this->logger->warning('Expected file not found from project autoload: ' . $absolutePath);
$phpFilesAbsolutePaths = array_map(
fn($file) => $file->getSourcePath(),
$phpFilePaths->getFiles()
);
$projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesAbsolutePaths);
}
protected function addLicenses(ReplaceConfigInterface $config): void
{
$this->logger->info('Adding licenses...');
$username = trim(shell_exec('git config user.name'));
$email = trim(shell_exec('git config user.email'));
if (!empty($username) && !empty($email)) {
// e.g. "Brian Henry <BrianHenryIE@gmail.com>".
$author = $username . ' <' . $email . '>';
} else {
// e.g. "brianhenry".
$author = get_current_user();
}
// TODO: Update to use DiscoveredFiles
$dependencies = $this->flatDependencyTree;
$licenser = new Licenser($config, $this->workingDir, $dependencies, $author, $this->filesystem, $this->logger);
$licenser->copyLicenses();
$modifiedFiles = $this->replacer->getModifiedFiles();
$licenser->addInformationToUpdatedFiles($modifiedFiles);
}
}