Rake main application object. When invoking rake
from the
command line, a Rake::Application object is
created and run.
- A
- C
- D
- F
- G
- H
- I
- L
- N
- O
- P
- R
- S
- T
- U
- W
DEFAULT_RAKEFILES | = | ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].freeze |
[R] | name | The name of the application (typically 'rake') |
[R] | original_dir | The original directory where rake was invoked. |
[R] | rakefile | Name of the actual rakefile used. |
[RW] | terminal_columns | Number of columns on the terminal |
[R] | top_level_tasks | List of the top level task names (task names from the command line). |
Initialize a Rake::Application object.
# File ../ruby/lib/rake/application.rb, line 34 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? @terminal_columns = ENV['RAKE_COLUMNS'].to_i end
Add a file to the list of files to be imported.
Add a loader to handle imported files ending in the extension
ext
.
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.
Warn about deprecated use of top level constant names.
# File ../ruby/lib/rake/application.rb, line 575 def const_warning(const_name) @const_warning ||= false if ! @const_warning $stderr.puts %Q{WARNING: Deprecated reference to top-level constant '#{const_name}' } + %Q{found at: #{rakefile_location}} # ' $stderr.puts %Q{ Use --classic-namespace on rake command} $stderr.puts %Q{ or 'require "rake/classic_namespace"' in Rakefile} end @const_warning = true end
Warn about deprecated usage.
Example:
Rake.application.deprecate("import", "Rake.import", caller.first)
Display the error message that caused the exception.
# File ../ruby/lib/rake/application.rb, line 148 def display_error_message(ex) $stderr.puts "#{name} aborted!" $stderr.puts ex.message if options.trace $stderr.puts ex.backtrace.join("\n") else $stderr.puts rakefile_location(ex.backtrace) end $stderr.puts "Tasks: #{ex.chain}" if has_chain?(ex) $stderr.puts "(See full trace by running task with --trace)" unless options.trace end
Display the tasks and prerequisites
Display the tasks and comments.
# File ../ruby/lib/rake/application.rb, line 209 def display_tasks_and_comments displayable_tasks = tasks.select { |t| t.comment && t.name =~ options.show_task_pattern } case options.show_tasks when :tasks 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 when :describe displayable_tasks.each do |t| puts "#{name} #{t.name_with_args}" t.full_comment.split("\n").each do |line| puts " #{line}" end puts end when :lines displayable_tasks.each do |t| t.locations.each do |loc| printf "#{name} %-30s %s\n",t.name_with_args, loc end end else fail "Unknown show task mode: '#{options.show_tasks}'" end end
Calculate the dynamic width of the
Read and handle the command line options.
# File ../ruby/lib/rake/application.rb, line 422 def handle_options options.rakelib = ['rakelib'] OptionParser.new do |opts| opts.banner = "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 standard_rake_options.each { |args| opts.on(*args) } opts.environment('RAKEOPT') end.parse! # If class namespaces are requested, set the global options # according to the values in the options structure. if options.classic_namespace $show_tasks = options.show_tasks $show_prereqs = options.show_prereqs $trace = options.trace $dryrun = options.dryrun $silent = options.silent end end
True if one of the files in RAKEFILES is in the current directory. If a match is found, it is copied into @rakefile.
Initialize the command line parameters and app name.
private —————————————————————-
Load the pending list of imported files.
# File ../ruby/lib/rake/application.rb, line 561 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
Application options from the command line
Similar to the regular Ruby require
command, but will check
for *.rake files in addition to *.rb files.
# File ../ruby/lib/rake/application.rb, line 452 def rake_require(file_name, paths=$LOAD_PATH, loaded=$") fn = file_name + ".rake" return false if loaded.include?(fn) paths.each do |path| full_path = File.join(path, fn) if File.exist?(full_path) Rake.load_rakefile(full_path) loaded << fn return true end end fail LoadError, "Can't find #{file_name}" end
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. Then define any tasks. Finally,
call top_level
to run your top level tasks.
Provide standard exception handling for the given block.
# File ../ruby/lib/rake/application.rb, line 131 def standard_exception_handling begin yield rescue SystemExit => ex # Exit silently with current status raise rescue OptionParser::InvalidOption => ex $stderr.puts ex.message exit(false) rescue Exception => ex # Exit with error message display_error_message(ex) exit(false) end end
A list of all the standard options used in rake, suitable for passing to OptionParser.
# File ../ruby/lib/rake/application.rb, line 291 def standard_rake_options [ ['--classic-namespace', '-C', "Put Task and FileTask in the top level namespace", lambda { |value| require 'rake/classic_namespace' options.classic_namespace = true } ], ['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.", lambda { |value| options.show_tasks = :describe options.show_task_pattern = Regexp.new(value || '') TaskManager.record_task_metadata = true } ], ['--dry-run', '-n', "Do a dry run without executing actions.", lambda { |value| Rake.verbose(true) Rake.nowrite(true) options.dryrun = true options.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) } ], ['--no-search', '--nosearch', '-N', "Do not search parent directories for the Rakefile.", lambda { |value| options.nosearch = true } ], ['--prereqs', '-P', "Display the tasks and dependencies, then exit.", lambda { |value| options.show_prereqs = true } ], ['--quiet', '-q', "Do not log messages to standard output.", lambda { |value| Rake.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')", # HACK Use File::PATH_SEPARATOR lambda { |value| options.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 raise ex end end } ], ['--rules', "Trace the rules resolution.", lambda { |value| options.trace_rules = true } ], ['--silent', '-s', "Like --quiet, but also suppresses the 'in directory' announcement.", lambda { |value| Rake.verbose(false) options.silent = true } ], ['--system', '-g', "Using system wide (global) rakefiles (usually '~/.rake/*.rake').", lambda { |value| options.load_system = true } ], ['--no-system', '--nosystem', '-G', "Use standard project Rakefile search paths, ignore system wide rakefiles.", lambda { |value| options.ignore_system = true } ], ['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.", lambda { |value| options.show_tasks = :tasks options.show_task_pattern = Regexp.new(value || '') Rake::TaskManager.record_task_metadata = true } ], ['--trace', '-t', "Turn on invoke/execute tracing, enable full backtrace.", lambda { |value| options.trace = true Rake.verbose(true) } ], ['--verbose', '-v', "Log message to standard output.", lambda { |value| Rake.verbose(true) } ], ['--version', '-V', "Display the program version.", lambda { |value| puts "rake, version #{RAKEVERSION}" exit } ], ['--where', '-W [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.", lambda { |value| options.show_tasks = :lines options.show_task_pattern = Regexp.new(value || '') Rake::TaskManager.record_task_metadata = true } ], ['--no-deprecation-warnings', '-X', "Disable the deprecation warnings.", lambda { |value| options.ignore_deprecate = true } ], ] end
The directory path containing the system wide rakefiles.
Run the top level tasks of a Rake application.
We will truncate output if we are outputting to a TTY or if we've been given an explicit column width to honor
Override the detected TTY output state (mostly for testing)
True if we are outputting to TTY, false otherwise