-
-
Notifications
You must be signed in to change notification settings - Fork 201
Add ability to exclude files and entire folders from built application #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a089c97
1bd5d55
60295d9
9a6733d
afb6e75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ public function handle() | |
$this->info('Minifying application…'); | ||
|
||
$this->cleanUpEnvFile($appPath); | ||
$this->removeIgnoredFilesAndFolders($appPath); | ||
|
||
$compactor = new Php(); | ||
|
||
|
@@ -61,4 +62,50 @@ protected function cleanUpEnvFile(string $appPath): void | |
|
||
file_put_contents($envFile, $envValues); | ||
} | ||
|
||
protected function removeIgnoredFilesAndFolders(string $appPath): void | ||
{ | ||
$this->info('Cleaning up ignored files and folders…'); | ||
|
||
$itemsToRemove = config('nativephp.cleanup_exclude_files', []); | ||
|
||
foreach ($itemsToRemove as $item) { | ||
$fullPath = $appPath.'/'.$item; | ||
|
||
8000 | if (file_exists($fullPath)) { | |
if (is_dir($fullPath)) { | ||
$this->delete_directory_recursive($fullPath); | ||
} else { | ||
array_map('unlink', glob($fullPath)); | ||
} | ||
} else { | ||
foreach (glob($item) as $pathFound) { | ||
unlink($pathFound); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private function delete_directory_recursive($dir) | ||
{ | ||
if (! file_exists($dir)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I'm clear on what you mean by "space". Can you please elaborate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that clean code is the goal but I don't agree that removing the space makes it easier to read. In fact, (and this is strictly my own personal opinion) I believe that the space makes it more readable as it's easy to see that there are two characters "(!" indicating something weird is about to happen, separated by a space, and then the variable name that the weirdness is about to apply to, making it easy for someone reading the code to quickly see that the expression is being negated. If Marcel is against this, I'll revert it, but for now, I'm going to leave it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference, it's being applied via the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I apologize! Thank you for your explain, It is my mistake! |
||
return true; | ||
} | ||
|
||
if (! is_dir($dir)) { | ||
return unlink($dir); | ||
} | ||
|
||
foreach (scandir($dir) as $item) { | ||
if ($item == '.' || $item == '..') { | ||
continue; | ||
} | ||
|
||
if (! $this->delete_directory_recursive($dir.'/'.$item)) { | ||
return false; | ||
} | ||
} | ||
|
||
return rmdir($dir); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
it('will remove laravel.log by default before building', function () { | ||
$logPath = 'resources/app/storage/logs'; | ||
$laravelLog = $logPath.'/laravel.log'; | ||
|
||
// Create a dummy copy of the file | ||
if (! file_exists($logPath)) { | ||
8000 mkdir($logPath, 0755, true); | ||
} | ||
file_put_contents($laravelLog, 'TEST'); | ||
|
||
// Run the test | ||
$this->artisan('native:minify resources/app'); | ||
$this->assertFalse(file_exists($laravelLog)); | ||
|
||
// Clean up after ourselves | ||
if (file_exists($laravelLog)) { | ||
unlink($laravelLog); | ||
} | ||
if (file_exists('resources/app/storage/logs')) { | ||
rmdir('resources/app/storage/logs'); | ||
} | ||
if (file_exists('resources/app/storage')) { | ||
rmdir('resources/app/storage'); | ||
} | ||
removeAppFolder(); | ||
}); | ||
|
||
it('will remove the content folder by default before building', function () { | ||
$contentPath = 'resources/app/content'; | ||
|
||
// Create a dummy copy of the folder | ||
if (! file_exists($contentPath)) { | ||
mkdir($contentPath, 0755, true); | ||
} | ||
|
||
// Run the test | ||
$this->artisan('native:minify resources/app'); | ||
$this->assertFalse(file_exists($contentPath)); | ||
|
||
// Clean up after ourselves | ||
if (file_exists($contentPath)) { | ||
unlink($contentPath); | ||
} | ||
removeAppFolder(); | ||
}); | ||
|
||
it('will remove only files that match a globbed path', function () { | ||
$wildcardPath = 'resources/app/wildcardPath'; | ||
$yes1DeletePath = $wildcardPath.'/YES1.txt'; | ||
$yes2DeletePath = $wildcardPath.'/YES2.txt'; | ||
$noDeletePath = $wildcardPath.'/NO.txt'; | ||
|
||
config()->set('nativephp.cleanup_exclude_files', [$wildcardPath.'/YES*']); | ||
|
||
// Create some dummy files | ||
if (! file_exists($wildcardPath)) { | ||
mkdir($wildcardPath, 0755, true); | ||
} | ||
file_put_contents($yes1DeletePath, 'PLEASE DELETE ME'); | ||
file_put_contents($yes2DeletePath, 'PLEASE DELETE ME TOO'); | ||
file_put_contents($noDeletePath, 'DO NOT DELETE ME'); | ||
|
||
// Run the test | ||
$this->artisan('native:minify resources/app'); | ||
$this->assertFalse(file_exists($yes1DeletePath)); | ||
$this->assertFalse(file_exists($yes2DeletePath)); | ||
$this->assertTrue(file_exists($noDeletePath)); | ||
|
||
// Clean up after ourselves | ||
foreach ([$yes1DeletePath, $yes2DeletePath, $noDeletePath] as $remove) { | ||
if (file_exists($remove)) { | ||
unlink($remove); | ||
} | ||
} | ||
if (file_exists($wildcardPath)) { | ||
rmdir($wildcardPath); | ||
} | ||
removeAppFolder(); | ||
}); | ||
|
||
function removeAppFolder() | ||
{ | ||
if (file_exists('resources/app')) { | ||
rmdir('resources/app'); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.