| 1 |
# Rake DSL functions. |
| 2 |
require 'rake/file_utils_ext' |
| 3 |
|
| 4 |
module Rake |
| 5 |
module DSL |
| 6 |
|
| 7 |
# Include the FileUtils file manipulation functions in the top |
| 8 |
# level module, but mark them private so that they don't |
| 9 |
# unintentionally define methods on other objects. |
| 10 |
|
| 11 |
include FileUtilsExt |
| 12 |
private(*FileUtils.instance_methods(false)) |
| 13 |
private(*FileUtilsExt.instance_methods(false)) |
| 14 |
|
| 15 |
private |
| 16 |
|
| 17 |
# Declare a basic task. |
| 18 |
# |
| 19 |
# Example: |
| 20 |
# task :clobber => [:clean] do |
| 21 |
# rm_rf "html" |
| 22 |
# end |
| 23 |
# |
| 24 |
def task(*args, &block) |
| 25 |
Rake::Task.define_task(*args, &block) |
| 26 |
end |
| 27 |
|
| 28 |
|
| 29 |
# Declare a file task. |
| 30 |
# |
| 31 |
# Example: |
| 32 |
# file "config.cfg" => ["config.template"] do |
| 33 |
# open("config.cfg", "w") do |outfile| |
| 34 |
# open("config.template") do |infile| |
| 35 |
# while line = infile.gets |
| 36 |
# outfile.puts line |
| 37 |
# end |
| 38 |
# end |
| 39 |
# end |
| 40 |
# end |
| 41 |
# |
| 42 |
def file(*args, &block) |
| 43 |
Rake::FileTask.define_task(*args, &block) |
| 44 |
end |
| 45 |
|
| 46 |
# Declare a file creation task. |
| 47 |
# (Mainly used for the directory command). |
| 48 |
def file_create(args, &block) |
| 49 |
Rake::FileCreationTask.define_task(args, &block) |
| 50 |
end |
| 51 |
|
| 52 |
# Declare a set of files tasks to create the given directories on |
| 53 |
# demand. |
| 54 |
# |
| 55 |
# Example: |
| 56 |
# directory "testdata/doc" |
| 57 |
# |
| 58 |
def directory(dir) |
| 59 |
Rake.each_dir_parent(dir) do |d| |
| 60 |
file_create d do |t| |
| 61 |
mkdir_p t.name if ! File.exist?(t.name) |
| 62 |
end |
| 63 |
end |
| 64 |
end |
| 65 |
|
| 66 |
# Declare a task that performs its prerequisites in |
| 67 |
# parallel. Multitasks does *not* guarantee that its prerequisites |
| 68 |
# will execute in any given order (which is obvious when you think |
| 69 |
# about it) |
| 70 |
# |
| 71 |
# Example: |
| 72 |
# multitask :deploy => [:deploy_gem, :deploy_rdoc] |
| 73 |
# |
| 74 |
def multitask(args, &block) |
| 75 |
Rake::MultiTask.define_task(args, &block) |
| 76 |
end |
| 77 |
|
| 78 |
# Create a new rake namespace and use it for evaluating the given |
| 79 |
# block. Returns a NameSpace object that can be used to lookup |
| 80 |
# tasks defined in the namespace. |
| 81 |
# |
| 82 |
# E.g. |
| 83 |
# |
| 84 |
# ns = namespace "nested" do |
| 85 |
# task :run |
| 86 |
# end |
| 87 |
# task_run = ns[:run] # find :run in the given namespace. |
| 88 |
# |
| 89 |
def namespace(name=nil, &block) |
| 90 |
name = name.to_s if name.kind_of?(Symbol) |
| 91 |
name = name.to_str if name.respond_to?(:to_str) |
| 92 |
unless name.kind_of?(String) || name.nil? |
| 93 |
raise ArgumentError, "Expected a String or Symbol for a namespace name" |
| 94 |
end |
| 95 |
Rake.application.in_namespace(name, &block) |
| 96 |
end |
| 97 |
|
| 98 |
# Declare a rule for auto-tasks. |
| 99 |
# |
| 100 |
# Example: |
| 101 |
# rule '.o' => '.c' do |t| |
| 102 |
# sh %{cc -o #{t.name} #{t.source}} |
| 103 |
# end |
| 104 |
# |
| 105 |
def rule(*args, &block) |
| 106 |
Rake::Task.create_rule(*args, &block) |
| 107 |
end |
| 108 |
|
| 109 |
# Describe the next rake task. |
| 110 |
# |
| 111 |
# Example: |
| 112 |
# desc "Run the Unit Tests" |
| 113 |
# task :test => [:build] |
| 114 |
# runtests |
| 115 |
# end |
| 116 |
# |
| 117 |
def desc(description) |
| 118 |
Rake.application.last_description = description |
| 119 |
end |
| 120 |
|
| 121 |
# Import the partial Rakefiles +fn+. Imported files are loaded |
| 122 |
# _after_ the current file is completely loaded. This allows the |
| 123 |
# import statement to appear anywhere in the importing file, and yet |
| 124 |
# allowing the imported files to depend on objects defined in the |
| 125 |
# importing file. |
| 126 |
# |
| 127 |
# A common use of the import statement is to include files |
| 128 |
# containing dependency declarations. |
| 129 |
# |
| 130 |
# See also the --rakelibdir command line option. |
| 131 |
# |
| 132 |
# Example: |
| 133 |
# import ".depend", "my_rules" |
| 134 |
# |
| 135 |
def import(*fns) |
| 136 |
fns.each do |fn| |
| 137 |
Rake.application.add_import(fn) |
| 138 |
end |
| 139 |
end |
| 140 |
end |
| 141 |
|
| 142 |
module DeprecatedObjectDSL |
| 143 |
Commands = Object.new.extend DSL |
| 144 |
DSL.private_instance_methods(false).each do |name| |
| 145 |
line = __LINE__+1 |
| 146 |
class_eval %{ |
| 147 |
def #{name}(*args, &block) |
| 148 |
unless Rake.application.options.ignore_deprecate |
| 149 |
unless @rake_dsl_warning |
| 150 |
$stderr.puts "WARNING: Global access to Rake DSL methods is deprecated. Please include" |
| 151 |
$stderr.puts " ... Rake::DSL into classes and modules which use the Rake DSL methods." |
| 152 |
@rake_dsl_warning = true |
| 153 |
end |
| 154 |
$stderr.puts "WARNING: DSL method \#{self.class}##{name} called at \#{caller.first}" |
| 155 |
end |
| 156 |
Rake::DeprecatedObjectDSL::Commands.send(:#{name}, *args, &block) |
| 157 |
end |
| 158 |
private :#{name} |
| 159 |
}, __FILE__, line |
| 160 |
end |
| 161 |
end |
| 162 |
|
| 163 |
extend FileUtilsExt |
| 164 |
end |
| 165 |
|
| 166 |
self.extend Rake::DSL |
| 167 |
include Rake::DeprecatedObjectDSL |