Skip to content

Commit

Permalink
Add safe invocation CLI syntax
Browse files Browse the repository at this point in the history
SUch syntax is useful for projects like buildpacks
and other automated CI/CD tools.

It would allow these tools to very easily invoke a task
if it exists, without failing if it doesn't.
  • Loading branch information
byroot committed Feb 17, 2021
1 parent c2eeae2 commit 0824cce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,21 @@ def thread_pool # :nodoc:

# Invokes a task with arguments that are extracted from +task_string+
def invoke_task(task_string) # :nodoc:
name, args = parse_task_string(task_string)
name, args, safe_call = parse_task_string(task_string)
return if safe_call && !lookup(name)

t = self[name]
t.invoke(*args)
end

def parse_task_string(string) # :nodoc:
/^([^\[]+)(?:\[(.*)\])$/ =~ string.to_s

/^([^\[]+)(?:\[(.*)\])(\?)?$/ =~ string.to_s
name = $1
remaining_args = $2
safe_call = !!$~.begin(3)

return string, [] unless name
return name, [] if remaining_args.empty?
return string, [], safe_call unless name
return name, [], safe_call if remaining_args.empty?

args = []

Expand All @@ -178,7 +180,7 @@ def parse_task_string(string) # :nodoc:
args << $1.gsub(/\\(.)/, '\1')
end while remaining_args

return name, args
return name, args, safe_call
end

# Provide standard exception handling for the given block.
Expand Down
20 changes: 20 additions & 0 deletions test/test_rake_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,26 @@ def test_good_run
assert_equal "DEFAULT\n", out
end

def test_safe_run
ran = false

@app.options.silent = true

@app.instance_eval do
intern(Rake::Task, "default").enhance { ran = true }
end

rakefile_default

out, err = capture_io do
@app.run %w[--rakelib="" default missing?]
end

assert ran
assert_empty err
assert_equal "DEFAULT\n", out
end

def test_display_task_run
ran = false
@app.last_description = "COMMENT"
Expand Down

0 comments on commit 0824cce

Please sign in to comment.