| 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 65
65: def copy(file_name, dest_file)
66: log "Copying file #{file_name} to #{dest_file}"
67: File.copy(file_name, dest_file)
68: end
Copy all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 71
71: def copy_files(wildcard, dest_dir)
72: for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) }
73: 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 100
100: def delete(*wildcards)
101: wildcards.each do |wildcard|
102: Dir[wildcard].each do |fn|
103: if File.directory?(fn)
104: log "Deleting directory #{fn}"
105: Dir.delete(fn)
106: else
107: log "Deleting file #{fn}"
108: File.delete(fn)
109: end
110: end
111: end
112: end
Recursively delete all files and directories matching wildcard.
# File lib/rake/contrib/sys.rb, line 115
115: def delete_all(*wildcards)
116: wildcards.each do |wildcard|
117: Dir[wildcard].each do |fn|
118: next if ! File.exist?(fn)
119: if File.directory?(fn)
120: Dir["#{fn}/*"].each do |subfn|
121: next if subfn=='.' || subfn=='..'
122: delete_all(subfn)
123: end
124: log "Deleting directory #{fn}"
125: Dir.delete(fn)
126: else
127: log "Deleting file #{fn}"
128: File.delete(fn)
129: end
130: end
131: end
132: end
Perform a block with each file matching a set of wildcards.
# File lib/rake/contrib/sys.rb, line 180
180: def for_files(*wildcards)
181: wildcards.each do |wildcard|
182: Dir[wildcard].each do |fn|
183: yield(fn)
184: end
185: end
186: end
Make dir the current working directory for the duration of executing the given block.
# File lib/rake/contrib/sys.rb, line 144
144: def indir(dir)
145: olddir = Dir.pwd
146: Dir.chdir(dir)
147: yield
148: ensure
149: Dir.chdir(olddir)
150: 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 47
47: def install(wildcard, dest_dir, mode)
48: Dir[wildcard].each do |fn|
49: File.install(fn, dest_dir, mode, $verbose)
50: end
51: end
Link file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 76
76: def link(file_name, dest_file)
77: log "Linking file #{file_name} to #{dest_file}"
78: File.link(file_name, dest_file)
79: end
Link all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 82
82: def link_files(wildcard, dest_dir)
83: for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
84: end
Make the directories given in dirs.
# File lib/rake/contrib/sys.rb, line 135
135: def makedirs(*dirs)
136: dirs.each do |fn|
137: log "Making directory #{fn}"
138: File.makedirs(fn)
139: end
140: end
Run a Ruby interpreter with the given arguments.
# File lib/rake/contrib/sys.rb, line 60
60: def ruby(*args)
61: run "#{RUBY} #{args.join(' ')}"
62: end
Run the system command cmd.
# File lib/rake/contrib/sys.rb, line 54
54: def run(cmd)
55: log cmd
56: system(cmd) or fail "Command Failed: [#{cmd}]"
57: 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 156
156: def split_all(path)
157: head, tail = File.split(path)
158: return [tail] if head == '.' || tail == '/'
159: return [head, tail] if head == '/'
160: return split_all(head) + [tail]
161: end
Symlink file_name to dest_file.
# File lib/rake/contrib/sys.rb, line 87
87: def symlink(file_name, dest_file)
88: log "Symlinking file #{file_name} to #{dest_file}"
89: File.symlink(file_name, dest_file)
90: end
Symlink all files matching wildcard into the directory dest_dir.
# File lib/rake/contrib/sys.rb, line 93
93: def symlink_files(wildcard, dest_dir)
94: for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
95: end