pa 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ script: bundle exec rspec spec
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source :rubygems
2
2
 
3
+ gem "pd"
4
+
3
5
  group :development do
4
6
  gem "thor"
5
7
  gem "rspec"
data/Gemfile.lock CHANGED
@@ -1,7 +1,10 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ awesome_print (1.0.2)
4
5
  diff-lcs (1.1.3)
6
+ pd (1.0.5)
7
+ awesome_print
5
8
  rspec (2.8.0)
6
9
  rspec-core (~> 2.8.0)
7
10
  rspec-expectations (~> 2.8.0)
@@ -17,6 +20,7 @@ PLATFORMS
17
20
  ruby
18
21
 
19
22
  DEPENDENCIES
23
+ pd
20
24
  rspec
21
25
  thor
22
26
  watchr
data/README.md CHANGED
@@ -1,5 +1,4 @@
1
- Pa, a path libraray for Ruby
2
- ========================
1
+ # Pa, a path libraray for Ruby [![Build Status](https://secure.travis-ci.org/GutenYe/pa.png)](http://travis-ci.org/GutenYe/pa)
3
2
 
4
3
  | Homepage: | https://github.com/GutenYe/pa |
5
4
  |----------------|--------------------------------------|
data/lib/pa.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "tmpdir"
2
+ require "pd"
2
3
 
3
4
  =begin rdoc
4
5
  Pa(Path) is similary to Pathname, but more powerful.
@@ -204,10 +205,11 @@ class Pa
204
205
  end
205
206
 
206
207
  DELEGATE_METHODS2 = [ :join2 ]
207
- DELEGATE_METHODS = [ :dir, :build, :join ]
208
+ DELEGATE_METHODS = [ :build, :join ]
209
+ DELEGATE_ATTR_METHODS = [ :dir, :rel, :rea ]
208
210
 
209
211
  attr_reader :path2
210
- attr_reader :absolute2, :dir2, :dir_strict2, :base2, :fname2, :name2, :short2, :ext2, :fext2, :rel, :rea
212
+ attr_reader :absolute2, :dir2, :dir_strict2, :base2, :fname2, :name2, :short2, :ext2, :fext2, :rel2, :rea2
211
213
  attr_reader :options
212
214
 
213
215
  # @param [Hash] o option
@@ -220,8 +222,7 @@ class Pa
220
222
  @path2.sub!(/^~/, ENV["HOME"].to_s) if @path2 # nil
221
223
  @options = o
222
224
 
223
- @rel = o[:rel] || ""
224
- @base_dir = o[:base_dir]
225
+ @base_dir = o[:base_dir] || "."
225
226
 
226
227
  initialize_variables
227
228
  end
@@ -231,41 +232,40 @@ class Pa
231
232
  end
232
233
  include chainable
233
234
 
234
- def rel
235
- raise Error, "don't have a :rel option" unless rel?
236
-
237
- @rel ||= options[:rel]
238
- end
239
-
240
235
  def base_dir
241
- raise Error, "don't have a :base_dir option" unless base_dir?
242
-
243
- @base_dir ||= options[:base_dir]
236
+ @base_dir ||= (options[:base_dir] || ".")
244
237
  end
245
238
 
246
- def rea
247
- raise Error, "don't have a :base_dir option" unless base_dir?
248
-
249
- @rea ||= File.join(options[:base_dir], path)
239
+ def rel2
240
+ @rel2 ||= (options[:rel] || "" )
250
241
  end
251
242
 
252
- def rel?
253
- options.has_key?(:rel)
243
+ def rea2
244
+ @rea2 ||= options[:base_dir] ? File.join(base_dir, path) : path
254
245
  end
255
246
 
256
- def base_dir?
257
- options.has_key?(:base_dir)
247
+ def absolute2
248
+ @absolute2 ||= File.absolute_path(rea2)
258
249
  end
259
250
 
260
- def absolute2
261
- @absolute2 ||= File.absolute_path(options[:base_dir] ? rea : path)
251
+ def absolute
252
+ @absolute ||= Pa(absolute2)
262
253
  end
263
254
 
264
255
  # => ".", "..", "/", "c:"
256
+ #
257
+ # "foo" => "."
258
+ # "./foo" => "."
259
+ # "../../foo" => "../.."
260
+ #
265
261
  def dir2
266
262
  @dir2 ||= File.dirname(path)
267
263
  end
268
264
 
265
+ def dir
266
+ @dir ||= Pa(dir2)
267
+ end
268
+
269
269
  # Pa("foo") => ""
270
270
  # Pa("./foo") => "."
271
271
  def dir_strict2
@@ -273,13 +273,17 @@ class Pa
273
273
 
274
274
  dir = File.dirname(path)
275
275
 
276
- @dir_strict2 = if %w[. ..].include?(dir) && path !~ %r!^\.\.?/!
276
+ @dir_strict2 = if %w[.].include?(dir) && path !~ %r~^\./~
277
277
  ""
278
278
  else
279
279
  dir
280
280
  end
281
281
  end
282
282
 
283
+ def dir_strict
284
+ @dir_strict ||= Pa(dir_strict2)
285
+ end
286
+
283
287
  def base2
284
288
  @base2 ||= File.basename(path)
285
289
  end
@@ -308,7 +312,7 @@ class Pa
308
312
  alias ext ext2
309
313
  alias fext fext2
310
314
 
311
- # abbretive
315
+ # abbreviate
312
316
  alias p2 path2
313
317
  alias p2 path
314
318
  alias a2 absolute2
@@ -326,6 +330,10 @@ class Pa
326
330
  alias e ext
327
331
  alias fe fext
328
332
 
333
+ alias a absolute
334
+ alias d dir
335
+ alias d_s dir_strict
336
+
329
337
  # return '#<Pa @path="foo", @absolute="/home/foo">'
330
338
  #
331
339
  # @return [String]
@@ -383,6 +391,10 @@ class Pa
383
391
  @short2 ||= Pa.shorten2(@path)
384
392
  end
385
393
 
394
+ def short
395
+ @short ||= Pa(short2)
396
+ end
397
+
386
398
  # @return [String]
387
399
  def sub2(*args, &blk)
388
400
  path.sub(*args, &blk)
@@ -458,6 +470,14 @@ class Pa
458
470
  end
459
471
  EOF
460
472
  }
473
+
474
+ DELEGATE_ATTR_METHODS.each {|mth|
475
+ class_eval <<-EOF
476
+ def #{mth}(*args, &blk)
477
+ @#{mth} ||= Pa(#{mth}2(*args, &blk))
478
+ end
479
+ EOF
480
+ }
461
481
  end
462
482
 
463
483
  require "pa/path"
@@ -476,9 +496,10 @@ private
476
496
  # a very convient function.
477
497
  #
478
498
  # @example
499
+ #
479
500
  # Pa('/home').exists?
480
- def Pa(path)
501
+ def Pa(path, o={})
481
502
  return path if Pa===path
482
- Pa.new path
503
+ Pa.new path, o
483
504
  end
484
505
  end
data/lib/pa/cmd.rb CHANGED
@@ -9,6 +9,23 @@ rm family
9
9
  === Example
10
10
  rm path # it's clear: remove a file
11
11
  rmdir path # it's clear: remove a directory
12
+
13
+ :verbose and :show_cmd options, almost every cmd has these two options.
14
+
15
+ Pa.mv "dira", "dirb", :show_cmd => true
16
+ > mv dira dirb
17
+
18
+ Pa.mv "dira", "dirb", :verbose => true
19
+ > rename dira/filea dirb/filea
20
+ > rename dira/fileb dirb/fileb
21
+
22
+ Pa.touch "a b c", :show_cmd => true
23
+ > touch a b c
24
+
25
+ Pa.touch "a b c", :verbose => true
26
+ > touch a
27
+ > touch b
28
+ > touch c
12
29
  =end
13
30
  class Pa
14
31
  module Cmd
@@ -27,6 +44,9 @@ class Pa
27
44
  #
28
45
  # @param [Array<String>, String] src_s support globbing
29
46
  # @param [String,Pa] dest
47
+ # @param [Hash] o
48
+ # @option o [Boolean] :show_cmd puts cmd
49
+ # @option o [Boolean] :verbose verbose mode
30
50
  # @return [nil]
31
51
  def ln(src_s, dest, o={})
32
52
  _ln(:link, src_s, dest, o)
@@ -69,9 +89,10 @@ class Pa
69
89
  # @param [String,Pa] path
70
90
  # @param [Hash] o
71
91
  # @option o [Boolean] :verbose verbose mode
92
+ # @option o [Boolean] :show_cmd puts cmd
72
93
  def cd(path=ENV["HOME"], o={}, &blk)
73
94
  p = get(path)
74
- puts "cd #{p}" if o[:verbose]
95
+ puts "cd #{p}" if (o[:verbose] or o[:show_cmd])
75
96
  Dir.chdir(p, &blk)
76
97
  end
77
98
 
@@ -81,10 +102,11 @@ class Pa
81
102
  # @param [String] path
82
103
  # @param [Hash] o
83
104
  # @option o [Boolean] :verbose verbose mode
105
+ # @option o [Boolean] :show_cmd puts cmd
84
106
  # @return [nil]
85
107
  def chroot(path, o={})
86
108
  p = get(path)
87
- puts "chdroot #{p}" if o[:verbose]
109
+ puts "chdroot #{p}" if (o[:verbose] or o[:show_cmd])
88
110
  Dir.chroot(p)
89
111
  end
90
112
 
@@ -96,7 +118,8 @@ class Pa
96
118
  # @option o [Fixnum,String] :mode (0664)
97
119
  # @option o [Boolean] :mkdir auto mkdir if path contained directory not exists.
98
120
  # @option o [Boolean] :force
99
- # @option o [Boolean] :verbose
121
+ # @option o [Boolean] :verbose verbose mode
122
+ # @option o [Boolean] :show_cmd puts cmd
100
123
  # @return [nil]
101
124
  def touch(*args)
102
125
  paths, o = Util.extract_options(args)
@@ -121,7 +144,8 @@ class Pa
121
144
  # @param [Hash] o option
122
145
  # @option o [Fixnum] :mode (0775)
123
146
  # @option o [Boolean] :force
124
- # @option o [Boolean] :verbose
147
+ # @option o [Boolean] :verbose verbose mode
148
+ # @option o [Boolean] :show_cmd puts cmd
125
149
  # @return [nil]
126
150
  def mkdir(*args)
127
151
  paths, o = Util.extract_options(args)
@@ -144,14 +168,15 @@ class Pa
144
168
  # @overload mktmpdir(name, o={}, &blk)
145
169
  # @param [Hash] o options
146
170
  # @option o [String] :tmpdir (ENV["TEMP"])
147
- # @option o [Symbol] :verbose
171
+ # @option o [Symbol] :verbose verbose mode
172
+ # @option o [Symbol] :show_cmd puts cmd
148
173
  # @return [String] path
149
174
  # @overload mktmpdir(o={}, &blk) # name=$$
150
175
  def mktmpdir(*args, &blk)
151
176
  (name,), o = Util.extract_options(args)
152
177
 
153
178
  p = _mktmpname(name, o)
154
- puts "mktmpdir #{p}" if o[:verbose]
179
+ puts "mktmpdir #{p}" if (o[:verbose] or o[:show_cmd])
155
180
 
156
181
  Dir.mkdir(p)
157
182
 
@@ -169,14 +194,15 @@ class Pa
169
194
  #
170
195
  # @overload mktmpfile2(name=$$, o={}, &blk)
171
196
  # @param [Hash] o options
172
- # @option o [Boolean] :verbose
197
+ # @option o [Boolean] :verbose verbose mode
198
+ # @option o [Boolean] :show_cmd puts cmd
173
199
  # @option o [String] :tmpdir
174
200
  # @return [String] path
175
201
  def mktmpfile2(*args, &blk)
176
202
  (name,), o = Util.extract_options(args)
177
203
 
178
204
  p = _mktmpname(name, o)
179
- puts "mktmpfile #{p}" if o[:verbose]
205
+ puts "mktmpfile #{p}" if (o[:verbose] or o[:show_cmd])
180
206
 
181
207
  begin
182
208
  blk.call(p)
@@ -192,7 +218,7 @@ class Pa
192
218
  (name,), o = Util.extract_options(args)
193
219
 
194
220
  p = _mktmpname(name, o)
195
- puts "mktmpfile #{p}" if o[:verbose]
221
+ puts "mktmpfile #{p}" if (o[:verbose] or o[:show_cmd])
196
222
 
197
223
  begin
198
224
  blk.call(Pa(p))
@@ -207,12 +233,15 @@ class Pa
207
233
  #
208
234
  # @overload rm(*paths, o={})
209
235
  # @param [String] *paths support globbing
210
- # @param o [Boolean] :verbose
236
+ # @param o [Boolean] :verbose verbose mode
237
+ # @param o [Boolean] :show_cmd puts cmd
211
238
  # @return [nil]
212
239
  def rm(*paths)
213
240
  paths, o = Util.extract_options(paths)
214
- glob(*paths) { |pa|
215
- extra_doc = o[:force] ? "-f " : nil
241
+ extra_doc = o[:force] ? "-f " : nil
242
+ puts "rm #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
243
+
244
+ Pa.glob(*paths) { |pa|
216
245
  puts "rm #{extra_doc}#{pa.p}" if o[:verbose]
217
246
 
218
247
  if File.directory?(pa.p)
@@ -236,12 +265,16 @@ class Pa
236
265
  # rm directory only. still remove if directory is not empty.
237
266
  #
238
267
  # @param [String] *paths support globbing
268
+ # @param [Hash] o options
269
+ # @option o [Boolean] :verbose verbose mode
270
+ # @option o [Boolean] :show_cmd puts cmd
239
271
  # @return [nil]
240
272
  def rmdir(*paths)
241
273
  paths, o = Util.extract_options(paths)
242
- glob(*paths) { |pa|
243
- extra_doc = o[:force] ? "-f " : nil
244
- puts "rmdir #{extra_doc}#{pa.p}" if o[:verbose]
274
+ extra_doc = o[:force] ? "-f " : nil
275
+ puts "rmdir #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
276
+ Pa.glob(*paths) { |pa|
277
+ puts " rmdir #{extra_doc}#{pa.p}" if o[:verbose]
245
278
 
246
279
  if not File.directory?(pa.p)
247
280
  if o[:force]
@@ -259,14 +292,49 @@ class Pa
259
292
  o[:force] = true
260
293
  rmdir *paths, o
261
294
  end
295
+
296
+ # empty a directory.
297
+ #
298
+ # @example
299
+ #
300
+ # empty_dir("foo")
301
+ #
302
+ # @param [String] *dirs
303
+ # @param [Hash] o options
304
+ # @option o [Boolean] :verbose verbose mode
305
+ # @option o [Boolean] :show_cmd puts cmd
306
+ # @return [nil]
307
+ def empty_dir(*dirs)
308
+ dirs, o = Util.extract_options(dirs)
309
+ extra_doc = o[:force] ? "-f " : nil
310
+ puts "empty_dir #{extra_doc}#{dirs.join(" ")}" if o[:show_cmd]
311
+
312
+ dirs.each {|dir|
313
+ dir = Pa(dir)
314
+ if not File.exists?(dir.p)
315
+ raise Errno::ENOENT, "not exists -- #{dir}" unless o[:force]
316
+ elsif not File.directory?(dir.p)
317
+ raise Errno::ENOTDIR, "not a directory -- #{dir}" unless o[:force]
318
+ else
319
+ rm_r *Pa.glob2("#{dir}/*", :dotmatch => true)
320
+ end
321
+ }
322
+ end
262
323
 
324
+ def empty_dir_f(*dirs)
325
+ dirs, o = Util.extract_options(dirs)
326
+ o[:force] = true
327
+ empty_dir *dirs, o
328
+ end
329
+
263
330
  # rm recusive, rm both file and directory
264
331
  #
265
332
  # @see rm
266
333
  # @return [nil]
267
334
  def rm_r(*paths)
268
335
  paths, o = Util.extract_options(paths)
269
- glob(*paths){ |pa|
336
+ puts "rm -r #{path.join(" ")}" if o[:show_cmd]
337
+ Pa.glob(*paths){ |pa|
270
338
  puts "rm -r #{pa.p}" if o[:verbose]
271
339
  File.directory?(pa.p) ? _rmdir(pa) : File.delete(pa.p)
272
340
  }
@@ -287,7 +355,7 @@ class Pa
287
355
  # @return [nil]
288
356
  def rm_if(*paths, &blk)
289
357
  paths, o = Util.extract_options(paths)
290
- glob(*paths) do |pa|
358
+ Pa.glob(*paths) do |pa|
291
359
  rm_r pa, o if blk.call(pa)
292
360
  end
293
361
  end
@@ -316,6 +384,7 @@ class Pa
316
384
  # @param [Hash] o option
317
385
  # @option o [Boolean] :mkdir mkdir(dest) if dest not exists.
318
386
  # @option o [Boolean] :verbose puts cmd when execute
387
+ # @option o [Boolean] :show_cmd puts cmd
319
388
  # @option o [Boolean] :folsymlink follow symlink
320
389
  # @option o [Boolean] :force force dest file if dest is a file
321
390
  # @option o [Boolean] :special special copy, when cp a directory, only mkdir, not cp the directory's content, usefull in Pa.each_r
@@ -324,8 +393,9 @@ class Pa
324
393
  # @yield [src,dest,o]
325
394
  # @return [nil]
326
395
  def cp(src_s, dest, o={}, &blk)
327
- srcs = glob(*Util.wrap_array(src_s)).map{|v| v.path}
396
+ srcs = Pa.glob(*Util.wrap_array(src_s)).map{|v| v.path}
328
397
  dest = Pa.get(dest)
398
+ puts "cp #{srcs.join(" ")} #{dest}" if o[:show_cmd]
329
399
 
330
400
  if o[:mkdir] and (not File.exists?(dest))
331
401
  Pa.mkdir dest
@@ -357,14 +427,18 @@ class Pa
357
427
  # @see cp
358
428
  #
359
429
  # @param [Hash] o option
360
- # @option o [Boolean] :verbose
430
+ # @option o [Boolean] :verbose verbose mode
431
+ # @option o [Boolean] :show_cmd puts cmd
361
432
  # @option o [Boolean] :mkdir
362
433
  # @option o [Boolean] :fore
363
434
  # @return [nil]
364
435
  def mv(src_s, dest, o={}, &blk)
365
- srcs = glob(*Util.wrap_array(src_s)).map{|v| get(v)}
436
+ srcs = Pa.glob(*Util.wrap_array(src_s)).map{|v| get(v)}
366
437
  dest = get(dest)
367
438
 
439
+ extra_doc = o[:force] ? "-f " : nil
440
+ puts "mv #{extra_doc}#{srcs.join(" ")} #{dest}" if o[:show_cmd]
441
+
368
442
  if o[:mkdir] and (not File.exists?(dest))
369
443
  mkdir dest
370
444
  end
@@ -425,8 +499,9 @@ class Pa
425
499
  def _touch(paths, o)
426
500
  o[:mode] ||= 0644
427
501
  paths.map!{|v|get(v)}
502
+ extra_doc = o[:force] ? "-f " : nil
503
+ puts "touch #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
428
504
  paths.each {|p|
429
- extra_doc = o[:force] ? "-f " : nil
430
505
  puts "touch #{extra_doc}#{p}" if o[:verbose]
431
506
 
432
507
  if File.exists?(p)
@@ -446,29 +521,47 @@ class Pa
446
521
 
447
522
  # @param [Array,String,#path] src_s
448
523
  # @param [String,#path] dest
524
+ #
449
525
  def _ln(method, src_s, dest, o={})
526
+ srcs = Util.wrap_array(src_s)
450
527
  dest = get(dest)
451
- glob(*Util.wrap_array(src_s)) {|src|
528
+ extra_doc = ""
529
+ extra_doc << (method==:symlink ? "-s " : "")
530
+ extra_doc << (o[:force] ? "-f " : "")
531
+ puts "ln #{extra_doc}#{srcs.join(" ")} #{dest}" if o[:show_cmd]
532
+
533
+ Pa.glob(*srcs) {|src|
452
534
  src = get(src)
453
535
  dest = File.join(dest, File.basename(src)) if File.directory?(dest)
454
- Pa.rm_r(dest) if o[:force] and File.exists?(dest)
455
- extra_doc = ""
456
- extra_doc << (method==:symlink ? "-s " : "")
457
- extra_doc << (o[:force] ? "-f " : "")
458
- puts "ln #{extra_doc}#{src} #{dest}" if o[:verbose]
536
+
537
+ if File.exists?(dest)
538
+ if o[:force]
539
+ Pa.rm_r(dest)
540
+ else
541
+ raise Errno::EEXIST, "dest exists -- #{dest}"
542
+ end
543
+ end
544
+
545
+ puts "ln #{extra_doc}#{src} #{dest}" if o[:verbose]
459
546
 
460
547
  File.send(method, src, dest)
461
548
  }
462
549
  end
463
550
 
464
551
  def _mkdir(paths, o)
552
+ puts "mkdir #{paths.join(" ")}" if o[:show_cmd]
553
+
465
554
  o[:mode] ||= 0775
466
555
  paths.map!{|v|get(v)}
467
556
  paths.each {|p|
468
557
  puts "mkdir #{p}" if o[:verbose]
469
558
 
470
559
  if File.exists?(p)
471
- o[:force] ? next : raise(Errno::EEXIST, "File exist -- #{p}")
560
+ if o[:force]
561
+ next
562
+ else
563
+ raise Errno::EEXIST, "File exist -- #{p}"
564
+ end
472
565
  end
473
566
 
474
567
  stack = []
data/lib/pa/directory.rb CHANGED
@@ -24,15 +24,15 @@ class Pa
24
24
  extend Util::Concern
25
25
 
26
26
  module ClassMethods
27
- # path globbing, exclude '.' '..' for :dotmatch
27
+ # path globbing, exclude '.' '..'
28
28
  # @note glob is * ** ? [set] {a,b}
29
29
  #
30
30
  # @overload glob2(*paths, o={})
31
31
  # @param [String] path
32
32
  # @param [Hash] o option
33
- # @option o [Boolean] :dotmatch glob not match dot file by default.
34
- # @option o [Boolean] :pathname wildcard doesn't match /
35
- # @option o [Boolean] :noescape makes '\\' ordinary
33
+ # @option o [Boolean] :dotmatch (false) match dot file. FNM_DOTMATCH
34
+ # @option o [Boolean] :pathname (false) wildcard doesn't match /. FNM_PATHNAME
35
+ # @option o [Boolean] :noescape (false) makes '\\' ordinary. FNM_NOESCAPE
36
36
  # @return [Array<String>]
37
37
  # @overload glob2(*paths, o={})
38
38
  # @yieldparam [String] path
@@ -47,10 +47,8 @@ class Pa
47
47
  flag |= File.const_get("FNM_#{option.upcase}") if value
48
48
  end
49
49
 
50
- files = Dir.glob(paths, flag)
51
-
52
- # delete . .. for '.*'
53
- %w(. ..).each {|v| files.delete(v)}
50
+ # delete . ..
51
+ files = Dir.glob(paths, flag).delete_if{|v| v =~ %r~(^|/)\.\.?$~}
54
52
 
55
53
  ret = []
56
54
  files.each { |path|
@@ -192,7 +190,7 @@ class Pa
192
190
 
193
191
  args, o = Util.extract_options(args)
194
192
  each2_r *args, o do |path, abs, rel, err, rea|
195
- blk.call Pa(path), abs, rel, err, rea
193
+ blk.call Pa(path, :rel => rel), abs, rel, err, rea
196
194
  end
197
195
  end
198
196
 
data/lib/pa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Pa
2
- VERSION = "1.2.3"
2
+ VERSION = "1.3.0"
3
3
  end
data/pa.gemspec CHANGED
@@ -14,4 +14,5 @@ a path library for Ruby
14
14
  s.rubyforge_project = "xx"
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
+ s.add_dependency "pd"
17
18
  end
data/spec/pa/cmd_spec.rb CHANGED
@@ -47,65 +47,46 @@ describe Pa do
47
47
 
48
48
  describe "#ln" do
49
49
  # file1
50
+ # file2
50
51
  before :each do
51
- FileUtils.touch %w[file1]
52
+ FileUtils.touch %w[file1 file2]
52
53
  end
53
54
 
54
55
  it "works" do
55
56
  Pa.ln("file1", "lna")
56
57
  File.identical?("file1", "lna").should be_true
57
-
58
- Pa.ln(Pa("file1"), Pa("lnb"))
59
- File.identical?("file1", "lnb").should be_true
60
58
  end
61
- end
62
59
 
63
- describe "#ln_f" do
64
- # file1
65
- # file2
66
- # file3
67
- before :each do
68
- FileUtils.touch %w[file1 file2 file3]
60
+ it "raises Errno::EEXIST" do
61
+ lambda{ Pa.ln("file1", "file2") }.should raise_error(Errno::EEXIST)
69
62
  end
70
63
 
71
- it "works" do
72
- Pa.ln_f("file1", "file2")
64
+ it "doesn't raise Errno::EEXIST with :force" do
65
+ lambda{ Pa.ln("file1", "file2", :force => true) }.should_not raise_error(Errno::EEXIST)
73
66
  File.identical?("file1", "file2").should be_true
74
-
75
- Pa.ln_f(Pa("file1"), Pa("file3"))
76
- File.identical?("file1", "file3").should be_true
77
67
  end
78
68
  end
79
69
 
80
70
  describe "#symln" do
81
71
  # file1
72
+ # file2
82
73
  before :each do
83
- FileUtils.touch %w[file1]
74
+ FileUtils.touch %w[file1 file2]
84
75
  end
85
76
 
86
77
  it "works" do
87
78
  Pa.symln("file1", "syma")
88
79
  File.symlink?("syma").should be_true
89
-
90
- Pa.symln(Pa("file1"), Pa("symb"))
91
- File.symlink?("symb").should be_true
92
80
  end
93
- end
94
81
 
95
- describe "#symln_f" do
96
- # file1
97
- # file2
98
- # file3
99
- before :each do
100
- FileUtils.touch %w[file1 file2 file3]
82
+ it "raises Errno::EEXIST" do
83
+ lambda{ Pa.symln("file1", "file2") }.should raise_error(Errno::EEXIST)
101
84
  end
102
85
 
103
- it "works" do
104
- Pa.symln_f("file1", "file2")
86
+ it "doesn't raise Errno::EEXIST with :force" do
87
+ lambda{ Pa.symln("file1", "file2", :force => true) }.should_not raise_error(Errno::EEXIST)
105
88
  File.symlink?("file2").should be_true
106
-
107
- Pa.symln_f(Pa("file1"), Pa("file3"))
108
- File.symlink?("file3").should be_true
89
+ File.readlink("file2").should == "file1"
109
90
  end
110
91
  end
111
92
 
@@ -182,21 +163,23 @@ describe Pa do
182
163
  end
183
164
 
184
165
  describe "#mkdir" do
185
- it "mkdir" do
186
- Pa.mkdir("guten/tag")
187
- File.exists?("guten/tag").should be_true
188
- end
189
- end
190
-
191
- describe "#mkdir_f" do
192
166
  # dir1/dira
193
167
  before :each do
194
168
  FileUtils.mkdir_p %w[dir1/dira]
195
169
  end
196
170
 
197
- it "mkdir" do
198
- Pa.mkdir_f "dir1/dira"
171
+ it "works" do
172
+ Pa.mkdir("dir2/dirb")
173
+ File.exists?("dir2/dirb").should be_true
199
174
  end
175
+
176
+ it "raise Errno::EEXIST" do
177
+ lambda{ Pa.mkdir("dir1/dira") }.should raise_error(Errno::EEXIST)
178
+ end
179
+
180
+ it "doesn't raise Errno::EEXIST with :force" do
181
+ lambda{ Pa.mkdir("dir1/dira", :force) }.should_not raise_error(Errno::EEXIST)
182
+ end
200
183
  end
201
184
 
202
185
  describe "#_mktmpname" do
@@ -263,23 +246,14 @@ describe Pa do
263
246
  it "remove file" do
264
247
  Pa.rm "a"
265
248
  File.exists?("a").should be_false
266
- lambda{Pa.rm("dir")}.should raise_error(Errno::EISDIR)
267
249
  end
268
- end
269
250
 
270
- describe "#rm_f" do
271
- # rm family
272
- # a
273
- # dir/
274
- # dir1/
275
- # a
276
- before :each do
277
- FileUtils.mkdir_p %w[dir/dir1]
278
- FileUtils.touch %w[a dir/a]
251
+ it "raises Errno::EISDIR" do
252
+ lambda{ Pa.rm("dir") }.should raise_error(Errno::EISDIR)
279
253
  end
280
254
 
281
- it "remove file force" do
282
- lambda{Pa.rm_f("dir")}.should_not raise_error(Errno::EISDIR)
255
+ it "doens't raises Errno::EISDIR with :force" do
256
+ lambda{ Pa.rm("dir", :force => true) }.should_not raise_error(Errno::EISDIR)
283
257
  end
284
258
  end
285
259
 
@@ -297,12 +271,18 @@ describe Pa do
297
271
  it "remove directory" do
298
272
  Pa.rmdir "dir"
299
273
  File.exists?("dir").should be_false
300
- lambda{Pa.rmdir("a")}.should raise_error(Errno::ENOTDIR)
274
+ end
275
+
276
+ it "raises Errno::ENOTDIR" do
277
+ lambda{ Pa.rmdir("a") }.should raise_error(Errno::ENOTDIR)
278
+ end
279
+
280
+ it "doesn't raise Errno::ENOTDIR with :force" do
281
+ lambda{ Pa.rmdir_r("a", :force => true) }.should_not raise_error(Errno::ENOTDIR)
301
282
  end
302
283
  end
303
284
 
304
- describe "#rmdir_f" do
305
- # rm family
285
+ describe "#empty_dir" do
306
286
  # a
307
287
  # dir/
308
288
  # dir1/
@@ -312,8 +292,25 @@ describe Pa do
312
292
  FileUtils.touch %w[a dir/a]
313
293
  end
314
294
 
315
- it "remove directory force" do
316
- lambda{Pa.rmdir_r("a")}.should_not raise_error(Errno::ENOTDIR)
295
+ it "empty a directory" do
296
+ Pa.empty_dir "dir"
297
+ Dir.entries("dir").should == %w[. ..]
298
+ end
299
+
300
+ it "raises Errno::ENOTDIR" do
301
+ lambda{ Pa.empty_dir("a") }.should raise_error(Errno::ENOTDIR)
302
+ end
303
+
304
+ it "doesn't raise Errno::ENOTDIR with :force" do
305
+ lambda{ Pa.empty_dir("a", :force => true) }.should_not raise_error(Errno::ENOTDIR)
306
+ end
307
+
308
+ it "raises Errno::ENOENT" do
309
+ lambda{ Pa.empty_dir "ENOENT" }.should raise_error(Errno::ENOENT)
310
+ end
311
+
312
+ it "doesn't raise Errno::ENOENT with :force" do
313
+ lambda{ Pa.empty_dir("ENOENT", :force => true) }.should_not raise_error(Errno::ENOENT)
317
314
  end
318
315
  end
319
316
 
@@ -505,20 +502,25 @@ describe Pa do
505
502
  end
506
503
 
507
504
  describe "#mv" do
508
- # a b c
509
- # dir/ aa
505
+ # file1 with foo
506
+ # file2
507
+ # file3
508
+ # dir1/
509
+ # filea
510
+ # dir2/
511
+ # dir1/
510
512
  before :each do
511
513
  FileUtils.mkdir_p %w[dir]
512
514
  FileUtils.touch %w[a b c dir/aa]
513
- end
514
515
 
515
- it "mv a dir/" do
516
- Pa.mv "a", "dir"
517
- File.exists?("dir/a").should be_true
516
+ FileUtils.mkdir_p %w[dir1 dir2/dir1]
517
+ FileUtils.touch %w[file1 file2 file3 dir1/filea]
518
+ open("file1", "w") {|f| f.write("foo")}
518
519
  end
519
520
 
520
- it "mv a b .. file" do
521
- lambda{Pa.mv(%w(a b), "c")}.should raise_error(Errno::ENOTDIR)
521
+ it "mv file1 dir1" do
522
+ Pa.mv "file1", "dir1"
523
+ File.exists?("dir1/file1").should be_true
522
524
  end
523
525
 
524
526
  it "mv file1 file2 .. dir/" do
@@ -526,26 +528,21 @@ describe Pa do
526
528
  File.exists?("dir/a").should be_true
527
529
  File.exists?("dir/b").should be_true
528
530
  end
529
- end
530
531
 
531
- describe "#mv_f" do
532
- # file1 with foo
533
- # file2
534
- # dir1/
535
- # filea
536
- # dir2/
537
- # dir1/
538
- before :each do
539
- FileUtils.mkdir_p %w[dir1 dir2/dir1]
540
- FileUtils.touch %w[file1 file2 dir1/filea]
541
- open("file1", "w") {|f| f.write("foo")}
532
+ it "raises dest Errno::ENOTDIR" do
533
+ lambda{ Pa.mv(%w[file1 file2], "file3") }.should raise_error(Errno::ENOTDIR)
534
+ end
535
+
536
+ it "raises Errno::EEXIST" do
537
+ lambda{ Pa.mv("file1", "file2") }.should raise_error(Errno::EEXIST)
538
+ lambda{ Pa.mv("dir1", "dir2") }.should raise_error(Errno::EEXIST)
542
539
  end
543
540
 
544
- it "works" do
545
- Pa.mv_f "file1", "file2"
541
+ it "doesn't raise Errno::ENOTDIR with :force" do
542
+ lambda{ Pa.mv("file1", "file2", :force => true) }.should_not raise_error(Errno::EEXIST)
546
543
  File.read("file2").should == "foo"
547
544
 
548
- Pa.mv_f "dir1", "dir2"
545
+ lambda{ Pa.mv("dir1", "dir2", :force => true) }.should_not raise_error(Errno::EEXIST)
549
546
  File.exists?("dir2/dir1/filea").should be_true
550
547
  end
551
548
  end
@@ -55,6 +55,11 @@ describe Pa do
55
55
  it "#glob returns Pa instead" do
56
56
  Pa.glob("*")[0].should be_an_instance_of Pa
57
57
  end
58
+
59
+ it "remove . .." do
60
+ Pa.glob2(".*").should have(1).items
61
+ Pa.glob2("#{@tmpdir}/.*").should have(1).items
62
+ end
58
63
  end
59
64
 
60
65
  describe ".each2" do
@@ -196,8 +201,8 @@ describe Pa do
196
201
  end
197
202
 
198
203
  it "works" do
199
- Pa.ls2.should == %w[filea dira dirb]
200
- Pa.ls2("dira").should == %w[fileb]
204
+ Pa.ls2.sort.should == %w[dira dirb filea]
205
+ Pa.ls2("dira").sort.should == %w[fileb]
201
206
  end
202
207
 
203
208
  it "list multi paths" do
@@ -218,7 +223,7 @@ describe Pa do
218
223
  end
219
224
 
220
225
  it "call a block returns filtered result" do
221
- Pa.ls2 {|p| File.directory?(p)}.should == %w[dira dirb]
226
+ Pa.ls2 {|p| File.directory?(p)}.sort.should == %w[dira dirb]
222
227
  end
223
228
  end
224
229
 
@@ -235,8 +240,8 @@ describe Pa do
235
240
  end
236
241
 
237
242
  it "works" do
238
- Pa.ls2_r.should == %w[filea dira dira/fileb dirb dirb/dirb1 dirb/dirb1/fileb1]
239
- Pa.ls2_r("dirb").should == %w[dirb1 dirb1/fileb1]
243
+ Pa.ls2_r.sort.should == %w[dira dira/fileb dirb dirb/dirb1 dirb/dirb1/fileb1 filea]
244
+ Pa.ls2_r("dirb").sort.should == %w[dirb1 dirb1/fileb1]
240
245
  end
241
246
 
242
247
  it "list multi paths" do
@@ -257,7 +262,7 @@ describe Pa do
257
262
  end
258
263
 
259
264
  it "call a block returns filtered result" do
260
- Pa.ls2_r {|p, fn| File.directory?(p)}.should == %w[dira dirb dirb/dirb1]
265
+ Pa.ls2_r {|p, fn| File.directory?(p)}.sort.should == %w[dira dirb dirb/dirb1]
261
266
  end
262
267
  end
263
268
 
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "bundler/setup"
2
+ require "stringio"
1
3
  require "pa"
2
4
 
3
5
  $specdir = Pa.dir(__FILE__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pa
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-23 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-08-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pd
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: ! 'a path library for Ruby
15
31
 
16
32
  '
@@ -21,6 +37,7 @@ extra_rdoc_files: []
21
37
  files:
22
38
  - .gitignore
23
39
  - .rspec
40
+ - .travis.yml
24
41
  - Gemfile
25
42
  - Gemfile.lock
26
43
  - README.md
@@ -54,6 +71,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
71
  - - ! '>='
55
72
  - !ruby/object:Gem::Version
56
73
  version: '0'
74
+ segments:
75
+ - 0
76
+ hash: 1635973500048505799
57
77
  required_rubygems_version: !ruby/object:Gem::Requirement
58
78
  none: false
59
79
  requirements:
@@ -62,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
82
  version: '0'
63
83
  requirements: []
64
84
  rubyforge_project: xx
65
- rubygems_version: 1.8.11
85
+ rubygems_version: 1.8.21
66
86
  signing_key:
67
87
  specification_version: 3
68
88
  summary: a path library for Ruby