Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improve error message for source_root and add a shortcut to define it.

  • Loading branch information...
commit 75231d19d84cc58146a5776b015a06d67c2dfcff 1 parent fd2b32b
@josevalim josevalim authored
View
23 lib/thor/actions.rb
@@ -20,7 +20,13 @@ module ClassMethods
# inherited paths and the source root.
#
def source_paths
- @source_paths ||= []
+ @_source_paths ||= []
+ end
+
+ # Stores and return the source root for this class
+ def source_root(path=nil)
+ @_source_root = path if path
+ @_source_root
end
# Returns the source paths in the following order:
@@ -32,7 +38,7 @@ def source_paths
def source_paths_for_search
paths = []
paths += self.source_paths
- paths << self.source_root if self.respond_to?(:source_root)
+ paths << self.source_root if self.source_root
paths += from_superclass(:source_paths, [])
paths
end
@@ -126,12 +132,19 @@ def find_in_source_paths(file)
return source_file if File.exists?(source_file)
end
+ message = "Could not find #{file.inspect} in any of your source paths. "
+
+ unless self.class.source_root
+ message << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. "
+ end
+
if source_paths.empty?
- raise Error, "You don't have any source path defined for class #{self.class.name}. To fix this, " <<
- "you can define a source_root in your class."
+ message << "Currently you have no source paths."
else
- raise Error, "Could not find #{file.inspect} in source paths: \n#{source_paths.join("\n")}"
+ message << "Your current source paths are: \n#{source_paths.join("\n")}"
end
+
+ raise Error, message
end
# Do something in the root or on a provided subfolder. If a relative path
View
2  spec/actions/directory_spec.rb
@@ -37,7 +37,7 @@ def exists_and_identical?(source_path, destination_path)
it "raises an error if the source does not exist" do
lambda {
invoke! "unknown"
- }.must raise_error(Thor::Error, /Could not find "unknown" in source paths/)
+ }.must raise_error(Thor::Error, /Could not find "unknown" in any of your source paths/)
end
it "copies the whole directory recursively to the default destination" do
View
2  spec/actions_spec.rb
@@ -107,7 +107,7 @@ def file
it "raises an error if source path is empty" do
lambda {
A.new.find_in_source_paths("foo")
- }.must raise_error(Thor::Error, /You don't have any source path defined for class A/)
+ }.must raise_error(Thor::Error, /Currently you have no source paths/)
end
it "finds a template inside the source path" do
View
4 spec/fixtures/group.thor
@@ -6,9 +6,7 @@ class MyCounter < Thor::Group
from_superclass(:get_from_super, 13)
end
- def self.source_root
- File.expand_path(File.dirname(__FILE__))
- end
+ source_root File.expand_path(File.dirname(__FILE__))
source_paths << File.expand_path("broken", File.dirname(__FILE__))
argument :first, :type => :numeric
Please sign in to comment.
Something went wrong with that request. Please try again.