Module: Sys

Extended by:
Sys
Included in:
Sys
Defined in:
lib/rake/contrib/sys.rb

Overview

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.

Constant Summary collapse

RUBY =
Config::CONFIG['ruby_install_name']

Instance Method Summary collapse

Instance Method Details

#copy(file_name, dest_file) ⇒ Object

Copy a single file from file_name to dest_file.



62
63
64
65
# File 'lib/rake/contrib/sys.rb', line 62

def copy(file_name, dest_file)
  log "Copying file #{file_name} to #{dest_file}"
  File.copy(file_name, dest_file)
end

#copy_files(wildcard, dest_dir) ⇒ Object

Copy all files matching wildcard into the directory dest_dir.



68
69
70
# File 'lib/rake/contrib/sys.rb', line 68

def copy_files(wildcard, dest_dir)
  for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) }
end

#delete(*wildcards) ⇒ Object

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.



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rake/contrib/sys.rb', line 97

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

#delete_all(*wildcards) ⇒ Object

Recursively delete all files and directories matching wildcard.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rake/contrib/sys.rb', line 112

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

#for_files(*wildcards) ⇒ Object

Perform a block with each file matching a set of wildcards.



177
178
179
180
181
182
183
# File 'lib/rake/contrib/sys.rb', line 177

def for_files(*wildcards)
  wildcards.each do |wildcard|
    Dir[wildcard].each do |fn|
	yield(fn)
    end
  end
end

#indir(dir) ⇒ Object

Make dir the current working directory for the duration of executing the given block.



141
142
143
144
145
146
147
# File 'lib/rake/contrib/sys.rb', line 141

def indir(dir)
  olddir = Dir.pwd
  Dir.chdir(dir)
  yield
ensure
  Dir.chdir(olddir)
end

#install(wildcard, dest_dir, mode) ⇒ Object

Install all the files matching wildcard into the dest_dir directory. The permission mode is set to mode.



44
45
46
47
48
# File 'lib/rake/contrib/sys.rb', line 44

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.



73
74
75
76
# File 'lib/rake/contrib/sys.rb', line 73

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.



79
80
81
# File 'lib/rake/contrib/sys.rb', line 79

def link_files(wildcard, dest_dir)
  for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
end

#log(msg) ⇒ Object

Write a message to standard out if $verbose is enabled.



161
162
163
164
# File 'lib/rake/contrib/sys.rb', line 161

def log(msg)
  print "  " if $trace && $verbose
  puts msg if $verbose
end

#makedirs(*dirs) ⇒ Object

Make the directories given in dirs.



132
133
134
135
136
137
# File 'lib/rake/contrib/sys.rb', line 132

def makedirs(*dirs)
  dirs.each do |fn|
    log "Making directory #{fn}"
    File.makedirs(fn)
  end
end

#quiet(&block) ⇒ Object

Perform a block with $verbose disabled.



167
168
169
# File 'lib/rake/contrib/sys.rb', line 167

def quiet(&block)
  with_verbose(false, &block)
end

#ruby(*args) ⇒ Object

Run a Ruby interpreter with the given arguments.



57
58
59
# File 'lib/rake/contrib/sys.rb', line 57

def ruby(*args)
  run "#{RUBY} #{args.join(' ')}"
end

#run(cmd) ⇒ Object

Run the system command cmd.



51
52
53
54
# File 'lib/rake/contrib/sys.rb', line 51

def run(cmd)
  log cmd
  system(cmd) or fail "Command Failed: [#{cmd}]"
end

#split_all(path) ⇒ Object

Split a file path into individual directory names.

For example:

split_all("a/b/c") =>  ['a', 'b', 'c']


153
154
155
156
157
158
# File 'lib/rake/contrib/sys.rb', line 153

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.



84
85
86
87
# File 'lib/rake/contrib/sys.rb', line 84

def symlink(file_name, dest_file)
  log "Symlinking file #{file_name} to #{dest_file}"
  File.symlink(file_name, dest_file)
end

Symlink all files matching wildcard into the directory dest_dir.



90
91
92
# File 'lib/rake/contrib/sys.rb', line 90

def symlink_files(wildcard, dest_dir)
  for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
end

#verbose(&block) ⇒ Object

Perform a block with $verbose enabled.



172
173
174
# File 'lib/rake/contrib/sys.rb', line 172

def verbose(&block)
  with_verbose(true, &block)
end