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