Skip to content

Commit

Permalink
Update keyword arguments merger
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Sep 1, 2019
1 parent 6c0626d commit baa23cc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/rake/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def cleanup_files(file_names)
end
end

def cleanup(file_name, opts={})
def cleanup(file_name, **opts)
begin
opts = { verbose: Rake.application.options.trace }.merge(opts)
rm_r file_name, opts
rm_r file_name, **opts
rescue StandardError => ex
puts "Failed to remove #{file_name}: #{ex}" unless file_already_gone?(file_name)
end
Expand Down
13 changes: 6 additions & 7 deletions lib/rake/file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,27 @@ def set_verbose_option(options) # :nodoc:
# Example:
# ruby %{-pe '$_.upcase!' <README}
#
def ruby(*args, &block)
options = (Hash === args.last) ? args.pop : {}
def ruby(*args, **options, &block)
if args.length > 1
sh(*([RUBY] + args + [options]), &block)
sh(RUBY, *args, **options, &block)
else
sh("#{RUBY} #{args.first}", options, &block)
sh("#{RUBY} #{args.first}", **options, &block)
end
end

LN_SUPPORTED = [true]

# Attempt to do a normal file link, but fall back to a copy if the link
# fails.
def safe_ln(*args)
def safe_ln(*args, **options)
if LN_SUPPORTED[0]
begin
return ln(*args)
return options.empty? ? ln(*args) : ln(*args, **options)
rescue StandardError, NotImplementedError
LN_SUPPORTED[0] = false
end
end
cp(*args)
options.empty? ? cp(*args) : cp(*args, **options)
end

# Split a file path into individual directory names.
Expand Down
23 changes: 6 additions & 17 deletions lib/rake/file_utils_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ class << self
opts = FileUtils.options_of name
default_options = []
if opts.include?("verbose")
default_options << ":verbose => FileUtilsExt.verbose_flag"
default_options << "verbose: FileUtilsExt.verbose_flag"
end
if opts.include?("noop")
default_options << ":noop => FileUtilsExt.nowrite_flag"
default_options << "noop: FileUtilsExt.nowrite_flag"
end

next if default_options.empty?
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{name}( *args, &block )
super(
*rake_merge_option(args,
#{default_options.join(', ')}
), &block)
def #{name}(*args, **options, &block)
super(*args,
#{default_options.join(', ')},
**options, &block)
end
EOS
end
Expand Down Expand Up @@ -113,16 +112,6 @@ def when_writing(msg=nil)
end
end

# Merge the given options with the default values.
def rake_merge_option(args, defaults)
if Hash === args.last
defaults.update(args.last)
args.pop
end
args.push defaults
args
end

# Send the message to the default rake output (which is $stderr).
def rake_output_message(message)
$stderr.puts(message)
Expand Down
6 changes: 5 additions & 1 deletion lib/rake/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ def execute(args=nil)
end
application.trace "** Execute #{name}" if application.options.trace
application.enhance_with_matching_rule(name) if @actions.empty?
@actions.each { |act| act.call(self, args) }
if opts = Hash.try_convert(args) and !opts.empty?
@actions.each { |act| act.call(self, args, **opts)}
else
@actions.each { |act| act.call(self, args)}
end
end

# Is this task needed?
Expand Down
16 changes: 16 additions & 0 deletions test/test_rake_file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ def test_rm_filelist
refute File.exist?("b")
end

def test_rm_nowrite
create_file("a")
nowrite(true) {
rm_rf "a"
}
assert File.exist?("a")
nowrite(false) {
rm_rf "a", noop: true
}
assert File.exist?("a")
nowrite(true) {
rm_rf "a", noop: false
}
refute File.exist?("a")
end

def test_ln
open("a", "w") { |f| f.puts "TEST_LN" }

Expand Down

0 comments on commit baa23cc

Please sign in to comment.