Rake main application object. When invoking rake from the command line, a Rake::Application object is created and run.
- add_import
- add_loader
- collect_tasks
- command_line_options
- const_warning
- display_prerequisites
- display_tasks_and_comments
- do_option
- handle_options
- have_rakefile
- help
- load_imports
- load_rakefile
- new
- options
- rake_require
- rakefile_location
- run
- usage
| DEFAULT_RAKEFILES | = | ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].freeze |
| 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"], ] |
| [R] | original_dir | The original directory where rake was invoked. |
| [R] | rakefile | The original directory where rake was invoked. |
Create a Rake::Application object.
[ show source ]
# File lib/rake.rb, line 1696
1696: def initialize
1697: super
1698: @rakefiles = DEFAULT_RAKEFILES.dup
1699: @rakefile = nil
1700: @pending_imports = []
1701: @imported = []
1702: @loaders = {}
1703: @default_loader = Rake::DefaultLoader.new
1704: @original_dir = Dir.pwd
1705: add_loader('rf', DefaultLoader.new)
1706: add_loader('rake', DefaultLoader.new)
1707: end
Add a file to the list of files to be imported.
[ show source ]
# File lib/rake.rb, line 1900
1900: def add_import(fn)
1901: @pending_imports << fn
1902: end
Add a loader to handle imported files ending in the extension ext.
[ show source ]
# File lib/rake.rb, line 1920
1920: def add_loader(ext, loader)
1921: ext = ".#{ext}" unless ext =~ /^\./
1922: @loaders[ext] = loader
1923: end
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.
[ show source ]
# File lib/rake.rb, line 1886
1886: def collect_tasks
1887: tasks = []
1888: ARGV.each do |arg|
1889: if arg =~ /^(\w+)=(.*)$/
1890: ENV[$1] = $2
1891: else
1892: tasks << arg
1893: end
1894: end
1895: tasks.push("default") if tasks.size == 0
1896: tasks
1897: end
Return a list of the command line options supported by the program.
[ show source ]
# File lib/rake.rb, line 1769
1769: def command_line_options
1770: OPTIONS.collect { |lst| lst[0..-2] }
1771: end
Warn about deprecated use of top level constant names.
[ show source ]
# File lib/rake.rb, line 1926
1926: def const_warning(const_name)
1927: @const_warning ||= false
1928: if ! @const_warning
1929: $stderr.puts %{WARNING: Deprecated reference to top-level constant '#{const_name}' } +
1930: %{found at: #{rakefile_location}} # '
1931: $stderr.puts %{ Use --classic-namespace on rake command}
1932: $stderr.puts %{ or 'require "rake/classic_namespace"' in Rakefile}
1933: end
1934: @const_warning = true
1935: end
Display the tasks and prerequisites
[ show source ]
# File lib/rake.rb, line 1760
1760: def display_prerequisites
1761: tasks.each do |t|
1762: puts "rake #{t.name}"
1763: t.prerequisites.each { |pre| puts " #{pre}" }
1764: end
1765: end
Display the tasks and dependencies.
[ show source ]
# File lib/rake.rb, line 1749
1749: def display_tasks_and_comments
1750: displayable_tasks = tasks.select { |t|
1751: t.comment && t.name =~ options.show_task_pattern
1752: }
1753: width = displayable_tasks.collect { |t| t.name.length }.max
1754: displayable_tasks.each do |t|
1755: printf "rake %-#{width}s # %s\n", t.name, t.comment
1756: end
1757: end
Do the option defined by opt and value.
[ show source ]
# File lib/rake.rb, line 1774
1774: def do_option(opt, value)
1775: case opt
1776: when '--dry-run'
1777: verbose(true)
1778: nowrite(true)
1779: options.dryrun = true
1780: options.trace = true
1781: when '--help'
1782: help
1783: exit
1784: when '--libdir'
1785: $:.push(value)
1786: when '--nosearch'
1787: options.nosearch = true
1788: when '--prereqs'
1789: options.show_prereqs = true
1790: when '--quiet'
1791: verbose(false)
1792: when '--rakefile'
1793: @rakefiles.clear
1794: @rakefiles << value
1795: when '--rakelibdir'
1796: options.rakelib = value.split(':')
1797: when '--require'
1798: begin
1799: require value
1800: rescue LoadError => ex
1801: begin
1802: rake_require value
1803: rescue LoadError => ex2
1804: raise ex
1805: end
1806: end
1807: when '--silent'
1808: verbose(false)
1809: options.silent = true
1810: when '--tasks'
1811: options.show_tasks = true
1812: options.show_task_pattern = Regexp.new(value || '.')
1813: when '--trace'
1814: options.trace = true
1815: verbose(true)
1816: when '--usage'
1817: usage
1818: exit
1819: when '--verbose'
1820: verbose(true)
1821: when '--version'
1822: puts "rake, version #{RAKEVERSION}"
1823: exit
1824: when '--classic-namespace'
1825: require 'rake/classic_namespace'
1826: options.classic_namespace = true
1827: end
1828: end
Read and handle the command line options.
[ show source ]
# File lib/rake.rb, line 1831
1831: def handle_options
1832: options.rakelib = 'rakelib'
1833:
1834: opts = GetoptLong.new(*command_line_options)
1835: opts.each { |opt, value| do_option(opt, value) }
1836:
1837: # If class namespaces are requested, set the global options
1838: # according to the values in the options structure.
1839: if options.classic_namespace
1840: $show_tasks = options.show_tasks
1841: $show_prereqs = options.show_prereqs
1842: $trace = options.trace
1843: $dryrun = options.dryrun
1844: $silent = options.silent
1845: end
1846: end
True if one of the files in RAKEFILES is in the current directory. If a match is found, it is copied into @rakefile.
[ show source ]
# File lib/rake.rb, line 1716
1716: def have_rakefile
1717: @rakefiles.each do |fn|
1718: if File.exist?(fn) || fn == ''
1719: @rakefile = fn
1720: return true
1721: end
1722: end
1723: return false
1724: end
Display the rake command line help.
[ show source ]
# File lib/rake.rb, line 1732
1732: def help
1733: usage
1734: puts
1735: puts "Options are ..."
1736: puts
1737: OPTIONS.sort.each do |long, short, mode, desc|
1738: if mode == GetoptLong::REQUIRED_ARGUMENT
1739: if desc =~ /\b([A-Z]{2,})\b/
1740: long = long + "=#{$1}"
1741: end
1742: end
1743: printf " %-20s (%s)\n", long, short
1744: printf " %s\n", desc
1745: end
1746: end
Load the pending list of imported files.
[ show source ]
# File lib/rake.rb, line 1905
1905: def load_imports
1906: while fn = @pending_imports.shift
1907: next if @imported.member?(fn)
1908: if fn_task = lookup(fn)
1909: fn_task.invoke
1910: end
1911: ext = File.extname(fn)
1912: loader = @loaders[ext] || @default_loader
1913: loader.load(fn)
1914: @imported << fn
1915: end
1916: end
Find the rakefile and then load it and any pending imports.
[ show source ]
# File lib/rake.rb, line 1865
1865: def load_rakefile
1866: here = Dir.pwd
1867: while ! have_rakefile
1868: Dir.chdir("..")
1869: if Dir.pwd == here || options.nosearch
1870: fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})"
1871: end
1872: here = Dir.pwd
1873: end
1874: puts "(in #{Dir.pwd})" unless options.silent
1875: $rakefile = @rakefile
1876: load File.expand_path(@rakefile) if @rakefile != ''
1877: options.rakelib.each do |rlib|
1878: Dir["#{rlib}/*.rake"].each do |name| add_import name end
1879: end
1880: load_imports
1881: end
Application options from the command line
[ show source ]
# File lib/rake.rb, line 1710
1710: def options
1711: @options ||= OpenStruct.new
1712: end
Similar to the regular Ruby require command, but will check for .rake files in addition to .rb files.
[ show source ]
# File lib/rake.rb, line 1850
1850: def rake_require(file_name, paths=$LOAD_PATH, loaded=$")
1851: return false if loaded.include?(file_name)
1852: paths.each do |path|
1853: fn = file_name + ".rake"
1854: full_path = File.join(path, fn)
1855: if File.exist?(full_path)
1856: load full_path
1857: loaded << fn
1858: return true
1859: end
1860: end
1861: fail LoadError, "Can't find #{file_name}"
1862: end
[ show source ]
# File lib/rake.rb, line 1937
1937: def rakefile_location
1938: begin
1939: fail
1940: rescue RuntimeError => ex
1941: ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
1942: end
1943: end
Run the rake application.
[ show source ]
# File lib/rake.rb, line 1946
1946: def run
1947: begin
1948: handle_options
1949: tasks = collect_tasks
1950: load_rakefile
1951: if options.show_tasks
1952: display_tasks_and_comments
1953: elsif options.show_prereqs
1954: display_prerequisites
1955: else
1956: tasks.each { |task_name| self[task_name].invoke }
1957: end
1958: rescue SystemExit, GetoptLong::InvalidOption => ex
1959: # Exit silently
1960: exit(1)
1961: rescue Exception => ex
1962: # Exit with error message
1963: $stderr.puts "rake aborted!"
1964: $stderr.puts ex.message
1965: if options.trace
1966: $stderr.puts ex.backtrace.join("\n")
1967: else
1968: $stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
1969: $stderr.puts "(See full trace by running task with --trace)"
1970: end
1971: exit(1)
1972: end
1973: end
Display the program usage line.
[ show source ]
# File lib/rake.rb, line 1727
1727: def usage
1728: puts "rake [-f rakefile] {options} targets..."
1729: end