symfony / recipes

Symfony Recipes Repository
https://github.com/symfony/recipes/blob/flex/main/RECIPES.md
MIT License
979 stars 481 forks source link

[Proposal] Run script once after configuring a recipe #378

Closed slootjes closed 2 years ago

slootjes commented 6 years ago

I know we have composer-scripts option in a recipe but some things I just need to run once after the bundle is configured by the recipe. For instance I've got a recipe which includes Defuse library to work with encryption and I'd like to generate a key automatically so it's ready for use. The same applies to a custom recipe for Sonata Admin where I'd like to automatically generate admin users and to setup security rules.

I could use the composer-scripts option with a "encryption::assert-key" or something which runs every time but it sounds like a workaround so I want to propose a composer-scripts-once option where I can define scripts that should just run once after installation. I'm sure I can provide a PR myself for this when this idea is approved.

Pierstoval commented 6 years ago

I'm not sure executing commands in the name of the user is a good idea, I think it's too opinionated.

However, I recommend you to use the post-install.txt file to tell the user how to create a key, because there is not "one way to do it", and a key can be generated in several ways.

slootjes commented 6 years ago

When using Defuse there is just 1 way to do it so that's why I like to remove the requirement for the user to do custom stuff and prefer that the recipe takes care of this instead. But this is just an example, I'm sure other have multiple use cases where this could come in handy.

Also, can you explain me why this would be opinionated and putting it in composer-scripts isn't? I could also put it there and let the script remove itself from there on it's own. Again, this would be a workaround and I'm looking for a better solution.

Pierstoval commented 6 years ago

Having a script being executed only once is more opinionated because you can't prevent it to be executed (unless you take care of the --no-scripts option).

Having it in composer-scripts still allow you to run flex with --no-scripts and remove it from composer.json afterwards.

If there is only one single way to do it, instead of using the post-install.txt text, I would then recommend to add it to composer-scripts and the command must make sure to keep existing files, and not overwrite them, to avoid the script to replace an existing key and potentially break the app.

slootjes commented 6 years ago

Thanks for explaining. For now I've created the recipe which adds a command to composer-scripts, the script then does it's thing and removes itself from composer-scripts again. For this I'm simply using the configurators from Flex so it works like a charm. It's not the prettiest solution perhaps but it does the job very well to have a good DX for an internal recipe.

Nyholm commented 6 years ago

Thank you @Pierstoval. I take it that @slootjes solved his problem. I'm will close this issue now. If you really feel it should be open just say so and I'll reopen it.

slootjes commented 6 years ago

It would still be nice if someone else had an idea. There are just some things which are currently not possible with recipes which only need to run once to finish. Would like to see a solution for this so I'm open for others suggestions thats why I prefer to keep this open.

nicolas-grekas commented 2 years ago

Closing as this staled. Maybe maker-bundle is the right approach.