un.rb

Path: lib/un.rb
Last Update: Mon Jul 15 10:52:00 +0000 2013

un.rb

Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>

This program is free software. You can distribute/modify this program under the same terms of Ruby.

Utilities to replace common UNIX commands in Makefiles etc

SYNOPSIS

  ruby -run -e cp -- [OPTION] SOURCE DEST
  ruby -run -e ln -- [OPTION] TARGET LINK_NAME
  ruby -run -e mv -- [OPTION] SOURCE DEST
  ruby -run -e rm -- [OPTION] FILE
  ruby -run -e mkdir -- [OPTION] DIRS
  ruby -run -e rmdir -- [OPTION] DIRS
  ruby -run -e install -- [OPTION] SOURCE DEST
  ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
  ruby -run -e touch -- [OPTION] FILE
  ruby -run -e help [COMMAND]

Required files

fileutils   optparse  

Methods

chmod   cp   help   install   ln   mkdir   mv   rm   rmdir   setup   touch  

Public Instance methods

Change the mode of each FILE to OCTAL-MODE.

  ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE

  -v          verbose

[Source]

     # File lib/un.rb, line 195
195: def chmod
196:   setup do |argv, options|
197:     mode = argv.shift.oct
198:     FileUtils.chmod mode, argv, options
199:   end
200: end

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY

  ruby -run -e cp -- [OPTION] SOURCE DEST

  -p          preserve file attributes if possible
  -r          copy recursively
  -v          verbose

[Source]

    # File lib/un.rb, line 68
68: def 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
77: end

Display help message.

  ruby -run -e help [COMMAND]

[Source]

     # File lib/un.rb, line 222
222: def 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
235: end

Copy SOURCE to DEST.

  ruby -run -e install -- [OPTION] SOURCE DEST

  -p          apply access/modification times of SOURCE files to
              corresponding destination files
  -m          set permission mode (as in chmod), instead of 0755
  -v          verbose

[Source]

     # File lib/un.rb, line 177
177: def 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
185: end

Create a link to the specified TARGET with LINK_NAME.

  ruby -run -e ln -- [OPTION] TARGET LINK_NAME

  -s          make symbolic links instead of hard links
  -f          remove existing destination files
  -v          verbose

[Source]

    # File lib/un.rb, line 89
89: def 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
98: end

Create the DIR, if they do not already exist.

  ruby -run -e mkdir -- [OPTION] DIR

  -p          no error if existing, make parent directories as needed
  -v          verbose

[Source]

     # File lib/un.rb, line 144
144: def 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
150: end

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

  ruby -run -e mv -- [OPTION] SOURCE DEST

  -v          verbose

[Source]

     # File lib/un.rb, line 108
108: def 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
114: end

Remove the FILE

  ruby -run -e rm -- [OPTION] FILE

  -f          ignore nonexistent files
  -r          remove the contents of directories recursively
  -v          verbose

[Source]

     # File lib/un.rb, line 126
126: def 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
133: end

Remove the DIR.

  ruby -run -e rmdir -- [OPTION] DIR

  -v          verbose

[Source]

     # File lib/un.rb, line 160
160: def rmdir
161:   setup do |argv, options|
162:     FileUtils.rmdir argv, options
163:   end
164: end

[Source]

    # File lib/un.rb, line 32
32: def 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
56: end

Update the access and modification times of each FILE to the current time.

  ruby -run -e touch -- [OPTION] FILE

  -v          verbose

[Source]

     # File lib/un.rb, line 210
210: def touch
211:   setup do |argv, options|
212:     FileUtils.touch argv, options
213:   end
214: end

[Validate]