Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Bump thor to 0.20.3
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Mar 25, 2019
1 parent cd7a861 commit 16223dc
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 25 deletions.
18 changes: 14 additions & 4 deletions lib/bundler/vendor/thor/lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ def destination_root=(root)
# the script started).
#
def relative_to_original_destination_root(path, remove_dot = true)
path = path.dup
if path.gsub!(@destination_stack[0], ".")
root = @destination_stack[0]
if path.start_with?(root) && [File::SEPARATOR, File::ALT_SEPARATOR, nil, ''].include?(path[root.size..root.size])
path = path.dup
path[0...root.size] = '.'
remove_dot ? (path[2..-1] || "") : path
else
path
Expand Down Expand Up @@ -217,6 +219,7 @@ def apply(path, config = {})
shell.padding += 1 if verbose

contents = if is_uri
require "open-uri"
open(path, "Accept" => "application/x-thor-template", &:read)
else
open(path, &:read)
Expand Down Expand Up @@ -252,9 +255,16 @@ def run(command, config = {})

say_status :run, desc, config.fetch(:verbose, true)

unless options[:pretend]
config[:capture] ? `#{command}` : system(command.to_s)
return if options[:pretend]

result = config[:capture] ? `#{command}` : system(command.to_s)

if config[:abort_on_failure]
success = config[:capture] ? $?.success? : result
abort unless success
end

result
end

# Executes a ruby script (taking into account WIN32 platform quirks).
Expand Down
13 changes: 11 additions & 2 deletions lib/bundler/vendor/thor/lib/thor/actions/file_manipulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def link_file(source, *args)
# destination. If a block is given instead of destination, the content of
# the url is yielded and used as location.
#
# +get+ relies on open-uri, so passing application user input would provide
# a command injection attack vector.
#
# ==== Parameters
# source<String>:: the address of the given content.
# destination<String>:: the relative path to the destination root.
Expand Down Expand Up @@ -117,7 +120,13 @@ def template(source, *args, &block)
context = config.delete(:context) || instance_eval("binding")

create_file destination, nil, config do
content = CapturableERB.new(::File.binread(source), nil, "-", "@output_buffer").tap do |erb|
match = ERB.version.match(/(\d+\.\d+\.\d+)/)
capturable_erb = if match && match[1] >= "2.2.0" # Ruby 2.6+
CapturableERB.new(::File.binread(source), :trim_mode => "-", :eoutvar => "@output_buffer")
else
CapturableERB.new(::File.binread(source), nil, "-", "@output_buffer")
end
content = capturable_erb.tap do |erb|
erb.filename = source
end.result(context)
content = yield(content) if block
Expand Down Expand Up @@ -301,7 +310,7 @@ def uncomment_lines(path, flag, *args)
def comment_lines(path, flag, *args)
flag = flag.respond_to?(:source) ? flag.source : flag

gsub_file(path, /^(\s*)([^#|\n]*#{flag})/, '\1# \2', *args)
gsub_file(path, /^(\s*)([^#\n]*#{flag})/, '\1# \2', *args)
end

# Removes a file at the given location.
Expand Down
7 changes: 3 additions & 4 deletions lib/bundler/vendor/thor/lib/thor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,13 @@ def start(given_args = ARGV, config = {})
dispatch(nil, given_args.dup, nil, config)
rescue Bundler::Thor::Error => e
config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
exit(1) if exit_on_failure?
exit(false) if exit_on_failure?
rescue Errno::EPIPE
# This happens if a thor command is piped to something like `head`,
# which closes the pipe when it's done reading. This will also
# mean that if the pipe is closed, further unnecessary
# computation will not occur.
exit(0)
exit(true)
end

# Allows to use private methods from parent in child classes as commands.
Expand All @@ -493,8 +493,7 @@ def public_command(*names)
alias_method :public_task, :public_command

def handle_no_command_error(command, has_namespace = $thor_runner) #:nodoc:
raise UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace." if has_namespace
raise UndefinedCommandError, "Could not find command #{command.inspect}."
raise UndefinedCommandError.new(command, all_commands.keys, (namespace if has_namespace))
end
alias_method :handle_no_task_error, :handle_no_command_error

Expand Down
82 changes: 82 additions & 0 deletions lib/bundler/vendor/thor/lib/thor/error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
class Bundler::Thor
Correctable =
begin
require 'did_you_mean'

# In order to support versions of Ruby that don't have keyword
# arguments, we need our own spell checker class that doesn't take key
# words. Even though this code wouldn't be hit because of the check
# above, it's still necessary because the interpreter would otherwise be
# unable to parse the file.
class NoKwargSpellChecker < DidYouMean::SpellChecker # :nodoc:
def initialize(dictionary)
@dictionary = dictionary
end
end

DidYouMean::Correctable
rescue LoadError, NameError
end

# Bundler::Thor::Error is raised when it's caused by wrong usage of thor classes. Those
# errors have their backtrace suppressed and are nicely shown to the user.
#
Expand All @@ -10,6 +29,35 @@ class Error < StandardError

# Raised when a command was not found.
class UndefinedCommandError < Error
class SpellChecker
attr_reader :error

def initialize(error)
@error = error
end

def corrections
@corrections ||= spell_checker.correct(error.command).map(&:inspect)
end

def spell_checker
NoKwargSpellChecker.new(error.all_commands)
end
end

attr_reader :command, :all_commands

def initialize(command, all_commands, namespace)
@command = command
@all_commands = all_commands

message = "Could not find command #{command.inspect}"
message = namespace ? "#{message} in #{namespace.inspect} namespace." : "#{message}."

super(message)
end

prepend Correctable if Correctable
end
UndefinedTaskError = UndefinedCommandError

Expand All @@ -22,11 +70,45 @@ class InvocationError < Error
end

class UnknownArgumentError < Error
class SpellChecker
attr_reader :error

def initialize(error)
@error = error
end

def corrections
@corrections ||=
error.unknown.flat_map { |unknown| spell_checker.correct(unknown) }.uniq.map(&:inspect)
end

def spell_checker
@spell_checker ||= NoKwargSpellChecker.new(error.switches)
end
end

attr_reader :switches, :unknown

def initialize(switches, unknown)
@switches = switches
@unknown = unknown

super("Unknown switches #{unknown.map(&:inspect).join(', ')}")
end

prepend Correctable if Correctable
end

class RequiredArgumentMissingError < InvocationError
end

class MalformattedArgumentError < InvocationError
end

if Correctable
DidYouMean::SPELL_CHECKERS.merge!(
'Bundler::Thor::UndefinedCommandError' => UndefinedCommandError::SpellChecker,
'Bundler::Thor::UnknownArgumentError' => UnknownArgumentError::SpellChecker
)
end
end
4 changes: 2 additions & 2 deletions lib/bundler/vendor/thor/lib/thor/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def invoke(*names, &block)
invocations[name] = false
invocation_blocks[name] = block if block_given?

class_eval <<-METHOD, __FILE__, __LINE__
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def _invoke_#{name.to_s.gsub(/\W/, '_')}
klass, command = self.class.prepare_for_invocation(nil, #{name.inspect})
Expand Down Expand Up @@ -120,7 +120,7 @@ def invoke_from_option(*names, &block)
invocations[name] = true
invocation_blocks[name] = block if block_given?

class_eval <<-METHOD, __FILE__, __LINE__
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def _invoke_from_option_#{name.to_s.gsub(/\W/, '_')}
return unless options[#{name.inspect}]
Expand Down
9 changes: 7 additions & 2 deletions lib/bundler/vendor/thor/lib/thor/parser/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disabl
@shorts = {}
@switches = {}
@extra = []
@stopped_parsing_after_extra_index = nil

options.each do |option|
@switches[option.switch_name] = option
Expand All @@ -66,6 +67,7 @@ def peek
if result == OPTS_END
shift
@parsing_options = false
@stopped_parsing_after_extra_index ||= @extra.size
super
else
result
Expand Down Expand Up @@ -99,6 +101,7 @@ def parse(args) # rubocop:disable MethodLength
elsif @stop_on_unknown
@parsing_options = false
@extra << shifted
@stopped_parsing_after_extra_index ||= @extra.size
@extra << shift while peek
break
elsif match
Expand All @@ -120,9 +123,11 @@ def parse(args) # rubocop:disable MethodLength
end

def check_unknown!
to_check = @stopped_parsing_after_extra_index ? @extra[0...@stopped_parsing_after_extra_index] : @extra

# an unknown option starts with - or -- and has no more --'s afterward.
unknown = @extra.select { |str| str =~ /^--?(?:(?!--).)*$/ }
raise UnknownArgumentError, "Unknown switches '#{unknown.join(', ')}'" unless unknown.empty?
unknown = to_check.select { |str| str =~ /^--?(?:(?!--).)*$/ }
raise UnknownArgumentError.new(@switches.keys, unknown) unless unknown.empty?
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/bundler/vendor/thor/lib/thor/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "bundler/vendor/thor/lib/thor/core_ext/io_binary_read"

require "yaml"
require "digest"
require "digest/md5"
require "pathname"

class Bundler::Thor::Runner < Bundler::Thor #:nodoc: # rubocop:disable ClassLength
Expand Down Expand Up @@ -90,7 +90,7 @@ def install(name) # rubocop:disable MethodLength
end

thor_yaml[as] = {
:filename => Digest(:MD5).hexdigest(name + as),
:filename => Digest::MD5.hexdigest(name + as),
:location => location,
:namespaces => Bundler::Thor::Util.namespaces_in_content(contents, base)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/vendor/thor/lib/thor/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def shell

# Common methods that are delegated to the shell.
SHELL_DELEGATED_METHODS.each do |method|
module_eval <<-METHOD, __FILE__, __LINE__
module_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method}(*args,&block)
shell.#{method}(*args,&block)
end
Expand Down
Loading

0 comments on commit 16223dc

Please sign in to comment.