Module | Sys |
In: |
lib/rake/contrib/sys.rb
|
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.
RUBY | = | Config::CONFIG['ruby_install_name'] |
Copy a single file from file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 62 62: def copy(file_name, dest_file) 63: log "Copying file #{file_name} to #{dest_file}" 64: File.copy(file_name, dest_file) 65: end
Copy all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 68 68: def copy_files(wildcard, dest_dir) 69: for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) } 70: end
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 97 97: def delete(*wildcards) 98: wildcards.each do |wildcard| 99: Dir[wildcard].each do |fn| 100: if File.directory?(fn) 101: log "Deleting directory #{fn}" 102: Dir.delete(fn) 103: else 104: log "Deleting file #{fn}" 105: File.delete(fn) 106: end 107: end 108: end 109: end
Recursively delete all files and directories matching wildcard.
# File lib/rake/contrib/sys.rb, line 112 112: def delete_all(*wildcards) 113: wildcards.each do |wildcard| 114: Dir[wildcard].each do |fn| 115: next if ! File.exist?(fn) 116: if File.directory?(fn) 117: Dir["#{fn}/*"].each do |subfn| 118: next if subfn=='.' || subfn=='..' 119: delete_all(subfn) 120: end 121: log "Deleting directory #{fn}" 122: Dir.delete(fn) 123: else 124: log "Deleting file #{fn}" 125: File.delete(fn) 126: end 127: end 128: end 129: end
Perform a block with each file matching a set of wildcards.
# File lib/rake/contrib/sys.rb, line 177 177: def for_files(*wildcards) 178: wildcards.each do |wildcard| 179: Dir[wildcard].each do |fn| 180: yield(fn) 181: end 182: end 183: end
Make dir the current working directory for the duration of executing the given block.
# File lib/rake/contrib/sys.rb, line 141 141: def indir(dir) 142: olddir = Dir.pwd 143: Dir.chdir(dir) 144: yield 145: ensure 146: Dir.chdir(olddir) 147: end
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 44 44: def install(wildcard, dest_dir, mode) 45: Dir[wildcard].each do |fn| 46: File.install(fn, dest_dir, mode, $verbose) 47: end 48: end
Link file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 73 73: def link(file_name, dest_file) 74: log "Linking file #{file_name} to #{dest_file}" 75: File.link(file_name, dest_file) 76: end
Link all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 79 79: def link_files(wildcard, dest_dir) 80: for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) } 81: end
Make the directories given in dirs.
# File lib/rake/contrib/sys.rb, line 132 132: def makedirs(*dirs) 133: dirs.each do |fn| 134: log "Making directory #{fn}" 135: File.makedirs(fn) 136: end 137: end
Run a Ruby interpreter with the given arguments.
# File lib/rake/contrib/sys.rb, line 57 57: def ruby(*args) 58: run "#{RUBY} #{args.join(' ')}" 59: end
Run the system command cmd.
# File lib/rake/contrib/sys.rb, line 51 51: def run(cmd) 52: log cmd 53: system(cmd) or fail "Command Failed: [#{cmd}]" 54: end
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 153 153: def split_all(path) 154: head, tail = File.split(path) 155: return [tail] if head == '.' || tail == '/' 156: return [head, tail] if head == '/' 157: return split_all(head) + [tail] 158: end
Symlink file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 84 84: def symlink(file_name, dest_file) 85: log "Symlinking file #{file_name} to #{dest_file}" 86: File.symlink(file_name, dest_file) 87: end
Symlink all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 90 90: def symlink_files(wildcard, dest_dir) 91: for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) } 92: end