8000 Add ability to exclude files and entire folders from built application by nexxai · Pull Request #165 · NativePHP/laravel · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/nativephp.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
'NATIVEPHP_APPLE_TEAM_ID',
],

/**
* A list of files and folders that should be removed from the
* final app before it is bundled for production.
* You may use glob / wildcard patterns here.
*/
'cleanup_exclude_files' => [
'content',
'storage/app/framework/{sessions,testing,cache}',
'storage/logs/laravel.log',
],

/**
* The NativePHP updater configuration.
*/
Expand Down
47 changes: 47 additions & 0 deletions src/Commands/MinifyApplicationCommand.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function handle()
$this->info('Minifying application…');

$this->cleanUpEnvFile($appPath);
$this->removeIgnoredFilesAndFolders($appPath);

$compactor = new Php();

Expand Down Expand Up @@ -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;

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)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author
@nexxai nexxai Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, it's being applied via the not_operator_with_successor_space PHP-CS-Fixer rule and also referenced in Spatie's Laravel code style guide

Copy link
@nilBora nilBora Aug 18, 2023

Choose a reason for hiding this comment

The 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);
}
}
88 changes: 88 additions & 0 deletions tests/Command/IgnoreFilesAndFoldersTest.php
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');
}
}
0