Class: Rake::Application
- Inherits:
-
Object
- Object
- Rake::Application
- Includes:
- TaskManager
- Defined in:
- lib/rake/application.rb
Overview
Rake main application object. When invoking rake
from the command line, a Rake::Application object is created and run.
Constant Summary collapse
- DEFAULT_RAKEFILES =
['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The name of the application (typically ‘rake’).
-
#original_dir ⇒ Object
readonly
The original directory where rake was invoked.
-
#rakefile ⇒ Object
readonly
Name of the actual rakefile used.
-
#top_level_tasks ⇒ Object
readonly
List of the top level task names (task names from the command line).
Attributes included from TaskManager
Instance Method Summary collapse
-
#add_import(fn) ⇒ Object
Add a file to the list of files to be imported.
-
#add_loader(ext, loader) ⇒ Object
Add a loader to handle imported files ending in the extension
ext
. -
#collect_tasks ⇒ Object
Collect the list of tasks on the command line.
-
#const_warning(const_name) ⇒ Object
Warn about deprecated use of top level constant names.
-
#display_prerequisites ⇒ Object
Display the tasks and prerequisites.
-
#display_tasks_and_comments ⇒ Object
Display the tasks and comments.
-
#dynamic_width ⇒ Object
Calculate the dynamic width of the.
- #dynamic_width_stty ⇒ Object
- #dynamic_width_tput ⇒ Object
- #find_rakefile_location ⇒ Object
-
#handle_options ⇒ Object
Read and handle the command line options.
-
#have_rakefile ⇒ Object
True if one of the files in RAKEFILES is in the current directory.
-
#init(app_name = 'rake') ⇒ Object
Initialize the command line parameters and app name.
-
#initialize ⇒ Application
constructor
Initialize a Rake::Application object.
-
#invoke_task(task_string) ⇒ Object
private —————————————————————-.
-
#load_imports ⇒ Object
Load the pending list of imported files.
-
#load_rakefile ⇒ Object
Find the rakefile and then load it and any pending imports.
-
#options ⇒ Object
Application options from the command line.
- #parse_task_string(string) ⇒ Object
-
#rake_require(file_name, paths = $LOAD_PATH, loaded = $") ⇒ Object
Similar to the regular Ruby
require
command, but will check for *.rake files in addition to *.rb files. - #rakefile_location ⇒ Object
-
#raw_load_rakefile ⇒ Object
:nodoc:.
-
#run ⇒ Object
Run the Rake application.
-
#standard_exception_handling ⇒ Object
Provide standard execption handling for the given block.
-
#standard_rake_options ⇒ Object
A list of all the standard options used in rake, suitable for passing to OptionParser.
-
#system_dir ⇒ Object
The directory path containing the system wide rakefiles.
- #terminal_width ⇒ Object
-
#top_level ⇒ Object
Run the top level tasks of a Rake application.
- #truncate(string, width) ⇒ Object
-
#truncate_output? ⇒ Boolean
We will truncate output if we are outputting to a TTY or if we’ve been given an explicit column width to honor.
-
#tty_output=(tty_output_state) ⇒ Object
Override the detected TTY output state (mostly for testing).
-
#tty_output? ⇒ Boolean
True if we are outputting to TTY, false otherwise.
- #unix? ⇒ Boolean
- #windows? ⇒ Boolean
Methods included from TaskManager
#[], #clear, #create_rule, #current_scope, #define_task, #enhance_with_matching_rule, #in_namespace, #intern, #lookup, #resolve_args, #synthesize_file_task, #tasks, #tasks_in_scope
Constructor Details
#initialize ⇒ Application
Initialize a Rake::Application object.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rake/application.rb', line 28 def initialize super @name = 'rake' @rakefiles = DEFAULT_RAKEFILES.dup @rakefile = nil @pending_imports = [] @imported = [] @loaders = {} @default_loader = Rake::DefaultLoader.new @original_dir = Dir.pwd @top_level_tasks = [] add_loader('rb', DefaultLoader.new) add_loader('rf', DefaultLoader.new) add_loader('rake', DefaultLoader.new) @tty_output = STDOUT.tty? end |
Instance Attribute Details
#name ⇒ Object (readonly)
The name of the application (typically ‘rake’)
14 15 16 |
# File 'lib/rake/application.rb', line 14 def name @name end |
#original_dir ⇒ Object (readonly)
The original directory where rake was invoked.
17 18 19 |
# File 'lib/rake/application.rb', line 17 def original_dir @original_dir end |
#rakefile ⇒ Object (readonly)
Name of the actual rakefile used.
20 21 22 |
# File 'lib/rake/application.rb', line 20 def rakefile @rakefile end |
#top_level_tasks ⇒ Object (readonly)
List of the top level task names (task names from the command line).
23 24 25 |
# File 'lib/rake/application.rb', line 23 def top_level_tasks @top_level_tasks end |
Instance Method Details
#add_import(fn) ⇒ Object
Add a file to the list of files to be imported.
495 496 497 |
# File 'lib/rake/application.rb', line 495 def add_import(fn) @pending_imports << fn end |
#add_loader(ext, loader) ⇒ Object
Add a loader to handle imported files ending in the extension ext
.
93 94 95 96 |
# File 'lib/rake/application.rb', line 93 def add_loader(ext, loader) ext = ".#{ext}" unless ext =~ /^\./ @loaders[ext] = loader end |
#collect_tasks ⇒ Object
Collect the list of tasks on the command line. If no tasks are given, return a list containing only the default task. Environmental assignments are processed at this time as well.
482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/rake/application.rb', line 482 def collect_tasks @top_level_tasks = [] ARGV.each do |arg| if arg =~ /^(\w+)=(.*)$/ ENV[$1] = $2 else @top_level_tasks << arg unless arg =~ /^-/ end end @top_level_tasks.push("default") if @top_level_tasks.size == 0 end |
#const_warning(const_name) ⇒ Object
Warn about deprecated use of top level constant names.
514 515 516 517 518 519 520 521 522 523 |
# File 'lib/rake/application.rb', line 514 def const_warning(const_name) @const_warning ||= false if ! @const_warning $stderr.puts %{WARNING: Deprecated reference to top-level constant '#{const_name}' } + %{found at: #{rakefile_location}} # ' $stderr.puts %{ Use --classic-namespace on rake command} $stderr.puts %{ or 'require "rake/classic_namespace"' in Rakefile} end @const_warning = true end |
#display_prerequisites ⇒ Object
Display the tasks and prerequisites
240 241 242 243 244 245 |
# File 'lib/rake/application.rb', line 240 def display_prerequisites tasks.each do |t| puts "#{name} #{t.name}" t.prerequisites.each { |pre| puts " #{pre}" } end end |
#display_tasks_and_comments ⇒ Object
Display the tasks and comments.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rake/application.rb', line 177 def display_tasks_and_comments displayable_tasks = tasks.select { |t| t.comment && t.name =~ .show_task_pattern } if .full_description displayable_tasks.each do |t| puts "#{name} #{t.name_with_args}" t.full_comment.split("\n").each do |line| puts " #{line}" end puts end else width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10 max_column = truncate_output? ? terminal_width - name.size - width - 7 : nil displayable_tasks.each do |t| printf "#{name} %-#{width}s # %s\n", t.name_with_args, max_column ? truncate(t.comment, max_column) : t.comment end end end |
#dynamic_width ⇒ Object
Calculate the dynamic width of the
211 212 213 |
# File 'lib/rake/application.rb', line 211 def dynamic_width @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput) end |
#dynamic_width_stty ⇒ Object
215 216 217 |
# File 'lib/rake/application.rb', line 215 def dynamic_width_stty %x{stty size 2>/dev/null}.split[1].to_i end |
#dynamic_width_tput ⇒ Object
219 220 221 |
# File 'lib/rake/application.rb', line 219 def dynamic_width_tput %x{tput cols 2>/dev/null}.to_i end |
#find_rakefile_location ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/rake/application.rb', line 410 def find_rakefile_location here = Dir.pwd while ! (fn = have_rakefile) Dir.chdir("..") if Dir.pwd == here || .nosearch return nil end here = Dir.pwd end [fn, here] ensure Dir.chdir(Rake.original_dir) end |
#handle_options ⇒ Object
Read and handle the command line options.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/rake/application.rb', line 367 def .rakelib = ['rakelib'] OptionParser.new do |opts| opts. = "rake [-f rakefile] {options} targets..." opts.separator "" opts.separator "Options are ..." opts.on_tail("-h", "--help", "-H", "Display this help message.") do puts opts exit end .each { |args| opts.on(*args) } end.parse! # If class namespaces are requested, set the global options # according to the values in the options structure. if .classic_namespace $show_tasks = .show_tasks $show_prereqs = .show_prereqs $trace = .trace $dryrun = .dryrun $silent = .silent end end |
#have_rakefile ⇒ Object
True if one of the files in RAKEFILES is in the current directory. If a match is found, it is copied into @rakefile.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rake/application.rb', line 148 def have_rakefile @rakefiles.each do |fn| if File.exist?(fn) others = Dir.glob(fn, File::FNM_CASEFOLD) return others.size == 1 ? others.first : fn elsif fn == '' return fn end end return nil end |
#init(app_name = 'rake') ⇒ Object
Initialize the command line parameters and app name.
63 64 65 66 67 68 69 |
# File 'lib/rake/application.rb', line 63 def init(app_name='rake') standard_exception_handling do @name = app_name collect_tasks end end |
#invoke_task(task_string) ⇒ Object
private —————————————————————-
105 106 107 108 109 |
# File 'lib/rake/application.rb', line 105 def invoke_task(task_string) name, args = parse_task_string(task_string) t = self[name] t.invoke(*args) end |
#load_imports ⇒ Object
Load the pending list of imported files.
500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/rake/application.rb', line 500 def load_imports while fn = @pending_imports.shift next if @imported.member?(fn) if fn_task = lookup(fn) fn_task.invoke end ext = File.extname(fn) loader = @loaders[ext] || @default_loader loader.load(fn) @imported << fn end end |
#load_rakefile ⇒ Object
Find the rakefile and then load it and any pending imports.
72 73 74 75 76 |
# File 'lib/rake/application.rb', line 72 def load_rakefile standard_exception_handling do raw_load_rakefile end end |
#options ⇒ Object
Application options from the command line
99 100 101 |
# File 'lib/rake/application.rb', line 99 def @options ||= OpenStruct.new end |
#parse_task_string(string) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rake/application.rb', line 111 def parse_task_string(string) if string =~ /^([^\[]+)(\[(.*)\])$/ name = $1 args = $3.split(/\s*,\s*/) else name = string args = [] end [name, args] end |
#rake_require(file_name, paths = $LOAD_PATH, loaded = $") ⇒ Object
Similar to the regular Ruby require
command, but will check for *.rake files in addition to *.rb files.
396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/rake/application.rb', line 396 def rake_require(file_name, paths=$LOAD_PATH, loaded=$") return false if loaded.include?(file_name) paths.each do |path| fn = file_name + ".rake" full_path = File.join(path, fn) if File.exist?(full_path) load full_path loaded << fn return true end end fail LoadError, "Can't find #{file_name}" end |
#rakefile_location ⇒ Object
525 526 527 528 529 530 531 |
# File 'lib/rake/application.rb', line 525 def rakefile_location begin fail rescue RuntimeError => ex ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" end end |
#raw_load_rakefile ⇒ Object
:nodoc:
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/rake/application.rb', line 424 def raw_load_rakefile # :nodoc: rakefile, location = find_rakefile_location if (! .ignore_system) && (.load_system || rakefile.nil?) && system_dir && File.directory?(system_dir) puts "(in #{Dir.pwd})" unless .silent glob("#{system_dir}/*.rake") do |name| add_import name end else fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})" if rakefile.nil? @rakefile = rakefile Dir.chdir(location) puts "(in #{Dir.pwd})" unless .silent $rakefile = @rakefile if .classic_namespace load File.(@rakefile) if @rakefile && @rakefile != '' .rakelib.each do |rlib| glob("#{rlib}/*.rake") do |name| add_import name end end end load_imports end |
#run ⇒ Object
Run the Rake application. The run method performs the following three steps:
-
Initialize the command line options (
init
). -
Define the tasks (
load_rakefile
). -
Run the top level tasks (
run_tasks
).
If you wish to build a custom rake command, you should call init
on your application. The define any tasks. Finally, call top_level
to run your top level tasks.
54 55 56 57 58 59 60 |
# File 'lib/rake/application.rb', line 54 def run standard_exception_handling do init load_rakefile top_level end end |
#standard_exception_handling ⇒ Object
Provide standard execption handling for the given block.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rake/application.rb', line 123 def standard_exception_handling begin yield rescue SystemExit => ex # Exit silently with current status raise rescue OptionParser::InvalidOption => ex # Exit silently exit(false) rescue Exception => ex # Exit with error message $stderr.puts "#{name} aborted!" $stderr.puts ex. if .trace $stderr.puts ex.backtrace.join("\n") else $stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" $stderr.puts "(See full trace by running task with --trace)" end exit(false) end end |
#standard_rake_options ⇒ Object
A list of all the standard options used in rake, suitable for passing to OptionParser.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/rake/application.rb', line 249 def [ ['--classic-namespace', '-C', "Put Task and FileTask in the top level namespace", lambda { |value| require 'rake/classic_namespace' .classic_namespace = true } ], ['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.", lambda { |value| .show_tasks = true .full_description = true .show_task_pattern = Regexp.new(value || '') } ], ['--dry-run', '-n', "Do a dry run without executing actions.", lambda { |value| verbose(true) nowrite(true) .dryrun = true .trace = true } ], ['--execute', '-e CODE', "Execute some Ruby code and exit.", lambda { |value| eval(value) exit } ], ['--execute-print', '-p CODE', "Execute some Ruby code, print the result, then exit.", lambda { |value| puts eval(value) exit } ], ['--execute-continue', '-E CODE', "Execute some Ruby code, then continue with normal task processing.", lambda { |value| eval(value) } ], ['--libdir', '-I LIBDIR', "Include LIBDIR in the search path for required modules.", lambda { |value| $:.push(value) } ], ['--prereqs', '-P', "Display the tasks and dependencies, then exit.", lambda { |value| .show_prereqs = true } ], ['--quiet', '-q', "Do not log messages to standard output.", lambda { |value| verbose(false) } ], ['--rakefile', '-f [FILE]', "Use FILE as the rakefile.", lambda { |value| value ||= '' @rakefiles.clear @rakefiles << value } ], ['--rakelibdir', '--rakelib', '-R RAKELIBDIR', "Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')", lambda { |value| .rakelib = value.split(':') } ], ['--require', '-r MODULE', "Require MODULE before executing rakefile.", lambda { |value| begin require value rescue LoadError => ex begin rake_require value rescue LoadError => ex2 raise ex end end } ], ['--rules', "Trace the rules resolution.", lambda { |value| .trace_rules = true } ], ['--no-search', '--nosearch', '-N', "Do not search parent directories for the Rakefile.", lambda { |value| .nosearch = true } ], ['--silent', '-s', "Like --quiet, but also suppresses the 'in directory' announcement.", lambda { |value| verbose(false) .silent = true } ], ['--system', '-g', "Using system wide (global) rakefiles (usually '~/.rake/*.rake').", lambda { |value| .load_system = true } ], ['--no-system', '--nosystem', '-G', "Use standard project Rakefile search paths, ignore system wide rakefiles.", lambda { |value| .ignore_system = true } ], ['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.", lambda { |value| .show_tasks = true .show_task_pattern = Regexp.new(value || '') .full_description = false } ], ['--trace', '-t', "Turn on invoke/execute tracing, enable full backtrace.", lambda { |value| .trace = true verbose(true) } ], ['--verbose', '-v', "Log message to standard output.", lambda { |value| verbose(true) } ], ['--version', '-V', "Display the program version.", lambda { |value| puts "rake, version #{RAKEVERSION}" exit } ] ] end |
#system_dir ⇒ Object
The directory path containing the system wide rakefiles.
456 457 458 459 460 461 462 463 464 465 |
# File 'lib/rake/application.rb', line 456 def system_dir @system_dir ||= begin if ENV['RAKE_SYSTEM'] ENV['RAKE_SYSTEM'] else standard_system_dir end end end |
#terminal_width ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/rake/application.rb', line 199 def terminal_width if ENV['RAKE_COLUMNS'] result = ENV['RAKE_COLUMNS'].to_i else result = unix? ? dynamic_width : 80 end (result < 10) ? 80 : result rescue 80 end |
#top_level ⇒ Object
Run the top level tasks of a Rake application.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rake/application.rb', line 79 def top_level standard_exception_handling do if .show_tasks display_tasks_and_comments elsif .show_prereqs display_prerequisites else top_level_tasks.each { |task_name| invoke_task(task_name) } end end end |
#truncate(string, width) ⇒ Object
231 232 233 234 235 236 237 |
# File 'lib/rake/application.rb', line 231 def truncate(string, width) if string.length <= width string else ( string[0, width-3] || "" ) + "..." end end |
#truncate_output? ⇒ Boolean
We will truncate output if we are outputting to a TTY or if we’ve been given an explicit column width to honor
172 173 174 |
# File 'lib/rake/application.rb', line 172 def truncate_output? tty_output? || ENV['RAKE_COLUMNS'] end |
#tty_output=(tty_output_state) ⇒ Object
Override the detected TTY output state (mostly for testing)
166 167 168 |
# File 'lib/rake/application.rb', line 166 def tty_output=( tty_output_state ) @tty_output = tty_output_state end |
#tty_output? ⇒ Boolean
True if we are outputting to TTY, false otherwise
161 162 163 |
# File 'lib/rake/application.rb', line 161 def tty_output? @tty_output end |
#unix? ⇒ Boolean
223 224 225 |
# File 'lib/rake/application.rb', line 223 def unix? RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i end |