-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
init
executable file
·98 lines (80 loc) · 3.97 KB
/
init
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
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
(new Application('init', '1.0.0'))
->register('init')
->setCode(function(InputInterface $input, OutputInterface $output) {
$questionHelper = new QuestionHelper();
$finder = new Finder();
$vendor = $questionHelper->ask($input, $output, new Question('What is your plugin namespace (usually this is your organization name)? '));
$q = new Question('What is your plugin name? ');
$q->setValidator(static function (mixed $answer): string {
if (!is_string($answer)) {
throw new \RuntimeException(
'Please enter the plugin name.'
);
}
return $answer;
});
$pluginName = $questionHelper->ask($input, $output, $q);
if (str_ends_with($pluginName, 'Plugin')) {
$pluginName = substr($pluginName, 0, -6);
}
$pluginNameWithSuffix = $pluginName . 'Plugin';
$yamlNamespace = Container::underscore($vendor) . '_' . Container::underscore($pluginName);
$composerName = str_replace('_', '-', Container::underscore($vendor)) . '/' . str_replace('_', '-', Container::underscore($pluginNameWithSuffix));
$finder
->in('.')
->exclude(['vendor', 'node_modules', '.github', '.idea'])
->ignoreVCSIgnored(true)
->ignoreVCS(true)
->contains('/acme/i')
->ignoreDotFiles(false)
->notName('init') // this file
;
foreach ($finder as $file) {
replaceInFile($file->getPathname(), [
'acme_sylius_example' => $yamlNamespace,
'Acme\\SyliusExamplePlugin' => $vendor . '\\' . $pluginNameWithSuffix,
'AcmeSyliusExamplePlugin' => $vendor.$pluginNameWithSuffix,
'AcmeSyliusExample' => $vendor.$pluginName,
'Acme' => $vendor,
'SyliusExamplePlugin' => $pluginNameWithSuffix,
]);
}
replaceInFile('composer.json', [
'setono/sylius-plugin-skeleton' => $composerName,
'SyliusExamplePlugin' => $pluginNameWithSuffix,
]);
replaceInFile('README.md', [
'Setono Sylius Plugin Skeleton' => $vendor . ' ' . $pluginNameWithSuffix,
'Setono/SyliusPluginSkeleton' => $vendor . '/' . $pluginNameWithSuffix,
'setono/sylius-plugin-skeleton' => $composerName,
]);
rename('src/DependencyInjection/AcmeSyliusExampleExtension.php', sprintf('src/DependencyInjection/%s%sExtension.php', $vendor, $pluginName));
rename('src/AcmeSyliusExamplePlugin.php', sprintf('src/%s%s.php', $vendor, $pluginNameWithSuffix));
rename('tests/DependencyInjection/AcmeSyliusExampleExtensionTest.php', sprintf('tests/DependencyInjection/%s%sExtensionTest.php', $vendor, $pluginName));
rename('tests/Application/config/packages/acme_sylius_example.yaml', sprintf('tests/Application/config/packages/%s.yaml', $yamlNamespace));
rename('tests/Application/config/routes/acme_sylius_example.yaml', sprintf('tests/Application/config/routes/%s.yaml', $yamlNamespace));
$process = new Process(['composer', 'dump-autoload']);
$process->run();
})
->getApplication()
->setDefaultCommand('init', true) // Single command application
->run()
;
function replaceInFile(string $file, array $replacements): void
{
$search = array_keys($replacements);
$replace = array_values($replacements);
$data = file_get_contents($file);
file_put_contents($file, str_replace($search, $replace, $data));
}