Permalink
Browse files

Add support for thor bundles

  • Loading branch information...
wycats committed Dec 13, 2008
1 parent e8dd789 commit a5f6270592c67ed4295ac044b90b507a2f943dc0
Showing with 35 additions and 17 deletions.
  1. +26 −10 lib/thor/runner.rb
  2. +2 −2 lib/thor/util.rb
  3. +7 −5 spec/thor_runner_spec.rb
View
@@ -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
@@ -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]
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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)
View
@@ -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+>::/, '')}
View
@@ -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
@@ -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

0 comments on commit a5f6270

Please sign in to comment.