Skip to content

Commit

Permalink
Merge pull request #15 from Muetze42/development
Browse files Browse the repository at this point in the history
Add `custom-assets:after-composer-update` command
  • Loading branch information
Muetze42 authored Jun 26, 2022
2 parents c57bf71 + 86f9973 commit d5df87e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Attention. The original vendor files will be overwritten.

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`
Tip: You can replace `@php artisan nova:publish` with `@php artisan custom-assets:after-composer-update` in Your `composer.json`

## Install

Expand All @@ -32,6 +32,12 @@ php artisan nova:custom-assets

### Optional

#### Run Command Only If A Nova Update Is Detected (Or The Package Has No Version Saved)

```
php aritsan custom-assets:after-composer-update
```

#### Publish Nova Assets Via Command

```
Expand Down Expand Up @@ -59,10 +65,12 @@ After a Nova update, you need to check your resource files to see if they are st
I make not a release for every example. For all example resources take a look in the `resources` folder of the GitHub repository

## Other Composer Or NPM Command

Create a command:
`php artisan make:command CustomAssetsCommand`

with followingen content:

```php
<?php

Expand Down
48 changes: 48 additions & 0 deletions src/Console/Commands/AfterComposerUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace NormanHuth\NovaAssetsChanger\Console\Commands;

use Laravel\Nova\Nova;

class AfterComposerUpdateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'custom-assets:after-composer-update';

/**
* The console command description.
*
* @var string
*/
protected $description = 'run command only if a Nova update is detected (or the package has no version saved)';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$currentNovaVersion = Nova::version();
$changerVersion = null;
if ($this->storage->exists($this->memoryFile)) {
$content = json_decode($this->storage->get($this->memoryFile), true);
if (!empty($content[$this->lastUseNovaVersionKey])) {
$changerVersion = $content[$this->lastUseNovaVersionKey];
}
}

if ($currentNovaVersion != $changerVersion) {
$this->info('Run `nova:custom-assets` command');
$this->call('nova:custom-assets');
return 0;
}

$this->alert('No Nova Update detected. Don’t run `nova:custom-assets` command');
return 0;
}
}
8 changes: 8 additions & 0 deletions src/Console/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class Command extends BaseCommand
*/
protected string $resourcePath = 'Nova';

/**
* File for saving package data
*
* @var string
*/
protected string $memoryFile = 'asset-changer.json';
protected string $lastUseNovaVersionKey = 'last-used-nova-version';

/**
* Create a new console command instance.
* Create 2 new On-Demand Disks
Expand Down
16 changes: 13 additions & 3 deletions src/Console/Commands/CustomAssetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NormanHuth\NovaAssetsChanger\Console\Commands;

use Laravel\Nova\Nova;
use NormanHuth\NovaAssetsChanger\Helpers\Process;

class CustomAssetsCommand extends Command
Expand Down Expand Up @@ -78,10 +79,19 @@ public function handle(): int
$this->npmInstall();
$this->npmProduction();
$this->publishNovaAssets();
$this->saveCurrentNovaVersion();

return 0;
}

/**
* @return void
*/
protected function saveCurrentNovaVersion(): void
{
$this->storage->put($this->memoryFile, json_encode([$this->lastUseNovaVersionKey => Nova::version()]));
}

/**
* @return void
*/
Expand Down Expand Up @@ -113,15 +123,15 @@ protected function npmProduction(): void
protected function reinstallNova(): void
{
$this->info('Reinstall laravel/nova');
$succes = false;
$success = false;
$this->process->runCommand($this->composerCommand.' reinstall laravel/nova');
foreach ($this->process->getOutput() as $output) {
if (str_contains($output, $this->installStrContainsCheck1) && str_contains($output, $this->installStrContainsCheck2)) {
$succes = true;
$success = true;
}
$this->line($output);
}
if (!$succes) {
if (!$success) {
$this->error('It could’t detect a new installation of Nova.');
die();
}
Expand Down
24 changes: 16 additions & 8 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace NormanHuth\NovaAssetsChanger;

use Illuminate\Support\ServiceProvider;
use NormanHuth\NovaAssetsChanger\Console\Commands\CustomAssetsCommand;
use NormanHuth\NovaAssetsChanger\Console\Commands\PublishCommand;
use NormanHuth\NovaAssetsChanger\Console\Commands\PublishFieldCommand;

class PackageServiceProvider extends ServiceProvider
{
Expand All @@ -19,12 +16,23 @@ public function boot(): void
$this->publishes([
__DIR__.'/../resources' => resource_path('Nova'),
]);

if ($this->app->runningInConsole()) {
$this->commands([
CustomAssetsCommand::class,
PublishCommand::class,
PublishFieldCommand::class,
]);
$this->commands($this->getCommands());
}
}

/**
* Get all package commands
*
* @return array
*/
protected function getCommands(): array
{
return array_filter(array_map(function ($item) {
return '\\'.__NAMESPACE__.'\\Console\\Commands\\'.pathinfo($item, PATHINFO_FILENAME);
}, glob(__DIR__.'/Console/Commands/*.php')), function ($item) {
return class_basename($item) != 'Command';
});
}
}

0 comments on commit d5df87e

Please sign in to comment.