Skip to content

Commit d1f89d7

Browse files
author
jimweirich
committed
added task parameters, named parameter via desc, column limited output
git-svn-id: svn+ssh://rubyforge.org/var/svn/rake/trunk@591 5af023f1-ac1a-0410-98d6-829a145c37ef
1 parent 0b08813 commit d1f89d7

File tree

5 files changed

+238
-34
lines changed

5 files changed

+238
-34
lines changed

CHANGES

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
== Pre-Version 0.7.4
44

5-
* Added task parameters (e.g. "rake build[version7]svn ")
5+
* Added task parameters (e.g. "rake build[version7]")
6+
* Made task parameters passable to prerequisites.
7+
* The 'desc' command will now document task argument names.
8+
* Comments are limited to 80 columns or so (suggested by Jamis Buck).
9+
* Added -D to display full comments (suggested by Jamis Buck).
610

711
== Version 0.7.3
812

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ task :update_version => [:prerelease] do
357357
end
358358
end
359359

360-
desc "Tag all the CVS files with the latest release number (REL=x.y.z)"
360+
desc "[rel] Tag all the CVS files with the latest release number (REL=x.y.z)"
361361
task :tag => [:prerelease] do
362362
reltag = "REL_#{PKG_VERSION.gsub(/\./, '_')}"
363363
reltag << ENV['REUSE'].gsub(/\./, '_') if ENV['REUSE']
@@ -381,6 +381,7 @@ end
381381

382382
load 'xforge.rf' if File.exist?('xforge.rf')
383383

384+
desc "Where is the current directory. This task displays\nthe current rake directory"
384385
task :where_am_i do
385386
puts Rake.original_dir
386387
end

lib/rake.rb

Lines changed: 132 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#--
44

5-
# Copyright (c) 2003, 2004, 2005, 2006 Jim Weirich
5+
# Copyright (c) 2003, 2004, 2005, 2006, 2007 Jim Weirich
66
#
77
# Permission is hereby granted, free of charge, to any person obtaining a copy
88
# of this software and associated documentation files (the "Software"), to
@@ -303,15 +303,19 @@ class Task
303303
# Application owning this task.
304304
attr_accessor :application
305305

306-
# Comment for this task.
307-
attr_accessor :comment
306+
# Comment for this task. Restricted to a single line of no more than 50
307+
# characters.
308+
attr_reader :comment
309+
310+
# Full text of the (possibly multi-line) comment.
311+
attr_reader :full_comment
308312

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

312316
# List of arguments to the task
313317
attr_accessor :args
314-
318+
315319
# Return task name
316320
def to_s
317321
name
@@ -335,11 +339,13 @@ def initialize(task_name, app)
335339
@prerequisites = FileList[]
336340
@actions = []
337341
@already_invoked = false
342+
@full_comment = nil
338343
@comment = nil
339344
@lock = Mutex.new
340345
@application = app
341346
@scope = app.current_scope
342347
@args = []
348+
@arg_names = nil
343349
end
344350

345351
# Enhance a task with prerequisites or actions. Returns self.
@@ -353,6 +359,35 @@ def enhance(deps=nil, &block)
353359
def name
354360
@name.to_s
355361
end
362+
363+
# Name of task with argument list description.
364+
def name_with_args # :nodoc:
365+
if arg_description
366+
"#{name}#{arg_description}"
367+
else
368+
name
369+
end
370+
end
371+
372+
# Argument description (nil if none).
373+
def arg_description # :nodoc:
374+
@arg_names ? "[#{(arg_names || []).join(',')}]" : nil
375+
end
376+
377+
# Hash of argument names to argument positions (0 based). (empty if no
378+
# named arguments).
379+
def arg_map
380+
result = {}
381+
arg_names.each_with_index do |n, i|
382+
result[n] = i
383+
end
384+
result
385+
end
386+
387+
# Name of arguments for this task.
388+
def arg_names
389+
@arg_names || []
390+
end
356391

357392
# Invoke the task if it is needed. Prerequites are invoked first.
358393
def invoke
@@ -370,10 +405,22 @@ def invoke
370405
# Invoke all the prerequisites of a task.
371406
def invoke_prerequisites
372407
@prerequisites.each { |n|
373-
application[n, @scope].invoke
408+
prereq = application[n, @scope]
409+
setup_arguments(prereq)
410+
prereq.invoke
374411
}
375412
end
376413

414+
# Setup the arguments to be passed to prerequesites.
415+
def setup_arguments(prereq) # :nodoc:
416+
if ! arg_names.empty? && ! prereq.arg_names.empty?
417+
prereq.args = prereq.arg_names.collect do |name|
418+
arg_map[name] ? args[arg_map[name]] : nil
419+
end
420+
end
421+
end
422+
private :setup_arguments
423+
377424
# Format the trace flags for display.
378425
def format_trace_flags
379426
flags = []
@@ -416,17 +463,45 @@ def timestamp
416463
@prerequisites.collect { |p| application[p].timestamp }.max || Time.now
417464
end
418465

466+
# Add a description to the task. The description can consist of an option
467+
# argument list (enclosed brackets) and an optional comment.
468+
def add_description(description)
469+
return if ! description
470+
if description =~ %r{\A\s*(\[([^\]]*)\])\s*(.*)\Z}m
471+
arg_string = $2
472+
comment = $3.strip
473+
else
474+
comment = description.strip
475+
end
476+
set_arg_names(arg_string) if arg_string
477+
add_comment(comment) if comment && ! comment.empty?
478+
end
479+
419480
# Add a comment to the task. If a comment alread exists, separate
420481
# the new comment with " / ".
421482
def add_comment(comment)
422-
return if ! comment
423-
if @comment
424-
@comment << " / "
483+
if @full_comment
484+
@full_comment << " / "
485+
else
486+
@full_comment = ''
487+
end
488+
@full_comment << comment
489+
if @full_comment =~ /\A([^.]+?\.)/
490+
@comment = $1
425491
else
426-
@comment = ''
492+
@comment = @full_comment
493+
end
494+
if @comment.length > 50
495+
@comment = @comment[0, 47] + "..."
427496
end
428-
@comment << comment
429497
end
498+
private :add_comment
499+
500+
# Set the names of the arguments for this task.
501+
def set_arg_names(arg_string)
502+
@arg_names = arg_string.split(',').collect { |n| n.strip }
503+
end
504+
private :set_arg_names
430505

431506
# Return a string describing the internal state of a task. Useful for
432507
# debugging.
@@ -673,8 +748,8 @@ def rule(args, &block)
673748
# runtests
674749
# end
675750
#
676-
def desc(comment)
677-
Rake.application.last_comment = comment
751+
def desc(description)
752+
Rake.application.last_description = description
678753
end
679754

680755
# Import the partial Rakefiles +fn+. Imported files are loaded _after_ the
@@ -1442,14 +1517,15 @@ def tasks
14421517
# The TaskManager module is a mixin for managing tasks.
14431518
module TaskManager
14441519
# Track the last comment made in the Rakefile.
1445-
attr_accessor :last_comment
1520+
attr_accessor :last_description
1521+
alias :last_comment :last_description # Backwards compatibility
14461522

14471523
def initialize
14481524
super
14491525
@tasks = Hash.new
14501526
@rules = Array.new
14511527
@scope = Array.new
1452-
@last_comment = nil
1528+
@last_description = nil
14531529
end
14541530

14551531
def create_rule(args, &block)
@@ -1464,8 +1540,8 @@ def define_task(task_class, args, &block)
14641540
deps = [deps] unless deps.respond_to?(:to_ary)
14651541
deps = deps.collect {|d| d.to_s }
14661542
task = intern(task_class, task_name)
1467-
task.add_comment(@last_comment)
1468-
@last_comment = nil
1543+
task.add_description(@last_description)
1544+
@last_description = nil
14691545
task.enhance(deps, &block)
14701546
task
14711547
end
@@ -1658,38 +1734,40 @@ class Application
16581734
DEFAULT_RAKEFILES = ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].freeze
16591735

16601736
OPTIONS = [ # :nodoc:
1661-
['--dry-run', '-n', GetoptLong::NO_ARGUMENT,
1662-
"Do a dry run without executing actions."],
1737+
['--classic-namespace', '-C', GetoptLong::NO_ARGUMENT,
1738+
"Put Task and FileTask in the top level namespace"],
1739+
['--describe', '-D', GetoptLong::OPTIONAL_ARGUMENT,
1740+
"Describe the tasks (matching optional PATTERN), then exit."],
1741+
['--rakefile', '-f', GetoptLong::OPTIONAL_ARGUMENT,
1742+
"Use FILE as the rakefile."],
1743+
['--usage', '-h', GetoptLong::NO_ARGUMENT,
1744+
"Display usage."],
16631745
['--help', '-H', GetoptLong::NO_ARGUMENT,
16641746
"Display this help message."],
16651747
['--libdir', '-I', GetoptLong::REQUIRED_ARGUMENT,
16661748
"Include LIBDIR in the search path for required modules."],
1667-
['--rakelibdir', '-R', GetoptLong::REQUIRED_ARGUMENT,
1668-
"Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')"],
1749+
['--dry-run', '-n', GetoptLong::NO_ARGUMENT,
1750+
"Do a dry run without executing actions."],
16691751
['--nosearch', '-N', GetoptLong::NO_ARGUMENT,
16701752
"Do not search parent directories for the Rakefile."],
16711753
['--prereqs', '-P', GetoptLong::NO_ARGUMENT,
16721754
"Display the tasks and dependencies, then exit."],
16731755
['--quiet', '-q', GetoptLong::NO_ARGUMENT,
16741756
"Do not log messages to standard output."],
1675-
['--rakefile', '-f', GetoptLong::OPTIONAL_ARGUMENT,
1676-
"Use FILE as the rakefile."],
16771757
['--require', '-r', GetoptLong::REQUIRED_ARGUMENT,
16781758
"Require MODULE before executing rakefile."],
1759+
['--rakelibdir', '-R', GetoptLong::REQUIRED_ARGUMENT,
1760+
"Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')"],
16791761
['--silent', '-s', GetoptLong::NO_ARGUMENT,
16801762
"Like --quiet, but also suppresses the 'in directory' announcement."],
16811763
['--tasks', '-T', GetoptLong::OPTIONAL_ARGUMENT,
16821764
"Display the tasks (matching optional PATTERN) with descriptions, then exit."],
16831765
['--trace', '-t', GetoptLong::NO_ARGUMENT,
16841766
"Turn on invoke/execute tracing, enable full backtrace."],
1685-
['--usage', '-h', GetoptLong::NO_ARGUMENT,
1686-
"Display usage."],
16871767
['--verbose', '-v', GetoptLong::NO_ARGUMENT,
16881768
"Log message to standard output (default)."],
16891769
['--version', '-V', GetoptLong::NO_ARGUMENT,
16901770
"Display the program version."],
1691-
['--classic-namespace', '-C', GetoptLong::NO_ARGUMENT,
1692-
"Put Task and FileTask in the top level namespace"],
16931771
]
16941772

16951773
# Initialize a Rake::Application object.
@@ -1851,9 +1929,29 @@ def display_tasks_and_comments
18511929
displayable_tasks = tasks.select { |t|
18521930
t.comment && t.name =~ options.show_task_pattern
18531931
}
1854-
width = displayable_tasks.collect { |t| t.name.length }.max
1855-
displayable_tasks.each do |t|
1856-
printf "#{name} %-#{width}s # %s\n", t.name, t.comment
1932+
if options.full_description
1933+
displayable_tasks.each do |t|
1934+
puts "task #{t.name_with_args}"
1935+
t.full_comment.each do |line|
1936+
puts " #{line}"
1937+
end
1938+
puts
1939+
end
1940+
else
1941+
width = displayable_tasks.collect { |t| t.name_with_args.length }.max
1942+
max_column = 80 - name.size - width - 7
1943+
displayable_tasks.each do |t|
1944+
printf "#{name} %-#{width}s # %s\n",
1945+
t.name_with_args, truncate(t.comment, max_column)
1946+
end
1947+
end
1948+
end
1949+
1950+
def truncate(string, width)
1951+
if string.length <= width
1952+
string
1953+
else
1954+
string[0, width-3] + "..."
18571955
end
18581956
end
18591957

@@ -1874,6 +1972,10 @@ def command_line_options
18741972
# Do the option defined by +opt+ and +value+.
18751973
def do_option(opt, value)
18761974
case opt
1975+
when '--describe'
1976+
options.show_tasks = true
1977+
options.show_task_pattern = Regexp.new(value || '.')
1978+
options.full_description = true
18771979
when '--dry-run'
18781980
verbose(true)
18791981
nowrite(true)
@@ -1911,6 +2013,7 @@ def do_option(opt, value)
19112013
when '--tasks'
19122014
options.show_tasks = true
19132015
options.show_task_pattern = Regexp.new(value || '.')
2016+
options.full_description = false
19142017
when '--trace'
19152018
options.trace = true
19162019
verbose(true)

test/test_application.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_constant_warning
3131

3232
def test_display_tasks
3333
@app.options.show_task_pattern = //
34-
@app.last_comment = "COMMENT"
34+
@app.last_description = "COMMENT"
3535
@app.define_task(Rake::Task, "t")
3636
out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
3737
assert_match(/^rake t/, out)
@@ -134,7 +134,7 @@ def test_display_task_run
134134
ran = false
135135
ARGV.clear
136136
ARGV << '-f' << '-s' << '--tasks'
137-
@app.last_comment = "COMMENT"
137+
@app.last_description = "COMMENT"
138138
@app.define_task(Rake::Task, "default")
139139
out = capture_stdout { @app.run }
140140
assert @app.options.show_tasks
@@ -147,7 +147,7 @@ def test_display_prereqs
147147
ran = false
148148
ARGV.clear
149149
ARGV << '-f' << '-s' << '--prereqs'
150-
@app.last_comment = "COMMENT"
150+
@app.last_description = "COMMENT"
151151
t = @app.define_task(Rake::Task, "default")
152152
t.enhance([:a, :b])
153153
@app.define_task(Rake::Task, "a")

0 commit comments

Comments
 (0)