From df78d7b25b17e23ed2be015a90ec92288a25da86 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Mon, 13 Jan 2025 22:56:17 +0100 Subject: [PATCH 1/6] update cleanup files defaults --- config/nativephp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/nativephp.php b/config/nativephp.php index 5c6e7a8f..baac1ae7 100644 --- a/config/nativephp.php +++ b/config/nativephp.php @@ -61,8 +61,8 @@ */ 'cleanup_exclude_files' => [ 'content', - 'storage/app/framework/{sessions,testing,cache}', - 'storage/logs/laravel.log', + 'vendor/bin', + 'node_modules', ], /** From b2d709160d3331d64574924e89b9c5d567b65cba Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Tue, 14 Jan 2025 18:29:24 +0100 Subject: [PATCH 2/6] add default cleanup_exclude_files to internal config --- config/nativephp-internal.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/config/nativephp-internal.php b/config/nativephp-internal.php index 4210df92..26774dc2 100644 --- a/config/nativephp-internal.php +++ b/config/nativephp-internal.php @@ -29,4 +29,37 @@ * The URL to the NativePHP API. */ 'api_url' => env('NATIVEPHP_API_URL', 'http://localhost:4000/api/'), + + /** + * The default list of files to exclude from the build + */ + 'cleanup_exclude_files' => [ + // .git and dev directories + '.git', + 'dist', + 'docker', + 'packages', + '**/.github', + + // Potentially containing sensitive info + 'database/*.sqlite', + 'database/*.sqlite-shm', + 'database/*.sqlite-wal', + + 'storage/framework/sessions/*', + 'storage/framework/testing/*', + 'storage/framework/cache/*', + 'storage/framework/views/*', + 'storage/logs/*', + + // Only needed for local testing + 'vendor/nativephp/electron/resources', + 'vendor/nativephp/electron/vendor', + 'vendor/nativephp/electron/bin', + 'vendor/nativephp/laravel/vendor', + 'vendor/nativephp/php-bin', + + // Also deleted in PrunesVendorDirectory after fresh composer install + 'vendor/bin' + ] ]; From b848f60cc917c08b290d1e362d16da7d0ac4627d Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Tue, 14 Jan 2025 18:29:55 +0100 Subject: [PATCH 3/6] remove vendor/bin & add */tests to exclude list --- config/nativephp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/nativephp.php b/config/nativephp.php index baac1ae7..93642061 100644 --- a/config/nativephp.php +++ b/config/nativephp.php @@ -61,8 +61,8 @@ */ 'cleanup_exclude_files' => [ 'content', - 'vendor/bin', 'node_modules', + '*/tests' ], /** From e1e45ff9432028a08f646edc9580a0812c876e56 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 15 Jan 2025 18:49:13 +0100 Subject: [PATCH 4/6] remove MinifyApplication command --- src/Commands/MinifyApplicationCommand.php | 111 ---------------------- src/NativeServiceProvider.php | 2 - 2 files changed, 113 deletions(-) delete mode 100644 src/Commands/MinifyApplicationCommand.php diff --git a/src/Commands/MinifyApplicationCommand.php b/src/Commands/MinifyApplicationCommand.php deleted file mode 100644 index 37d8378b..00000000 --- a/src/Commands/MinifyApplicationCommand.php +++ /dev/null @@ -1,111 +0,0 @@ -argument('app')); - - if (! is_dir($appPath)) { - $this->error('The app path is not a directory'); - - return; - } - - $this->info('Minifying application…'); - - $this->cleanUpEnvFile($appPath); - $this->removeIgnoredFilesAndFolders($appPath); - - $compactor = new Php; - - $phpFiles = Finder::create() - ->files() - ->name('*.php') - ->in($appPath); - - foreach ($phpFiles as $phpFile) { - $minifiedContent = $compactor->compact($phpFile->getRealPath(), $phpFile->getContents()); - file_put_contents($phpFile->getRealPath(), $minifiedContent); - } - } - - protected function cleanUpEnvFile(string $appPath): void - { - $envFile = $appPath.'/.env'; - - if (! file_exists($envFile)) { - return; - } - - $this->info('Cleaning up .env file…'); - - $cleanUpKeys = config('nativephp.cleanup_env_keys', []); - - $envContent = file_get_contents($envFile); - $envValues = collect(explode("\n", $envContent)) - ->filter(function (string $line) use ($cleanUpKeys) { - $key = Str::before($line, '='); - - return ! Str::is($cleanUpKeys, $key); - }) - ->join("\n"); - - 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): bool - { - 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; - } - } - - return rmdir($directory); - } -} diff --git a/src/NativeServiceProvider.php b/src/NativeServiceProvider.php index 6ca8d626..b5b32252 100644 --- a/src/NativeServiceProvider.php +++ b/src/NativeServiceProvider.php @@ -12,7 +12,6 @@ use Native\Laravel\Commands\LoadPHPConfigurationCommand; use Native\Laravel\Commands\LoadStartupConfigurationCommand; use Native\Laravel\Commands\MigrateCommand; -use Native\Laravel\Commands\MinifyApplicationCommand; use Native\Laravel\Commands\SeedDatabaseCommand; use Native\Laravel\Contracts\ChildProcess as ChildProcessContract; use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract; @@ -39,7 +38,6 @@ public function configurePackage(Package $package): void MigrateCommand::class, FreshCommand::class, SeedDatabaseCommand::class, - MinifyApplicationCommand::class, ]) ->hasConfigFile() ->hasRoute('api') From 136b80dc76729ee5fd6c5273039972eec57b5db1 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 15 Jan 2025 18:50:08 +0100 Subject: [PATCH 5/6] remove php compactor --- src/Compactor/Php.php | 189 ------------------------------------------ 1 file changed, 189 deletions(-) delete mode 100644 src/Compactor/Php.php diff --git a/src/Compactor/Php.php b/src/Compactor/Php.php deleted file mode 100644 index 7b5d3ad1..00000000 --- a/src/Compactor/Php.php +++ /dev/null @@ -1,189 +0,0 @@ -canProcessFile($file)) { - return $this->compactContent($contents); - } - - return $this->compactContent($contents); - } - - protected function compactContent(string $contents): string - { - $output = ''; - $tokens = PhpToken::tokenize($contents); - $tokenCount = count($tokens); - - for ($index = 0; $index < $tokenCount; $index++) { - $token = $tokens[$index]; - $tokenText = $token->text; - - if ($token->is([T_COMMENT, T_DOC_COMMENT])) { - if (str_starts_with($tokenText, '#[')) { - // This is, in all likelihood, the start of a PHP >= 8.0 attribute. - // Note: $tokens may be updated by reference as well! - $retokenized = $this->retokenizeAttribute($tokens, $index); - - if ($retokenized !== null) { - array_splice($tokens, $index, 1, $retokenized); - $tokenCount = count($tokens); - } - - $attributeCloser = self::findAttributeCloser($tokens, $index); - - if (is_int($attributeCloser)) { - $output .= '#['; - } else { - // Turns out this was not an attribute. Treat it as a plain comment. - $output .= str_repeat("\n", mb_substr_count($tokenText, "\n")); - } - } elseif (str_contains($tokenText, '@')) { - try { - $output .= $this->compactAnnotations($tokenText); - } catch (RuntimeException) { - $output .= $tokenText; - } - } else { - $output .= str_repeat("\n", mb_substr_count($tokenText, "\n")); - } - } elseif ($token->is(T_WHITESPACE)) { - $whitespace = $tokenText; - $previousIndex = ($index - 1); - - // Handle whitespace potentially being split into two tokens after attribute retokenization. - $nextToken = $tokens[$index + 1] ?? null; - - if ($nextToken !== null - && $nextToken->is(T_WHITESPACE) - ) { - $whitespace .= $nextToken->text; - $index++; - } - - // reduce wide spaces - $whitespace = preg_replace('{[ \t]+}', ' ', $whitespace); - - // normalize newlines to \n - $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace); - - // If the new line was split off from the whitespace token due to it being included in - // the previous (comment) token (PHP < 8), remove leading spaces. - - $previousToken = $tokens[$previousIndex]; - - if ($previousToken->is(T_COMMENT) - && str_contains($previousToken->text, "\n") - ) { - $whitespace = ltrim($whitespace, ' '); - } - - // trim leading spaces - $whitespace = preg_replace('{\n +}', "\n", $whitespace); - - $output .= $whitespace; - } else { - $output .= $tokenText; - } - } - - return $output; - } - - private function compactAnnotations(string $docblock): string - { - return $docblock; - } - - /** - * @param list $tokens - */ - private static function findAttributeCloser(array $tokens, int $opener): ?int - { - $tokenCount = count($tokens); - $brackets = [$opener]; - $closer = null; - - for ($i = ($opener + 1); $i < $tokenCount; $i++) { - $tokenText = $tokens[$i]->text; - - // Allow for short arrays within attributes. - if ($tokenText === '[') { - $brackets[] = $i; - - continue; - } - - if ($tokenText === ']') { - array_pop($brackets); - - if (count($brackets) === 0) { - $closer = $i; - break; - } - } - } - - return $closer; - } - - /** - * @param non-empty-list $tokens - */ - private function retokenizeAttribute(array &$tokens, int $opener): ?array - { - Assert::keyExists($tokens, $opener); - - $token = $tokens[$opener]; - $attributeBody = mb_substr($token->text, 2); - $subTokens = PhpToken::tokenize('text; - } - - $subTokens = PhpToken::tokenize(' Date: Thu, 16 Jan 2025 17:36:41 +0100 Subject: [PATCH 6/6] moved internal cleanup_exclude files back again This is hard to test when the config I'm asserting on lives on a different repo from the test itself. --- config/nativephp-internal.php | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/config/nativephp-internal.php b/config/nativephp-internal.php index 26774dc2..4210df92 100644 --- a/config/nativephp-internal.php +++ b/config/nativephp-internal.php @@ -29,37 +29,4 @@ * The URL to the NativePHP API. */ 'api_url' => env('NATIVEPHP_API_URL', 'http://localhost:4000/api/'), - - /** - * The default list of files to exclude from the build - */ - 'cleanup_exclude_files' => [ - // .git and dev directories - '.git', - 'dist', - 'docker', - 'packages', - '**/.github', - - // Potentially containing sensitive info - 'database/*.sqlite', - 'database/*.sqlite-shm', - 'database/*.sqlite-wal', - - 'storage/framework/sessions/*', - 'storage/framework/testing/*', - 'storage/framework/cache/*', - 'storage/framework/views/*', - 'storage/logs/*', - - // Only needed for local testing - 'vendor/nativephp/electron/resources', - 'vendor/nativephp/electron/vendor', - 'vendor/nativephp/electron/bin', - 'vendor/nativephp/laravel/vendor', - 'vendor/nativephp/php-bin', - - // Also deleted in PrunesVendorDirectory after fresh composer install - 'vendor/bin' - ] ];