Skip to content

Commit 48d7302

Browse files
committed
Improve rename script UX and naming conventions
1 parent 7154643 commit 48d7302

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

bin/rename-plugin.php

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@ protected function configure(): void
4949
->addOption('plugin-name', null, InputOption::VALUE_REQUIRED, 'Plugin name (PascalCase)')
5050
->addOption('description', null, InputOption::VALUE_REQUIRED, 'Plugin description')
5151
->addOption('skip-interaction', null, InputOption::VALUE_NONE, 'Skip interactive mode (useful for automation)')
52-
->addOption('sylius', null, InputOption::VALUE_NONE, 'Use Sylius official plugin naming convention (Sylius\{Name}Plugin)')
5352
;
5453
}
5554

5655
protected function execute(InputInterface $input, OutputInterface $output): int
5756
{
5857
$io = new SymfonyStyle($input, $output);
5958

60-
$io->title('Sylius Plugin Renamer');
61-
6259
$skipInteraction = (bool) $input->getOption('skip-interaction') || getenv('SKIP_INTERACTION') === '1';
6360
$dryRun = (bool) $input->getOption('dry-run');
6461
if ($dryRun) {
@@ -72,24 +69,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7269
}
7370
}
7471

75-
$syliusMode = (bool) $input->getOption('sylius');
72+
$syliusMode = getenv('SYLIUS') === '1';
7673

7774
$names = $this->getPluginInformation($input, $io, $syliusMode, $skipInteraction);
7875
$description = $names['description'];
7976

8077
$io->section('Configuration Summary');
81-
$io->table(
82-
['Property', 'Value'],
83-
[
84-
['Full namespace', "{$names['company']}\\{$names['plugin']}"],
85-
['Full class name', $names['fullClass']],
86-
['Extension class', $names['extensionClass']],
87-
['Package name', $names['package']],
88-
['Database name', $names['db']],
89-
['Config key', $names['configKey']],
90-
['Description', $description],
91-
],
92-
);
78+
$io->newLine();
79+
$io->writeln(' <info>Plugin</info>');
80+
$io->writeln(" Namespace {$names['company']}\\{$names['plugin']}");
81+
$io->newLine();
82+
$io->writeln(' <info>Composer</info>');
83+
$io->writeln(" Package {$names['package']}");
84+
$io->writeln(" Description {$description}");
85+
$io->newLine();
9386

9487
if (!$dryRun && !$skipInteraction) {
9588
if (!$io->confirm('Continue with this configuration?', true)) {
@@ -131,16 +124,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
131124

132125
private function getPluginInformation(InputInterface $input, SymfonyStyle $io, bool $syliusMode, bool $skipInteraction): array
133126
{
134-
$io->section('Plugin Information');
135-
136127
$envCompany = getenv('COMPANY') ?: null;
137128
$envPluginName = getenv('PLUGIN_NAME') ?: null;
138129
$envDescription = getenv('DESCRIPTION') ?: null;
139-
$envSylius = getenv('SYLIUS') === '1';
140-
141-
if ($envSylius) {
142-
$syliusMode = true;
143-
}
144130

145131
$detected = $this->detectFromDirectory();
146132

@@ -149,17 +135,13 @@ private function getPluginInformation(InputInterface $input, SymfonyStyle $io, b
149135
?? $detected['pluginName']
150136
?? null;
151137

152-
$wasAutoDetected = $detected !== null && $envPluginName === null && $input->getOption('plugin-name') === null;
153-
if ($wasAutoDetected) {
154-
$io->note("Auto-detected from directory: {$pluginName}Plugin");
155-
}
156-
157138
if ($pluginName !== null && is_string($pluginName) && !$this->validateName($pluginName)) {
158139
throw new \InvalidArgumentException('Plugin name must be in PascalCase (start with uppercase letter, no spaces or special characters)');
159140
}
160141

142+
$detectedPluginDefault = $detected['pluginName'] ?? null;
161143
if ($pluginName === null || !is_string($pluginName)) {
162-
$question = new Question('Plugin name (PascalCase)');
144+
$question = new Question('Plugin name (PascalCase)', $detectedPluginDefault);
163145
$question->setValidator(function ($answer) {
164146
if (!is_string($answer) || !$this->validateName($answer)) {
165147
throw new \RuntimeException('Plugin name must be in PascalCase');
@@ -171,7 +153,6 @@ private function getPluginInformation(InputInterface $input, SymfonyStyle $io, b
171153
}
172154

173155
if ($syliusMode) {
174-
$io->note('Using Sylius official naming convention: Sylius\{Name}Plugin');
175156
$companyName = 'Sylius';
176157
} else {
177158
$companyName = $envCompany ?? $input->getOption('company') ?? null;
@@ -220,7 +201,7 @@ private function detectFromDirectory(): ?array
220201
{
221202
$dirName = basename(dirname(__DIR__));
222203

223-
if (preg_match('/^(?:[A-Z][a-zA-Z0-9]*)?([A-Z][a-zA-Z0-9]*)Plugin$/', $dirName, $matches)) {
204+
if (preg_match('/^([A-Z][a-zA-Z0-9]*)Plugin$/', $dirName, $matches)) {
224205
return [
225206
'pluginName' => $matches[1],
226207
];
@@ -245,26 +226,31 @@ private function generateNameVariations(string $company, string $pluginName, boo
245226
'fullClass' => $fullClass,
246227
'extensionClass' => $extensionClass,
247228
'package' => 'sylius/' . $pluginKebab . '-plugin',
248-
'db' => 'sylius_' . $pluginSnake,
229+
'db' => 'sylius_' . $pluginSnake . '_plugin',
249230
'configKey' => 'sylius_' . $pluginSnake,
250231
];
251232
}
252233

253-
$fullClass = $company . $plugin;
254-
$extensionClass = $company . $pluginName . 'Extension';
234+
$hasSyliusPrefix = str_starts_with($pluginName, 'Sylius');
235+
$syliusPlugin = $hasSyliusPrefix ? $plugin : 'Sylius' . $plugin;
236+
$syliusPluginName = $hasSyliusPrefix ? $pluginName : 'Sylius' . $pluginName;
237+
238+
$fullClass = $company . $syliusPlugin;
239+
$extensionClass = $company . $syliusPluginName . 'Extension';
255240

256241
$companyKebab = $this->toKebabCase($company);
257-
$fullPluginKebab = $this->toKebabCase($plugin);
242+
$fullPluginKebab = $this->toKebabCase($syliusPlugin);
258243
$companySnake = $this->toSnakeCase($company);
244+
$pluginSnakeForDb = $hasSyliusPrefix ? $pluginSnake : 'sylius_' . $pluginSnake;
259245

260246
return [
261247
'company' => $company,
262-
'plugin' => $plugin,
248+
'plugin' => $syliusPlugin,
263249
'fullClass' => $fullClass,
264250
'extensionClass' => $extensionClass,
265251
'package' => $companyKebab . '/' . $fullPluginKebab,
266-
'db' => $companySnake . '_' . $pluginSnake,
267-
'configKey' => $companySnake . '_' . $pluginSnake,
252+
'db' => $companySnake . '_' . $pluginSnakeForDb . '_plugin',
253+
'configKey' => $companySnake . '_' . $pluginSnakeForDb,
268254
];
269255
}
270256

@@ -314,7 +300,7 @@ private function updateFileContents(SymfonyStyle $io, array $names, bool $dryRun
314300
$io->section('Updating file contents');
315301

316302
$replacements = [
317-
'acme_sylius_example_plugin_%kernel.environment%' => 'sylius_%kernel.environment%',
303+
'acme_sylius_example_plugin_%kernel.environment%' => "{$names['db']}_%kernel.environment%",
318304
'Acme\\SyliusExamplePlugin' => "{$names['company']}\\{$names['plugin']}",
319305
'AcmeSyliusExamplePlugin' => $names['fullClass'],
320306
'AcmeSyliusExampleExtension' => $names['extensionClass'],
@@ -400,6 +386,9 @@ private function updateComposerJson(SymfonyStyle $io, string $packageName, strin
400386
unset($composer['autoload-dev']['psr-4']['Tests\\Acme\\SyliusExamplePlugin\\']);
401387
$composer['autoload-dev']['psr-4']["Tests\\{$names['company']}\\{$names['plugin']}\\"] = ['tests/', 'tests/TestApplication/src/'];
402388

389+
unset($composer['scripts']['post-create-project-cmd']);
390+
unset($composer['scripts']['post-root-package-install']);
391+
403392
if ($dryRun) {
404393
$io->writeln('[DRY RUN] Would update composer.json with:');
405394
$io->writeln(' - name: ' . $packageName);

0 commit comments

Comments
 (0)