bridgetownrb / bridgetown

A next-generation progressive site generator & fullstack framework, powered by Ruby
https://www.bridgetownrb.com
MIT License
1.16k stars 112 forks source link

Commands from plugins not picked up #675

Closed svoop closed 2 months ago

svoop commented 1 year ago

In a gemified plugin, I've declared Thor-like (as shown in the guides) in lib/mygem/commands/doit.rb and require the file in lib/mygem.rb. For tying, I've pathed in the gem into a Bridgetown site, installed the bundle. I'd expect bridgetown to include these commands, but it doesn't. Maybe something has to be added to the initializer block? I couldn't find another plugin with commands, but it certainly feels as if some reference is missing... or something broken.

(Transferred from Discord to this issue as per your request.)

Update: Just tried it as a not-gemified, in-site plugin, same problem.

As a side note: The guides are not very clear as how to declare the commands. In .rb and require them in the root mygem.rb entry point? Or not require them but make them .thor files?

Bridgetown Version: 1.2.0.beta4

To Reproduce

Current behavior Commands from plugins are not listed with bridgetown or bt, nor are they executable with bt NAMESPACE COMMAND.

Computing environment (please complete the following information):

Thanks for your help... and your great work with Brigetown!

jaredcwhite commented 1 year ago

Thanks for writing this up @svoop. A solution is going to be tricky, because there's a timing problem I hadn't foreseen with the new initializer system. Thor needs to have its available command set registered before it can process the CLI arguments, but the exact point at which a site's initializers are loaded comes after a particular command gets run like build or start. Somehow there will need to be a way to register commands earlier in the boot process. Hmm…

jaredcwhite commented 1 year ago

Upon further thought, there's a workaround which I think if we document is OK for now. I've been thinking about building a more intuitive system of adding new commands anyway (which would be an improvement over using Rake tasks for commands for site builders), so that's probably something to tackle as a unified feature.

The workaround is to add a config/boot.rb:

Bundler.setup(:default, Bridgetown.env)

require "some_gem_here" # adds a command to Thor

I'll document this for the 1.2 release so folks aren't left hanging.

jaredcwhite commented 1 year ago

Let me know if this makes sense @svoop

https://edge.bridgetownrb.com/docs/plugins/commands

svoop commented 1 year ago

@jaredcwhite Yep, makes perfect sense and works! I'll mention it in the README of the bridgetown_credentials gem I'm working on. Maybe there's a better solution some day, but I guess there are bigger fish to fry at the moment. Thanks a lot for this workaround and Merry Christmas to you!