Skip to content

Commit 5ba1fa3

Browse files
committed
removing files that exist and shouldn't anymore
1 parent ef696a5 commit 5ba1fa3

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Configuration
4848
| path | Path where environments will be stored | environments/ |
4949
| files | Files that will be stored for each environment | [ '.env', 'phpunit.xml', 'public/.htaccess', ] |
5050
| clear_directory_when_overwriting | If set to true, overwriting environment will be cleared out before putting new files there | false |
51+
| keep_existing_file_when_missing | If set to true, existing file in base directory will be not deleted when this file is missing in environment set to active | false |
5152

5253

5354
Usage
@@ -128,6 +129,9 @@ vendor/bin/phpunit
128129
Changelog
129130
---------
130131

132+
2.1.0
133+
* Removing files that are exist in base folder but not exist in environment being set to active
134+
131135
2.0.0
132136
* Support for all Laravel 5.* versions (to-date)
133137

config/laravel-environments.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@
2323
* Potential danger of delete some files that are stored with purpose.
2424
*/
2525
'clear_directory_when_overwriting' => false,
26+
27+
/*
28+
* Keep existing file in base directory when this file is
29+
* not existing in environment that being set as active.
30+
*/
31+
'keep_existing_file_when_missing' => false,
2632
];

src/Services/EnvironmentManagerService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ public function setActive($name)
177177

178178
$this->activateFiles();
179179

180+
if (! $this->getConfig('keep_existing_file_when_missing'))
181+
$this->deleteNotExistingFiles();
182+
180183
return true;
181184
}
182185

@@ -203,4 +206,18 @@ protected function stripEnvPath($file)
203206
{
204207
return str_replace($this->path, '', $file);
205208
}
209+
210+
protected function deleteNotExistingFiles()
211+
{
212+
$files = $this->getConfig('files');
213+
214+
collect($files)
215+
->each(function ($file) {
216+
if (File::exists($this->path . $file)) {
217+
return;
218+
}
219+
220+
File::delete(base_path($file));
221+
});
222+
}
206223
}

tests/unit/SetCommandTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,84 @@ public function sequentional_environment_activation()
137137
$this->assertEquals('env staging content', File::get($file));
138138
});
139139
}
140+
141+
/** @test */
142+
public function set_env_without_one_of_files_and_delete_this_missing_file()
143+
{
144+
config([
145+
'laravel-environments.files' => [
146+
'.env',
147+
'missing.php',
148+
],
149+
]);
150+
151+
File::put($this->getBaseDirectory('.env'), 'env content');
152+
File::put($this->getBaseDirectory('missing.php'), 'missing content');
153+
154+
tap($this->getBaseDirectory('missing.php'), function ($file) {
155+
$this->assertTrue(File::exists($file));
156+
});
157+
158+
$this->executeCreate([
159+
'name' => 'local5',
160+
]);
161+
162+
tap($this->getTempDirectory('local5').'missing.php', function ($file) {
163+
File::delete($file);
164+
$this->assertFalse(File::exists($file));
165+
});
166+
167+
$this->executeSet([
168+
'name' => 'local5',
169+
]);
170+
171+
tap($this->getBaseDirectory('missing.php'), function ($file) {
172+
$this->assertFalse(File::exists($file));
173+
});
174+
175+
tap($this->getBaseDirectory('.env'), function ($file) {
176+
$this->assertTrue(File::exists($file));
177+
$this->assertEquals('env content', File::get($file));
178+
});
179+
}
180+
181+
/** @test */
182+
public function set_env_without_one_of_files_and_not_delete_this_missing_file_because_of_config()
183+
{
184+
config([
185+
'laravel-environments.files' => [
186+
'.env',
187+
'missing.php',
188+
],
189+
]);
190+
191+
config([
192+
'laravel-environments.keep_existing_file_when_missing' => true,
193+
]);
194+
195+
File::put($this->getBaseDirectory('.env'), 'env content');
196+
File::put($this->getBaseDirectory('missing.php'), 'missing content');
197+
198+
tap($this->getBaseDirectory('missing.php'), function ($file) {
199+
$this->assertTrue(File::exists($file));
200+
});
201+
202+
$this->executeCreate([
203+
'name' => 'local6',
204+
]);
205+
206+
tap($this->getTempDirectory('local6').'missing.php', function ($file) {
207+
File::delete($file);
208+
$this->assertFalse(File::exists($file));
209+
});
210+
211+
$this->executeSet([
212+
'name' => 'local6',
213+
]);
214+
215+
tap($this->getBaseDirectory('missing.php'), function ($file) {
216+
$this->assertTrue(File::exists($file));
217+
$this->assertEquals('missing content', File::get($file));
218+
});
219+
}
140220
}

0 commit comments

Comments
 (0)