Permalink
Browse files

Automatically wrap thor files in Thor::Tasks to avoid top-level pollu…

…tion.
  • Loading branch information...
1 parent e97df90 commit e8dd78972937742439d5805761214751b8066bbd @wycats wycats committed Dec 13, 2008
Showing with 25 additions and 7 deletions.
  1. +3 −3 lib/thor/runner.rb
  2. +1 −1 lib/thor/tasks.rb
  3. +21 −3 lib/thor/util.rb
View
@@ -64,7 +64,7 @@ def install(name)
puts "Storing thor file in your system repository"
File.open(File.join(thor_root, yaml[as][:filename]), "w") do |file|
- file.puts contents
+ file.puts "class Thor\n module Tasks\n#{contents}\n end\nend"
end
yaml[as][:filename] # Indicate sucess
@@ -121,8 +121,8 @@ def list(search = "")
search = ".*#{search}" if options["substring"]
search = /^#{search}.*/i
group = options[:group] || 'standard'
-
- classes = Thor.subclasses.select do |k|
+
+ classes = Thor.subclasses.select do |k|
(options[:all] || k.group_name == group) &&
Thor::Util.constant_to_thor_path(k.name) =~ search
end
View
@@ -22,7 +22,7 @@ def self.install_task(spec)
old_stderr, $stderr = $stderr.dup, File.open(null, "w")
package
$stderr = old_stderr
- system %{#{sudo} #{gem} install pkg/#{spec.name}-#{spec.version} --no-rdoc --no-ri --no-update-sources}
+ system %{#{sudo} #{Gem.ruby} -S #{gem} install pkg/#{spec.name}-#{spec.version} --no-rdoc --no-ri --no-update-sources}
end
end
View
@@ -15,10 +15,24 @@ def classes
end
class Thor
+ module Tasks; end
+
module Util
+ def self.full_const_get(obj, name)
+ list = name.split("::")
+ list.shift if list.first.empty?
+ list.each do |x|
+ # This is required because const_get tries to look for constants in the
+ # ancestor chain, but we only want constants that are HERE
+ obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
+ end
+ obj
+ end
+
def self.constant_to_thor_path(str, remove_default = true)
- str = snake_case(str.to_s).squeeze(":")
+ str = str.to_s.gsub(/^Thor::Tasks::/, "")
+ str = snake_case(str).squeeze(":")
str.gsub!(/^default/, '') if remove_default
str
end
@@ -43,8 +57,12 @@ def self.constants_in_contents(str)
klasses.map! {|k| k.to_s.gsub(/#<Module:\w+>::/, '')}
end
- def self.make_constant(str)
- list = str.split("::").inject(Object) {|obj, x| obj.const_get(x)}
+ def self.make_constant(str, base = [Thor::Tasks, Object])
+ which = base.find do |obj|
+ full_const_get(obj, str) rescue nil
+ end
+ return full_const_get(which, str) if which
+ raise NameError, "uninitialized constant #{str}"
end
def self.snake_case(str)

0 comments on commit e8dd789

Please sign in to comment.