Permalink
Browse files

thor thor thor

  • Loading branch information...
indirect committed Apr 25, 2013
1 parent 7411b80 commit a7031aa429971d3854c13eaf9e3a1f9235a2f922
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,6 +1,6 @@
require 'fileutils'
require 'uri'
-require 'thor/core_ext/file_binary_read'
+require 'thor/core_ext/io_binary_read'
require 'thor/actions/create_file'
require 'thor/actions/create_link'
require 'thor/actions/directory'
@@ -73,13 +73,13 @@ def add_runtime_options!
#
def initialize(args=[], options={}, config={})
self.behavior = case config[:behavior].to_s
- when "force", "skip"
- _cleanup_options_and_set(options, config[:behavior])
- :invoke
- when "revoke"
- :revoke
- else
- :invoke
+ when "force", "skip"
+ _cleanup_options_and_set(options, config[:behavior])
+ :invoke
+ when "revoke"
+ :revoke
+ else
+ :invoke
end
super
@@ -227,7 +227,7 @@ def apply(path, config={})
# ==== Parameters
# command<String>:: the command to be executed.
# 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.
+ # to append an executable to command execution.
#
# ==== Example
#
@@ -268,8 +268,8 @@ def run_ruby_script(command, config={})
# switches.
#
# ==== Parameters
- # task<String>:: the task to be invoked
- # args<Array>:: arguments to the task
+ # command<String>:: the command to be invoked
+ # args<Array>:: arguments to the command
# config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output.
# Other options are given as parameter to Thor.
#
@@ -282,13 +282,13 @@ def run_ruby_script(command, config={})
# thor :list, :all => true, :substring => 'rails'
# #=> thor list --all --substring=rails
#
- def thor(task, *args)
+ def thor(command, *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.unshift(command)
args.push Thor::Options.to_switches(config)
command = args.join(' ').strip
@@ -305,12 +305,12 @@ def _shared_configuration #:nodoc:
def _cleanup_options_and_set(options, key) #:nodoc:
case options
- when Array
- %w(--force -f --skip -s).each { |i| options.delete(i) }
- options << "--#{key}"
- when Hash
- [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
- options.merge!(key => true)
+ when Array
+ %w(--force -f --skip -s).each { |i| options.delete(i) }
+ options << "--#{key}"
+ when Hash
+ [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
+ options.merge!(key => true)
end
end
@@ -52,6 +52,9 @@ def invoke!
given_destination
end
+ def exists?
+ super || File.symlink?(destination)
+ end
end
end
end
@@ -39,6 +39,7 @@ module Actions
# config<Hash>:: give :verbose => false to not log the status.
# If :recursive => false, does not look for paths recursively.
# If :mode => :preserve, preserve the file mode from the source.
+ # If :exclude_pattern => /regexp/, prevents copying files that match that regexp.
#
# ==== Examples
#
@@ -74,26 +75,45 @@ def revoke!
def execute!
lookup = Util.escape_globs(source)
lookup = config[:recursive] ? File.join(lookup, '**') : lookup
- lookup = File.join(lookup, '{*,.[a-z]*}')
+ lookup = file_level_lookup(lookup)
- Dir[lookup].sort.each do |file_source|
+ files(lookup).sort.each do |file_source|
next if File.directory?(file_source)
+ next if config[:exclude_pattern] && file_source.match(config[:exclude_pattern])
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
file_destination.gsub!('/./', '/')
case file_source
- when /\.empty_directory$/
- dirname = File.dirname(file_destination).gsub(/\/\.$/, '')
- next if dirname == given_destination
- base.empty_directory(dirname, config)
- when /\.tt$/
- destination = base.template(file_source, file_destination[0..-4], config, &@block)
- else
- destination = base.copy_file(file_source, file_destination, config, &@block)
+ when /\.empty_directory$/
+ dirname = File.dirname(file_destination).gsub(/\/\.$/, '')
+ next if dirname == given_destination
+ base.empty_directory(dirname, config)
+ when /\.tt$/
+ destination = base.template(file_source, file_destination[0..-4], config, &@block)
+ else
+ destination = base.copy_file(file_source, file_destination, config, &@block)
end
end
end
+ if RUBY_VERSION < '2.0'
+ def file_level_lookup(previous_lookup)
+ File.join(previous_lookup, '{*,.[a-z]*}')
+ end
+
+ def files(lookup)
+ Dir[lookup]
+ end
+ else
+ def file_level_lookup(previous_lookup)
+ File.join(previous_lookup, '*')
+ end
+
+ def files(lookup)
+ Dir.glob(lookup, File::FNM_DOTMATCH)
+ end
+ end
+
end
end
end
@@ -97,28 +97,12 @@ def destination=(destination)
#
# user.rb
#
- # The method referenced by %-string SHOULD be public. Otherwise you
- # get the exception with the corresponding error message.
+ # The method referenced can be either public or private.
#
def convert_encoded_instructions(filename)
filename.gsub(/%(.*?)%/) do |initial_string|
- call_public_method($1.strip) or initial_string
- end
- end
-
- # Calls `base`'s public method `sym`.
- # Returns:: result of `base.sym` or `nil` if `sym` wasn't found in
- # `base`
- # Raises:: Thor::PrivateMethodEncodedError if `sym` references
- # a private method.
- def call_public_method(sym)
- if base.respond_to?(sym)
- base.send(sym)
- elsif base.respond_to?(sym, true)
- raise Thor::PrivateMethodEncodedError,
- "Method #{base.class}##{sym} should be public, not private"
- else
- nil
+ method = $1.strip
+ base.respond_to?(method, true) ? base.send(method) : initial_string
end
end
@@ -129,7 +129,7 @@ def template(source, *args, &block)
#
# ==== Example
#
- # chmod "script/*", 0755
+ # chmod "script/server", 0755
#
def chmod(path, mode, config={})
return unless behavior == :invoke
@@ -251,7 +251,7 @@ def gsub_file(path, flag, *args, &block)
def uncomment_lines(path, flag, *args)
flag = flag.respond_to?(:source) ? flag.source : flag
- gsub_file(path, /^(\s*)#\s*(.*#{flag})/, '\1\2', *args)
+ gsub_file(path, /^(\s*)#[[:blank:]]*(.*#{flag})/, '\1\2', *args)
end
# Comment all lines matching a given regex. It will leave the space
@@ -293,8 +293,11 @@ def remove_file(path, config={})
end
alias :remove_dir :remove_file
- attr_accessor :output_buffer
+ attr_accessor :output_buffer
+ private :output_buffer, :output_buffer=
+
private
+
def concat(string)
@output_buffer.concat(string)
end
Oops, something went wrong.

0 comments on commit a7031aa

Please sign in to comment.