| 1 |
require 'rake/file_utils' |
| 2 |
|
| 3 |
module Rake |
| 4 |
# |
| 5 |
# FileUtilsExt provides a custom version of the FileUtils methods |
| 6 |
# that respond to the <tt>verbose</tt> and <tt>nowrite</tt> |
| 7 |
# commands. |
| 8 |
# |
| 9 |
module FileUtilsExt |
| 10 |
include FileUtils |
| 11 |
|
| 12 |
class << self |
| 13 |
attr_accessor :verbose_flag, :nowrite_flag |
| 14 |
end |
| 15 |
FileUtilsExt.verbose_flag = nil |
| 16 |
FileUtilsExt.nowrite_flag = false |
| 17 |
|
| 18 |
$fileutils_verbose = true |
| 19 |
$fileutils_nowrite = false |
| 20 |
|
| 21 |
FileUtils::OPT_TABLE.each do |name, opts| |
| 22 |
default_options = [] |
| 23 |
if opts.include?(:verbose) || opts.include?("verbose") |
| 24 |
default_options << ':verbose => FileUtilsExt.verbose_flag' |
| 25 |
end |
| 26 |
if opts.include?(:noop) || opts.include?("noop") |
| 27 |
default_options << ':noop => FileUtilsExt.nowrite_flag' |
| 28 |
end |
| 29 |
|
| 30 |
next if default_options.empty? |
| 31 |
module_eval(<<-EOS, __FILE__, __LINE__ + 1) |
| 32 |
def #{name}( *args, &block ) |
| 33 |
super( |
| 34 |
*rake_merge_option(args, |
| 35 |
#{default_options.join(', ')} |
| 36 |
), &block) |
| 37 |
end |
| 38 |
EOS |
| 39 |
end |
| 40 |
|
| 41 |
# Get/set the verbose flag controlling output from the FileUtils |
| 42 |
# utilities. If verbose is true, then the utility method is |
| 43 |
# echoed to standard output. |
| 44 |
# |
| 45 |
# Examples: |
| 46 |
# verbose # return the current value of the |
| 47 |
# # verbose flag |
| 48 |
# verbose(v) # set the verbose flag to _v_. |
| 49 |
# verbose(v) { code } # Execute code with the verbose flag set |
| 50 |
# # temporarily to _v_. Return to the |
| 51 |
# # original value when code is done. |
| 52 |
def verbose(value=nil) |
| 53 |
oldvalue = FileUtilsExt.verbose_flag |
| 54 |
FileUtilsExt.verbose_flag = value unless value.nil? |
| 55 |
if block_given? |
| 56 |
begin |
| 57 |
yield |
| 58 |
ensure |
| 59 |
FileUtilsExt.verbose_flag = oldvalue |
| 60 |
end |
| 61 |
end |
| 62 |
FileUtilsExt.verbose_flag |
| 63 |
end |
| 64 |
|
| 65 |
# Get/set the nowrite flag controlling output from the FileUtils |
| 66 |
# utilities. If verbose is true, then the utility method is |
| 67 |
# echoed to standard output. |
| 68 |
# |
| 69 |
# Examples: |
| 70 |
# nowrite # return the current value of the |
| 71 |
# # nowrite flag |
| 72 |
# nowrite(v) # set the nowrite flag to _v_. |
| 73 |
# nowrite(v) { code } # Execute code with the nowrite flag set |
| 74 |
# # temporarily to _v_. Return to the |
| 75 |
# # original value when code is done. |
| 76 |
def nowrite(value=nil) |
| 77 |
oldvalue = FileUtilsExt.nowrite_flag |
| 78 |
FileUtilsExt.nowrite_flag = value unless value.nil? |
| 79 |
if block_given? |
| 80 |
begin |
| 81 |
yield |
| 82 |
ensure |
| 83 |
FileUtilsExt.nowrite_flag = oldvalue |
| 84 |
end |
| 85 |
end |
| 86 |
oldvalue |
| 87 |
end |
| 88 |
|
| 89 |
# Use this function to prevent potentially destructive ruby code |
| 90 |
# from running when the :nowrite flag is set. |
| 91 |
# |
| 92 |
# Example: |
| 93 |
# |
| 94 |
# when_writing("Building Project") do |
| 95 |
# project.build |
| 96 |
# end |
| 97 |
# |
| 98 |
# The following code will build the project under normal |
| 99 |
# conditions. If the nowrite(true) flag is set, then the example |
| 100 |
# will print: |
| 101 |
# |
| 102 |
# DRYRUN: Building Project |
| 103 |
# |
| 104 |
# instead of actually building the project. |
| 105 |
# |
| 106 |
def when_writing(msg=nil) |
| 107 |
if FileUtilsExt.nowrite_flag |
| 108 |
$stderr.puts "DRYRUN: #{msg}" if msg |
| 109 |
else |
| 110 |
yield |
| 111 |
end |
| 112 |
end |
| 113 |
|
| 114 |
# Merge the given options with the default values. |
| 115 |
def rake_merge_option(args, defaults) |
| 116 |
if Hash === args.last |
| 117 |
defaults.update(args.last) |
| 118 |
args.pop |
| 119 |
end |
| 120 |
args.push defaults |
| 121 |
args |
| 122 |
end |
| 123 |
|
| 124 |
# Send the message to the default rake output (which is $stderr). |
| 125 |
def rake_output_message(message) |
| 126 |
$stderr.puts(message) |
| 127 |
end |
| 128 |
|
| 129 |
# Check that the options do not contain options not listed in |
| 130 |
# +optdecl+. An ArgumentError exception is thrown if non-declared |
| 131 |
# options are found. |
| 132 |
def rake_check_options(options, *optdecl) |
| 133 |
h = options.dup |
| 134 |
optdecl.each do |name| |
| 135 |
h.delete name |
| 136 |
end |
| 137 |
raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty? |
| 138 |
end |
| 139 |
|
| 140 |
extend self |
| 141 |
end |
| 142 |
end |