Skip to content

Commit

Permalink
Provide some backtrace info when a thor file fails to load.
Browse files Browse the repository at this point in the history
By default the first line will be sent to stderr. If the --debug flag is
passed then the whole backtrace will be printed.
  • Loading branch information
Brian Donovan committed May 14, 2010
1 parent 8c16b91 commit 396c4a3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/thor/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def method_missing(meth, *args)
def install(name)
initialize_thorfiles

# If a directory name is provided as the argument, look for a 'main.thor'
# If a directory name is provided as the argument, look for a 'main.thor'
# task in said directory.
begin
if File.directory?(File.expand_path(name))
Expand Down Expand Up @@ -139,7 +139,7 @@ def installed
end

desc "list [SEARCH]", "List the available thor tasks (--substring means .*SEARCH)"
method_options :substring => :boolean, :group => :string, :all => :boolean
method_options :substring => :boolean, :group => :string, :all => :boolean, :debug => :boolean
def list(search="")
initialize_thorfiles

Expand Down Expand Up @@ -199,7 +199,7 @@ def self.exit_on_failure?
#
def initialize_thorfiles(relevant_to=nil, skip_lookup=false)
thorfiles(relevant_to, skip_lookup).each do |f|
Thor::Util.load_thorfile(f) unless Thor::Base.subclass_files.keys.include?(File.expand_path(f))
Thor::Util.load_thorfile(f, nil, options[:debug]) unless Thor::Base.subclass_files.keys.include?(File.expand_path(f))
end
end

Expand Down
7 changes: 6 additions & 1 deletion lib/thor/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ def self.find_class_and_task_by_namespace(namespace, fallback = true)
# Receives a path and load the thor file in the path. The file is evaluated
# inside the sandbox to avoid namespacing conflicts.
#
def self.load_thorfile(path, content=nil)
def self.load_thorfile(path, content=nil, debug=false)
content ||= File.binread(path)

begin
Thor::Sandbox.class_eval(content, path)
rescue Exception => e
$stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.message}"
if debug
$stderr.puts *e.backtrace
else
$stderr.puts e.backtrace.first
end
end
end

Expand Down

0 comments on commit 396c4a3

Please sign in to comment.