Skip to content

Instantly share code, notes, and snippets.

@granth
Created January 21, 2009 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save granth/50043 to your computer and use it in GitHub Desktop.
Save granth/50043 to your computer and use it in GitHub Desktop.
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