source: extensions/gsdl-video/trunk/installed/cmdline/lib/ruby/1.8/un.rb@ 18425

Last change on this file since 18425 was 18425, checked in by davidb, 15 years ago

Video extension to Greenstone

File size: 4.5 KB
Line 
1#
2# = un.rb
3#
4# Copyright (c) 2003 WATANABE Hirofumi <[email protected]>
5#
6# This program is free software.
7# You can distribute/modify this program under the same terms of Ruby.
8#
9# == Utilities to replace common UNIX commands in Makefiles etc
10#
11# == SYNOPSIS
12#
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]
23
24require "fileutils"
25require "optparse"
26
27module FileUtils
28# @fileutils_label = ""
29 @fileutils_output = $stdout
30end
31
32def setup(options = "")
33 ARGV.map! do |x|
34 case x
35 when /^-/
36 x.delete "^-#{options}v"
37 when /[*?\[{]/
38 Dir[x]
39 else
40 x
41 end
42 end
43 ARGV.flatten!
44 ARGV.delete_if{|x| x == "-"}
45 opt_hash = {}
46 OptionParser.new do |o|
47 options.scan(/.:?/) do |s|
48 o.on("-" + s.tr(":", " ")) do |val|
49 opt_hash[s.delete(":").intern] = val
50 end
51 end
52 o.on("-v") do opt_hash[:verbose] = true end
53 o.parse!
54 end
55 yield ARGV, opt_hash
56end
57
58##
59# Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
60#
61# ruby -run -e cp -- [OPTION] SOURCE DEST
62#
63# -p preserve file attributes if possible
64# -r copy recursively
65# -v verbose
66#
67
68def cp
69 setup("pr") do |argv, options|
70 cmd = "cp"
71 cmd += "_r" if options.delete :r
72 options[:preserve] = true if options.delete :p
73 dest = argv.pop
74 argv = argv[0] if argv.size == 1
75 FileUtils.send cmd, argv, dest, options
76 end
77end
78
79##
80# Create a link to the specified TARGET with LINK_NAME.
81#
82# ruby -run -e ln -- [OPTION] TARGET LINK_NAME
83#
84# -s make symbolic links instead of hard links
85# -f remove existing destination files
86# -v verbose
87#
88
89def ln
90 setup("sf") do |argv, options|
91 cmd = "ln"
92 cmd += "_s" if options.delete :s
93 options[:force] = true if options.delete :f
94 dest = argv.pop
95 argv = argv[0] if argv.size == 1
96 FileUtils.send cmd, argv, dest, options
97 end
98end
99
100##
101# Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
102#
103# ruby -run -e mv -- [OPTION] SOURCE DEST
104#
105# -v verbose
106#
107
108def mv
109 setup do |argv, options|
110 dest = argv.pop
111 argv = argv[0] if argv.size == 1
112 FileUtils.mv argv, dest, options
113 end
114end
115
116##
117# Remove the FILE
118#
119# ruby -run -e rm -- [OPTION] FILE
120#
121# -f ignore nonexistent files
122# -r remove the contents of directories recursively
123# -v verbose
124#
125
126def rm
127 setup("fr") do |argv, options|
128 cmd = "rm"
129 cmd += "_r" if options.delete :r
130 options[:force] = true if options.delete :f
131 FileUtils.send cmd, argv, options
132 end
133end
134
135##
136# Create the DIR, if they do not already exist.
137#
138# ruby -run -e mkdir -- [OPTION] DIR
139#
140# -p no error if existing, make parent directories as needed
141# -v verbose
142#
143
144def mkdir
145 setup("p") do |argv, options|
146 cmd = "mkdir"
147 cmd += "_p" if options.delete :p
148 FileUtils.send cmd, argv, options
149 end
150end
151
152##
153# Remove the DIR.
154#
155# ruby -run -e rmdir -- [OPTION] DIR
156#
157# -v verbose
158#
159
160def rmdir
161 setup do |argv, options|
162 FileUtils.rmdir argv, options
163 end
164end
165
166##
167# Copy SOURCE to DEST.
168#
169# ruby -run -e install -- [OPTION] SOURCE DEST
170#
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
174# -v verbose
175#
176
177def install
178 setup("pm:") do |argv, options|
179 options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
180 options[:preserve] = true if options.delete :p
181 dest = argv.pop
182 argv = argv[0] if argv.size == 1
183 FileUtils.install argv, dest, options
184 end
185end
186
187##
188# Change the mode of each FILE to OCTAL-MODE.
189#
190# ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
191#
192# -v verbose
193#
194
195def chmod
196 setup do |argv, options|
197 mode = argv.shift.oct
198 FileUtils.chmod mode, argv, options
199 end
200end
201
202##
203# Update the access and modification times of each FILE to the current time.
204#
205# ruby -run -e touch -- [OPTION] FILE
206#
207# -v verbose
208#
209
210def touch
211 setup do |argv, options|
212 FileUtils.touch argv, options
213 end
214end
215
216##
217# Display help message.
218#
219# ruby -run -e help [COMMAND]
220#
221
222def help
223 setup do |argv,|
224 all = argv.empty?
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(/^# ?/, "")
230 end
231 end
232 end
233 end
234 end
235end
Note: See TracBrowser for help on using the repository browser.