Created
November 14, 2012 16:19
-
-
Save raws/4073068 to your computer and use it in GitHub Desktop.
Crazy Thor extensions I wrote and didn't need because they're dumb and complex but I might want to look at them again later so I'm saving them here also what is a run-on sentence
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
require 'active_support/core_ext/module/aliasing' | |
require 'thor' | |
require 'thor/group' | |
class Thor | |
class << self | |
def method_options_from(option) | |
{ | |
aliases: option.aliases, | |
banner: option.banner, | |
default: option.default, | |
desc: option.description, | |
enum: option.enum, | |
group: option.group, | |
hide: option.hide, | |
lazy_default: option.lazy_default, | |
name: option.name, | |
required: option.required, | |
type: option.type | |
} | |
end | |
def task_name_for(klass) | |
task_name = klass.name.dup | |
task_name.sub!(/#{namespace}::/i, '') | |
task_name.gsub!('::', ':') | |
task_name.gsub!(/([a-z])([A-Z])/, '\1_\2') | |
task_name.downcase | |
end | |
def task_usage_for(klass) | |
base_name = task_name_for(klass).split(':').last | |
(klass.usage || '').sub /^\s*#{base_name}\s*/i, '' | |
end | |
def register_with_reflection(klass) | |
task_name = task_name_for(klass) | |
task_usage = "#{task_name} #{task_usage_for(klass)}" | |
long_desc klass.long_desc | |
klass.class_options.each do |name, option| | |
option name, method_options_from(option) | |
end | |
register_without_reflection klass, task_name, task_usage, klass.desc | |
end | |
alias_method_chain :register, :reflection | |
end | |
end | |
class Thor::Group | |
class << self | |
def desc(desc = nil) | |
@desc ||= desc | |
end | |
def long_desc(long_desc = nil) | |
@long_desc ||= long_desc | |
end | |
def usage(usage = nil) | |
@usage ||= usage | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment