[R] |
original_dir |
The original directory where rake was invoked.
|
Included modules
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.
1853: def add_import(fn)
1854: @pending_imports << fn
1855: end
Add a loader to handle imported files ending in the extension ext.
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.
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.
1720: def command_line_options
1721: OPTIONS.collect { |lst| lst[0..-2] }
1722: end
Warn about deprecated use of top level constant names.
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
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.
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.
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.
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:
1791:
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.
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.
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.
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.
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
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.
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
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.
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.
1676: def usage
1677: puts "rake [-f rakefile] {options} targets..."
1678: end