RAKEVERSION = '0.8.7'
require 'rbconfig'
require 'fileutils'
require 'singleton'
require 'monitor'
require 'optparse'
require 'ostruct'
require 'rake/win32'
$trace = false
class Module
def rake_extension(method)
if method_defined?(method)
$stderr.puts "WARNING: Possible conflict with Rake extension: #{self}##{method} already exists"
else
yield
end
end
end
class String
rake_extension("ext") do
def ext(newext='')
return self.dup if ['.', '..'].include? self
if newext != ''
newext = (newext =~ /^\./) ? newext : ("." + newext)
end
self.chomp(File.extname(self)) << newext
end
end
rake_extension("pathmap") do
def pathmap_explode
head, tail = File.split(self)
return [self] if head == self
return [tail] if head == '.' || tail == '/'
return [head, tail] if head == '/'
return head.pathmap_explode + [tail]
end
protected :pathmap_explode
def pathmap_partial(n)
dirs = File.dirname(self).pathmap_explode
partial_dirs =
if n > 0
dirs[0...n]
elsif n < 0
dirs.reverse[0...-n].reverse
else
"."
end
File.join(partial_dirs)
end
protected :pathmap_partial
def pathmap_replace(patterns, &block)
result = self
patterns.split(';').each do |pair|
pattern, replacement = pair.split(',')
pattern = Regexp.new(pattern)
if replacement == '*' && block_given?
result = result.sub(pattern, &block)
elsif replacement
result = result.sub(pattern, replacement)
else
result = result.sub(pattern, '')
end
end
result
end
protected :pathmap_replace
def pathmap(spec=nil, &block)
return self if spec.nil?
result = ''
spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag|
case frag
when '%f'
result << File.basename(self)
when '%n'
result << File.basename(self).ext
when '%d'
result << File.dirname(self)
when '%x'
result << File.extname(self)
when '%X'
result << self.ext
when '%p'
result << self
when '%s'
result << (File::ALT_SEPARATOR || File::SEPARATOR)
when '%-'
when '%%'
result << "%"
when /%(-?\d+)d/
result << pathmap_partial($1.to_i)
when /^%\{([^}]*)\}(\d*[dpfnxX])/
patterns, operator = $1, $2
result << pathmap('%' + operator).pathmap_replace(patterns, &block)
when /^%/
fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'"
else
result << frag
end
end
result
end
end
end
module Rake
class TaskArgumentError < ArgumentError
end
class RuleRecursionOverflowError < StandardError
def initialize(*args)
super
@targets = []
end
def add_target(target)
@targets << target
end
def message
super + ": [" + @targets.reverse.join(' => ') + "]"
end
end
class << self
def application
@application ||= Rake::Application.new
end
def application=(app)
@application = app
end
def original_dir
application.original_dir
end
end
module Cloneable
def dup
sibling = self.class.new
instance_variables.each do |ivar|
value = self.instance_variable_get(ivar)
new_value = value.clone rescue value
sibling.instance_variable_set(ivar, new_value)
end
sibling.taint if tainted?
sibling
end
def clone
sibling = dup
sibling.freeze if frozen?
sibling
end
end
class PseudoStatus
attr_reader :exitstatus
def initialize(code=0)
@exitstatus = code
end
def to_i
@exitstatus << 8
end
def >>(n)
to_i >> n
end
def stopped?
false
end
def exited?
true
end
end
class TaskArguments
include Enumerable
attr_reader :names
def initialize(names, values, parent=nil)
@names = names
@parent = parent
@hash = {}
names.each_with_index { |name, i|
@hash[name.to_sym] = values[i] unless values[i].nil?
}
end
def new_scope(names)
values = names.collect { |n| self[n] }
self.class.new(names, values, self)
end
def [](index)
lookup(index.to_sym)
end
def with_defaults(defaults)
@hash = defaults.merge(@hash)
end
def each(&block)
@hash.each(&block)
end
def method_missing(sym, *args, &block)
lookup(sym.to_sym)
end
def to_hash
@hash
end
def to_s
@hash.inspect
end
def inspect
to_s
end
protected
def lookup(name)
if @hash.has_key?(name)
@hash[name]
elsif ENV.has_key?(name.to_s)
ENV[name.to_s]
elsif ENV.has_key?(name.to_s.upcase)
ENV[name.to_s.upcase]
elsif @parent
@parent.lookup(name)
end
end
end
EMPTY_TASK_ARGS = TaskArguments.new([], [])
class InvocationChain
def initialize(value, tail)
@value = value
@tail = tail
end
def member?(obj)
@value == obj || @tail.member?(obj)
end
def append(value)
if member?(value)
fail RuntimeError, "Circular dependency detected: #{to_s} => #{value}"
end
self.class.new(value, self)
end
def to_s
"#{prefix}#{@value}"
end
def self.append(value, chain)
chain.append(value)
end
private
def prefix
"#{@tail.to_s} => "
end
class EmptyInvocationChain
def member?(obj)
false
end
def append(value)
InvocationChain.new(value, self)
end
def to_s
"TOP"
end
end
EMPTY = EmptyInvocationChain.new
end
end
module Rake
class Task
attr_reader :prerequisites
attr_reader :actions
attr_accessor :application
attr_reader :comment
attr_reader :full_comment
attr_reader :scope
def to_s
name
end
def inspect
"<#{self.class} #{name} => [#{prerequisites.join(', ')}]>"
end
attr_writer :sources
def sources
@sources ||= []
end
def source
@sources.first if defined?(@sources)
end
def initialize(task_name, app)
@name = task_name.to_s
@prerequisites = []
@actions = []
@already_invoked = false
@full_comment = nil
@comment = nil
@lock = Monitor.new
@application = app
@scope = app.current_scope
@arg_names = nil
end
def enhance(deps=nil, &block)
@prerequisites |= deps if deps
@actions << block if block_given?
self
end
def name
@name.to_s
end
def name_with_args # :nodoc:
if arg_description
"#{name}#{arg_description}"
else
name
end
end
def arg_description # :nodoc:
@arg_names ? "[#{(arg_names || []).join(',')}]" : nil
end
def arg_names
@arg_names || []
end
def reenable
@already_invoked = false
end
def clear
clear_prerequisites
clear_actions
self
end
def clear_prerequisites
prerequisites.clear
self
end
def clear_actions
actions.clear
self
end
def invoke(*args)
task_args = TaskArguments.new(arg_names, args)
invoke_with_call_chain(task_args, InvocationChain::EMPTY)
end
def invoke_with_call_chain(task_args, invocation_chain) new_chain = InvocationChain.append(self, invocation_chain)
@lock.synchronize do
if application.options.trace
puts "** Invoke #{name} #{format_trace_flags}"
end
return if @already_invoked
@already_invoked = true
invoke_prerequisites(task_args, new_chain)
execute(task_args) if needed?
end
end
protected :invoke_with_call_chain
def invoke_prerequisites(task_args, invocation_chain) @prerequisites.each { |n|
prereq = application[n, @scope]
prereq_args = task_args.new_scope(prereq.arg_names)
prereq.invoke_with_call_chain(prereq_args, invocation_chain)
}
end
def format_trace_flags
flags = []
flags << "first_time" unless @already_invoked
flags << "not_needed" unless needed?
flags.empty? ? "" : "(" + flags.join(", ") + ")"
end
private :format_trace_flags
def execute(args=nil)
args ||= EMPTY_TASK_ARGS
if application.options.dryrun
puts "** Execute (dry run) #{name}"
return
end
if application.options.trace
puts "** Execute #{name}"
end
application.enhance_with_matching_rule(name) if @actions.empty?
@actions.each do |act|
case act.arity
when 1
act.call(self)
else
act.call(self, args)
end
end
end
def needed?
true
end
def timestamp
@prerequisites.collect { |p| application[p].timestamp }.max || Time.now
end
def add_description(description)
return if ! description
comment = description.strip
add_comment(comment) if comment && ! comment.empty?
end
def comment=(description)
add_description(description)
end
def add_comment(comment)
if @full_comment
@full_comment << " / "
else
@full_comment = ''
end
@full_comment << comment
if @full_comment =~ /\A([^.]+?\.)( |$)/
@comment = $1
else
@comment = @full_comment
end
end
private :add_comment
def set_arg_names(args)
@arg_names = args.map { |a| a.to_sym }
end
def investigation
result = "------------------------------\n"
result << "Investigating #{name}\n"
result << "class: #{self.class}\n"
result << "task needed: #{needed?}\n"
result << "timestamp: #{timestamp}\n"
result << "pre-requisites: \n"
prereqs = @prerequisites.collect {|name| application[name]}
prereqs.sort! {|a,b| a.timestamp <=> b.timestamp}
prereqs.each do |p|
result << "--#{p.name} (#{p.timestamp})\n"
end
latest_prereq = @prerequisites.collect{|n| application[n].timestamp}.max
result << "latest-prerequisite time: #{latest_prereq}\n"
result << "................................\n\n"
return result
end
class << self
def clear
Rake.application.clear
end
def tasks
Rake.application.tasks
end
def [](task_name)
Rake.application[task_name]
end
def task_defined?(task_name)
Rake.application.lookup(task_name) != nil
end
def define_task(*args, &block)
Rake.application.define_task(self, *args, &block)
end
def create_rule(*args, &block)
Rake.application.create_rule(*args, &block)
end
def scope_name(scope, task_name)
(scope + [task_name]).join(':')
end
end end
class FileTask < Task
def needed?
! File.exist?(name) || out_of_date?(timestamp)
end
def timestamp
if File.exist?(name)
File.mtime(name.to_s)
else
Rake::EARLY
end
end
private
def out_of_date?(stamp)
@prerequisites.any? { |n| application[n].timestamp > stamp}
end
class << self
def scope_name(scope, task_name)
task_name
end
end
end
class FileCreationTask < FileTask
def needed?
! File.exist?(name)
end
def timestamp
Rake::EARLY
end
end
class MultiTask < Task
private
def invoke_prerequisites(args, invocation_chain)
threads = @prerequisites.collect { |p|
Thread.new(p) { |r| application[r].invoke_with_call_chain(args, invocation_chain) }
}
threads.each { |t| t.join }
end
end
end
def task(*args, &block)
Rake::Task.define_task(*args, &block)
end
def file(*args, &block)
Rake::FileTask.define_task(*args, &block)
end
def file_create(args, &block)
Rake::FileCreationTask.define_task(args, &block)
end
def directory(dir)
Rake.each_dir_parent(dir) do |d|
file_create d do |t|
mkdir_p t.name if ! File.exist?(t.name)
end
end
end
def multitask(args, &block)
Rake::MultiTask.define_task(args, &block)
end
def namespace(name=nil, &block)
Rake.application.in_namespace(name, &block)
end
def rule(*args, &block)
Rake::Task.create_rule(*args, &block)
end
def desc(description)
Rake.application.last_description = description
end
def import(*fns)
fns.each do |fn|
Rake.application.add_import(fn)
end
end
module FileUtils
RUBY_EXT = ((Config::CONFIG['ruby_install_name'] =~ /\.(com|cmd|exe|bat|rb|sh)$/) ?
"" :
Config::CONFIG['EXEEXT'])
RUBY = File.join(
Config::CONFIG['bindir'],
Config::CONFIG['ruby_install_name'] + RUBY_EXT).
sub(/.*\s.*/m, '"\&"')
OPT_TABLE['sh'] = %w(noop verbose)
OPT_TABLE['ruby'] = %w(noop verbose)
def sh(*cmd, &block)
options = (Hash === cmd.last) ? cmd.pop : {}
unless block_given?
show_command = cmd.join(" ")
show_command = show_command[0,42] + "..." unless $trace
block = lambda { |ok, status|
ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
}
end
if RakeFileUtils.verbose_flag == :default
options[:verbose] = true
else
options[:verbose] ||= RakeFileUtils.verbose_flag
end
options[:noop] ||= RakeFileUtils.nowrite_flag
rake_check_options options, :noop, :verbose
rake_output_message cmd.join(" ") if options[:verbose]
unless options[:noop]
res = rake_system(*cmd)
status = $?
status = PseudoStatus.new(1) if !res && status.nil?
block.call(res, status)
end
end
def rake_system(*cmd)
Rake::AltSystem.system(*cmd)
end
private :rake_system
def ruby(*args,&block)
options = (Hash === args.last) ? args.pop : {}
if args.length > 1 then
sh(*([RUBY] + args + [options]), &block)
else
sh("#{RUBY} #{args.first}", options, &block)
end
end
LN_SUPPORTED = [true]
def safe_ln(*args)
unless LN_SUPPORTED[0]
cp(*args)
else
begin
ln(*args)
rescue StandardError, NotImplementedError => ex
LN_SUPPORTED[0] = false
cp(*args)
end
end
end
def split_all(path)
head, tail = File.split(path)
return [tail] if head == '.' || tail == '/'
return [head, tail] if head == '/'
return split_all(head) + [tail]
end
end
module RakeFileUtils
include FileUtils
class << self
attr_accessor :verbose_flag, :nowrite_flag
end
RakeFileUtils.verbose_flag = :default
RakeFileUtils.nowrite_flag = false
$fileutils_verbose = true
$fileutils_nowrite = false
FileUtils::OPT_TABLE.each do |name, opts|
default_options = []
if opts.include?(:verbose) || opts.include?("verbose")
default_options << ':verbose => RakeFileUtils.verbose_flag'
end
if opts.include?(:noop) || opts.include?("noop")
default_options << ':noop => RakeFileUtils.nowrite_flag'
end
next if default_options.empty?
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{name}( *args, &block )
super(
*rake_merge_option(args,
#{default_options.join(', ')}
), &block)
end
EOS
end
def verbose(value=nil)
oldvalue = RakeFileUtils.verbose_flag
RakeFileUtils.verbose_flag = value unless value.nil?
if block_given?
begin
yield
ensure
RakeFileUtils.verbose_flag = oldvalue
end
end
RakeFileUtils.verbose_flag
end
def nowrite(value=nil)
oldvalue = RakeFileUtils.nowrite_flag
RakeFileUtils.nowrite_flag = value unless value.nil?
if block_given?
begin
yield
ensure
RakeFileUtils.nowrite_flag = oldvalue
end
end
oldvalue
end
def when_writing(msg=nil)
if RakeFileUtils.nowrite_flag
puts "DRYRUN: #{msg}" if msg
else
yield
end
end
def rake_merge_option(args, defaults)
if Hash === args.last
defaults.update(args.last)
args.pop
end
args.push defaults
args
end
private :rake_merge_option
def rake_output_message(message)
$stderr.puts(message)
end
private :rake_output_message
def rake_check_options(options, *optdecl)
h = options.dup
optdecl.each do |name|
h.delete name
end
raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
end
private :rake_check_options
extend self
end
include RakeFileUtils
private(*FileUtils.instance_methods(false))
private(*RakeFileUtils.instance_methods(false))
module Rake
class FileList
include Cloneable
ARRAY_METHODS = (Array.instance_methods - Object.instance_methods).map { |n| n.to_s }
MUST_DEFINE = %w[to_a inspect]
MUST_NOT_DEFINE = %w[to_a to_ary partition *]
SPECIAL_RETURN = %w[
map collect sort sort_by select find_all reject grep
compact flatten uniq values_at
+ - & |
]
DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).collect{ |s| s.to_s }.sort.uniq
DELEGATING_METHODS.each_with_index do |sym, i|
if SPECIAL_RETURN.include?(sym)
ln = __LINE__+1
class_eval %{
def #{sym}(*args, &block)
resolve
result = @items.send(:#{sym}, *args, &block)
FileList.new.import(result)
end
}, __FILE__, ln
else
ln = __LINE__+1
class_eval %{
def #{sym}(*args, &block)
resolve
result = @items.send(:#{sym}, *args, &block)
result.object_id == @items.object_id ? self : result
end
}, __FILE__, ln
end
end
def initialize(*patterns)
@pending_add = []
@pending = false
@exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
@exclude_procs = DEFAULT_IGNORE_PROCS.dup
@exclude_re = nil
@items = []
patterns.each { |pattern| include(pattern) }
yield self if block_given?
end
def include(*filenames)
filenames.each do |fn|
if fn.respond_to? :to_ary
include(*fn.to_ary)
else
@pending_add << fn
end
end
@pending = true
self
end
alias :add :include
def exclude(*patterns, &block)
patterns.each do |pat|
@exclude_patterns << pat
end
if block_given?
@exclude_procs << block
end
resolve_exclude if ! @pending
self
end
def clear_exclude
@exclude_patterns = []
@exclude_procs = []
calculate_exclude_regexp if ! @pending
self
end
def ==(array)
to_ary == array
end
def to_a
resolve
@items
end
def to_ary
to_a
end
def is_a?(klass)
klass == Array || super(klass)
end
alias kind_of? is_a?
def *(other)
result = @items * other
case result
when Array
FileList.new.import(result)
else
result
end
end
def resolve
if @pending
@pending = false
@pending_add.each do |fn| resolve_add(fn) end
@pending_add = []
resolve_exclude
end
self
end
def calculate_exclude_regexp
ignores = []
@exclude_patterns.each do |pat|
case pat
when Regexp
ignores << pat
when /[*?]/
Dir[pat].each do |p| ignores << p end
else
ignores << Regexp.quote(pat)
end
end
if ignores.empty?
@exclude_re = /^$/
else
re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
@exclude_re = Regexp.new(re_str)
end
end
def resolve_add(fn)
case fn
when %r{[*?\[\{]}
add_matching(fn)
else
self << fn
end
end
private :resolve_add
def resolve_exclude
calculate_exclude_regexp
reject! { |fn| exclude?(fn) }
self
end
private :resolve_exclude
def sub(pat, rep)
inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
end
def gsub(pat, rep)
inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
end
def sub!(pat, rep)
each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
self
end
def gsub!(pat, rep)
each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
self
end
def pathmap(spec=nil)
collect { |fn| fn.pathmap(spec) }
end
def ext(newext='')
collect { |fn| fn.ext(newext) }
end
def egrep(pattern, *options)
each do |fn|
open(fn, "rb", *options) do |inf|
count = 0
inf.each do |line|
count += 1
if pattern.match(line)
if block_given?
yield fn, count, line
else
puts "#{fn}:#{count}:#{line}"
end
end
end
end
end
end
def existing
select { |fn| File.exist?(fn) }
end
def existing!
resolve
@items = @items.select { |fn| File.exist?(fn) }
self
end
def partition(&block) resolve
result = @items.partition(&block)
[
FileList.new.import(result[0]),
FileList.new.import(result[1]),
]
end
def to_s
resolve
self.join(' ')
end
def add_matching(pattern)
Dir[pattern].each do |fn|
self << fn unless exclude?(fn)
end
end
private :add_matching
def exclude?(fn)
calculate_exclude_regexp unless @exclude_re
fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) }
end
DEFAULT_IGNORE_PATTERNS = [
/(^|[\/\\])CVS([\/\\]|$)/,
/(^|[\/\\])\.svn([\/\\]|$)/,
/\.bak$/,
/~$/
]
DEFAULT_IGNORE_PROCS = [
proc { |fn| fn =~ /(^|[\/\\])core$/ && ! File.directory?(fn) }
]
def import(array)
@items = array
self
end
class << self
def [](*args)
new(*args)
end
end
end end
module Rake
class << self
def each_dir_parent(dir) old_length = nil
while dir != '.' && dir.length != old_length
yield(dir)
old_length = dir.length
dir = File.dirname(dir)
end
end
end
end
FileList = Rake::FileList
module Rake
class DefaultLoader
def load(fn)
Kernel.load(File.expand_path(fn))
end
end
class EarlyTime
include Comparable
include Singleton
def <=>(other)
-1
end
def to_s
"<EARLY TIME>"
end
end
EARLY = EarlyTime.instance
end
class Time
alias rake_original_time_compare :<=>
def <=>(other)
if Rake::EarlyTime === other
- other.<=>(self)
else
rake_original_time_compare(other)
end
end
end
module Rake
class NameSpace
def initialize(task_manager, scope_list)
@task_manager = task_manager
@scope = scope_list.dup
end
def [](name)
@task_manager.lookup(name, @scope)
end
def tasks
@task_manager.tasks_in_scope(@scope)
end
end
module TaskManager
attr_accessor :last_description
alias :last_comment :last_description
def initialize
super
@tasks = Hash.new
@rules = Array.new
@scope = Array.new
@last_description = nil
end
def create_rule(*args, &block)
pattern, arg_names, deps = resolve_args(args)
pattern = Regexp.new(Regexp.quote(pattern) + '$') if String === pattern
@rules << [pattern, deps, block]
end
def define_task(task_class, *args, &block)
task_name, arg_names, deps = resolve_args(args)
task_name = task_class.scope_name(@scope, task_name)
deps = [deps] unless deps.respond_to?(:to_ary)
deps = deps.collect {|d| d.to_s }
task = intern(task_class, task_name)
task.set_arg_names(arg_names) unless arg_names.empty?
task.add_description(@last_description)
@last_description = nil
task.enhance(deps, &block)
task
end
def intern(task_class, task_name)
@tasks[task_name.to_s] ||= task_class.new(task_name, self)
end
def [](task_name, scopes=nil)
task_name = task_name.to_s
self.lookup(task_name, scopes) or
enhance_with_matching_rule(task_name) or
synthesize_file_task(task_name) or
fail "Don't know how to build task '#{task_name}'"
end
def synthesize_file_task(task_name)
return nil unless File.exist?(task_name)
define_task(Rake::FileTask, task_name)
end
def resolve_args(args)
if args.last.is_a?(Hash)
deps = args.pop
resolve_args_with_dependencies(args, deps)
else
resolve_args_without_dependencies(args)
end
end
def resolve_args_without_dependencies(args)
task_name = args.shift
if args.size == 1 && args.first.respond_to?(:to_ary)
arg_names = args.first.to_ary
else
arg_names = args
end
[task_name, arg_names, []]
end
private :resolve_args_without_dependencies
def resolve_args_with_dependencies(args, hash) fail "Task Argument Error" if hash.size != 1
key, value = hash.map { |k, v| [k,v] }.first
if args.empty?
task_name = key
arg_names = []
deps = value
elsif key == :needs
task_name = args.shift
arg_names = args
deps = value
else
task_name = args.shift
arg_names = key
deps = value
end
deps = [deps] unless deps.respond_to?(:to_ary)
[task_name, arg_names, deps]
end
private :resolve_args_with_dependencies
def enhance_with_matching_rule(task_name, level=0)
fail Rake::RuleRecursionOverflowError,
"Rule Recursion Too Deep" if level >= 16
@rules.each do |pattern, extensions, block|
if md = pattern.match(task_name)
task = attempt_rule(task_name, extensions, block, level)
return task if task
end
end
nil
rescue Rake::RuleRecursionOverflowError => ex
ex.add_target(task_name)
fail ex
end
def tasks
@tasks.values.sort_by { |t| t.name }
end
def tasks_in_scope(scope)
prefix = scope.join(":")
tasks.select { |t|
/^#{prefix}:/ =~ t.name
}
end
def clear
@tasks.clear
@rules.clear
end
def lookup(task_name, initial_scope=nil)
initial_scope ||= @scope
task_name = task_name.to_s
if task_name =~ /^rake:/
scopes = []
task_name = task_name.sub(/^rake:/, '')
elsif task_name =~ /^(\^+)/
scopes = initial_scope[0, initial_scope.size - $1.size]
task_name = task_name.sub(/^(\^+)/, '')
else
scopes = initial_scope
end
lookup_in_scope(task_name, scopes)
end
def lookup_in_scope(name, scope)
n = scope.size
while n >= 0
tn = (scope[0,n] + [name]).join(':')
task = @tasks[tn]
return task if task
n -= 1
end
nil
end
private :lookup_in_scope
def current_scope
@scope.dup
end
def in_namespace(name)
name ||= generate_name
@scope.push(name)
ns = NameSpace.new(self, @scope)
yield(ns)
ns
ensure
@scope.pop
end
private
def generate_name
@seed ||= 0
@seed += 1
"_anon_#{@seed}"
end
def trace_rule(level, message)
puts "#{" "*level}#{message}" if Rake.application.options.trace_rules
end
def attempt_rule(task_name, extensions, block, level)
sources = make_sources(task_name, extensions)
prereqs = sources.collect { |source|
trace_rule level, "Attempting Rule #{task_name} => #{source}"
if File.exist?(source) || Rake::Task.task_defined?(source)
trace_rule level, "(#{task_name} => #{source} ... EXIST)"
source
elsif parent = enhance_with_matching_rule(source, level+1)
trace_rule level, "(#{task_name} => #{source} ... ENHANCE)"
parent.name
else
trace_rule level, "(#{task_name} => #{source} ... FAIL)"
return nil
end
}
task = FileTask.define_task({task_name => prereqs}, &block)
task.sources = prereqs
task
end
def make_sources(task_name, extensions)
extensions.collect { |ext|
case ext
when /%/
task_name.pathmap(ext)
when %r{/}
ext
when /^\./
task_name.ext(ext)
when String
ext
when Proc
if ext.arity == 1
ext.call(task_name)
else
ext.call
end
else
fail "Don't know how to handle rule dependent: #{ext.inspect}"
end
}.flatten
end
end
class Application
include TaskManager
attr_reader :name
attr_reader :original_dir
attr_reader :rakefile
attr_reader :top_level_tasks
DEFAULT_RAKEFILES = ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].freeze
def initialize
super
@name = 'rake'
@rakefiles = DEFAULT_RAKEFILES.dup
@rakefile = nil
@pending_imports = []
@imported = []
@loaders = {}
@default_loader = Rake::DefaultLoader.new
@original_dir = Dir.pwd
@top_level_tasks = []
add_loader('rb', DefaultLoader.new)
add_loader('rf', DefaultLoader.new)
add_loader('rake', DefaultLoader.new)
@tty_output = STDOUT.tty?
end
def run
standard_exception_handling do
init
load_rakefile
top_level
end
end
def init(app_name='rake')
standard_exception_handling do
@name = app_name
handle_options
collect_tasks
end
end
def load_rakefile
standard_exception_handling do
raw_load_rakefile
end
end
def top_level
standard_exception_handling do
if options.show_tasks
display_tasks_and_comments
elsif options.show_prereqs
display_prerequisites
else
top_level_tasks.each { |task_name| invoke_task(task_name) }
end
end
end
def add_loader(ext, loader)
ext = ".#{ext}" unless ext =~ /^\./
@loaders[ext] = loader
end
def options
@options ||= OpenStruct.new
end
def invoke_task(task_string)
name, args = parse_task_string(task_string)
t = self[name]
t.invoke(*args)
end
def parse_task_string(string)
if string =~ /^([^\[]+)(\[(.*)\])$/
name = $1
args = $3.split(/\s*,\s*/)
else
name = string
args = []
end
[name, args]
end
def standard_exception_handling
begin
yield
rescue SystemExit => ex
raise
rescue OptionParser::InvalidOption => ex
exit(false)
rescue Exception => ex
$stderr.puts "#{name} aborted!"
$stderr.puts ex.message
if options.trace
$stderr.puts ex.backtrace.join("\n")
else
$stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
$stderr.puts "(See full trace by running task with --trace)"
end
exit(false)
end
end
def have_rakefile
@rakefiles.each do |fn|
if File.exist?(fn)
others = Dir.glob(fn, File::FNM_CASEFOLD)
return others.size == 1 ? others.first : fn
elsif fn == ''
return fn
end
end
return nil
end
def tty_output?
@tty_output
end
def tty_output=( tty_output_state )
@tty_output = tty_output_state
end
def truncate_output?
tty_output? || ENV['RAKE_COLUMNS']
end
def display_tasks_and_comments
displayable_tasks = tasks.select { |t|
t.comment && t.name =~ options.show_task_pattern
}
if options.full_description
displayable_tasks.each do |t|
puts "#{name} #{t.name_with_args}"
t.full_comment.split("\n").each do |line|
puts " #{line}"
end
puts
end
else
width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10
max_column = truncate_output? ? terminal_width - name.size - width - 7 : nil
displayable_tasks.each do |t|
printf "#{name} %-#{width}s # %s\n",
t.name_with_args, max_column ? truncate(t.comment, max_column) : t.comment
end
end
end
def terminal_width
if ENV['RAKE_COLUMNS']
result = ENV['RAKE_COLUMNS'].to_i
else
result = unix? ? dynamic_width : 80
end
(result < 10) ? 80 : result
rescue
80
end
def dynamic_width
@dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
end
def dynamic_width_stty
%x{stty size 2>/dev/null}.split[1].to_i
end
def dynamic_width_tput
%x{tput cols 2>/dev/null}.to_i
end
def unix?
RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
end
def windows?
Win32.windows?
end
def truncate(string, width)
if string.length <= width
string
else
( string[0, width-3] || "" ) + "..."
end
end
def display_prerequisites
tasks.each do |t|
puts "#{name} #{t.name}"
t.prerequisites.each { |pre| puts " #{pre}" }
end
end
def standard_rake_options
[
['--classic-namespace', '-C', "Put Task and FileTask in the top level namespace",
lambda { |value|
require 'rake/classic_namespace'
options.classic_namespace = true
}
],
['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
lambda { |value|
options.show_tasks = true
options.full_description = true
options.show_task_pattern = Regexp.new(value || '')
}
],
['--dry-run', '-n', "Do a dry run without executing actions.",
lambda { |value|
verbose(true)
nowrite(true)
options.dryrun = true
options.trace = true
}
],
['--execute', '-e CODE', "Execute some Ruby code and exit.",
lambda { |value|
eval(value)
exit
}
],
['--execute-print', '-p CODE', "Execute some Ruby code, print the result, then exit.",
lambda { |value|
puts eval(value)
exit
}
],
['--execute-continue', '-E CODE',
"Execute some Ruby code, then continue with normal task processing.",
lambda { |value| eval(value) }
],
['--libdir', '-I LIBDIR', "Include LIBDIR in the search path for required modules.",
lambda { |value| $:.push(value) }
],
['--prereqs', '-P', "Display the tasks and dependencies, then exit.",
lambda { |value| options.show_prereqs = true }
],
['--quiet', '-q', "Do not log messages to standard output.",
lambda { |value| verbose(false) }
],
['--rakefile', '-f [FILE]', "Use FILE as the rakefile.",
lambda { |value|
value ||= ''
@rakefiles.clear
@rakefiles << value
}
],
['--rakelibdir', '--rakelib', '-R RAKELIBDIR',
"Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')",
lambda { |value| options.rakelib = value.split(':') }
],
['--require', '-r MODULE', "Require MODULE before executing rakefile.",
lambda { |value|
begin
require value
rescue LoadError => ex
begin
rake_require value
rescue LoadError => ex2
raise ex
end
end
}
],
['--rules', "Trace the rules resolution.",
lambda { |value| options.trace_rules = true }
],
['--no-search', '--nosearch', '-N', "Do not search parent directories for the Rakefile.",
lambda { |value| options.nosearch = true }
],
['--silent', '-s', "Like --quiet, but also suppresses the 'in directory' announcement.",
lambda { |value|
verbose(false)
options.silent = true
}
],
['--system', '-g',
"Using system wide (global) rakefiles (usually '~/.rake/*.rake').",
lambda { |value| options.load_system = true }
],
['--no-system', '--nosystem', '-G',
"Use standard project Rakefile search paths, ignore system wide rakefiles.",
lambda { |value| options.ignore_system = true }
],
['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.",
lambda { |value|
options.show_tasks = true
options.show_task_pattern = Regexp.new(value || '')
options.full_description = false
}
],
['--trace', '-t', "Turn on invoke/execute tracing, enable full backtrace.",
lambda { |value|
options.trace = true
verbose(true)
}
],
['--verbose', '-v', "Log message to standard output.",
lambda { |value| verbose(true) }
],
['--version', '-V', "Display the program version.",
lambda { |value|
puts "rake, version #{RAKEVERSION}"
exit
}
]
]
end
def handle_options
options.rakelib = ['rakelib']
OptionParser.new do |opts|
opts.banner = "rake [-f rakefile] {options} targets..."
opts.separator ""
opts.separator "Options are ..."
opts.on_tail("-h", "--help", "-H", "Display this help message.") do
puts opts
exit
end
standard_rake_options.each { |args| opts.on(*args) }
end.parse!
if options.classic_namespace
$show_tasks = options.show_tasks
$show_prereqs = options.show_prereqs
$trace = options.trace
$dryrun = options.dryrun
$silent = options.silent
end
end
def rake_require(file_name, paths=$LOAD_PATH, loaded=$")
return false if loaded.include?(file_name)
paths.each do |path|
fn = file_name + ".rake"
full_path = File.join(path, fn)
if File.exist?(full_path)
load full_path
loaded << fn
return true
end
end
fail LoadError, "Can't find #{file_name}"
end
def find_rakefile_location
here = Dir.pwd
while ! (fn = have_rakefile)
Dir.chdir("..")
if Dir.pwd == here || options.nosearch
return nil
end
here = Dir.pwd
end
[fn, here]
ensure
Dir.chdir(Rake.original_dir)
end
def raw_load_rakefile # :nodoc:
rakefile, location = find_rakefile_location
if (! options.ignore_system) &&
(options.load_system || rakefile.nil?) &&
system_dir && File.directory?(system_dir)
puts "(in #{Dir.pwd})" unless options.silent
glob("#{system_dir}/*.rake") do |name|
add_import name
end
else
fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})" if
rakefile.nil?
@rakefile = rakefile
Dir.chdir(location)
puts "(in #{Dir.pwd})" unless options.silent
$rakefile = @rakefile if options.classic_namespace
load File.expand_path(@rakefile) if @rakefile && @rakefile != ''
options.rakelib.each do |rlib|
glob("#{rlib}/*.rake") do |name|
add_import name
end
end
end
load_imports
end
def glob(path, &block)
Dir[path.gsub("\\", '/')].each(&block)
end
private :glob
def system_dir
@system_dir ||=
begin
if ENV['RAKE_SYSTEM']
ENV['RAKE_SYSTEM']
else
standard_system_dir
end
end
end
if Win32.windows?
def standard_system_dir #:nodoc:
Win32.win32_system_dir
end
else
def standard_system_dir #:nodoc:
File.join(File.expand_path('~'), '.rake')
end
end
private :standard_system_dir
def collect_tasks
@top_level_tasks = []
ARGV.each do |arg|
if arg =~ /^(\w+)=(.*)$/
ENV[$1] = $2
else
@top_level_tasks << arg unless arg =~ /^-/
end
end
@top_level_tasks.push("default") if @top_level_tasks.size == 0
end
def add_import(fn)
@pending_imports << fn
end
def load_imports
while fn = @pending_imports.shift
next if @imported.member?(fn)
if fn_task = lookup(fn)
fn_task.invoke
end
ext = File.extname(fn)
loader = @loaders[ext] || @default_loader
loader.load(fn)
@imported << fn
end
end
def const_warning(const_name)
@const_warning ||= false
if ! @const_warning
$stderr.puts %{WARNING: Deprecated reference to top-level constant '#{const_name}' } +
%{found at: #{rakefile_location}} $stderr.puts %{ Use --classic-namespace on rake command}
$stderr.puts %{ or 'require "rake/classic_namespace"' in Rakefile}
end
@const_warning = true
end
def rakefile_location
begin
fail
rescue RuntimeError => ex
ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
end
end
end
end
class Module
alias :rake_original_const_missing :const_missing
def const_missing(const_name)
case const_name
when :Task
Rake.application.const_warning(const_name)
Rake::Task
when :FileTask
Rake.application.const_warning(const_name)
Rake::FileTask
when :FileCreationTask
Rake.application.const_warning(const_name)
Rake::FileCreationTask
when :RakeApp
Rake.application.const_warning(const_name)
Rake::Application
else
rake_original_const_missing(const_name)
end
end
end