4 # Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>
6 # This program is free software.
7 # You can distribute/modify this program under the same terms of Ruby.
9 # == Utilities to replace common UNIX commands in Makefiles etc
13 # ruby -run -e cp -- [OPTION] SOURCE DEST
14 # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
15 # ruby -run -e mv -- [OPTION] SOURCE DEST
16 # ruby -run -e rm -- [OPTION] FILE
17 # ruby -run -e mkdir -- [OPTION] DIRS
18 # ruby -run -e rmdir -- [OPTION] DIRS
19 # ruby -run -e install -- [OPTION] SOURCE DEST
20 # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
21 # ruby -run -e touch -- [OPTION] FILE
22 # ruby -run -e wait_writable -- [OPTION] FILE
23 # ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION]
24 # ruby -run -e help [COMMAND]
30 # @fileutils_label = ""
31 @fileutils_output = $stdout
34 def setup(options = "", *long_options)
37 OptionParser.new do |o|
38 options.scan(/.:?/) do |s|
39 opt_name = s.delete(":").intern
40 o.on("-" + s.tr(":", " ")) do |val|
41 opt_hash[opt_name] = val
44 long_options.each do |s|
45 opt_name = s[/\A(?:--)?([^\s=]+)/, 1].intern
46 o.on(s.sub(/\A(?!--)/, '--')) do |val|
47 opt_hash[opt_name] = val
50 o.on("-v") do opt_hash[:verbose] = true end
63 # Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
65 # ruby -run -e cp -- [OPTION] SOURCE DEST
67 # -p preserve file attributes if possible
73 setup("pr") do |argv, options|
75 cmd += "_r" if options.delete :r
76 options[:preserve] = true if options.delete :p
78 argv = argv[0] if argv.size == 1
79 FileUtils.send cmd, argv, dest, options
84 # Create a link to the specified TARGET with LINK_NAME.
86 # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
88 # -s make symbolic links instead of hard links
89 # -f remove existing destination files
94 setup("sf") do |argv, options|
96 cmd += "_s" if options.delete :s
97 options[:force] = true if options.delete :f
99 argv = argv[0] if argv.size == 1
100 FileUtils.send cmd, argv, dest, options
105 # Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
107 # ruby -run -e mv -- [OPTION] SOURCE DEST
113 setup do |argv, options|
115 argv = argv[0] if argv.size == 1
116 FileUtils.mv argv, dest, options
123 # ruby -run -e rm -- [OPTION] FILE
125 # -f ignore nonexistent files
126 # -r remove the contents of directories recursively
131 setup("fr") do |argv, options|
133 cmd += "_r" if options.delete :r
134 options[:force] = true if options.delete :f
135 FileUtils.send cmd, argv, options
140 # Create the DIR, if they do not already exist.
142 # ruby -run -e mkdir -- [OPTION] DIR
144 # -p no error if existing, make parent directories as needed
149 setup("p") do |argv, options|
151 cmd += "_p" if options.delete :p
152 FileUtils.send cmd, argv, options
159 # ruby -run -e rmdir -- [OPTION] DIR
165 setup do |argv, options|
166 FileUtils.rmdir argv, options
171 # Copy SOURCE to DEST.
173 # ruby -run -e install -- [OPTION] SOURCE DEST
175 # -p apply access/modification times of SOURCE files to
176 # corresponding destination files
177 # -m set permission mode (as in chmod), instead of 0755
182 setup("pm:") do |argv, options|
183 options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
184 options[:preserve] = true if options.delete :p
186 argv = argv[0] if argv.size == 1
187 FileUtils.install argv, dest, options
192 # Change the mode of each FILE to OCTAL-MODE.
194 # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
200 setup do |argv, options|
201 mode = argv.shift.oct
202 FileUtils.chmod mode, argv, options
207 # Update the access and modification times of each FILE to the current time.
209 # ruby -run -e touch -- [OPTION] FILE
215 setup do |argv, options|
216 FileUtils.touch argv, options
221 # Wait until the file becomes writable.
223 # ruby -run -e wait_writable -- [OPTION] FILE
225 # -n RETRY count to retry
226 # -w SEC each wait time in seconds
231 setup("n:w:v") do |argv, options|
232 verbose = options[:verbose]
233 n = options[:n] and n = Integer(n)
234 wait = (wait = options[:w]) ? Float(wait) : 0.2
240 rescue Errno::EACCES => e
241 raise if n and (n -= 1) <= 0
252 # Create makefile using mkmf.
254 # ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION]
256 # -d ARGS run dir_config
257 # -h ARGS run have_header
258 # -l ARGS run have_library
259 # -f ARGS run have_func
260 # -v ARGS run have_var
261 # -t ARGS run have_type
262 # -m ARGS run have_macro
263 # -c ARGS run have_const
264 # --vendor install to vendor_ruby
268 setup("d:h:l:f:v:t:m:c:", "vendor") do |argv, options|
270 opt = options[:d] and opt.split(/:/).each {|n| dir_config(*n.split(/,/))}
271 opt = options[:h] and opt.split(/:/).each {|n| have_header(*n.split(/,/))}
272 opt = options[:l] and opt.split(/:/).each {|n| have_library(*n.split(/,/))}
273 opt = options[:f] and opt.split(/:/).each {|n| have_func(*n.split(/,/))}
274 opt = options[:v] and opt.split(/:/).each {|n| have_var(*n.split(/,/))}
275 opt = options[:t] and opt.split(/:/).each {|n| have_type(*n.split(/,/))}
276 opt = options[:m] and opt.split(/:/).each {|n| have_macro(*n.split(/,/))}
277 opt = options[:c] and opt.split(/:/).each {|n| have_const(*n.split(/,/))}
278 $configure_args["--vendor"] = true if options[:vendor]
279 create_makefile(*argv)
284 # Display help message.
286 # ruby -run -e help [COMMAND]
292 open(__FILE__) do |me|
293 while me.gets("##\n")
294 if help = me.gets("\n\n")
295 if all or argv.delete help[/-e \w+/].sub(/-e /, "")
296 print help.gsub(/^# ?/, "")