1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Administration\Command; |
4
|
|
|
|
5
|
|
|
use Shopware\Administration\Administration; |
6
|
|
|
use Shopware\Core\Framework\Log\Package; |
7
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
|
|
|
9
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
13
|
|
|
use Symfony\Component\Finder\Finder; |
|
|
|
|
14
|
|
|
|
15
|
|
|
#[AsCommand( |
16
|
|
|
name: 'administration:delete-files-after-build', |
17
|
|
|
description: 'Deletes all uneccessary files of the administration after the build process.', |
18
|
|
|
)] |
19
|
|
|
#[Package('admin')] |
20
|
|
|
class DeleteAdminFilesAfterBuildCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected function configure(): void |
26
|
|
|
{ |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
30
|
|
|
{ |
31
|
|
|
$helper = $this->getHelper('question'); |
32
|
|
|
|
33
|
|
|
$question = new ConfirmationQuestion('This will delete all files necessary to build the administration. Do you want to continue? (y/n) ', false); |
34
|
|
|
|
35
|
|
|
if (!$helper->ask($input, $output, $question)) { |
36
|
|
|
$output->writeln('Command aborted!'); |
37
|
|
|
|
38
|
|
|
return 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$adminDir = \dirname((string) (new \ReflectionClass(Administration::class))->getFileName()); |
42
|
|
|
$output->writeln('Deleting uneccessary files of the administration after the build process...'); |
43
|
|
|
$progressBar = new ProgressBar($output, 100); |
44
|
|
|
|
45
|
|
|
$finder = new Finder(); |
46
|
|
|
|
47
|
|
|
// Find all files in Administration/Resources/app/administration/src/module except for de-DE.json and en-GB.json |
48
|
|
|
$finder->in($adminDir . '/Resources/app/administration/src/module') |
49
|
|
|
->notName('de-DE.json') |
50
|
|
|
->notName('en-GB.json') |
51
|
|
|
->files(); |
52
|
|
|
|
53
|
|
|
foreach ($finder as $file) { |
54
|
|
|
unlink($file->getRealPath()); |
55
|
|
|
} |
56
|
|
|
$progressBar->advance(25); |
57
|
|
|
|
58
|
|
|
$this->deleteEmptyDirectories($adminDir . '/Resources/app/administration/src/module'); |
59
|
|
|
$progressBar->advance(25); |
60
|
|
|
|
61
|
|
|
// Find all the following directories and files and delete them |
62
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/adapter'); |
63
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/assets'); |
64
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/asyncComponent'); |
65
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/component'); |
66
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/decorator'); |
67
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/directive'); |
68
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/filter'); |
69
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/init'); |
70
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/init-post'); |
71
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/init-pre'); |
72
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/mixin'); |
73
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/plugin'); |
74
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/route'); |
75
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/service'); |
76
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/app/state'); |
77
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/core'); |
78
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/meta'); |
79
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/src/scripts'); |
80
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/patches'); |
81
|
|
|
unlink($adminDir . '/Resources/app/administration/package-lock.json'); |
82
|
|
|
$progressBar->advance(25); |
83
|
|
|
|
84
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/static'); |
85
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/build'); |
86
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/scripts'); |
87
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/eslint-rules'); |
88
|
|
|
$this->removeDirectory($adminDir . '/Resources/app/administration/test'); |
89
|
|
|
$progressBar->advance(25); |
90
|
|
|
$progressBar->finish(); |
91
|
|
|
|
92
|
|
|
$output->writeln(''); |
93
|
|
|
$output->writeln('All uneccessary files of the administration after the build process have been deleted.'); |
94
|
|
|
|
95
|
|
|
return 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function deleteEmptyDirectories(string $dir): void |
99
|
|
|
{ |
100
|
|
|
if (!is_dir($dir)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$files = scandir($dir); |
105
|
|
|
if (!$files) { |
|
|
|
|
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
foreach ($files as $file) { |
110
|
|
|
if ($file === '.' || $file === '..') { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$path = $dir . '/' . $file; |
115
|
|
|
if (is_dir($path)) { |
116
|
|
|
$this->deleteEmptyDirectories($path); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (\count(scandir($dir) ?: []) === 2) { |
121
|
|
|
rmdir($dir); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function removeDirectory(string $dir): void |
126
|
|
|
{ |
127
|
|
|
if (!is_dir($dir) || str_contains('/snippet', $dir)) { |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$files = scandir($dir); |
132
|
|
|
if (!$files) { |
|
|
|
|
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($files as $file) { |
137
|
|
|
if ($file === '.' || $file === '..') { |
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$path = $dir . '/' . $file; |
142
|
|
|
if (is_dir($path)) { |
143
|
|
|
$this->removeDirectory($path); |
144
|
|
|
} else { |
145
|
|
|
unlink($path); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
rmdir($dir); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths