wikimedia / composer-merge-plugin

Merge one or more additional composer.json files at Composer runtime
MIT License
934 stars 159 forks source link

Add ability to execute post-install/post-update command event handlers from included files #126

Closed jjtroberts closed 7 years ago

jjtroberts commented 8 years ago

Is there any way to execute scripts from composer.local.json? I trying to allow custom sites to add additional themes and ensure that theme is activated after install or update. Perhaps with an option to append or override scripts from the parent composer.json file?

bd808 commented 8 years ago

Looking at the main Composer entry point, script config is processed very early before delegating to Symfony\Component\Console\Application to process the command line arguments and choose the Command to execute. The current upstream implementation directly reads and parses the root composer.json. I don't see any way to modify the Commands that are registered via a plugin.

bd808 commented 8 years ago

It probably would be possible to call post-install/post-update actions that were declared in an included composer.json file. If nothing else, composer-merge-plugin could call such scripts itself when it is called for those events.

jjtroberts commented 8 years ago

Thanks for taking a look at this. I really just need a way to call post-instal/post-update actions if they exist in an included composer.json file. Our default composer.json installs a base theme, and the included composer.json offers the ability to install a different version of that base theme. Having the means within composer to activate the included theme would be immensely helpful.

bd808 commented 8 years ago

A reasonable first attempt at this feature would be to:

I don't have any time at the moment to work on adding this functionality, but I would be more than happy to review a pull request implementing it.

jjtroberts commented 8 years ago

Making progress. I have a feature flag set to false by default, but when set to true it allows MergePlugin.php to retrieve a list of handlers registered in an included file (haven't tested recursive yet). I could use some advice at this point regarding what to do with the resulting array of scripts. How and where do I execute them?

bd808 commented 8 years ago

The collected scripts would be executed from the corresponding event handler in MergePlugin. The logic to call a handler is in Composer's EventDispatcher class. You already have the Event to dispatch passed into the compsoer-merge-plugin handler.

The easiest thing to do might be to introduce a subclass of Composer\EventDispatcher\EventDispatcher so you can get at the protected doDispatch method. Then you'd just need to call addListener for each of the callbacks you found and redeliver them locally available event.

jjtroberts commented 8 years ago

Been so long since I've done OO programming. This is out of my league at the moment. I probably need to just allow Jenkins to do the automation and let composer do what it is good at.

This works for my use-case, but it is a hack: https://github.com/gd-jroberts/composer-merge-plugin/commit/de251a33308ccf12b0aa33ca5b4e37bd259a0210

bd808 commented 7 years ago

At least partially handled by merge-scripts (#135 & #136) and 586ae1edd4d9492eaaf8fb21ad8ad86306d02b3d where we started hooking Composer 1.1's INIT event.