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
- 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::REQUIRED_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::NO_ARGUMENT, "Display the tasks and dependencies, 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 1484 1484: def initialize 1485: super 1486: @rakefile = nil 1487: @pending_imports = [] 1488: @imported = [] 1489: @loaders = {} 1490: @default_loader = Rake::DefaultLoader.new 1491: @original_dir = Dir.pwd 1492: add_loader('rf', DefaultLoader.new) 1493: add_loader('rake', DefaultLoader.new) 1494: end
Add a file to the list of files to be imported.
[ show source ]
# File lib/rake.rb, line 1666 1666: def add_import(fn) 1667: @pending_imports << fn 1668: end
Add a loader to handle imported files ending in the extension ext.
[ show source ]
# File lib/rake.rb, line 1684 1684: def add_loader(ext, loader) 1685: ext = ".#{ext}" unless ext =~ /^\./ 1686: @loaders[ext] = loader 1687: 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 1652 1652: def collect_tasks 1653: tasks = [] 1654: ARGV.each do |arg| 1655: if arg =~ /^(\w+)=(.*)$/ 1656: ENV[$1] = $2 1657: else 1658: tasks << arg 1659: end 1660: end 1661: tasks.push("default") if tasks.size == 0 1662: tasks 1663: end
Return a list of the command line options supported by the program.
[ show source ]
# File lib/rake.rb, line 1559 1559: def command_line_options 1560: OPTIONS.collect { |lst| lst[0..-2] } 1561: end
Warn about deprecated use of top level constant names.
[ show source ]
# File lib/rake.rb, line 1690 1690: def const_warning(const_name) 1691: @const_warning ||= false 1692: if ! @const_warning 1693: puts %{WARNING: Deprecated reference to top-level constant '#{const_name}'} + 1694: %{found at: #{rakefile_location}} # ' 1695: puts %{ Use --classic-namespace on rake command} 1696: puts %{ or 'require "rake/classic_namespace"' in Rakefile} 1697: end 1698: @const_warning = true 1699: end
Display the tasks and prerequisites
[ show source ]
# File lib/rake.rb, line 1550 1550: def display_prerequisites 1551: Rake::Task.tasks.each do |t| 1552: puts "rake #{t.name}" 1553: t.prerequisites.each { |pre| puts " #{pre}" } 1554: end 1555: end
Display the tasks and dependencies.
[ show source ]
# File lib/rake.rb, line 1536 1536: def display_tasks_and_comments 1537: width = Rake::Task.tasks.select { |t| 1538: t.comment 1539: }.collect { |t| 1540: t.name.length 1541: }.max 1542: Rake::Task.tasks.each do |t| 1543: if t.comment 1544: printf "rake %-#{width}s # %s\n", t.name, t.comment 1545: end 1546: end 1547: end
Do the option defined by opt and value.
[ show source ]
# File lib/rake.rb, line 1564 1564: def do_option(opt, value) 1565: case opt 1566: when '--dry-run' 1567: verbose(true) 1568: nowrite(true) 1569: options.dryrun = true 1570: options.trace = true 1571: when '--help' 1572: help 1573: exit 1574: when '--libdir' 1575: $:.push(value) 1576: when '--nosearch' 1577: options.nosearch = true 1578: when '--prereqs' 1579: options.show_prereqs = true 1580: when '--quiet' 1581: verbose(false) 1582: when '--rakefile' 1583: RAKEFILES.clear 1584: RAKEFILES << value 1585: when '--rakelibdir' 1586: options.rakelib = value.split(':') 1587: when '--require' 1588: require value 1589: when '--silent' 1590: verbose(false) 1591: options.silent = true 1592: when '--tasks' 1593: options.show_tasks = true 1594: when '--trace' 1595: options.trace = true 1596: verbose(true) 1597: when '--usage' 1598: usage 1599: exit 1600: when '--verbose' 1601: verbose(true) 1602: when '--version' 1603: puts "rake, version #{RAKEVERSION}" 1604: exit 1605: when '--classic-namespace' 1606: require 'rake/classic_namespace' 1607: options.classic_namespace = true 1608: else 1609: fail "Unknown option: #{opt}" 1610: end 1611: end
Read and handle the command line options.
[ show source ]
# File lib/rake.rb, line 1614 1614: def handle_options 1615: options.rakelib = 'rakelib' 1616: 1617: opts = GetoptLong.new(*command_line_options) 1618: opts.each { |opt, value| do_option(opt, value) } 1619: 1620: # If class namespaces are requested, set the global options 1621: # according to the values in the options structure. 1622: if options.classic_namespace 1623: $show_tasks = options.show_tasks 1624: $show_prereqs = options.show_prereqs 1625: $trace = options.trace 1626: $dryrun = options.dryrun 1627: $silent = options.silent 1628: end 1629: 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 1503 1503: def have_rakefile 1504: RAKEFILES.each do |fn| 1505: if File.exist?(fn) 1506: @rakefile = fn 1507: return true 1508: end 1509: end 1510: return false 1511: end
Display the rake command line help.
[ show source ]
# File lib/rake.rb, line 1519 1519: def help 1520: usage 1521: puts 1522: puts "Options are ..." 1523: puts 1524: OPTIONS.sort.each do |long, short, mode, desc| 1525: if mode == GetoptLong::REQUIRED_ARGUMENT 1526: if desc =~ /\b([A-Z]{2,})\b/ 1527: long = long + "=#{$1}" 1528: end 1529: end 1530: printf " %-20s (%s)\n", long, short 1531: printf " %s\n", desc 1532: end 1533: end
Load the pending list of imported files.
[ show source ]
# File lib/rake.rb, line 1671 1671: def load_imports 1672: while fn = @pending_imports.shift 1673: next if @imported.member?(fn) 1674: Rake::Task[fn].invoke if Rake::Task.task_defined?(fn) 1675: ext = File.extname(fn) 1676: loader = @loaders[ext] || @default_loader 1677: loader.load(fn) 1678: @imported << fn 1679: end 1680: end
[ show source ]
# File lib/rake.rb, line 1631 1631: def load_rakefile 1632: here = Dir.pwd 1633: while ! have_rakefile 1634: Dir.chdir("..") 1635: if Dir.pwd == here || options.nosearch 1636: fail "No Rakefile found (looking for: #{RAKEFILES.join(', ')})" 1637: end 1638: here = Dir.pwd 1639: end 1640: puts "(in #{Dir.pwd})" unless options.silent 1641: $rakefile = @rakefile 1642: load File.expand_path(@rakefile) 1643: options.rakelib.each do |rlib| 1644: Dir["#{rlib}/*.rake"].each do |name| add_import name end 1645: end 1646: load_imports 1647: end
Application options from the command line
[ show source ]
# File lib/rake.rb, line 1497 1497: def options 1498: @options ||= OpenStruct.new 1499: end
[ show source ]
# File lib/rake.rb, line 1701 1701: def rakefile_location 1702: begin 1703: fail 1704: rescue RuntimeError => ex 1705: ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" 1706: end 1707: end
Run the rake application.
[ show source ]
# File lib/rake.rb, line 1710 1710: def run 1711: handle_options 1712: begin 1713: tasks = collect_tasks 1714: load_rakefile 1715: if options.show_tasks 1716: display_tasks_and_comments 1717: elsif options.show_prereqs 1718: display_prerequisites 1719: else 1720: tasks.each { |task_name| Rake::Task[task_name].invoke } 1721: end 1722: rescue Exception => ex 1723: puts "rake aborted!" 1724: puts ex.message 1725: if options.trace 1726: puts ex.backtrace.join("\n") 1727: else 1728: puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || "" 1729: puts "(See full trace by running task with --trace)" 1730: end 1731: exit(1) 1732: end 1733: end
Display the program usage line.
[ show source ]
# File lib/rake.rb, line 1514 1514: def usage 1515: puts "rake [-f rakefile] {options} targets..." 1516: end