Skip to content

Commit

Permalink
Add support for thor bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Dec 13, 2008
1 parent e8dd789 commit a5f6270
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
36 changes: 26 additions & 10 deletions lib/thor/runner.rb
Expand Up @@ -18,8 +18,17 @@ def self.globs_for(path)
method_options :as => :optional, :relative => :boolean
def install(name)
initialize_thorfiles

base = name
package = :file

begin
contents = open(name).read
if File.directory?(File.expand_path(name))
base, package = File.join(name, "main.thor"), :directory
contents = open(base).read
else
contents = open(name).read
end
rescue OpenURI::HTTPError
raise Error, "Error opening URI `#{name}'"
rescue Errno::ENOENT
Expand All @@ -35,9 +44,7 @@ def install(name)

return false unless response =~ /^\s*y/i

constants = Thor::Util.constants_in_contents(contents)

# name = name =~ /\.thor$/ || is_uri ? name : "#{name}.thor"
constants = Thor::Util.constants_in_contents(contents, base)

as = options["as"] || begin
first_line = contents.split("\n")[0]
Expand All @@ -63,8 +70,12 @@ 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 "class Thor\n module Tasks\n#{contents}\n end\nend"
destination = File.join(thor_root, yaml[as][:filename])

if package == :file
File.open(destination, "w") {|f| f.puts contents }
else
FileUtils.cp_r(name, destination)
end

yaml[as][:filename] # Indicate sucess
Expand All @@ -78,7 +89,7 @@ def uninstall(name)
puts "Uninstalling #{name}."

file = File.join(thor_root, "#{yaml[name][:filename]}")
File.delete(file)
FileUtils.rm_rf(file)
yaml.delete(name)
save_yaml(yaml)

Expand Down Expand Up @@ -154,7 +165,7 @@ def self.thor_root_glob
# C:\Documents and Settings\james\.thor
#
# If we don't #gsub the \ character, Dir.glob will fail.
Dir["#{thor_root.gsub(/\\/, '/')}/**/*"]
Dir["#{thor_root.gsub(/\\/, '/')}/*"]
end

private
Expand Down Expand Up @@ -252,8 +263,9 @@ def initialize_thorfiles(relevant_to = nil)
end

def load_thorfile(path)
txt = File.read(path)
begin
load path
Thor::Tasks.class_eval txt, path
rescue Object => e
$stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.message}"
end
Expand All @@ -272,8 +284,12 @@ def thorfiles(relevant_to = nil)

# We want to load system-wide Thorfiles first
# so the local Thorfiles will override them.
(relevant_to ? thorfiles_relevant_to(relevant_to) :
files = (relevant_to ? thorfiles_relevant_to(relevant_to) :
thor_root_glob) + thorfiles - ["#{thor_root}/thor.yml"]

files.map! do |file|
File.directory?(file) ? File.join(file, "main.thor") : file
end
end

def thorfiles_relevant_to(meth)
Expand Down
4 changes: 2 additions & 2 deletions lib/thor/util.rb
Expand Up @@ -49,9 +49,9 @@ def self.to_constant(str)
str.gsub(/:(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

def self.constants_in_contents(str)
def self.constants_in_contents(str, file = __FILE__)
klasses = ObjectSpace.classes.dup
Module.new.class_eval(str)
Module.new.class_eval(str, file)
klasses = ObjectSpace.classes - klasses
klasses = klasses.select {|k| k < Thor }
klasses.map! {|k| k.to_s.gsub(/#<Module:\w+>::/, '')}
Expand Down
12 changes: 7 additions & 5 deletions spec/thor_runner_spec.rb
Expand Up @@ -28,10 +28,12 @@ def zoo
end
end

class Default < Thor
desc "test", "prints 'test'"
def test
puts "test"
module Thor::Tasks
class Default < Thor
desc "test", "prints 'test'"
def test
puts "test"
end
end
end

Expand All @@ -57,7 +59,7 @@ class ThorTask2 < Thor

describe Thor do
it "tracks its subclasses, grouped by the files they come from" do
Thor.subclass_files[File.expand_path(__FILE__)].must == [MyTasks::ThorTask, MyTasks::AdvancedTask, Default, Amazing, ThorTask2]
Thor.subclass_files[File.expand_path(__FILE__)].must == [MyTasks::ThorTask, MyTasks::AdvancedTask, Thor::Tasks::Default, Amazing, ThorTask2]
end

it "tracks a single subclass across multiple files" do
Expand Down

0 comments on commit a5f6270

Please sign in to comment.