Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more
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.
Copy a single file from file_name to dest_file.
# File rake/contrib/sys.rb, line 47
def copy(file_name, dest_file)
log "Copying file #{file_name} to #{dest_file}"
File.copy(file_name, dest_file)
end
Copy all files matching wildcard into the directory
dest_dir.
# File rake/contrib/sys.rb, line 53
def copy_files(wildcard, dest_dir)
for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) }
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 rake/contrib/sys.rb, line 82
def delete(*wildcards)
wildcards.each do |wildcard|
Dir[wildcard].each do |fn|
if File.directory?(fn)
log "Deleting directory #{fn}"
Dir.delete(fn)
else
log "Deleting file #{fn}"
File.delete(fn)
end
end
end
end
Recursively delete all files and directories matching
wildcard.
# File rake/contrib/sys.rb, line 97
def delete_all(*wildcards)
wildcards.each do |wildcard|
Dir[wildcard].each do |fn|
next if ! File.exist?(fn)
if File.directory?(fn)
Dir["#{fn}/*"].each do |subfn|
next if subfn=='.' || subfn=='..'
delete_all(subfn)
end
log "Deleting directory #{fn}"
Dir.delete(fn)
else
log "Deleting file #{fn}"
File.delete(fn)
end
end
end
end
Perform a block with each file matching a set of wildcards.
# File rake/contrib/sys.rb, line 162
def for_files(*wildcards)
wildcards.each do |wildcard|
Dir[wildcard].each do |fn|
yield(fn)
end
end
end
Make dir the current working directory for the duration of
executing the given block.
# File rake/contrib/sys.rb, line 126
def indir(dir)
olddir = Dir.pwd
Dir.chdir(dir)
yield
ensure
Dir.chdir(olddir)
end
Install all the files matching wildcard into the
dest_dir directory. The permission mode is set to
mode.
# File rake/contrib/sys.rb, line 29
def install(wildcard, dest_dir, mode)
Dir[wildcard].each do |fn|
File.install(fn, dest_dir, mode, $verbose)
end
end
Link file_name to dest_file.
# File rake/contrib/sys.rb, line 58
def link(file_name, dest_file)
log "Linking file #{file_name} to #{dest_file}"
File.link(file_name, dest_file)
end
Link all files matching wildcard into the directory
dest_dir.
# File rake/contrib/sys.rb, line 64
def link_files(wildcard, dest_dir)
for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
end
Write a message to standard error if $verbose is enabled.
# File rake/contrib/sys.rb, line 146
def log(msg)
print " " if $trace && $verbose
$stderr.puts msg if $verbose
end
Make the directories given in dirs.
# File rake/contrib/sys.rb, line 117
def makedirs(*dirs)
dirs.each do |fn|
log "Making directory #{fn}"
File.makedirs(fn)
end
end
Perform a block with $verbose disabled.
# File rake/contrib/sys.rb, line 152
def quiet(&block)
with_verbose(false, &block)
end
Run a Ruby interpreter with the given arguments.
# File rake/contrib/sys.rb, line 42
def ruby(*args)
run "#{RUBY} #{args.join(' ')}"
end
Run the system command cmd.
# File rake/contrib/sys.rb, line 36
def run(cmd)
log cmd
system(cmd) or fail "Command Failed: [#{cmd}]"
end
Split a file path into individual directory names.
For example:
split_all("a/b/c") => ['a', 'b', 'c']
# File rake/contrib/sys.rb, line 138
def split_all(path)
head, tail = File.split(path)
return [tail] if head == '.' || tail == '/'
return [head, tail] if head == '/'
return split_all(head) + [tail]
end
Symlink file_name to dest_file.
# File rake/contrib/sys.rb, line 69
def symlink(file_name, dest_file)
log "Symlinking file #{file_name} to #{dest_file}"
File.symlink(file_name, dest_file)
end
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.