/
RemoveIgnoredFiles.php
163 lines (142 loc) · 4.92 KB
/
RemoveIgnoredFiles.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
<?php
namespace Laravel\VaporCli\BuildProcess;
use Illuminate\Support\Str;
use Laravel\VaporCli\Helpers;
use Laravel\VaporCli\Manifest;
use Laravel\VaporCli\Path;
use Symfony\Component\Finder\Finder;
class RemoveIgnoredFiles
{
use ParticipatesInBuildProcess;
/**
* Execute the build process step.
*
* @return void
*/
public function __invoke()
{
Helpers::step('<options=bold>Removing Ignored Files</>');
$this->removeDefaultIgnoredFiles();
$this->removeDefaultIgnoredDirectories();
$this->removeSymfonyTests();
$this->removeUserIgnoredFiles();
}
/**
* Remove the files that are ignored by default.
*
* @return void
*/
protected function removeDefaultIgnoredFiles()
{
$defaultFiles = [
Path::app().'/.env',
Path::app().'/.env.example',
Path::app().'/.phpunit.result.cache',
Path::app().'/package-lock.json',
Path::app().'/phpunit.xml',
Path::app().'/readme.md',
Path::app().'/server.php',
Path::app().'/storage/oauth-private.key',
Path::app().'/storage/oauth-public.key',
Path::app().'/webpack.mix.js',
Path::app().'/yarn.lock',
];
foreach ($defaultFiles as $file) {
if ($this->files->exists($file)) {
$this->files->delete($file);
}
}
if (! $this->files->exists(Path::app().'/database')) {
return;
}
$files = (new Finder())
->in(Path::app().'/database')
->depth('== 0')
->name('*.sqlite');
foreach ($files as $file) {
$this->files->delete($file->getRealPath());
}
}
/**
* Remove the directories that are ignored by default.
*
* @return void
*/
protected function removeDefaultIgnoredDirectories()
{
$defaultDirectories = [
// Path::app().'/database/factories',
// Path::app().'/database/seeds',
Path::app().'/resources/css',
Path::app().'/resources/js',
Path::app().'/resources/less',
Path::app().'/resources/sass',
Path::app().'/resources/scss',
Path::app().'/storage/cache',
Path::app().'/storage/debugbar',
Path::app().'/storage/logs',
Path::app().'/storage/sessions',
Path::app().'/storage/testing',
Path::app().'/vendor/aws/aws-sdk-php/.changes',
Path::app().'/vendor/monolog/monolog/tests',
Path::app().'/vendor/swiftmailer/swiftmailer/doc',
Path::app().'/vendor/swiftmailer/swiftmailer/tests',
];
foreach ($defaultDirectories as $directory) {
if ($this->files->isDirectory($directory)) {
$this->files->deleteDirectory($directory, $preserve = true);
}
}
}
/**
* Remove the tests from the Symfony components.
*
* @return void
*/
protected function removeSymfonyTests()
{
foreach ($this->files->directories(Path::app().'/vendor/symfony') as $component) {
if ($this->files->isDirectory($component.'/Tests')) {
$this->files->deleteDirectory($component.'/Tests', $preserve = false);
}
}
}
/**
* Remove the user ignored files specified in the project manifest.
*
* @return void
*/
protected function removeUserIgnoredFiles()
{
foreach (Manifest::ignoredFiles() as $pattern) {
[$directory, $filePattern] = $this->parsePattern($pattern);
if ($this->files->exists($directory.'/'.$filePattern) && $this->files->isDirectory($directory.'/'.$filePattern)) {
Helpers::step('<comment>Removing Ignored Directory:</comment> '.$filePattern.'/');
$this->files->deleteDirectory($directory.'/'.$filePattern, $preserve = false);
} else {
$files = (new Finder())
->in($directory)
->depth('== 0')
->ignoreDotFiles(false)
->name($filePattern);
foreach ($files as $file) {
Helpers::step('<comment>Removing Ignored File:</comment> '.str_replace(Path::app().'/', '', $file->getRealPath()));
$this->files->delete($file->getRealPath());
}
}
}
}
/**
* Parse the given ignore pattern into a base directory and file pattern.
*
* @param string $pattern
* @return array
*/
protected function parsePattern($pattern)
{
$filePattern = basename(trim($pattern, '/'));
return Str::contains(trim($pattern, '/'), '/')
? [dirname(Path::app().'/'.trim($pattern, '/')), $filePattern]
: [Path::app(), $filePattern];
}
}