diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d23752eb..3da0169e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. [Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.12.2...master) -------------- +- Added `post_discover` hook to run commands after package discovery [\#1185 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1185) 2022-02-08, 2.12.2 ------------------ diff --git a/README.md b/README.md index 83d44333f..44a1588b6 100644 --- a/README.md +++ b/README.md @@ -88,16 +88,13 @@ Note: `bootstrap/compiled.php` has to be cleared first, so run `php artisan clea This will generate the file `_ide_helper.php` which is expected to be additionally parsed by your IDE for autocomplete. You can use the config `filename` to change its name. -You can configure your `composer.json` to do this each time you update your dependencies: - -```js -"scripts": { - "post-update-cmd": [ - "Illuminate\\Foundation\\ComposerScripts::postUpdate", - "@php artisan ide-helper:generate", - "@php artisan ide-helper:meta" - ] -}, +You can use the `post_discover` config to run any artisan commands after installing/update packages: + +```php +'post_discover' => [ + 'ide-helper:generate', + 'ide-helper:meta', +], ``` You can also publish the config file to change implementations (ie. interface to specific class) or set defaults for `--helpers`. diff --git a/composer.json b/composer.json index d1f98e78c..294b05aaf 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "vimeo/psalm": "^3.12" }, "suggest": { - "illuminate/events": "Required for automatic helper generation (^6|^7|^8|^9)." + "illuminate/events": "Required for automatic helper generation (^8|^9)." }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/config/ide-helper.php b/config/ide-helper.php index 51fcdf24a..39b118b32 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -304,6 +304,20 @@ */ 'additional_relation_types' => [], + /* + |-------------------------------------------------------------------------- + | Run artisan commands after package discovery + |-------------------------------------------------------------------------- + | + | The specified commands should run after `package:discover` run. This command + | is executed by default after every composer install, update or dump-autoload. + | + */ + 'post_discover' => [ + // 'ide-helper:generate', + // 'ide-helper:meta', + ], + /* |-------------------------------------------------------------------------- | Run artisan commands after migrations to generate model helpers diff --git a/src/IdeHelperServiceProvider.php b/src/IdeHelperServiceProvider.php index 13b573f1a..31525f492 100644 --- a/src/IdeHelperServiceProvider.php +++ b/src/IdeHelperServiceProvider.php @@ -15,6 +15,7 @@ use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand; use Barryvdh\LaravelIdeHelper\Console\MetaCommand; use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; +use Barryvdh\LaravelIdeHelper\Listeners\GenerateHelperAndMeta; use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper; use Illuminate\Console\Events\CommandFinished; use Illuminate\Contracts\Support\DeferrableProvider; @@ -34,6 +35,10 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv */ public function boot() { + if ($this->app['config']->get('ide-helper.post_discover', [])) { + $this->app['events']->listen(CommandFinished::class, GenerateHelperAndMeta::class); + } + if (!$this->app->runningUnitTests() && $this->app['config']->get('ide-helper.post_migrate', [])) { $this->app['events']->listen(CommandFinished::class, GenerateModelHelper::class); $this->app['events']->listen(MigrationsEnded::class, function () { diff --git a/src/Listeners/GenerateHelperAndMeta.php b/src/Listeners/GenerateHelperAndMeta.php new file mode 100644 index 000000000..2f831aa9f --- /dev/null +++ b/src/Listeners/GenerateHelperAndMeta.php @@ -0,0 +1,59 @@ +artisan = $artisan; + $this->config = $config; + } + + /** + * Handle the event. + * + * @param CommandFinished $event + */ + public function handle(CommandFinished $event) + { + if ($this->shouldntRun($event)) { + return; + } + + foreach ($this->config->get('ide-helper.post_discover', []) as $command) { + $this->artisan->call($command, [], $event->output); + } + } + + /** + * Decides whether the helper generation should be skipped or not. + * + * It should be skipped if the executed command failed, was not + * `package:discover`, or a no-op parameter is present. + * + * @param CommandFinished $event + * @return bool + */ + protected function shouldntRun(CommandFinished $event): bool + { + return $event->exitCode != 0 || + $event->command != 'package:discover' || + $event->input->hasParameterOption(['--version', '-V'], true) || + $event->input->hasParameterOption(['--help', '-h'], true); + } +}