mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
parent
124cd77470
commit
862b30287a
7 changed files with 52 additions and 20 deletions
|
@ -242,6 +242,35 @@ class Bundler::Thor
|
|||
insert_into_file(path, *(args << config), &block)
|
||||
end
|
||||
|
||||
# Run a regular expression replacement on a file, raising an error if the
|
||||
# contents of the file are not changed.
|
||||
#
|
||||
# ==== Parameters
|
||||
# path<String>:: path of the file to be changed
|
||||
# flag<Regexp|String>:: the regexp or string to be replaced
|
||||
# replacement<String>:: the replacement, can be also given as a block
|
||||
# config<Hash>:: give :verbose => false to not log the status, and
|
||||
# :force => true, to force the replacement regardless of runner behavior.
|
||||
#
|
||||
# ==== Example
|
||||
#
|
||||
# gsub_file! 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'
|
||||
#
|
||||
# gsub_file! 'README', /rake/, :green do |match|
|
||||
# match << " no more. Use thor!"
|
||||
# end
|
||||
#
|
||||
def gsub_file!(path, flag, *args, &block)
|
||||
config = args.last.is_a?(Hash) ? args.pop : {}
|
||||
|
||||
return unless behavior == :invoke || config.fetch(:force, false)
|
||||
|
||||
path = File.expand_path(path, destination_root)
|
||||
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
|
||||
|
||||
actually_gsub_file(path, flag, args, true, &block) unless options[:pretend]
|
||||
end
|
||||
|
||||
# Run a regular expression replacement on a file.
|
||||
#
|
||||
# ==== Parameters
|
||||
|
@ -267,11 +296,7 @@ class Bundler::Thor
|
|||
path = File.expand_path(path, destination_root)
|
||||
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
|
||||
|
||||
unless options[:pretend]
|
||||
content = File.binread(path)
|
||||
content.gsub!(flag, *args, &block)
|
||||
File.open(path, "wb") { |file| file.write(content) }
|
||||
end
|
||||
actually_gsub_file(path, flag, args, false, &block) unless options[:pretend]
|
||||
end
|
||||
|
||||
# Uncomment all lines matching a given regex. Preserves indentation before
|
||||
|
@ -348,7 +373,7 @@ class Bundler::Thor
|
|||
end
|
||||
|
||||
def with_output_buffer(buf = "".dup) #:nodoc:
|
||||
raise ArgumentError, "Buffer can not be a frozen object" if buf.frozen?
|
||||
raise ArgumentError, "Buffer cannot be a frozen object" if buf.frozen?
|
||||
old_buffer = output_buffer
|
||||
self.output_buffer = buf
|
||||
yield
|
||||
|
@ -357,6 +382,17 @@ class Bundler::Thor
|
|||
self.output_buffer = old_buffer
|
||||
end
|
||||
|
||||
def actually_gsub_file(path, flag, args, error_on_no_change, &block)
|
||||
content = File.binread(path)
|
||||
success = content.gsub!(flag, *args, &block)
|
||||
|
||||
if success.nil? && error_on_no_change
|
||||
raise Bundler::Thor::Error, "The content of #{path} did not change"
|
||||
end
|
||||
|
||||
File.open(path, "wb") { |file| file.write(content) }
|
||||
end
|
||||
|
||||
# Bundler::Thor::Actions#capture depends on what kind of buffer is used in ERB.
|
||||
# Thus CapturableERB fixes ERB to use String buffer.
|
||||
class CapturableERB < ERB
|
||||
|
|
|
@ -144,7 +144,7 @@ class Bundler::Thor
|
|||
def check_exclusive!
|
||||
opts = @assigns.keys
|
||||
# When option A and B are exclusive, if A and B are given at the same time,
|
||||
# the diffrence of argument array size will decrease.
|
||||
# the difference of argument array size will decrease.
|
||||
found = @exclusives.find{ |ex| (ex - opts).size < ex.size - 1 }
|
||||
if found
|
||||
names = names_to_switch_names(found & opts).map{|n| "'#{n}'"}
|
||||
|
|
2
lib/bundler/vendor/thor/lib/thor/runner.rb
vendored
2
lib/bundler/vendor/thor/lib/thor/runner.rb
vendored
|
@ -1,7 +1,6 @@
|
|||
require_relative "../thor"
|
||||
require_relative "group"
|
||||
|
||||
require "yaml"
|
||||
require "digest/sha2"
|
||||
require "pathname"
|
||||
|
||||
|
@ -195,6 +194,7 @@ private
|
|||
def thor_yaml
|
||||
@thor_yaml ||= begin
|
||||
yaml_file = File.join(thor_root, "thor.yml")
|
||||
require "yaml"
|
||||
yaml = YAML.load_file(yaml_file) if File.exist?(yaml_file)
|
||||
yaml || {}
|
||||
end
|
||||
|
|
10
lib/bundler/vendor/thor/lib/thor/shell/basic.rb
vendored
10
lib/bundler/vendor/thor/lib/thor/shell/basic.rb
vendored
|
@ -314,7 +314,7 @@ class Bundler::Thor
|
|||
diff_cmd = ENV["THOR_DIFF"] || ENV["RAILS_DIFF"] || "diff -u"
|
||||
|
||||
require "tempfile"
|
||||
Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
|
||||
Tempfile.open(File.basename(destination), File.dirname(destination), binmode: true) do |temp|
|
||||
temp.write content
|
||||
temp.rewind
|
||||
system %(#{diff_cmd} "#{destination}" "#{temp.path}")
|
||||
|
@ -372,16 +372,12 @@ class Bundler::Thor
|
|||
Tempfile.open([File.basename(destination), File.extname(destination)], File.dirname(destination)) do |temp|
|
||||
temp.write content
|
||||
temp.rewind
|
||||
system %(#{merge_tool} "#{temp.path}" "#{destination}")
|
||||
system(merge_tool, temp.path, destination)
|
||||
end
|
||||
end
|
||||
|
||||
def merge_tool #:nodoc:
|
||||
@merge_tool ||= ENV["THOR_MERGE"] || git_merge_tool
|
||||
end
|
||||
|
||||
def git_merge_tool #:nodoc:
|
||||
`git config merge.tool`.rstrip rescue ""
|
||||
@merge_tool ||= ENV["THOR_MERGE"] || "git difftool --no-index"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
2
lib/bundler/vendor/thor/lib/thor/version.rb
vendored
2
lib/bundler/vendor/thor/lib/thor/version.rb
vendored
|
@ -1,3 +1,3 @@
|
|||
class Bundler::Thor
|
||||
VERSION = "1.3.2"
|
||||
VERSION = "1.4.0"
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue