Created
May 23, 2014 23:21
-
-
Save laserlemon/77827a46b3a3bcabd84c to your computer and use it in GitHub Desktop.
Thor is hard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my_gem/cli.rb | |
require "thor" | |
require "my_gem/cli/foo_bar" | |
require "my_gem/cli/foo_baz" | |
require "my_gem/cli/install" | |
module MyGem | |
class CLI < Thor | |
desc "foo:bar", "Do bar to foo" | |
define_method "foo:bar" do | |
# MyGem::CLI::FooBar is my own independently unit-test bit of code. | |
FooBar.run(options) | |
end | |
desc "baz", "Do baz to foo" | |
method_option "force", | |
aliases: ["-f"], | |
desc: "Force the issue" | |
define_method "foo:baz" do | |
# MyGem::CLI::FooBaz is my own independently unit-test bit of code. | |
FooBaz.run(options) | |
end | |
desc "install", "Install MyGem" | |
method_option "dry-run", | |
desc: "Fake it" | |
def install | |
# MyGem::CLI::Install is a Thor::Group including Thor::Actions. | |
# It's used very much like a Rails generator. | |
# | |
# The problem is that the "install" task shows up twice when | |
# calling the my_gem executable, once as "install" and once | |
# more as "my_gem:c_l_i:install". | |
# | |
# I also have to duplicate options between the method options | |
# here and the class options in the Install class. | |
# | |
# How can I have a command that acts as a generator without | |
# accidentally creating two commands? | |
Install.start | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my_gem/cli/install.rb | |
module MyGem | |
class CLI < Thor | |
class Install < Thor::Group | |
include Thor::Actions | |
desc "install", "Install MyGem" | |
class_option "dry-run", | |
desc: "Fake it" | |
source_root File.expand_path("../templates", __FILE__) | |
def do_first_thing | |
# TODO: Create some file. | |
end | |
def do_next_thing | |
# TODO: Update some other file. | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# bin/my_gem | |
require "my_gem/cli" | |
MyGem::CLI.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment