class Rake::Application
command line, a Rake::Application object is created and run.
Rake main application object. When invoking rake from the
#####################################################################
def add_import(fn)
def add_import(fn) @pending_imports << fn end
def add_loader(ext, loader)
Add a loader to handle imported files ending in the extension
def add_loader(ext, loader) ext = ".#{ext}" unless ext =~ /^\./ @loaders[ext] = loader end
def collect_tasks
given, return a list containing only the default task.
Collect the list of tasks on the command line. If no tasks are
def collect_tasks @top_level_tasks = [] ARGV.each do |arg| if arg =~ /^(\w+)=(.*)$/ ENV[$1] = $2 else @top_level_tasks << arg end end @top_level_tasks.push("default") if @top_level_tasks.size == 0 end
def command_line_options
Return a list of the command line options supported by the
def command_line_options OPTIONS.collect { |lst| lst[0..-2] } end
def const_warning(const_name)
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
def display_prerequisites
def display_prerequisites tasks.each do |t| puts "rake #{t.name}" t.prerequisites.each { |pre| puts " #{pre}" } end end
def display_tasks_and_comments
def display_tasks_and_comments displayable_tasks = tasks.select { |t| t.comment && t.name =~ options.show_task_pattern } if options.full_description displayable_tasks.each do |t| puts "rake #{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 = 80 - name.size - width - 7 displayable_tasks.each do |t| printf "#{name} %-#{width}s # %s\n", t.name_with_args, truncate(t.comment, max_column) end end end
def do_option(opt, value)
def do_option(opt, value) case opt when '--describe' options.show_tasks = true options.show_task_pattern = Regexp.new(value || '.') options.full_description = true when '--dry-run' verbose(true) nowrite(true) options.dryrun = true options.trace = true when '--help' help exit when '--libdir' $:.push(value) when '--nosearch' options.nosearch = true when '--prereqs' options.show_prereqs = true when '--quiet' verbose(false) when '--rakefile' @rakefiles.clear @rakefiles << value when '--rakelibdir' options.rakelib = value.split(':') when '--require' begin require value rescue LoadError => ex begin rake_require value rescue LoadError => ex2 raise ex end end when '--silent' verbose(false) options.silent = true when '--tasks' options.show_tasks = true options.show_task_pattern = Regexp.new(value || '.') options.full_description = false when '--trace' options.trace = true verbose(true) when '--verbose' verbose(true) when '--version' puts "rake, version #{RAKEVERSION}" exit when '--classic-namespace' require 'rake/classic_namespace' options.classic_namespace = true end end
def handle_options
def handle_options options.rakelib = ['rakelib'] opts = GetoptLong.new(*command_line_options) opts.each { |opt, value| do_option(opt, value) } # 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 rescue NoMethodError => ex raise GetoptLong::InvalidOption, "While parsing options, error = #{ex.class}:#{ex.message}" end
def have_rakefile
True if one of the files in RAKEFILES is in the current directory.
def have_rakefile @rakefiles.each do |fn| if File.exist?(fn) || fn == '' @rakefile = fn return true end end return false end
def help
def help puts "rake [-f rakefile] {options} targets..." puts puts "Options are ..." puts OPTIONS.sort.each do |long, short, mode, desc| if mode == GetoptLong::REQUIRED_ARGUMENT if desc =~ /\b([A-Z]{2,})\b/ long = long + "=#{$1}" end end printf " %-20s (%s)\n", long, short printf " %s\n", desc end end
def init(app_name='rake')
def init(app_name='rake') standard_exception_handling do @name = app_name handle_options collect_tasks end end
def initialize
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('rf', DefaultLoader.new) add_loader('rake', DefaultLoader.new) end
def invoke_task(task_string)
def invoke_task(task_string) name, args = parse_task_string(task_string) t = self[name] t.invoke(*args) end
def load_imports
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
def load_rakefile
def load_rakefile standard_exception_handling do raw_load_rakefile end end
def options
def options @options ||= OpenStruct.new end
def parse_task_string(string)
def parse_task_string(string) if string =~ /^([^\[]+)(\[(.*)\])$/ name = $1 args = $3.split(/\s*,\s*/) else name = string args = [] end [name, args] end
def rake_require(file_name, paths=$LOAD_PATH, loaded=$")
Similar to the regular Ruby +require+ command, but will check
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
def rakefile_location
def rakefile_location begin fail rescue RuntimeError => ex ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" end end
def raw_load_rakefile # :nodoc:
def raw_load_rakefile # :nodoc: here = Dir.pwd while ! have_rakefile Dir.chdir("..") if Dir.pwd == here || options.nosearch fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})" end here = Dir.pwd end puts "(in #{Dir.pwd})" unless options.silent $rakefile = @rakefile load File.expand_path(@rakefile) if @rakefile != '' options.rakelib.each do |rlib| Dir["#{rlib}/*.rake"].each do |name| add_import name end end load_imports end
def run
application. The define any tasks. Finally, call +top_level+ to run your top
If you wish to build a custom rake command, you should call +init+ on your
* Run the top level tasks (+run_tasks+).
* Define the tasks (+load_rakefile+).
* Initialize the command line options (+init+).
Run the Rake application. The run method performs the following three steps:
def run standard_exception_handling do init load_rakefile top_level end end
def standard_exception_handling
def standard_exception_handling begin yield rescue SystemExit => ex # Exit silently with current status exit(ex.status) rescue SystemExit, GetoptLong::InvalidOption => ex # Exit silently exit(1) rescue Exception => ex # Exit with error message $stderr.puts "rake aborted!" $stderr.puts ex.message if options.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(1) end end
def top_level
def top_level standard_exception_handling do if options.show_tasks display_tasks_and_comments elsif options.show_prereqs display_prerequisites else top_level_tasks.each { |task_name| invoke_task(task_name) } end end end
def truncate(string, width)
def truncate(string, width) if string.length <= width string else string[0, width-3] + "..." end end