Skip to content

Commit baa23cc

Browse files
committed
Update keyword arguments merger
1 parent 6c0626d commit baa23cc

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

lib/rake/clean.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def cleanup_files(file_names)
2828
end
2929
end
3030

31-
def cleanup(file_name, opts={})
31+
def cleanup(file_name, **opts)
3232
begin
3333
opts = { verbose: Rake.application.options.trace }.merge(opts)
34-
rm_r file_name, opts
34+
rm_r file_name, **opts
3535
rescue StandardError => ex
3636
puts "Failed to remove #{file_name}: #{ex}" unless file_already_gone?(file_name)
3737
end

lib/rake/file_utils.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,27 @@ def set_verbose_option(options) # :nodoc:
9797
# Example:
9898
# ruby %{-pe '$_.upcase!' <README}
9999
#
100-
def ruby(*args, &block)
101-
options = (Hash === args.last) ? args.pop : {}
100+
def ruby(*args, **options, &block)
102101
if args.length > 1
103-
sh(*([RUBY] + args + [options]), &block)
102+
sh(RUBY, *args, **options, &block)
104103
else
105-
sh("#{RUBY} #{args.first}", options, &block)
104+
sh("#{RUBY} #{args.first}", **options, &block)
106105
end
107106
end
108107

109108
LN_SUPPORTED = [true]
110109

111110
# Attempt to do a normal file link, but fall back to a copy if the link
112111
# fails.
113-
def safe_ln(*args)
112+
def safe_ln(*args, **options)
114113
if LN_SUPPORTED[0]
115114
begin
116-
return ln(*args)
115+
return options.empty? ? ln(*args) : ln(*args, **options)
117116
rescue StandardError, NotImplementedError
118117
LN_SUPPORTED[0] = false
119118
end
120119
end
121-
cp(*args)
120+
options.empty? ? cp(*args) : cp(*args, **options)
122121
end
123122

124123
# Split a file path into individual directory names.

lib/rake/file_utils_ext.rb

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@ class << self
2323
opts = FileUtils.options_of name
2424
default_options = []
2525
if opts.include?("verbose")
26-
default_options << ":verbose => FileUtilsExt.verbose_flag"
26+
default_options << "verbose: FileUtilsExt.verbose_flag"
2727
end
2828
if opts.include?("noop")
29-
default_options << ":noop => FileUtilsExt.nowrite_flag"
29+
default_options << "noop: FileUtilsExt.nowrite_flag"
3030
end
3131

3232
next if default_options.empty?
3333
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
34-
def #{name}( *args, &block )
35-
super(
36-
*rake_merge_option(args,
37-
#{default_options.join(', ')}
38-
), &block)
34+
def #{name}(*args, **options, &block)
35+
super(*args,
36+
#{default_options.join(', ')},
37+
**options, &block)
3938
end
4039
EOS
4140
end
@@ -113,16 +112,6 @@ def when_writing(msg=nil)
113112
end
114113
end
115114

116-
# Merge the given options with the default values.
117-
def rake_merge_option(args, defaults)
118-
if Hash === args.last
119-
defaults.update(args.last)
120-
args.pop
121-
end
122-
args.push defaults
123-
args
124-
end
125-
126115
# Send the message to the default rake output (which is $stderr).
127116
def rake_output_message(message)
128117
$stderr.puts(message)

lib/rake/task.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,11 @@ def execute(args=nil)
274274
end
275275
application.trace "** Execute #{name}" if application.options.trace
276276
application.enhance_with_matching_rule(name) if @actions.empty?
277-
@actions.each { |act| act.call(self, args) }
277+
if opts = Hash.try_convert(args) and !opts.empty?
278+
@actions.each { |act| act.call(self, args, **opts)}
279+
else
280+
@actions.each { |act| act.call(self, args)}
281+
end
278282
end
279283

280284
# Is this task needed?

test/test_rake_file_utils.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ def test_rm_filelist
3939
refute File.exist?("b")
4040
end
4141

42+
def test_rm_nowrite
43+
create_file("a")
44+
nowrite(true) {
45+
rm_rf "a"
46+
}
47+
assert File.exist?("a")
48+
nowrite(false) {
49+
rm_rf "a", noop: true
50+
}
51+
assert File.exist?("a")
52+
nowrite(true) {
53+
rm_rf "a", noop: false
54+
}
55+
refute File.exist?("a")
56+
end
57+
4258
def test_ln
4359
open("a", "w") { |f| f.puts "TEST_LN" }
4460

0 commit comments

Comments
 (0)