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 all 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
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->deleteDirectoryRecursive($fullPath);
} else {
array_map('unlink', glob($fullPath));
}
} else {
foreach (glob($item) as $pathFound) {
unlink($pathFound);
}
}
}
}

private function deleteDirectoryRecursive(string $directory): void
{
if (! file_exists($directory)) {
return true;
}

if (! is_dir($directory)) {
return unlink($directory);
}

foreach (scandir($directory) as $item) {
if ($item == '.' || $item == '..') {
continue;
}

if (! $this->deleteDirectoryRecursive($directory.'/'.$item)) {
return false;
}
}

rmdir($directory);
}
}
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)) {
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');
}
}