Debian Codegraph @

rake.d/vendor/drake-0.9.2.0.3.1/lib/rake/contrib/sys.rb

raw
1 warn 'Sys has been deprecated in favor of FileUtils'
2
3 #--
4 # Copyright 2003-2010 by Jim Weirich ([email protected])
5 # All rights reserved.
6 #++
7 #
8 begin
9 require 'ftools'
10 rescue LoadError
11 end
12 require 'rbconfig'
13
14 ######################################################################
15 # Sys provides a number of file manipulation tools for the convenience
16 # of writing Rakefiles. All commands in this module will announce
17 # their activity on standard output if the $verbose flag is set
18 # ($verbose = true is the default). You can control this by globally
19 # setting $verbose or by using the +verbose+ and +quiet+ methods.
20 #
21 # Sys has been deprecated in favor of the FileUtils module available
22 # in Ruby 1.8.
23 #
24 module Sys
25 RUBY = RbConfig::CONFIG['ruby_install_name']
26
27 # Install all the files matching +wildcard+ into the +dest_dir+
28 # directory. The permission mode is set to +mode+.
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
34
35 # Run the system command +cmd+.
36 def run(cmd)
37 log cmd
38 system(cmd) or fail "Command Failed: [#{cmd}]"
39 end
40
41 # Run a Ruby interpreter with the given arguments.
42 def ruby(*args)
43 run "#{RUBY} #{args.join(' ')}"
44 end
45
46 # Copy a single file from +file_name+ to +dest_file+.
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
51
52 # Copy all files matching +wildcard+ into the directory +dest_dir+.
53 def copy_files(wildcard, dest_dir)
54 for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) }
55 end
56
57 # Link +file_name+ to +dest_file+.
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
62
63 # Link all files matching +wildcard+ into the directory +dest_dir+.
64 def link_files(wildcard, dest_dir)
65 for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
66 end
67
68 # Symlink +file_name+ to +dest_file+.
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
73
74 # Symlink all files matching +wildcard+ into the directory +dest_dir+.
75 def symlink_files(wildcard, dest_dir)
76 for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
77 end
78
79 # Remove all files matching +wildcard+. If a matching file is a
80 # directory, it must be empty to be removed. used +delete_all+ to
81 # recursively delete directories.
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
95
96 # Recursively delete all files and directories matching +wildcard+.
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
115
116 # Make the directories given in +dirs+.
117 def makedirs(*dirs)
118 dirs.each do |fn|
119 log "Making directory #{fn}"
120 File.makedirs(fn)
121 end
122 end
123
124 # Make +dir+ the current working directory for the duration of
125 # executing the given block.
126 def indir(dir)
127 olddir = Dir.pwd
128 Dir.chdir(dir)
129 yield
130 ensure
131 Dir.chdir(olddir)
132 end
133
134 # Split a file path into individual directory names.
135 #
136 # For example:
137 # split_all("a/b/c") => ['a', 'b', 'c']
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
144
145 # Write a message to standard error if $verbose is enabled.
146 def log(msg)
147 print " " if $trace && $verbose
148 $stderr.puts msg if $verbose
149 end
150
151 # Perform a block with $verbose disabled.
152 def quiet(&block)
153 with_verbose(false, &block)
154 end
155
156 # Perform a block with $verbose enabled.
157 def verbose(&block)
158 with_verbose(true, &block)
159 end
160
161 # Perform a block with each file matching a set of wildcards.
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
169
170 extend(self)
171
172 private # ----------------------------------------------------------
173
174 def for_matching_files(wildcard, dest_dir)
175 Dir[wildcard].each do |fn|
176 dest_file = File.join(dest_dir, fn)
177 parent = File.dirname(dest_file)
178 makedirs(parent) if ! File.directory?(parent)
179 yield(fn, dest_file)
180 end
181 end
182
183 def with_verbose(v)
184 oldverbose = $verbose
185 $verbose = v
186 yield
187 ensure
188 $verbose = oldverbose
189 end
190
191 end