Created
January 21, 2009 17:16
-
-
Save granth/50043 to your computer and use it in GitHub Desktop.
show the code for a Thor task
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
# thor install http://gist.github.com/50043.txt | |
# thor cat:task some:task | |
# thor cat:task thor:runner:install # built-in tasks | |
require 'ruby2ruby' | |
class Cat < Thor | |
$requires = [] | |
module ::Kernel | |
def require_with_record(file) | |
$requires << file if caller[1] =~ /thor\/runner/ | |
require_without_record file | |
end | |
alias_method :require_without_record, :require | |
alias_method :require, :require_with_record | |
end | |
desc "task TASK", "output Ruby code for TASK" | |
def task(meth) | |
runner = Thor::Runner.new | |
runner.send(:initialize_thorfiles, meth) # oh no private! | |
cat_task(meth) | |
end | |
def cat_task(meth) | |
task = Thor[meth] | |
obj = task.klass.new | |
unless obj.respond_to? task.meth | |
raise Error, "The #{task.namespace false} namespace doesn't have a `#{task.meth}' task" | |
end | |
$requires += ['ruby2ruby'] if meth == 'cat:task' | |
unless $requires.empty? | |
puts $requires.map { |r| "require #{r.inspect}" }.join("\n"), "\n" | |
end | |
puts %{class #{task.klass.to_s.gsub(/^Thor::Tasks::/, "")} < Thor} | |
puts %{ desc "#{task.usage}", "#{task.description}"} | |
puts Ruby2Ruby.translate(task.klass, task.meth).gsub(/^/, " ") | |
puts "end" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment