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 help [COMMAND]
28 # @fileutils_label = ""
29 @fileutils_output = $stdout
32 def setup(options = "")
36 x.delete "^-#{options}v"
44 ARGV.delete_if{|x| x == "-"}
46 OptionParser.new do |o|
47 options.scan(/.:?/) do |s|
48 o.on("-" + s.tr(":", " ")) do |val|
49 opt_hash[s.delete(":").intern] = val
52 o.on("-v") do opt_hash[:verbose] = true end
59 # Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
61 # ruby -run -e cp -- [OPTION] SOURCE DEST
63 # -p preserve file attributes if possible
69 setup("pr") do |argv, options|
71 cmd += "_r" if options.delete :r
72 options[:preserve] = true if options.delete :p
74 argv = argv[0] if argv.size == 1
75 FileUtils.send cmd, argv, dest, options
80 # Create a link to the specified TARGET with LINK_NAME.
82 # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
84 # -s make symbolic links instead of hard links
85 # -f remove existing destination files
90 setup("sf") do |argv, options|
92 cmd += "_s" if options.delete :s
93 options[:force] = true if options.delete :f
95 argv = argv[0] if argv.size == 1
96 FileUtils.send cmd, argv, dest, options
101 # Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
103 # ruby -run -e mv -- [OPTION] SOURCE DEST
109 setup do |argv, options|
111 argv = argv[0] if argv.size == 1
112 FileUtils.mv argv, dest, options
119 # ruby -run -e rm -- [OPTION] FILE
121 # -f ignore nonexistent files
122 # -r remove the contents of directories recursively
127 setup("fr") do |argv, options|
129 cmd += "_r" if options.delete :r
130 options[:force] = true if options.delete :f
131 FileUtils.send cmd, argv, options
136 # Create the DIR, if they do not already exist.
138 # ruby -run -e mkdir -- [OPTION] DIR
140 # -p no error if existing, make parent directories as needed
145 setup("p") do |argv, options|
147 cmd += "_p" if options.delete :p
148 FileUtils.send cmd, argv, options
155 # ruby -run -e rmdir -- [OPTION] DIR
161 setup do |argv, options|
162 FileUtils.rmdir argv, options
167 # Copy SOURCE to DEST.
169 # ruby -run -e install -- [OPTION] SOURCE DEST
171 # -p apply access/modification times of SOURCE files to
172 # corresponding destination files
173 # -m set permission mode (as in chmod), instead of 0755
178 setup("pm:") do |argv, options|
179 options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
180 options[:preserve] = true if options.delete :p
182 argv = argv[0] if argv.size == 1
183 FileUtils.install argv, dest, options
188 # Change the mode of each FILE to OCTAL-MODE.
190 # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
196 setup do |argv, options|
197 mode = argv.shift.oct
198 FileUtils.chmod mode, argv, options
203 # Update the access and modification times of each FILE to the current time.
205 # ruby -run -e touch -- [OPTION] FILE
211 setup do |argv, options|
212 FileUtils.touch argv, options
217 # Display help message.
219 # ruby -run -e help [COMMAND]
225 open(__FILE__) do |me|
226 while me.gets("##\n")
227 if help = me.gets("\n\n")
228 if all or argv.delete help[/-e \w+/].sub(/-e /, "")
229 print help.gsub(/^# ?/, "")