Skip to content

Commit 0780c70

Browse files
authored
Merge pull request #326 from nobu/update-keyword-args
Update keyword args
2 parents 05e36f1 + baa23cc commit 0780c70

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

Diff for: lib/rake/clean.rb

+2-2
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

Diff for: lib/rake/file_utils.rb

+7-10
Original file line numberDiff line numberDiff line change
@@ -97,30 +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)
114-
if !LN_SUPPORTED[0]
115-
cp(*args)
116-
else
112+
def safe_ln(*args, **options)
113+
if LN_SUPPORTED[0]
117114
begin
118-
ln(*args)
115+
return options.empty? ? ln(*args) : ln(*args, **options)
119116
rescue StandardError, NotImplementedError
120117
LN_SUPPORTED[0] = false
121-
cp(*args)
122118
end
123119
end
120+
options.empty? ? cp(*args) : cp(*args, **options)
124121
end
125122

126123
# Split a file path into individual directory names.

Diff for: lib/rake/file_utils_ext.rb

+6-17
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)

Diff for: lib/rake/task.rb

+5-1
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?

Diff for: test/test_rake_file_utils.rb

+16
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)