Rake main application object. When invoking
rake
from the command line, a RakeApp object is created and run.
Create a RakeApp object.
# File minirake, line 394 def initialize @rakefile = nil @nosearch = false end
Return a list of the command line options supported by the program.
# File minirake, line 443 def command_line_options OPTIONS.collect { |lst| lst[0..-2] } end
Display the tasks and dependencies.
# File minirake, line 434 def display_tasks MiniRake::Task.tasks.each do |t| puts "#{t.class} #{t.name}" t.prerequisites.each { |pre| puts " #{pre}" } end end
Do the option defined by opt
and value
.
# File minirake, line 448 def do_option(opt, value) case opt when '--dry-run' $dryrun = true $trace = true when '--help' help exit when '--libdir' $:.push(value) when '--nosearch' @nosearch = true when '--quiet' $verbose = false when '--rakefile' RAKEFILES.clear RAKEFILES << value when '--require' require value when '--tasks' $show_tasks = true when '--pull-gems' $pull_gems = true when '--trace' $trace = true when '--usage' usage exit when '--verbose' $verbose = true when '--version' puts "rake, version #{RAKEVERSION}" exit when '--directory' Dir.chdir value when '--jobs' $rake_jobs = [value.to_i, 1].max else fail "Unknown option: #{opt}" end end
Read and handle the command line options.
# File minirake, line 491 def handle_options $verbose = false $pull_gems = false opts = GetoptLong.new(*command_line_options) opts.each { |opt, value| do_option(opt, value) } end
True if one of the files in RAKEFILES is in the current directory. If a match is found, it is copied into @rakefile.
# File minirake, line 401 def have_rakefile RAKEFILES.each do |fn| if File.exist?(fn) @rakefile = fn return true end end return false end
Display the rake command line help.
# File minirake, line 417 def help usage 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
Run the rake
application.
# File minirake, line 499 def run handle_options unless $rake_root_fiber require 'fiber' $rake_root_fiber = Fiber.current end begin here = Dir.pwd while ! have_rakefile Dir.chdir("..") if Dir.pwd == here || @nosearch fail "No Rakefile found (looking for: #{RAKEFILES.join(', ')})" end here = Dir.pwd end root_tasks = [] ARGV.each do |task_name| if /^(\w+)=(.*)/.match(task_name) ENV[$1] = $2 else root_tasks << task_name end end puts "(in #{Dir.pwd})" $rakefile = @rakefile load @rakefile if $show_tasks display_tasks else root_tasks.push("default") if root_tasks.empty? # revese tasks for popping root_tasks.reverse! tasks = [] until root_tasks.empty? root_name = root_tasks.pop tasks << root_name until tasks.empty? task_name = tasks.pop t = MiniRake::Task[task_name] f = t.invoke # append additional tasks to task queue if f.kind_of?(Array) tasks.push(*f) tasks.uniq! end unless f.kind_of? Fiber tasks.insert 0, task_name unless t.done? if root_name == task_name wait_process end next end wait_process while $rake_fiber_table.size >= $rake_jobs f.transfer end end wait_process until $rake_fiber_table.empty? end rescue Exception => e begin $rake_failed << e wait_process until $rake_fiber_table.empty? rescue Exception => next_e e = next_e retry end end return if $rake_failed.empty? puts "rake aborted!" $rake_failed.each do |ex| puts ex.message if $trace || $verbose puts ex.backtrace.join("\n") else puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" end end exit 1 end
Display the program usage line.
# File minirake, line 412 def usage puts "rake [-f rakefile] {options} targets..." end
# File minirake, line 589 def wait_process(count = 0) dur = [0.0001 * (10 ** count), 1].min sleep dur exited = [] $rake_fiber_table.each do |pid, v| exited << pid unless v[:process_waiter].alive? end exited.each do |pid| ent = $rake_fiber_table.delete pid st = ent[:process_waiter].value # ignore process that isn't created by `sh` method return if ent.nil? if st.exitstatus != 0 raise "Command Failed: [#{ent[:command]}]" end fail 'task scheduling bug!' if $rake_fiber_table.size >= $rake_jobs ent[:fiber].transfer end wait_process(count + 1) if !$rake_fiber_table.empty? && exited.empty? end