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
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"], ] |
[R] | original_dir | The original directory where rake was invoked. |
Create a Rake::Application object.
[ show source ]
# File lib/rake.rb, line 1646 1646: def initialize 1647: super 1648: @rakefile = nil 1649: @pending_imports = [] 1650: @imported = [] 1651: @loaders = {} 1652: @default_loader = Rake::DefaultLoader.new 1653: @original_dir = Dir.pwd 1654: add_loader('rf', DefaultLoader.new) 1655: add_loader('rake', DefaultLoader.new) 1656: end
Add a file to the list of files to be imported.
[ show source ]
# File lib/rake.rb, line 1853 1853: def add_import(fn) 1854: @pending_imports << fn 1855: end
Add a loader to handle imported files ending in the extension ext.
[ show source ]
# File lib/rake.rb, line 1871 1871: def add_loader(ext, loader) 1872: ext = ".#{ext}" unless ext =~ /^\./ 1873: @loaders[ext] = loader 1874: end
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.
[ show source ]
# File lib/rake.rb, line 1839 1839: def collect_tasks 1840: tasks = [] 1841: ARGV.each do |arg| 1842: if arg =~ /^(\w+)=(.*)$/ 1843: ENV[$1] = $2 1844: else 1845: tasks << arg 1846: end 1847: end 1848: tasks.push("default") if tasks.size == 0 1849: tasks 1850: end
Return a list of the command line options supported by the program.
[ show source ]
# File lib/rake.rb, line 1720 1720: def command_line_options 1721: OPTIONS.collect { |lst| lst[0..-2] } 1722: end
Warn about deprecated use of top level constant names.
[ show source ]
# File lib/rake.rb, line 1877 1877: def const_warning(const_name) 1878: @const_warning ||= false 1879: if ! @const_warning 1880: puts %{WARNING: Deprecated reference to top-level constant '#{const_name}'} + 1881: %{found at: #{rakefile_location}} # ' 1882: puts %{ Use --classic-namespace on rake command} 1883: puts %{ or 'require "rake/classic_namespace"' in Rakefile} 1884: end 1885: @const_warning = true 1886: end
Display the tasks and prerequisites
[ show source ]
# File lib/rake.rb, line 1711 1711: def display_prerequisites 1712: Rake::Task.tasks.each do |t| 1713: puts "rake #{t.name}" 1714: t.prerequisites.each { |pre| puts " #{pre}" } 1715: end 1716: end
Display the tasks and dependencies.
[ show source ]
# File lib/rake.rb, line 1698 1698: def display_tasks_and_comments 1699: displayable_tasks = Rake::Task.tasks.select { |t| 1700: t.comment && t.name =~ options.show_task_pattern 1701: } 1702: width = displayable_tasks.collect { |t| 1703: t.name.length 1704: }.max 1705: displayable_tasks.each do |t| 1706: printf "rake %-#{width}s # %s\n", t.name, t.comment 1707: end 1708: end
Do the option defined by opt and value.
[ show source ]
# File lib/rake.rb, line 1725 1725: def do_option(opt, value) 1726: case opt 1727: when '--dry-run' 1728: verbose(true) 1729: nowrite(true) 1730: options.dryrun = true 1731: options.trace = true 1732: when '--help' 1733: help 1734: exit 1735: when '--libdir' 1736: $:.push(value) 1737: when '--nosearch' 1738: options.nosearch = true 1739: when '--prereqs' 1740: options.show_prereqs = true 1741: when '--quiet' 1742: verbose(false) 1743: when '--rakefile' 1744: RAKEFILES.clear 1745: RAKEFILES << value 1746: when '--rakelibdir' 1747: options.rakelib = value.split(':') 1748: when '--require' 1749: begin 1750: require value 1751: rescue LoadError => ex 1752: begin 1753: rake_require value 1754: rescue LoadError => ex2 1755: raise ex 1756: end 1757: end 1758: when '--silent' 1759: verbose(false) 1760: options.silent = true 1761: when '--tasks' 1762: options.show_tasks = true 1763: options.show_task_pattern = Regexp.new(value || '.') 1764: when '--trace' 1765: options.trace = true 1766: verbose(true) 1767: when '--usage' 1768: usage 1769: exit 1770: when '--verbose' 1771: verbose(true) 1772: when '--version' 1773: puts "rake, version #{RAKEVERSION}" 1774: exit 1775: when '--classic-namespace' 1776: require 'rake/classic_namespace' 1777: options.classic_namespace = true 1778: else 1779: fail "Unknown option: #{opt}" 1780: end 1781: end
Read and handle the command line options.
[ show source ]
# File lib/rake.rb, line 1784 1784: def handle_options 1785: options.rakelib = 'rakelib' 1786: 1787: opts = GetoptLong.new(*command_line_options) 1788: opts.each { |opt, value| do_option(opt, value) } 1789: 1790: # If class namespaces are requested, set the global options 1791: # according to the values in the options structure. 1792: if options.classic_namespace 1793: $show_tasks = options.show_tasks 1794: $show_prereqs = options.show_prereqs 1795: $trace = options.trace 1796: $dryrun = options.dryrun 1797: $silent = options.silent 1798: end 1799: 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 1665 1665: def have_rakefile 1666: RAKEFILES.each do |fn| 1667: if File.exist?(fn) || fn == '' 1668: @rakefile = fn 1669: return true 1670: end 1671: end 1672: return false 1673: end
Display the rake command line help.
[ show source ]
# File lib/rake.rb, line 1681 1681: def help 1682: usage 1683: puts 1684: puts "Options are ..." 1685: puts 1686: OPTIONS.sort.each do |long, short, mode, desc| 1687: if mode == GetoptLong::REQUIRED_ARGUMENT 1688: if desc =~ /\b([A-Z]{2,})\b/ 1689: long = long + "=#{$1}" 1690: end 1691: end 1692: printf " %-20s (%s)\n", long, short 1693: printf " %s\n", desc 1694: end 1695: end
Load the pending list of imported files.
[ show source ]
# File lib/rake.rb, line 1858 1858: def load_imports 1859: while fn = @pending_imports.shift 1860: next if @imported.member?(fn) 1861: Rake::Task[fn].invoke if Rake::Task.task_defined?(fn) 1862: ext = File.extname(fn) 1863: loader = @loaders[ext] || @default_loader 1864: loader.load(fn) 1865: @imported << fn 1866: end 1867: end
Find the rakefile and then load it and any pending imports.
[ show source ]
# File lib/rake.rb, line 1818 1818: def load_rakefile 1819: here = Dir.pwd 1820: while ! have_rakefile 1821: Dir.chdir("..") 1822: if Dir.pwd == here || options.nosearch 1823: fail "No Rakefile found (looking for: #{RAKEFILES.join(', ')})" 1824: end 1825: here = Dir.pwd 1826: end 1827: puts "(in #{Dir.pwd})" unless options.silent 1828: $rakefile = @rakefile 1829: load File.expand_path(@rakefile) if @rakefile != '' 1830: options.rakelib.each do |rlib| 1831: Dir["#{rlib}/*.rake"].each do |name| add_import name end 1832: end 1833: load_imports 1834: end
Application options from the command line
[ show source ]
# File lib/rake.rb, line 1659 1659: def options 1660: @options ||= OpenStruct.new 1661: 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 1803 1803: def rake_require(file_name, paths=$LOAD_PATH, loaded=$") 1804: return false if loaded.include?(file_name) 1805: paths.each do |path| 1806: fn = file_name + ".rake" 1807: full_path = File.join(path, fn) 1808: if File.exist?(full_path) 1809: load full_path 1810: loaded << fn 1811: return true 1812: end 1813: end 1814: fail LoadError, "Can't find #{file_name}" 1815: end
[ show source ]
# File lib/rake.rb, line 1888 1888: def rakefile_location 1889: begin 1890: fail 1891: rescue RuntimeError => ex 1892: ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" 1893: end 1894: end
Run the rake application.
[ show source ]
# File lib/rake.rb, line 1897 1897: def run 1898: handle_options 1899: begin 1900: tasks = collect_tasks 1901: load_rakefile 1902: if options.show_tasks 1903: display_tasks_and_comments 1904: elsif options.show_prereqs 1905: display_prerequisites 1906: else 1907: tasks.each { |task_name| Rake::Task[task_name].invoke } 1908: end 1909: rescue Exception => ex 1910: puts "rake aborted!" 1911: puts ex.message 1912: if options.trace 1913: puts ex.backtrace.join("\n") 1914: else 1915: puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" 1916: puts "(See full trace by running task with --trace)" 1917: end 1918: exit(1) 1919: end 1920: end
Display the program usage line.
[ show source ]
# File lib/rake.rb, line 1676 1676: def usage 1677: puts "rake [-f rakefile] {options} targets..." 1678: end