Improve CLI commands
Our implementation is based on thor
, which doesn’t work well with subcommands. Aside from that, the implementation of the generators is hard to maintain. Luckily we have a really good integration testing coverage so we can apply the following ideas.
Switch from thor
to optparse
As mentioned above, thor
doesn’t work well with subcommands and with exit codes. We can replace it with optparse
from Ruby’s standard library.
thor
ships with useful file manipulation utilities. We can promote our file helpers from our test suite as replacement.
Our helpers don’t provide a consistent indentation of Ruby code. We should find a solution for that (https://rubygems.org/gems/rubyfmt ?).
Commands as objects
thor
demands that commands are methods of an object.
class MyCli < Thor
def command_a
end
end
We want to Implement each (sub)command as an object. This approach allows us to put args validations in each class and it decouples the command (hanami routes
) from the concrete implementation that can be provided by us or a third party gem.
class MyCommand < Hanami::Command
# arguments validations DSL
def call(arguments)
# do something
end
end
Let commands to register themselves
class MyCommand < Hanami::Command
# hanami routes
register_as "routes"
# or ...
# hanami generate auth
register_as "auth", subcommand_of: "generate"
# ...
end
This opens an endless possibilities for our ecosystem.
Please note that a third party-gem can take over an entire (sub)command (eg. hanami-cool-orm
can take over hanami generate model
)
Commands dependencies
There are commands that don’t need the full blown Hanami project to do their job.
Think of hanami routes
, it only needs the routes from the project, without connecting to the database.
As of 0.9
, we already let some commands to pick that dependencies. The goal is to let all the commands to work with this approach. See this example: https://github.com/hanami/hanami/blob/master/lib/hanami/commands/routes.rb
These features can be shipped in the 1.x
series, as they don’t introduce breaking changes for our users.