From 16a3ba41a0bf03d6766546bf32538f939aa89b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Wed, 17 Mar 2021 08:24:50 +0100 Subject: [PATCH 1/8] Remove unsupported versions from suggestion --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, From ad8f19f5d6ba5a3248b3e7f0b1d41c2ad7eeae34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Wed, 17 Mar 2021 08:25:33 +0100 Subject: [PATCH 2/8] Add listener to generate helpers after discovery --- src/Listeners/GenerateHelperAndMeta.php | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Listeners/GenerateHelperAndMeta.php diff --git a/src/Listeners/GenerateHelperAndMeta.php b/src/Listeners/GenerateHelperAndMeta.php new file mode 100644 index 000000000..f6974e8c4 --- /dev/null +++ b/src/Listeners/GenerateHelperAndMeta.php @@ -0,0 +1,50 @@ +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); + } + } + + 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); + } +} From afe461254b092a70b72bb9d2b08406b34555231d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Wed, 17 Mar 2021 08:27:07 +0100 Subject: [PATCH 3/8] Add post discover options to config --- config/ide-helper.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 From 376ef9bff463561dc6147dfb074638dfe62e25ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Wed, 17 Mar 2021 08:27:20 +0100 Subject: [PATCH 4/8] Register listener --- src/IdeHelperServiceProvider.php | 5 +++++ 1 file changed, 5 insertions(+) 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 () { From 30fba87213230b923e9abf3e65e11c04575d6ab2 Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Thu, 1 Apr 2021 20:46:39 +0200 Subject: [PATCH 5/8] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d23752eb..43e36ab61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ All notable changes to this project will be documented in this file. ----------------- ### Added - Model hooks for adding custom information from external sources to model classes through the ModelsCommand [\#945 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/945) +- Added `post_discover` hook to run commands after package discovery [\#1185 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1185) ### Fixed - Fix ide-helper:models exception if model doesn't have factory [\#1196 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1196) From 6359d9a9faaece1f6e467c1201e06494f41e0d39 Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Thu, 1 Apr 2021 20:49:57 +0200 Subject: [PATCH 6/8] Update README to promote the post_discover hook --- README.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) 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`. From 000714b9f9d8ade0503bd780170802d4aaf122bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Wed, 2 Mar 2022 08:11:59 +0100 Subject: [PATCH 7/8] Move changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43e36ab61..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 ------------------ @@ -62,7 +63,6 @@ All notable changes to this project will be documented in this file. ----------------- ### Added - Model hooks for adding custom information from external sources to model classes through the ModelsCommand [\#945 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/945) -- Added `post_discover` hook to run commands after package discovery [\#1185 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1185) ### Fixed - Fix ide-helper:models exception if model doesn't have factory [\#1196 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1196) From 647dd4bc340f5bee954791756d8b0599461e8444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Wed, 2 Mar 2022 08:44:44 +0100 Subject: [PATCH 8/8] Add docblock --- src/Listeners/GenerateHelperAndMeta.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Listeners/GenerateHelperAndMeta.php b/src/Listeners/GenerateHelperAndMeta.php index f6974e8c4..2f831aa9f 100644 --- a/src/Listeners/GenerateHelperAndMeta.php +++ b/src/Listeners/GenerateHelperAndMeta.php @@ -40,6 +40,15 @@ public function handle(CommandFinished $event) } } + /** + * 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 ||