Skip to content

Commit

Permalink
Update vendored thor to 7416806146f252c589cfa0f7c154393bc498090a
Browse files Browse the repository at this point in the history
  • Loading branch information
indirect committed Jun 12, 2011
1 parent c78d29f commit 8b1af1a
Show file tree
Hide file tree
Showing 28 changed files with 913 additions and 85 deletions.
21 changes: 18 additions & 3 deletions lib/bundler/vendor/thor.rb 100755 → 100644
Expand Up @@ -18,6 +18,23 @@ def default_task(meth=nil)
end
end

# Registers another Thor subclass as a command.
#
# ==== Parameters
# klass<Class>:: Thor subclass to register
# command<String>:: Subcommand name to use
# usage<String>:: Short usage for the subcommand
# description<String>:: Description for the subcommand
def register(klass, subcommand_name, usage, description, options={})
if klass <= Thor::Group
desc usage, description, options
define_method(subcommand_name) { invoke klass }
else
desc usage, description, options
subcommand subcommand_name, klass
end
end

# Defines the usage and the description of the next task.
#
# ==== Parameters
Expand Down Expand Up @@ -252,8 +269,7 @@ def dispatch(meth, given_args, given_opts, config) #:nodoc:
# the namespace should be displayed as arguments.
#
def banner(task, namespace = nil, subcommand = false)
base = File.basename($0).split(" ").first
"#{base} #{task.formatted_usage(self, $thor_runner, subcommand)}"
"#{basename} #{task.formatted_usage(self, $thor_runner, subcommand)}"
end

def baseclass #:nodoc:
Expand Down Expand Up @@ -305,7 +321,6 @@ def subcommand_help(cmd)
def help(task = nil, subcommand = true); super; end
RUBY
end

end

include Thor::Base
Expand Down
39 changes: 28 additions & 11 deletions lib/bundler/vendor/thor/actions.rb 100755 → 100644
@@ -1,10 +1,12 @@
require 'fileutils'
require 'uri'
require 'thor/core_ext/file_binary_read'

Dir[File.join(File.dirname(__FILE__), "actions", "*.rb")].each do |action|
require action
end
require 'thor/actions/create_file'
require 'thor/actions/create_link'
require 'thor/actions/directory'
require 'thor/actions/empty_directory'
require 'thor/actions/file_manipulation'
require 'thor/actions/inject_into_file'

class Thor
module Actions
Expand Down Expand Up @@ -158,13 +160,23 @@ def find_in_source_paths(file)
#
def inside(dir='', config={}, &block)
verbose = config.fetch(:verbose, false)
pretend = options[:pretend]

say_status :inside, dir, verbose
shell.padding += 1 if verbose
@destination_stack.push File.expand_path(dir, destination_root)

FileUtils.mkdir_p(destination_root) unless File.exist?(destination_root)
FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
# If the directory doesnt exist and we're not pretending
if !File.exist?(destination_root) && !pretend
FileUtils.mkdir_p(destination_root)
end

if pretend
# In pretend mode, just yield down to the block
block.arity == 1 ? yield(destination_root) : yield
else
FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
end

@destination_stack.pop
shell.padding -= 1 if verbose
Expand Down Expand Up @@ -210,7 +222,7 @@ def apply(path, config={})
#
# ==== Parameters
# command<String>:: the command to be executed.
# config<Hash>:: give :verbose => false to not log the status. Specify :with
# config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output. Specify :with
# to append an executable to command executation.
#
# ==== Example
Expand All @@ -231,7 +243,10 @@ def run(command, config={})
end

say_status :run, desc, config.fetch(:verbose, true)
`#{command}` unless options[:pretend]

unless options[:pretend]
config[:capture] ? `#{command}` : system("#{command}")
end
end

# Executes a ruby script (taking into account WIN32 platform quirks).
Expand All @@ -251,8 +266,9 @@ def run_ruby_script(command, config={})
# ==== Parameters
# task<String>:: the task to be invoked
# args<Array>:: arguments to the task
# config<Hash>:: give :verbose => false to not log the status. Other options
# are given as parameter to Thor.
# config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output.
# Other options are given as parameter to Thor.
#
#
# ==== Examples
#
Expand All @@ -266,12 +282,13 @@ def thor(task, *args)
config = args.last.is_a?(Hash) ? args.pop : {}
verbose = config.key?(:verbose) ? config.delete(:verbose) : true
pretend = config.key?(:pretend) ? config.delete(:pretend) : false
capture = config.key?(:capture) ? config.delete(:capture) : false

args.unshift task
args.push Thor::Options.to_switches(config)
command = args.join(' ').strip

run command, :with => :thor, :verbose => verbose, :pretend => pretend
run command, :with => :thor, :verbose => verbose, :pretend => pretend, :capture => capture
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/bundler/vendor/thor/actions/create_file.rb 100755 → 100644
Expand Up @@ -18,7 +18,7 @@ module Actions
# "vhost.name = #{hostname}"
# end
#
# create_file "config/apach.conf", "your apache config"
# create_file "config/apache.conf", "your apache config"
#
def create_file(destination, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
Expand All @@ -27,7 +27,7 @@ def create_file(destination, *args, &block)
end
alias :add_file :create_file

# AddFile is a subset of Template, which instead of rendering a file with
# CreateFile is a subset of Template, which instead of rendering a file with
# ERB, it gets the content from the user.
#
class CreateFile < EmptyDirectory #:nodoc:
Expand Down
57 changes: 57 additions & 0 deletions lib/bundler/vendor/thor/actions/create_link.rb
@@ -0,0 +1,57 @@
require 'thor/actions/create_file'

class Thor
module Actions

# Create a new file relative to the destination root from the given source.
#
# ==== Parameters
# destination<String>:: the relative path to the destination root.
# source<String|NilClass>:: the relative path to the source root.
# config<Hash>:: give :verbose => false to not log the status.
# :: give :symbolic => false for hard link.
#
# ==== Examples
#
# create_link "config/apache.conf", "/etc/apache.conf"
#
def create_link(destination, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
source = args.first
action CreateLink.new(self, destination, source, config)
end
alias :add_link :create_link

# CreateLink is a subset of CreateFile, which instead of taking a block of
# data, just takes a source string from the user.
#
class CreateLink < CreateFile #:nodoc:
attr_reader :data

# Checks if the content of the file at the destination is identical to the rendered result.
#
# ==== Returns
# Boolean:: true if it is identical, false otherwise.
#
def identical?
exists? && File.identical?(render, destination)
end

def invoke!
invoke_with_conflict_check do
FileUtils.mkdir_p(File.dirname(destination))
# Create a symlink by default
config[:symbolic] ||= true
File.unlink(destination) if exists?
if config[:symbolic]
File.symlink(render, destination)
else
File.link(render, destination)
end
end
given_destination
end

end
end
end
4 changes: 2 additions & 2 deletions lib/bundler/vendor/thor/actions/directory.rb 100755 → 100644
Expand Up @@ -21,7 +21,7 @@ module Actions
# directory "doc"
#
# It will create a doc directory in the destination with the following
# files (assuming that the app_name is "blog"):
# files (assuming that the `app_name` method returns the value "blog"):
#
# doc/
# components/
Expand Down Expand Up @@ -70,7 +70,7 @@ def execute!
lookup = config[:recursive] ? File.join(source, '**') : source
lookup = File.join(lookup, '{*,.[a-z]*}')

Dir[lookup].each do |file_source|
Dir[lookup].sort.each do |file_source|
next if File.directory?(file_source)
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
file_destination.gsub!('/./', '/')
Expand Down
Empty file modified lib/bundler/vendor/thor/actions/empty_directory.rb 100755 → 100644
Empty file.
71 changes: 56 additions & 15 deletions lib/bundler/vendor/thor/actions/file_manipulation.rb 100755 → 100644
Expand Up @@ -30,6 +30,28 @@ def copy_file(source, *args, &block)
end
end

# Links the file from the relative source to the relative destination. If
# the destination is not given it's assumed to be equal to the source.
#
# ==== Parameters
# source<String>:: the relative path to the source root.
# destination<String>:: the relative path to the destination root.
# config<Hash>:: give :verbose => false to not log the status.
#
# ==== Examples
#
# link_file "README", "doc/README"
#
# link_file "doc/README"
#
def link_file(source, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
destination = args.first || source
source = File.expand_path(find_in_source_paths(source.to_s))

create_link destination, source, config
end

# Gets the content at the given address and places it at the given relative
# destination. If a block is given instead of destination, the content of
# the url is yielded and used as location.
Expand All @@ -51,7 +73,7 @@ def get(source, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
destination = args.first

source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^http\:\/\//
source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^https?\:\/\//
render = open(source) {|input| input.binmode.read }

destination ||= if block_given?
Expand Down Expand Up @@ -80,13 +102,13 @@ def get(source, *args, &block)
#
def template(source, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
destination = args.first || source
destination = args.first || source.sub(/\.tt$/, '')

source = File.expand_path(find_in_source_paths(source.to_s))
context = instance_eval('binding')

create_file destination, nil, config do
content = ERB.new(::File.binread(source), nil, '-').result(context)
content = ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context)
content = block.call(content) if block
content
end
Expand All @@ -110,7 +132,7 @@ def chmod(path, mode, config={})
FileUtils.chmod_R(mode, path) unless options[:pretend]
end

# Prepend text to a file. Since it depends on inject_into_file, it's reversible.
# Prepend text to a file. Since it depends on insert_into_file, it's reversible.
#
# ==== Parameters
# path<String>:: path of the file to be changed
Expand All @@ -119,19 +141,20 @@ def chmod(path, mode, config={})
#
# ==== Example
#
# prepend_file 'config/environments/test.rb', 'config.gem "rspec"'
# prepend_to_file 'config/environments/test.rb', 'config.gem "rspec"'
#
# prepend_file 'config/environments/test.rb' do
# prepend_to_file 'config/environments/test.rb' do
# 'config.gem "rspec"'
# end
#
def prepend_file(path, *args, &block)
def prepend_to_file(path, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
config.merge!(:after => /\A/)
inject_into_file(path, *(args << config), &block)
insert_into_file(path, *(args << config), &block)
end
alias_method :prepend_file, :prepend_to_file

# Append text to a file. Since it depends on inject_into_file, it's reversible.
# Append text to a file. Since it depends on insert_into_file, it's reversible.
#
# ==== Parameters
# path<String>:: path of the file to be changed
Expand All @@ -140,20 +163,21 @@ def prepend_file(path, *args, &block)
#
# ==== Example
#
# append_file 'config/environments/test.rb', 'config.gem "rspec"'
# append_to_file 'config/environments/test.rb', 'config.gem "rspec"'
#
# append_file 'config/environments/test.rb' do
# append_to_file 'config/environments/test.rb' do
# 'config.gem "rspec"'
# end
#
def append_file(path, *args, &block)
def append_to_file(path, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
config.merge!(:before => /\z/)
inject_into_file(path, *(args << config), &block)
insert_into_file(path, *(args << config), &block)
end
alias_method :append_file, :append_to_file

# Injects text right after the class definition. Since it depends on
# inject_into_file, it's reversible.
# insert_into_file, it's reversible.
#
# ==== Parameters
# path<String>:: path of the file to be changed
Expand All @@ -172,7 +196,7 @@ def append_file(path, *args, &block)
def inject_into_class(path, klass, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
config.merge!(:after => /class #{klass}\n|class #{klass} .*\n/)
inject_into_file(path, *(args << config), &block)
insert_into_file(path, *(args << config), &block)
end

# Run a regular expression replacement on a file.
Expand Down Expand Up @@ -225,5 +249,22 @@ def remove_file(path, config={})
end
alias :remove_dir :remove_file

private
attr_accessor :output_buffer
def concat(string)
@output_buffer.concat(string)
end

def capture(*args, &block)
with_output_buffer { block.call(*args) }
end

def with_output_buffer(buf = '') #:nodoc:
self.output_buffer, old_buffer = buf, output_buffer
yield
output_buffer
ensure
self.output_buffer = old_buffer
end
end
end

0 comments on commit 8b1af1a

Please sign in to comment.