Create a gist now

Instantly share code, notes, and snippets.

show the code for a Thor task
# 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