Skip to content

Commit 7b837c7

Browse files
authored
Merge pull request #165 from nexxai/main
Add ability to exclude files and entire folders from built application
2 parents a659790 + afb6e75 commit 7b837c7

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

config/nativephp.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@
5454
'NATIVEPHP_APPLE_TEAM_ID',
5555
],
5656

57+
/**
58+
* A list of files and folders that should be removed from the
59+
* final app before it is bundled for production.
60+
* You may use glob / wildcard patterns here.
61+
*/
62+
'cleanup_exclude_files' => [
63+
'content',
64+
'storage/app/framework/{sessions,testing,cache}',
65+
'storage/logs/laravel.log',
66+
],
67+
5768
/**
5869
* The NativePHP updater configuration.
5970
*/

src/Commands/MinifyApplicationCommand.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function handle()
2424
$this->info('Minifying application…');
2525

2626
$this->cleanUpEnvFile($appPath);
27+
$this->removeIgnoredFilesAndFolders($appPath);
2728

2829
$compactor = new Php();
2930

@@ -61,4 +62,50 @@ protected function cleanUpEnvFile(string $appPath): void
6162

6263
file_put_contents($envFile, $envValues);
6364
}
65+
66+
protected function removeIgnoredFilesAndFolders(string $appPath): void
67+
{
68+
$this->info('Cleaning up ignored files and folders…');
69+
70+
$itemsToRemove = config('nativephp.cleanup_exclude_files', []);
71+
72+
foreach ($itemsToRemove as $item) {
73+
$fullPath = $appPath.'/'.$item;
74+
75+
if (file_exists($fullPath)) {
76+
if (is_dir($fullPath)) {
77+
$this->deleteDirectoryRecursive($fullPath);
78+
} else {
79+
array_map('unlink', glob($fullPath));
80+
}
81+
} else {
82+
foreach (glob($item) as $pathFound) {
83+
unlink($pathFound);
84+
}
85+
}
86+
}
87+
}
88+
89+
private function deleteDirectoryRecursive(string $directory): void
90+
{
91+
if (! file_exists($directory)) {
92+
return true;
93+
}
94+
95+
if (! is_dir($directory)) {
96+
return unlink($directory);
97+
}
98+
99+
foreach (scandir($directory) as $item) {
100+
if ($item == '.' || $item == '..') {
101+
continue;
102+
}
103+
104+
if (! $this->deleteDirectoryRecursive($directory.'/'.$item)) {
105+
return false;
106+
}
107+
}
108+
109+
rmdir($directory);
110+
}
64111
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
it('will remove laravel.log by default before building', function () {
4+
$logPath = 'resources/app/storage/logs';
5+
$laravelLog = $logPath.'/laravel.log';
6+
7+
// Create a dummy copy of the file
8+
if (! file_exists($logPath)) {
9+
mkdir($logPath, 0755, true);
10+
}
11+
file_put_contents($laravelLog, 'TEST');
12+
13+
// Run the test
14+
$this->artisan('native:minify resources/app');
15+
$this->assertFalse(file_exists($laravelLog));
16+
17+
// Clean up after ourselves
18+
if (file_exists($laravelLog)) {
19+
unlink($laravelLog);
20+
}
21+
if (file_exists('resources/app/storage/logs')) {
22+
rmdir('resources/app/storage/logs');
23+
}
24+
if (file_exists('resources/app/storage')) {
25+
rmdir('resources/app/storage');
26+
}
27+
removeAppFolder();
28+
});
29+
30+
it('will remove the content folder by default before building', function () {
31+
$contentPath = 'resources/app/content';
32+
33+
// Create a dummy copy of the folder
34+
if (! file_exists($contentPath)) {
35+
mkdir($contentPath, 0755, true);
36+
}
37+
38+
// Run the test
39+
$this->artisan('native:minify resources/app');
40+
$this->assertFalse(file_exists($contentPath));
41+
42+
// Clean up after ourselves
43+
if (file_exists($contentPath)) {
44+
unlink($contentPath);
45+
}
46+
removeAppFolder();
47+
});
48+
49+
it('will remove only files that match a globbed path', function () {
50+
$wildcardPath = 'resources/app/wildcardPath';
51+
$yes1DeletePath = $wildcardPath.'/YES1.txt';
52+
$yes2DeletePath = $wildcardPath.'/YES2.txt';
53+
$noDeletePath = $wildcardPath.'/NO.txt';
54+
55+
config()->set('nativephp.cleanup_exclude_files', [$wildcardPath.'/YES*']);
56+
57+
// Create some dummy files
58+
if (! file_exists($wildcardPath)) {
59+
mkdir($wildcardPath, 0755, true);
60+
}
61+
file_put_contents($yes1DeletePath, 'PLEASE DELETE ME');
62+
file_put_contents($yes2DeletePath, 'PLEASE DELETE ME TOO');
63+
file_put_contents($noDeletePath, 'DO NOT DELETE ME');
64+
65+
// Run the test
66+
$this->artisan('native:minify resources/app');
67+
$this->assertFalse(file_exists($yes1DeletePath));
68+
$this->assertFalse(file_exists($yes2DeletePath));
69+
$this->assertTrue(file_exists($noDeletePath));
70+
71+
// Clean up after ourselves
72+
foreach ([$yes1DeletePath, $yes2DeletePath, $noDeletePath] as $remove) {
73+
if (file_exists($remove)) {
74+
unlink($remove);
75+
}
76+
}
77+
if (file_exists($wildcardPath)) {
78+
rmdir($wildcardPath);
79+
}
80+
removeAppFolder();
81+
});
82+
83+
function removeAppFolder()
84+
{
85+
if (file_exists('resources/app')) {
86+
rmdir('resources/app');
87+
}
88+
}

0 commit comments

Comments
 (0)