lib/pa/cmd.rb in pa-1.3.2 vs lib/pa/cmd.rb in pa-1.3.3
- old
+ new
@@ -42,11 +42,11 @@
# link
#
# @overload ln(src, dest)
# @overload ln([src,..], directory)
#
- # @param [Array<String>, String] src_s support globbing
+ # @param [Array<String>, String] src_s
# @param [String,Pa] dest
# @param [Hash] o
# @option o [Boolean] :show_cmd puts cmd
# @option o [Boolean] :verbose verbose mode
# @return [nil]
@@ -92,11 +92,11 @@
# @param [Hash] o
# @option o [Boolean] :verbose verbose mode
# @option o [Boolean] :show_cmd puts cmd
def cd(path=ENV["HOME"], o={}, &blk)
p = get(path)
- puts "cd #{p}" if (o[:verbose] or o[:show_cmd])
+ puts _wrap_cmd("cd #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
Dir.chdir(p, &blk)
end
# chroot
# @see {Dir.chroot}
@@ -106,11 +106,11 @@
# @option o [Boolean] :verbose verbose mode
# @option o [Boolean] :show_cmd puts cmd
# @return [nil]
def chroot(path, o={})
p = get(path)
- puts "chdroot #{p}" if (o[:verbose] or o[:show_cmd])
+ puts _wrap_cmd("chdroot #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
Dir.chroot(p)
end
# touch a blank file
#
@@ -176,11 +176,11 @@
# @overload mktmpdir(o={}, &blk) # name=$$
def mktmpdir(*args, &blk)
(name,), o = Util.extract_options(args)
p = _mktmpname(name, o)
- puts "mktmpdir #{p}" if (o[:verbose] or o[:show_cmd])
+ puts _wrap_cmd("mktmpdir #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
Dir.mkdir(p)
begin
blk.call(p)
@@ -202,11 +202,11 @@
# @return [String] path
def mktmpfile2(*args, &blk)
(name,), o = Util.extract_options(args)
p = _mktmpname(name, o)
- puts "mktmpfile #{p}" if (o[:verbose] or o[:show_cmd])
+ puts _wrap_cmd("mktmpfile #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
begin
blk.call(p)
ensure
File.delete(p)
@@ -218,11 +218,11 @@
# @return [Pa] path
def mktmpfile(*args, &blk)
(name,), o = Util.extract_options(args)
p = _mktmpname(name, o)
- puts "mktmpfile #{p}" if (o[:verbose] or o[:show_cmd])
+ puts _wrap_cmd("mktmpfile #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd])
begin
blk.call(Pa(p))
ensure
File.delete(p)
@@ -232,20 +232,21 @@
end
# rm file only
#
# @overload rm(*paths, o={})
- # @param [String] *paths support globbing
+ # @param [String] *paths
# @param o [Boolean] :verbose verbose mode
# @param o [Boolean] :show_cmd puts cmd
# @return [nil]
def rm(*paths)
paths, o = Util.extract_options(paths)
extra_doc = o[:force] ? "-f " : nil
- puts "rm #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
+ puts _wrap_cmd("rm #{extra_doc}#{paths.join(' ')}", o[:show_cmd]) if o[:show_cmd]
- Pa.glob(*paths) { |pa|
+ paths.each { |path|
+ pa = Pa(path)
puts "rm #{extra_doc}#{pa.p}" if o[:verbose]
if File.directory?(pa.p)
if o[:force]
next
@@ -264,20 +265,21 @@
rm *paths, o
end
# rm directory only. still remove if directory is not empty.
#
- # @param [String] *paths support globbing
+ # @param [String] *paths
# @param [Hash] o options
# @option o [Boolean] :verbose verbose mode
# @option o [Boolean] :show_cmd puts cmd
# @return [nil]
def rmdir(*paths)
paths, o = Util.extract_options(paths)
extra_doc = o[:force] ? "-f " : nil
- puts "rmdir #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
- Pa.glob(*paths) { |pa|
+ puts _wrap_cmd("rmdir #{extra_doc}#{paths.join(" ")}", o[:show_cmd]) if o[:show_cmd]
+ paths.each { |path|
+ pa = Pa(path)
puts " rmdir #{extra_doc}#{pa.p}" if o[:verbose]
if not File.directory?(pa.p)
if o[:force]
next
@@ -307,11 +309,11 @@
# @option o [Boolean] :show_cmd puts cmd
# @return [nil]
def empty_dir(*dirs)
dirs, o = Util.extract_options(dirs)
extra_doc = o[:force] ? "-f " : nil
- puts "empty_dir #{extra_doc}#{dirs.join(" ")}" if o[:show_cmd]
+ puts _wrap_cmd("empty_dir #{extra_doc}#{dirs.join(" ")}", o[:show_cmd]) if o[:show_cmd]
dirs.each {|dir|
dir = Pa(dir)
if not File.exists?(dir.p)
raise Errno::ENOENT, "not exists -- #{dir}" unless o[:force]
@@ -333,12 +335,13 @@
#
# @see rm
# @return [nil]
def rm_r(*paths)
paths, o = Util.extract_options(paths)
- puts "rm -r #{path.join(" ")}" if o[:show_cmd]
- Pa.glob(*paths){ |pa|
+ puts _wrap_cmd("rm -r #{path.join(' ')}", o[:show_cmd]) if o[:show_cmd]
+ paths.each { |path|
+ pa = Pa(path)
puts "rm -r #{pa.p}" if o[:verbose]
File.directory?(pa.p) ? _rmdir(pa) : File.delete(pa.p)
}
end
alias rm_rf rm_r
@@ -348,18 +351,19 @@
# @example
# Pa.rm_if '/tmp/**/*.rb' do |pa|
# pa.name == 'old'
# end
#
- # @param [String] *paths support globbing
+ # @param [String] *paths
# @yield [path]
# @yieldparam [Pa] path
# @yieldreturn [Boolean] rm_r path if true
# @return [nil]
def rm_if(*paths, &blk)
paths, o = Util.extract_options(paths)
- Pa.glob(*paths) do |pa|
+ paths.each do |path|
+ pa = Pa(path)
rm_r pa, o if blk.call(pa)
end
end
# copy
@@ -379,11 +383,11 @@
# end
#
# default: preverse mode, not owner.
#
# @overload cp(src_s, dest, o)
- # @param [Array<String>, String] src_s support globbing
+ # @param [Array<String>, String] src_s
# @param [String,Pa] dest
# @param [Hash] o option
# @option o [Boolean] :mkdir mkdir(dest) if dest not exists.
# @option o [Boolean] :verbose puts cmd when execute
# @option o [Boolean] :show_cmd puts cmd
@@ -393,11 +397,11 @@
# @return [nil]
# @overload cp(src_s, dest, o)
# @yield [src,dest,o]
# @return [nil]
def cp(src_s, dest, o={}, &blk)
- srcs = Pa.glob(*Util.wrap_array(src_s)).map{|v| v.path}
+ srcs = Util.wrap_array(src_s).map{|v| Pa.get(v)}
dest = Pa.get(dest)
puts "cp #{srcs.join(" ")} #{dest}" if o[:show_cmd]
if o[:mkdir] and (not File.exists?(dest))
Pa.mkdir dest
@@ -433,11 +437,11 @@
# @option o [Boolean] :show_cmd puts cmd
# @option o [Boolean] :mkdir
# @option o [Boolean] :fore
# @return [nil]
def mv(src_s, dest, o={}, &blk)
- srcs = Pa.glob(*Util.wrap_array(src_s)).map{|v| get(v)}
+ srcs = Util.wrap_array(src_s).map{|v| get(v)}
dest = get(dest)
extra_doc = o[:force] ? "-f " : nil
puts "mv #{extra_doc}#{srcs.join(" ")} #{dest}" if o[:show_cmd]
@@ -474,10 +478,13 @@
# @param [String] src
# @param [String] dest
def _move(src, dest, o)
raise Errno::EEXIST, "dest exists -- #{dest}" if File.exists?(dest) and (not o[:force])
+ # move same file.
+ return if File.absolute_path(src) == File.absolute_path(dest)
+
# :force. mv "dir", "dira" and 'dira' exists and is a directory.
if File.exists?(dest) and File.directory?(dest)
ls(src) { |pa|
dest1 = File.join(dest, File.basename(pa.p))
_move pa.p, dest1, o
@@ -532,11 +539,11 @@
extra_doc = ""
extra_doc << (method==:symlink ? "-s " : "")
extra_doc << (o[:force] ? "-f " : "")
puts "ln #{extra_doc}#{srcs.join(" ")} #{dest}" if o[:show_cmd]
- Pa.glob(*srcs) {|src|
+ srcs.each {|src|
src = get(src)
dest = File.join(dest, File.basename(src)) if File.directory?(dest)
if File.exists?(dest)
if o[:force]
@@ -660,9 +667,19 @@
eval <<-EOF
def #{mth}(*args, &blk)
Pa(#{mth}2(*args, &blk))
end
EOF
+ end
+
+ # @private
+ def _wrap_cmd(cmd, pretty)
+ case pretty
+ when "$", "#"
+ "#{pretty} cmd"
+ else
+ cmd
+ end
end
end
end
end