Last active
December 15, 2015 09:39
-
-
Save ryansch/5239584 to your computer and use it in GitHub Desktop.
Thor namespace with custom binary Fake gem name: example
This file contains hidden or 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
| # lib/example/cli.rb | |
| module Example | |
| class CLI < Thor | |
| include Thor::Actions | |
| include ExitCode | |
| namespace :default | |
| source_root Example.root_path | |
| class_option :verbose, :aliases => '-v', :type => :boolean | |
| class_option :version, :aliases => '-V', :type => :boolean | |
| desc "init", "Initialize the example" | |
| def init | |
| copy_file 'data/config.rb', 'config/example.rb' | |
| end | |
| desc "something <foo> [<other args>]", "Does something" | |
| def something(foo, *args) | |
| run "external_command FOO=#{rails_env} #{args.join(' ')}" | |
| end | |
| # Fixes thor's banners when used with :default namespace | |
| def self.banner(command, namespace = nil, subcommand = false) | |
| "#{basename} #{command.formatted_usage(self, false, subcommand)}" | |
| end | |
| class Bar < Thor | |
| include ExitCode | |
| namespace :bar | |
| desc "other [<more args>]", "Does other" | |
| def other(*args) | |
| run "other_external_command #{args.join(' ')}" | |
| end | |
| end | |
| end | |
| end | |
| # Usage: | |
| # example init | |
| # example something foo | |
| # example bar:other |
This file contains hidden or 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
| # bin/example | |
| #!/usr/bin/env ruby | |
| require 'example' | |
| $thor_runner = true | |
| Example::CLI.start(ARGV) |
This file contains hidden or 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
| # lib/example.rb | |
| module Example | |
| ROOT = File.expand_path('../../', __FILE__) | |
| require 'example/version' | |
| autoload :ExitCode, 'example/exit_code.rb' | |
| autoload :CLI, 'example/cli.rb' | |
| def self.root_path(*a) | |
| File.join ROOT, *a | |
| end | |
| end |
This file contains hidden or 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
| # lib/example/exit_code.rb | |
| module Example | |
| module ExitCode | |
| private | |
| def self.exit_on_failure? | |
| true | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment