Class: Rake::Application

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

Overview

Rake main application object. When invoking rake from the command line, a Rake::Application 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."],
  ['--rakelibdir', '-R', GetoptLong::REQUIRED_ARGUMENT,
	"Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')"],
  ['--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::OPTIONAL_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::OPTIONAL_ARGUMENT,
	"Display the tasks (matching optional PATTERN) with descriptions, then exit."],
  ['--trace',    '-t', GetoptLong::NO_ARGUMENT,
	"Turn on invoke/execute tracing, enable full backtrace."],
  ['--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."],
  ['--classic-namespace', '-C', GetoptLong::NO_ARGUMENT,
	"Put Task and FileTask in the top level namespace"],
]

Instance Attribute Summary collapse

Attributes included from TaskManager

#last_comment

Instance Method Summary collapse

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

Constructor Details

#initializeApplication

Create a Rake::Application object.



1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
# File 'lib/rake.rb', line 1645

def initialize
  super
  @rakefile = nil
  @pending_imports = []
  @imported = []
  @loaders = {}
  @default_loader = Rake::DefaultLoader.new
  @original_dir = Dir.pwd
  add_loader('rf', DefaultLoader.new)
  add_loader('rake', DefaultLoader.new)
end

Instance Attribute Details

#original_dirObject (readonly)

The original directory where rake was invoked.



1605
1606
1607
# File 'lib/rake.rb', line 1605

def original_dir
  @original_dir
end

Instance Method Details

#add_import(fn) ⇒ Object

Add a file to the list of files to be imported.



1852
1853
1854
# File 'lib/rake.rb', line 1852

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.



1870
1871
1872
1873
# File 'lib/rake.rb', line 1870

def add_loader(ext, loader)
  ext = ".#{ext}" unless ext =~ /^\./
  @loaders[ext] = loader
end

#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.



1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
# File 'lib/rake.rb', line 1838

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.



1719
1720
1721
# File 'lib/rake.rb', line 1719

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

#const_warning(const_name) ⇒ Object

Warn about deprecated use of top level constant names.



1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
# File 'lib/rake.rb', line 1876

def const_warning(const_name)
  @const_warning ||= false
  if ! @const_warning
	puts %{WARNING: Deprecated reference to top-level constant '#{const_name}'} +
	  %{found at: #{rakefile_location}} # '
	puts %{    Use --classic-namespace on rake command}
	puts %{    or 'require "rake/classic_namespace"' in Rakefile}
  end
  @const_warning = true
end

#display_prerequisitesObject

Display the tasks and prerequisites



1710
1711
1712
1713
1714
1715
# File 'lib/rake.rb', line 1710

def display_prerequisites
  Rake::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.



1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
# File 'lib/rake.rb', line 1697

def display_tasks_and_comments
  displayable_tasks = Rake::Task.tasks.select { |t|
	t.comment && t.name =~ options.show_task_pattern
  }
  width = displayable_tasks.collect { |t|
	t.name.length
  }.max
  displayable_tasks.each do |t|
	printf "rake %-#{width}s  # %s\n", t.name, t.comment
  end
end

#do_option(opt, value) ⇒ Object

Do the option defined by opt and value.



1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
# File 'lib/rake.rb', line 1724

def do_option(opt, value)
  case opt
  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 || '.')
  when '--trace'
	options.trace = true
	verbose(true)
  when '--usage'
	usage
	exit
  when '--verbose'
	verbose(true)
  when '--version'
	puts "rake, version #{RAKEVERSION}"
	exit
  when '--classic-namespace'
	require 'rake/classic_namespace'
	options.classic_namespace = true
  else
	fail "Unknown option: #{opt}"
  end
end

#handle_optionsObject

Read and handle the command line options.



1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
# File 'lib/rake.rb', line 1783

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
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.



1664
1665
1666
1667
1668
1669
1670
1671
1672
# File 'lib/rake.rb', line 1664

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

#helpObject

Display the rake command line help.



1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
# File 'lib/rake.rb', line 1680

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_importsObject

Load the pending list of imported files.



1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
# File 'lib/rake.rb', line 1857

def load_imports
  while fn = @pending_imports.shift
	next if @imported.member?(fn)
	Rake::Task[fn].invoke if Rake::Task.task_defined?(fn)
	ext = File.extname(fn)
	loader = @loaders[ext] || @default_loader
	loader.load(fn)
	@imported << fn
  end
end

#load_rakefileObject

Find the rakefile and then load it and any pending imports.



1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
# File 'lib/rake.rb', line 1817

def load_rakefile
  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

#optionsObject

Application options from the command line



1658
1659
1660
# File 'lib/rake.rb', line 1658

def options
  @options ||= OpenStruct.new
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.



1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
# File 'lib/rake.rb', line 1802

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_locationObject



1887
1888
1889
1890
1891
1892
1893
# File 'lib/rake.rb', line 1887

def rakefile_location
  begin
	fail
  rescue RuntimeError => ex
	ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
  end
end

#runObject

Run the rake application.



1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
# File 'lib/rake.rb', line 1896

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

#usageObject

Display the program usage line.



1675
1676
1677
# File 'lib/rake.rb', line 1675

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