Class: RakeApp

Inherits:
Object
  • Object
show all
Defined in:
lib/rake.rb

Overview

Rake main application object. When invoking rake from the command line, a RakeApp object is created and run.

Constant Summary collapse

RAKEFILES =
['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb']
OPTIONS =
[
  ['--dry-run',  '-n', GetoptLong::NO_ARGUMENT,
    "Do a dry run without executing actions."],
  ['--help',     '-H', GetoptLong::NO_ARGUMENT,
    "Display this help message."],
  ['--libdir',   '-I', GetoptLong::REQUIRED_ARGUMENT,
    "Include LIBDIR in the search path for required modules."],
  ['--nosearch', '-N', GetoptLong::NO_ARGUMENT,
    "Do not search parent directories for the Rakefile."],
  ['--prereqs',  '-P', GetoptLong::NO_ARGUMENT,
    "Display the tasks and dependencies, then exit."],
  ['--quiet',    '-q', GetoptLong::NO_ARGUMENT,
    "Do not log messages to standard output."],
  ['--rakefile', '-f', GetoptLong::REQUIRED_ARGUMENT,
    "Use FILE as the rakefile."],
  ['--require',  '-r', GetoptLong::REQUIRED_ARGUMENT,
    "Require MODULE before executing rakefile."],
  ['--silent',   '-s', GetoptLong::NO_ARGUMENT,
    "Like --quiet, but also suppresses the 'in directory' announcement."],
  ['--tasks',    '-T', GetoptLong::NO_ARGUMENT,
    "Display the tasks and dependencies, then exit."],
  ['--trace',    '-t', GetoptLong::NO_ARGUMENT,
    "Turn on invoke/execute tracing."],
  ['--usage',    '-h', GetoptLong::NO_ARGUMENT,
    "Display usage."],
  ['--verbose',  '-v', GetoptLong::NO_ARGUMENT,
    "Log message to standard output (default)."],
  ['--version',  '-V', GetoptLong::NO_ARGUMENT,
    "Display the program version."],
]

Instance Method Summary collapse

Constructor Details

#initializeRakeApp

Create a RakeApp object.



846
847
848
849
# File 'lib/rake.rb', line 846

def initialize
  @rakefile = nil
  @nosearch = false
end

Instance Method Details

#collect_tasksObject

Collect the list of tasks on the command line. If no tasks are give, return a list containing only the default task. Environmental assignments are processed at this time as well.



981
982
983
984
985
986
987
988
989
990
991
992
# File 'lib/rake.rb', line 981

def collect_tasks
  tasks = []
  ARGV.each do |arg|
    if arg =~ /^(\w+)=(.*)$/
	ENV[$1] = $2
    else
	tasks << arg
    end
  end
  tasks.push("default") if tasks.size == 0
  tasks
end

#command_line_optionsObject

Return a list of the command line options supported by the program.



909
910
911
# File 'lib/rake.rb', line 909

def command_line_options
  OPTIONS.collect { |lst| lst[0..-2] }
end

#display_prerequisitesObject

Display the tasks and prerequisites



900
901
902
903
904
905
# File 'lib/rake.rb', line 900

def display_prerequisites
  Task.tasks.each do |t|
    puts "rake #{t.name}"
    t.prerequisites.each { |pre| puts "    #{pre}" }
  end
end

#display_tasks_and_commentsObject

Display the tasks and dependencies.



886
887
888
889
890
891
892
893
894
895
896
897
# File 'lib/rake.rb', line 886

def display_tasks_and_comments
  width = Task.tasks.select { |t|
    t.comment
  }.collect { |t|
    t.name.length
  }.max
  Task.tasks.each do |t|
    if t.comment
	printf "rake %-#{width}s  # %s\n", t.name, t.comment
    end
  end
end

#do_option(opt, value) ⇒ Object

Do the option defined by opt and value.



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
# File 'lib/rake.rb', line 914

def do_option(opt, value)
  case opt
  when '--dry-run'
    verbose(true)
    nowrite(true)
    $dryrun = true
    $trace = true
  when '--help'
    help
    exit
  when '--libdir'
    $:.push(value)
  when '--nosearch'
    @nosearch = true
  when '--prereqs'
    $show_prereqs = true
  when '--quiet'
    verbose(false)
  when '--rakefile'
    RAKEFILES.clear
    RAKEFILES << value
  when '--require'
    require value
  when '--silent'
    verbose(false)
    $silent = true
  when '--tasks'
    $show_tasks = true
  when '--trace'
    $trace = true
    verbose(true)
  when '--usage'
    usage
    exit
  when '--verbose'
    verbose(true)
  when '--version'
    puts "rake, version #{RAKEVERSION}"
    exit
  else
    fail "Unknown option: #{opt}"
  end
end

#handle_optionsObject

Read and handle the command line options.



959
960
961
962
# File 'lib/rake.rb', line 959

def handle_options
  opts = GetoptLong.new(*command_line_options)
  opts.each { |opt, value| do_option(opt, value) }
end

#have_rakefileObject

True if one of the files in RAKEFILES is in the current directory. If a match is found, it is copied into @rakefile.



853
854
855
856
857
858
859
860
861
# File 'lib/rake.rb', line 853

def have_rakefile
  RAKEFILES.each do |fn|
    if File.exist?(fn)
	@rakefile = fn
	return true
    end
  end
  return false
end

#helpObject

Display the rake command line help.



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'lib/rake.rb', line 869

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

#load_rakefileObject



964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/rake.rb', line 964

def load_rakefile
  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
  puts "(in #{Dir.pwd})" unless $silent
  $rakefile = @rakefile
  load @rakefile
end

#runObject

Run the rake application.



995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/rake.rb', line 995

def run
  handle_options
  begin
    tasks = collect_tasks
    load_rakefile
    if $show_tasks
	display_tasks_and_comments
    elsif $show_prereqs
	display_prerequisites
    else
	tasks.each { |task_name| Task[task_name].invoke }
    end
  rescue Exception => ex
    puts "rake aborted!"
    puts ex.message
    if $trace
	puts ex.backtrace.join("\n")
    else
	puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
    end
    exit(1)
  end    
end

#usageObject

Display the program usage line.



864
865
866
# File 'lib/rake.rb', line 864

def usage
  puts "rake [-f rakefile] {options} targets..."
end