Module Sys |
|
Sys provides a number of file manipulation tools for the convenience of writing Rakefiles. All commands in this module will announce their activity on standard output if the $verbose flag is set ($verbose = true is the default). You can control this by globally setting $verbose or by using the verbose and quiet methods.
Sys has been deprecated in favor of the FileUtils module available in Ruby 1.8.
Methods |
Public Instance methods |
copy(file_name, dest_file) |
Copy a single file from file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 47 47: def copy(file_name, dest_file) 48: log "Copying file #{file_name} to #{dest_file}" 49: File.copy(file_name, dest_file) 50: end
copy_files(wildcard, dest_dir) |
Copy all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 53 53: def copy_files(wildcard, dest_dir) 54: for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) } 55: end
delete(*wildcards) |
Remove all files matching wildcard. If a matching file is a directory, it must be empty to be removed. used delete_all to recursively delete directories.
# File lib/rake/contrib/sys.rb, line 82 82: def delete(*wildcards) 83: wildcards.each do |wildcard| 84: Dir[wildcard].each do |fn| 85: if File.directory?(fn) 86: log "Deleting directory #{fn}" 87: Dir.delete(fn) 88: else 89: log "Deleting file #{fn}" 90: File.delete(fn) 91: end 92: end 93: end 94: end
delete_all(*wildcards) |
Recursively delete all files and directories matching wildcard.
# File lib/rake/contrib/sys.rb, line 97 97: def delete_all(*wildcards) 98: wildcards.each do |wildcard| 99: Dir[wildcard].each do |fn| 100: next if ! File.exist?(fn) 101: if File.directory?(fn) 102: Dir["#{fn}/*"].each do |subfn| 103: next if subfn=='.' || subfn=='..' 104: delete_all(subfn) 105: end 106: log "Deleting directory #{fn}" 107: Dir.delete(fn) 108: else 109: log "Deleting file #{fn}" 110: File.delete(fn) 111: end 112: end 113: end 114: end
for_files(*wildcards) {|fn| ...} |
Perform a block with each file matching a set of wildcards.
# File lib/rake/contrib/sys.rb, line 162 162: def for_files(*wildcards) 163: wildcards.each do |wildcard| 164: Dir[wildcard].each do |fn| 165: yield(fn) 166: end 167: end 168: end
indir(dir) {|| ...} |
Make dir the current working directory for the duration of executing the given block.
# File lib/rake/contrib/sys.rb, line 126 126: def indir(dir) 127: olddir = Dir.pwd 128: Dir.chdir(dir) 129: yield 130: ensure 131: Dir.chdir(olddir) 132: end
install(wildcard, dest_dir, mode) |
Install all the files matching wildcard into the dest_dir directory. The permission mode is set to mode.
# File lib/rake/contrib/sys.rb, line 29 29: def install(wildcard, dest_dir, mode) 30: Dir[wildcard].each do |fn| 31: File.install(fn, dest_dir, mode, $verbose) 32: end 33: end
link(file_name, dest_file) |
Link file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 58 58: def link(file_name, dest_file) 59: log "Linking file #{file_name} to #{dest_file}" 60: File.link(file_name, dest_file) 61: end
link_files(wildcard, dest_dir) |
Link all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 64 64: def link_files(wildcard, dest_dir) 65: for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) } 66: end
log(msg) |
Write a message to standard out if $verbose is enabled.
# File lib/rake/contrib/sys.rb, line 146 146: def log(msg) 147: print " " if $trace && $verbose 148: puts msg if $verbose 149: end
makedirs(*dirs) |
Make the directories given in dirs.
# File lib/rake/contrib/sys.rb, line 117 117: def makedirs(*dirs) 118: dirs.each do |fn| 119: log "Making directory #{fn}" 120: File.makedirs(fn) 121: end 122: end
quiet(&block) |
Perform a block with $verbose disabled.
# File lib/rake/contrib/sys.rb, line 152 152: def quiet(&block) 153: with_verbose(false, &block) 154: end
ruby(*args) |
Run a Ruby interpreter with the given arguments.
# File lib/rake/contrib/sys.rb, line 42 42: def ruby(*args) 43: run "#{RUBY} #{args.join(' ')}" 44: end
run(cmd) |
Run the system command cmd.
# File lib/rake/contrib/sys.rb, line 36 36: def run(cmd) 37: log cmd 38: system(cmd) or fail "Command Failed: [#{cmd}]" 39: end
split_all(path) |
Split a file path into individual directory names.
For example:
split_all("a/b/c") => ['a', 'b', 'c']
# File lib/rake/contrib/sys.rb, line 138 138: def split_all(path) 139: head, tail = File.split(path) 140: return [tail] if head == '.' || tail == '/' 141: return [head, tail] if head == '/' 142: return split_all(head) + [tail] 143: end
symlink(file_name, dest_file) |
Symlink file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 69 69: def symlink(file_name, dest_file) 70: log "Symlinking file #{file_name} to #{dest_file}" 71: File.symlink(file_name, dest_file) 72: end
symlink_files(wildcard, dest_dir) |
Symlink all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 75 75: def symlink_files(wildcard, dest_dir) 76: for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) } 77: end
verbose(&block) |
Perform a block with $verbose enabled.
# File lib/rake/contrib/sys.rb, line 157 157: def verbose(&block) 158: with_verbose(true, &block) 159: end