Skip to content

Commit

Permalink
Added task arguments
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/rake/trunk@587 5af023f1-ac1a-0410-98d6-829a145c37ef
  • Loading branch information
jimweirich committed Apr 27, 2007
1 parent 4b2de6d commit 9ca2c3d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
= Rake Changelog

== Pre-version 0.7.3
== Pre-Version 0.7.4

* Added task parameters (e.g. "rake build[version7]")

== Version 0.7.3

* Added existing and existing! methods to FileList
* FileLists now claim to be Arrays (via is_a?) to get better support
Expand Down
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ desc "Default Task"
task :default => :test_all

# Test Tasks ---------------------------------------------------------
task :dbg do |t|
puts "Arguments are: #{t.args.join(', ')}"
end

# Common Abbreviations ...

Expand Down
31 changes: 29 additions & 2 deletions lib/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# as a library via a require statement, but it can be distributed
# independently as an application.

RAKEVERSION = '0.7.3'
RAKEVERSION = '0.7.3.1'

require 'rbconfig'
require 'ftools'
Expand Down Expand Up @@ -308,6 +308,9 @@ class Task

# Array of nested namespaces names used for task lookup by this task.
attr_reader :scope

# List of arguments to the task
attr_accessor :args

# Return task name
def to_s
Expand Down Expand Up @@ -336,6 +339,7 @@ def initialize(task_name, app)
@lock = Mutex.new
@application = app
@scope = app.current_scope
@args = []
end

# Enhance a task with prerequisites or actions. Returns self.
Expand Down Expand Up @@ -1736,7 +1740,7 @@ def top_level
elsif options.show_prereqs
display_prerequisites
else
top_level_tasks.each { |task_name| self[task_name].invoke }
top_level_tasks.each { |task_name| invoke_task(task_name) }
end
end
end
Expand All @@ -1755,6 +1759,29 @@ def options

# private ----------------------------------------------------------------

def invoke_task(task_string)
name, args = parse_task_string(task_string)
t = self[name]
t.args = args if args
t.invoke
t.args = []
end

def parse_task_string(string)
if string =~ /^([^\[]+)(\[(.*)\])?$/
name = $1
if $3
args = $3.split(/\s*,\s*/)
else
args = []
end
else
name = string
args = []
end
[name, args]
end

# Provide standard execption handling for the given block.
def standard_exception_handling
begin
Expand Down
42 changes: 42 additions & 0 deletions test/test_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,45 @@ def @app.exit(*args)
end
end

class TestTaskArgumentParsing < Test::Unit::TestCase
def setup
@app = Rake::Application.new
end

def test_name_only
name, args = @app.parse_task_string("name")
assert_equal "name", name
assert_equal [], args
end

def test_empty_args
name, args = @app.parse_task_string("name[]")
assert_equal "name", name
assert_equal [], args
end

def test_one_argument
name, args = @app.parse_task_string("name[one]")
assert_equal "name", name
assert_equal ["one"], args
end

def test_two_arguments
name, args = @app.parse_task_string("name[one,two]")
assert_equal "name", name
assert_equal ["one", "two"], args
end

def test_can_handle_spaces_between_args
name, args = @app.parse_task_string("name[one, two,\tthree , \tfour]")
assert_equal "name", name
assert_equal ["one", "two", "three", "four"], args
end

def test_keeps_embedded_spaces
name, args = @app.parse_task_string("name[a one ana, two]")
assert_equal "name", name
assert_equal ["a one ana", "two"], args
end

end
12 changes: 12 additions & 0 deletions test/test_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_create
assert_equal t, arg
assert_nil t.source
assert_equal [], t.sources
assert_equal [], t.args
end

def test_invoke
Expand Down Expand Up @@ -135,6 +136,17 @@ def test_investigation_output
assert_match(/needed:\s*true/, out)
assert_match(/pre-requisites:\s*--t2/, out)
end

def test_tasks_can_access_arguments
t = intern(:t1).enhance { |t|
a, b, c = t.args
assert_equal 1, a
assert_equal 2, b
assert_equal 3, c
}
t.args = [1, 2, 3]
t.invoke
end

private

Expand Down

0 comments on commit 9ca2c3d

Please sign in to comment.