pelmered / composer-wp-language-updater

Automatically update the language files for core, plugins and themes after running install|update in composer.
6 stars 1 forks source link

Hooks "post-package-install" and "post-package-update" inappropriate? #2

Closed tyrann0us closed 6 years ago

tyrann0us commented 6 years ago

post-package-install and post-package-update will only be executed after package installation/update (see https://getcomposer.org/doc/articles/scripts.md#package-events). But language packs change independently of the package they belong to. At any time, translations can be updated/changed in GlotPress. AFAIK these language updates won't get installed with the currently used hooks.

So the more appropriate hooks probably should be post-install-cmd and post-update-cmd (see https://getcomposer.org/doc/articles/scripts.md#command-events) even if this causes the script to run more frequently.

When using the mentioned hooks though, I get the following fatal error:

> AngryCreative\WPLanguageUpdater\PostUpdateLanguageUpdate::install_t10ns
Fatal error: Uncaught TypeError: Argument 1 passed to AngryCreative\WPLanguageUpdater\PostUpdateLanguageUpdate::install_t10ns()
must be an instance of Composer\Installer\PackageEvent, instance of Composer\Script\Event given,
called in phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php on line 292
and defined in /path/to/project/vendor/pelmered/composer-plugin-language-update/src/PostUpdateLanguageUpdate.php:42
 Stack trace:
 #0 phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(292):
AngryCreative\WPLanguageUpdater\PostUpdateLanguageUpdate::install_t10ns(Object(Composer\Script\Event))
 #1 phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(215):
Composer\EventDispatcher\EventDispatcher->executeEventPhpScript('AngryCreative\\W...', 'install_t10ns', Object(Composer\Script\Event))
 #2 phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(81): Composer\EventDispatcher\EventDispatcher->doDispatch(Object in
/path/to/project/vendor/pelmered/composer-plugin-language-update/src/PostUpdateLanguageUpdate.php on line 42
Failed to update packages for  ./composer.json.

What do you think? Should the hooks be changed?

Any feedback appreciated! Thanks!

pelmered commented 6 years ago

Yes, you are right. That seams like a better idea. But when I did some research on this, I couldn't find a way to get the installed version of the dependencies on those events.

I tried for example:

$event->getComposer()->getPackage()->getRequires();

But then I only get a list of Link object and not the installed/resolved version: https://getcomposer.org/apidoc/1.3.0/Composer/Package/Link.html

I could probably read from the lock file but that seams like a bit too much of a hack.

Any ideas?

tyrann0us commented 6 years ago

Try:

$packages = $event
  ->getComposer()
  ->getRepositoryManager()
  ->getLocalRepository()
  ->getPackages();

If I'm right this is the documentation: https://getcomposer.org/apidoc/1.3.0/Composer/Repository/WritableRepositoryInterface.html#method_getPackages.

Untested, but used in a private package of my organization that also runs scripts on the post-install-cmd and post-update-cmd hooks.

pelmered commented 6 years ago

Thanks @tyrann0us! That seems to work 👍

I will try to get this done in the new few days.

pelmered commented 6 years ago

This is now implemented.