From 16f242021b18a1d0e13ef6d3b0fc24d66246cfb0 Mon Sep 17 00:00:00 2001 From: Norman Huth Date: Sun, 19 Jun 2022 20:09:48 +0200 Subject: [PATCH 1/3] Update asset command with filesystem and add backup --- README.md | 27 +++- src/Console/Commands/Command.php | 35 ++++++ src/Console/Commands/CustomAssetsCommand.php | 123 ++++++++----------- 3 files changed, 111 insertions(+), 74 deletions(-) create mode 100644 src/Console/Commands/Command.php diff --git a/README.md b/README.md index 70259d1..4235c75 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,60 @@ [![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://vshymanskyy.github.io/StandWithUkraine/) # Nova Assets Changer -This package is for Nova 4 and swaps the resources from the `resources/Nova/Nova` folder with those in the `vendor/laravel/nova/resources/js` folder. + +This package is for Nova 4 and swaps the resources from the `resources/Nova/Nova` folder with those in the `vendor/laravel/nova/resources/js` folder. Then the assets are recompiled and published with the Force option. +This package creates a backup of each file and prepare for changes + Attention. The original vendor files will be overwritten. -## Overflow Fix Nova 4.5.2 -[resources/Nova/js/layouts/AppLayout.vue](resources/Nova/js/layouts/AppLayout.vue) +# IMPORTANT + +You must run the `php artisan nova:custom-assets` after every composer update! + +Tip: You can replace `@php artisan nova:publish` with `@php artisan nova:custom-assets` in Your `composer.json` ## Install + ``` -composer require norman-huth/nova-assets-changer +composer require norman-huth/nova-assets-changer --dev ``` ### Optional + Publish example resources + ``` php artisan vendor:publish --provider="NormanHuth\NovaAssetsChanger\PackageServiceProvider" ``` ### Running + For the full process run this command: + ``` php artisan nova:custom-assets ``` +``` +composer reinstall laravel/nova +``` + If you want to skip NPM install use this command: + ``` php artisan nova:custom-assets --without-npm-install ``` ## Notice + After a Nova update, you need to check your resource files to see if they are still compatible. ___ + ## Examples + I make not a release for every example. For all example resources take a look in the `resources` folder of the GitHub repository --- diff --git a/src/Console/Commands/Command.php b/src/Console/Commands/Command.php new file mode 100644 index 0000000..3997756 --- /dev/null +++ b/src/Console/Commands/Command.php @@ -0,0 +1,35 @@ +storage = Storage::build([ + 'driver' => 'local', + 'root' => resource_path('Nova'), + 'throw' => false, + ]); + $this->novaStorage = Storage::build([ + 'driver' => 'local', + 'root' => base_path('vendor/laravel/nova'), + 'throw' => false, + ]); + + parent::__construct(); + } +} diff --git a/src/Console/Commands/CustomAssetsCommand.php b/src/Console/Commands/CustomAssetsCommand.php index a73f7fe..9295924 100644 --- a/src/Console/Commands/CustomAssetsCommand.php +++ b/src/Console/Commands/CustomAssetsCommand.php @@ -2,20 +2,12 @@ namespace NormanHuth\NovaAssetsChanger\Console\Commands; -use Illuminate\Console\Command; use NormanHuth\NovaAssetsChanger\Helpers\Process; -/** - * Todo: `nova:update` currently not exist in nova. Adjust this command if this changes - */ class CustomAssetsCommand extends Command { - protected string $novaPath; - protected string $novaResourcesPath; - protected string $appResourcePath; + protected string $novaPath = 'vendor/laravel/nova'; protected Process $process; - protected string $ds = DIRECTORY_SEPARATOR; - protected string $novaVersion = 'u'; /** * The name and signature of the console command. @@ -39,34 +31,31 @@ class CustomAssetsCommand extends Command public function handle(): int { $npmInstall = !$this->option('without-npm-install'); - $this->novaPath = base_path('vendor'.$this->ds.'laravel'.$this->ds.'nova'); - $this->novaResourcesPath = $this->novaPath.$this->ds.'resources'; - $this->appResourcePath = resource_path('Nova'.$this->ds.'Nova'); $this->process = new Process; + $this->novaPath = base_path($this->novaPath); + $this->reinstallNova(); + $this->replaceComponents(); $this->webpack(); if ($npmInstall) { - $this->installNPM(); + $this->npmInstall(); } - $this->replaceComponents($this->appResourcePath); - $this->productionRun(); - $this->info('Publish Nova assets'); - $this->call('vendor:publish', [ - '--tag' => 'nova-assets', - '--force' => true, - ]); + $this->npmProduction(); + $this->publishNovaAssets(); return 0; } - protected function getNovaVersion() + protected function publishNovaAssets() { - $manifest = json_decode(file_get_contents(base_path('vendor/laravel/nova/composer.json')), true); - $version = $manifest['version'] ?? '4.x'; - $this->novaVersion = $version; + $this->info('Publish Nova assets'); + $this->call('vendor:publish', [ + '--tag' => 'nova-assets', + '--force' => true, + ]); } - protected function productionRun() + protected function npmProduction() { $this->info('Run NPM production'); $command = 'cd '.$this->novaPath.' && npm run production'; @@ -76,68 +65,62 @@ protected function productionRun() } } - protected function replaceComponents(string $path) + protected function reinstallNova() + { + $this->info('Reinstall laravel/nova'); + $this->process->runCommand('composer reinstall laravel/nova'); + foreach ($this->process->getOutput() as $output) { + $this->line($output); + } + } + + protected function replaceComponents($path = 'Nova') { - $this->getNovaVersion(); - $files = glob($path.'/*'); + $files = $this->storage->files($path); foreach ($files as $file) { - if (in_array($file, ['..', '.'])) { + $base = explode('/', $file, 2)[1]; + $this->info('Processing '.$base); + if ($this->novaStorage->missing('resources/'.$base)) { + $this->error('Skip file. `'.$base.'` not found in the Nova installation'); continue; } - if (str_ends_with($file, '.vue')) { - $target = $this->novaResourcesPath.str_replace($this->appResourcePath, '', $file); - $this->line('Replace file: '.$file); - $this->replaceComponent($file, $target); - } elseif (is_dir($file)) { - $this->replaceComponents($file); + $customContent = $this->storage->get($file); + $novaContent = $this->novaStorage->get('resources/'.$base); + if ($this->storage->missing('Backup/'.$base)) { + $this->storage->put('Backup/'.$base, $novaContent); + } else { + $backupContent = $this->storage->get('Backup/'.$base); + if (trim($backupContent) != trim($novaContent)) { + if (!$this->confirm('The `'.$base.'` file seems to have changed. Do you wish to continue and renew the backup file?')) { + die('Abort'); + } else { + $this->storage->put('Backup/'.$base, $novaContent); + } + } + + $this->novaStorage->put('resources/'.$base, $customContent); } } - } - - protected function replaceComponent(string $file, string $target) - { - $fileInfo = pathinfo($file); - -// $backupFile = rtrim($fileInfo['dirname'], '/\\').'/'. -// str_replace($fileInfo['filename'], $fileInfo['filename'].'-nova-'.$this->novaVersion.'.'.$fileInfo['extension'], $fileInfo['filename']); - - $content = file_get_contents($file); - -// if (!file_exists($backupFile)) { -// file_put_contents($backupFile, $content); -// } - - file_put_contents($target, $content); - } - - protected function strReplaceLast(string $search, string $replace, string $string): string - { - if (($pos = strrpos($string, $search)) !== false) { - $searchLength = strlen($search); - $string = substr_replace($string, $replace, $pos, $searchLength); + $directories = $this->storage->directories($path); + foreach ($directories as $directory) { + $this->replaceComponents($directory); } - return $string; } - /** - * Install NPM packages - */ - protected function installNPM() + protected function npmInstall() { $this->info('Run NPM install'); - $command = 'cd '.$this->novaPath.' && npm i'; - $this->process->runCommand($command); + $this->process->runCommand('cd '.$this->novaPath.' && npm i'); foreach ($this->process->getOutput() as $output) { $this->line($output); } } - /** - * Create or overwrite Nova webpack.mix.js - */ protected function webpack() { - $content = file_get_contents($this->novaPath.'/webpack.mix.js.dist'); - file_put_contents($this->novaPath.'/webpack.mix.js', $content); + if ($this->novaStorage->exists('webpack.mix.js.dist')) { + $this->info('Create webpack.mix.js'); + $this->novaStorage->put('webpack.mix.js', $this->novaStorage->get('webpack.mix.js.dist')); + } } } From 95f526ae0cd6e3df1ea7d993508a12248203ff69 Mon Sep 17 00:00:00 2001 From: Norman Huth Date: Sun, 19 Jun 2022 20:17:40 +0200 Subject: [PATCH 2/3] Remove NPM install option (required for backup system) --- README.md | 6 ------ src/Console/Commands/CustomAssetsCommand.php | 15 +++++++-------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4235c75..b5f873e 100644 --- a/README.md +++ b/README.md @@ -41,12 +41,6 @@ php artisan nova:custom-assets composer reinstall laravel/nova ``` -If you want to skip NPM install use this command: - -``` -php artisan nova:custom-assets --without-npm-install -``` - ## Notice After a Nova update, you need to check your resource files to see if they are still compatible. diff --git a/src/Console/Commands/CustomAssetsCommand.php b/src/Console/Commands/CustomAssetsCommand.php index 9295924..f7f4fa0 100644 --- a/src/Console/Commands/CustomAssetsCommand.php +++ b/src/Console/Commands/CustomAssetsCommand.php @@ -8,13 +8,15 @@ class CustomAssetsCommand extends Command { protected string $novaPath = 'vendor/laravel/nova'; protected Process $process; + protected string $composerCommand = 'composer'; + protected string $npmCommand = 'npm'; /** * The name and signature of the console command. * * @var string */ - protected $signature = 'nova:custom-assets {--without-npm-install}'; + protected $signature = 'nova:custom-assets'; /** * The console command description. @@ -30,16 +32,13 @@ class CustomAssetsCommand extends Command */ public function handle(): int { - $npmInstall = !$this->option('without-npm-install'); $this->process = new Process; $this->novaPath = base_path($this->novaPath); $this->reinstallNova(); $this->replaceComponents(); $this->webpack(); - if ($npmInstall) { - $this->npmInstall(); - } + $this->npmInstall(); $this->npmProduction(); $this->publishNovaAssets(); @@ -58,7 +57,7 @@ protected function publishNovaAssets() protected function npmProduction() { $this->info('Run NPM production'); - $command = 'cd '.$this->novaPath.' && npm run production'; + $command = 'cd '.$this->novaPath.' && '.$this->npmCommand.' run production'; $this->process->runCommand($command); foreach ($this->process->getOutput() as $output) { $this->line($output); @@ -68,7 +67,7 @@ protected function npmProduction() protected function reinstallNova() { $this->info('Reinstall laravel/nova'); - $this->process->runCommand('composer reinstall laravel/nova'); + $this->process->runCommand($this->composerCommand.' reinstall laravel/nova'); foreach ($this->process->getOutput() as $output) { $this->line($output); } @@ -110,7 +109,7 @@ protected function replaceComponents($path = 'Nova') protected function npmInstall() { $this->info('Run NPM install'); - $this->process->runCommand('cd '.$this->novaPath.' && npm i'); + $this->process->runCommand('cd '.$this->novaPath.' && '.$this->npmCommand.' i'); foreach ($this->process->getOutput() as $output) { $this->line($output); } From 4d3bb7eb589c345d6578ec0ed069fa18fe41487d Mon Sep 17 00:00:00 2001 From: Norman Huth Date: Sun, 19 Jun 2022 21:05:48 +0200 Subject: [PATCH 3/3] Add publish command --- README.md | 8 +- .../Nova/js/components/Cards/HelpCard.vue | 230 ++++++++++++++++++ .../Dropdowns/InlineActionDropdown.vue | 1 + .../Nova/js/components/ResourceTableRow.vue | 1 + resources/Nova/js/layouts/AppLayout.vue | 67 ----- resources/Nova/js/layouts/MainHeader.vue | 1 + src/Console/Commands/CustomAssetsCommand.php | 43 +++- src/Console/Commands/PublishCommand.php | 117 +++++++++ src/Helpers/Process.php | 40 +-- src/PackageServiceProvider.php | 2 + 10 files changed, 400 insertions(+), 110 deletions(-) create mode 100644 resources/Nova/js/components/Cards/HelpCard.vue delete mode 100644 resources/Nova/js/layouts/AppLayout.vue create mode 100644 src/Console/Commands/PublishCommand.php diff --git a/README.md b/README.md index b5f873e..f342f62 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Nova Assets Changer -This package is for Nova 4 and swaps the resources from the `resources/Nova/Nova` folder with those in the `vendor/laravel/nova/resources/js` folder. +This package is for Nova 4 and swaps the resources from the `resources/Nova/Nova` folder with those in the `vendor/laravel/nova/resources/js` folder or use the `php artisan custom-assets:publish` command. Then the assets are recompiled and published with the Force option. This package creates a backup of each file and prepare for changes @@ -21,6 +21,12 @@ Tip: You can replace `@php artisan nova:publish` with `@php artisan nova:custom- composer require norman-huth/nova-assets-changer --dev ``` +### Publish Nova Assets +``` +php artisan custom-assets:publish +``` + + ### Optional Publish example resources diff --git a/resources/Nova/js/components/Cards/HelpCard.vue b/resources/Nova/js/components/Cards/HelpCard.vue new file mode 100644 index 0000000..6417c58 --- /dev/null +++ b/resources/Nova/js/components/Cards/HelpCard.vue @@ -0,0 +1,230 @@ + + + + diff --git a/resources/Nova/js/components/Dropdowns/InlineActionDropdown.vue b/resources/Nova/js/components/Dropdowns/InlineActionDropdown.vue index a8425c4..46e7e5d 100644 --- a/resources/Nova/js/components/Dropdowns/InlineActionDropdown.vue +++ b/resources/Nova/js/components/Dropdowns/InlineActionDropdown.vue @@ -1,3 +1,4 @@ +