fs 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -2
- data/.travis.yml +2 -1
- data/README.mdown +2 -1
- data/Rakefile +1 -7
- data/fs.gemspec +3 -5
- data/lib/fs/base.rb +36 -17
- data/lib/fs/find.rb +2 -2
- data/lib/fs/version.rb +1 -1
- data/spec/base_spec.rb +43 -9
- data/spec/support/test_dir_support.rb +2 -2
- metadata +13 -35
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.mdown
CHANGED
data/Rakefile
CHANGED
@@ -3,10 +3,4 @@ Bundler::GemHelper.install_tasks
|
|
3
3
|
|
4
4
|
require 'rspec/core/rake_task'
|
5
5
|
RSpec::Core::RakeTask.new
|
6
|
-
task :default => :spec
|
7
|
-
|
8
|
-
require 'yard'
|
9
|
-
YARD::Rake::YardocTask.new do |t|
|
10
|
-
t.files = ['lib/**/*.rb']
|
11
|
-
t.options = ["--title", "FS #{FS::VERSION}"]
|
12
|
-
end
|
6
|
+
task :default => :spec
|
data/fs.gemspec
CHANGED
@@ -17,8 +17,6 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
|
20
|
-
s.add_development_dependency 'rake'
|
21
|
-
s.add_development_dependency 'rspec'
|
22
|
-
|
23
|
-
s.add_development_dependency 'bluecloth', '2.2.0'
|
24
|
-
end
|
20
|
+
s.add_development_dependency 'rake'
|
21
|
+
s.add_development_dependency 'rspec'
|
22
|
+
end
|
data/lib/fs/base.rb
CHANGED
@@ -19,7 +19,7 @@ module FS
|
|
19
19
|
# FileUtils#rmdir
|
20
20
|
def removedir(*dirs)
|
21
21
|
dirs.each do |dir|
|
22
|
-
raise Errno::ENOTEMPTY unless
|
22
|
+
raise Errno::ENOTEMPTY unless empty?(dir)
|
23
23
|
end
|
24
24
|
FileUtils.rmdir(dirs)
|
25
25
|
end
|
@@ -31,15 +31,15 @@ module FS
|
|
31
31
|
|
32
32
|
# Dir#glob
|
33
33
|
def list(dir='.', pattern='*')
|
34
|
-
glob(dir, pattern
|
34
|
+
glob(dir, pattern)
|
35
35
|
end
|
36
36
|
|
37
37
|
def list_dirs(dir='.', pattern='*')
|
38
|
-
glob(dir, pattern
|
38
|
+
glob(dir, pattern) {|path| FS.directory?(path) }
|
39
39
|
end
|
40
40
|
|
41
41
|
def list_files(dir='.', pattern='*')
|
42
|
-
glob(dir, pattern
|
42
|
+
glob(dir, pattern) {|path| FS.file?(path) }
|
43
43
|
end
|
44
44
|
|
45
45
|
# TODO: find time to make this cool, not work only
|
@@ -83,10 +83,10 @@ module FS
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
# File#open(file, 'r')
|
86
|
+
# File#open(file, 'r').read
|
87
87
|
def read(file, &block)
|
88
88
|
if block_given?
|
89
|
-
File.open(file, 'r'
|
89
|
+
File.open(file, 'r') {|f| block[f.read] }
|
90
90
|
else
|
91
91
|
content = nil
|
92
92
|
File.open(file, 'r') {|f| content = f.read }
|
@@ -94,6 +94,28 @@ module FS
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
+
# File#open(file, 'r').each
|
98
|
+
def read_lines(file, &block)
|
99
|
+
if block_given?
|
100
|
+
File.open(file, 'r').each {|line| line.chomp!; block[line] }
|
101
|
+
else
|
102
|
+
lines = []
|
103
|
+
File.open(file, 'r').each {|line| line.chomp!; lines << line }
|
104
|
+
lines
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# File#open(file, 'r') =~ /REGEX/
|
109
|
+
def grep(file, regexp, &block)
|
110
|
+
if block_given?
|
111
|
+
File.open(file, 'r').each {|line| line.chomp!; block[line] if line =~ regexp }
|
112
|
+
else
|
113
|
+
lines = []
|
114
|
+
File.open(file, 'r').each {|line| line.chomp!; lines << line if line =~ regexp }
|
115
|
+
lines
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
97
119
|
# Dir#home
|
98
120
|
# the path is always expanded
|
99
121
|
def home(user=nil)
|
@@ -156,10 +178,10 @@ module FS
|
|
156
178
|
# uses File.size and Dir.entries
|
157
179
|
# for files it returns `nil` if file does not exist, `true` if it's empty
|
158
180
|
def empty?(path)
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
181
|
+
raise Errno::ENOENT unless File.exist?(path)
|
182
|
+
if File.directory?(path)
|
183
|
+
files = Dir.entries(path)
|
184
|
+
files.size == 2 && files.sort == ['.', '..']
|
163
185
|
else
|
164
186
|
File.size(path) == 0
|
165
187
|
end
|
@@ -217,7 +239,8 @@ module FS
|
|
217
239
|
return '' if path == '/' || path == '.'
|
218
240
|
base = File.basename(path)
|
219
241
|
ext = File.extname(path)
|
220
|
-
|
242
|
+
return base if ext.empty?
|
243
|
+
base[0...-ext.size]
|
221
244
|
end
|
222
245
|
|
223
246
|
# "tmp/foo/bar.todo" => ["tmp/foo", "bar", ".todo"]
|
@@ -239,16 +262,12 @@ module FS
|
|
239
262
|
|
240
263
|
private
|
241
264
|
|
242
|
-
def
|
243
|
-
raise "not a directory: #{path}" unless File.directory?(path)
|
244
|
-
end
|
245
|
-
|
246
|
-
def glob(dir, *patterns, condition)
|
265
|
+
def glob(dir, *patterns, &block)
|
247
266
|
fulldir = File.expand_path(dir)
|
248
267
|
regexp = /^#{Regexp.escape(fulldir)}\/?/
|
249
268
|
Dir.
|
250
269
|
glob(File.join(fulldir, patterns)).
|
251
|
-
select {|path|
|
270
|
+
select {|path| block.nil? || block[path] }.
|
252
271
|
map {|path| path.gsub(regexp, '') }.
|
253
272
|
sort
|
254
273
|
end
|
data/lib/fs/find.rb
CHANGED
@@ -29,12 +29,12 @@ module FS
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def find_dirs(dir, options={}, &block)
|
32
|
-
condition = ->(path){FileTest.directory?(path)}
|
32
|
+
condition = ->(path){ FileTest.directory?(path) }
|
33
33
|
find(dir, options.merge(:condition => condition), &block)
|
34
34
|
end
|
35
35
|
|
36
36
|
def find_files(dir, options={}, &block)
|
37
|
-
condition = ->(path){!FileTest.directory?(path)}
|
37
|
+
condition = ->(path){ !FileTest.directory?(path) }
|
38
38
|
find(dir, options.merge(:condition => condition), &block)
|
39
39
|
end
|
40
40
|
|
data/lib/fs/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -37,7 +37,7 @@ describe FS::Base do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'fails if a parent dir is missing' do
|
40
|
-
|
40
|
+
->{ FS.makedir('foo/bar') }.should raise_error
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -68,7 +68,7 @@ describe FS::Base do
|
|
68
68
|
it 'fails if dir not empty' do
|
69
69
|
FS.makedirs('foo/dir')
|
70
70
|
FS.touch('foo/file')
|
71
|
-
|
71
|
+
->{ FS.removedir('foo') }.should raise_error(Errno::ENOTEMPTY)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
@@ -269,14 +269,14 @@ describe FS::Base do
|
|
269
269
|
|
270
270
|
it 'fails on dirs' do
|
271
271
|
FS.makedir('dir')
|
272
|
-
|
272
|
+
->{ FS.remove('dir') }.should raise_error
|
273
273
|
end
|
274
274
|
|
275
275
|
# FIXME: fakefs
|
276
276
|
# it 'fails if the dir is not empty' do
|
277
277
|
# FS.makedir('/foo')
|
278
278
|
# FS.touch('/foo/bar')
|
279
|
-
#
|
279
|
+
# ->{ FS.remove('/foo') }.should raise_error
|
280
280
|
# end
|
281
281
|
end
|
282
282
|
|
@@ -293,14 +293,48 @@ describe FS::Base do
|
|
293
293
|
end
|
294
294
|
|
295
295
|
describe '::read' do
|
296
|
-
|
296
|
+
before do
|
297
297
|
File.open('foo.txt', 'w') {|f| f.write 'bar' }
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'reads the content to a string' do
|
298
301
|
FS.read('foo.txt').should eql('bar')
|
299
302
|
end
|
300
303
|
|
301
|
-
it '
|
302
|
-
|
303
|
-
|
304
|
+
it 'yields the content to a block' do
|
305
|
+
FS.read('foo.txt') {|content| content.should eql('bar')}
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
describe '::read_lines' do
|
310
|
+
before do
|
311
|
+
File.open('foo.txt', 'w') {|f| f.write "bar\nbaz\nqux" }
|
312
|
+
end
|
313
|
+
|
314
|
+
it 'reads all lines to an array' do
|
315
|
+
FS.read_lines('foo.txt').should eql(['bar', 'baz', 'qux'])
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'yields all lines to a block' do
|
319
|
+
lines = []
|
320
|
+
FS.read_lines('foo.txt') {|line| lines << line}
|
321
|
+
lines.should eql(['bar', 'baz', 'qux'])
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
describe '::grep' do
|
326
|
+
before do
|
327
|
+
File.open('foo.txt', 'w') {|f| f.write "this\nis\nthe\ntest" }
|
328
|
+
end
|
329
|
+
|
330
|
+
it 'greps matching lines to an array' do
|
331
|
+
FS.grep('foo.txt', /is/).should eql(['this', 'is'])
|
332
|
+
end
|
333
|
+
|
334
|
+
it 'yields matching lines to a block' do
|
335
|
+
lines = []
|
336
|
+
FS.grep('foo.txt', /is/) {|line| lines << line}
|
337
|
+
lines.should eql(['this', 'is'])
|
304
338
|
end
|
305
339
|
end
|
306
340
|
|
@@ -415,7 +449,7 @@ TXT
|
|
415
449
|
describe '::empty?' do
|
416
450
|
it 'returns nil if the path does not exist' do
|
417
451
|
FS.exist?('foobar').should be_false
|
418
|
-
FS.empty?('foobar').should
|
452
|
+
->{ FS.empty?('foobar') }.should raise_error(Errno::ENOENT)
|
419
453
|
end
|
420
454
|
|
421
455
|
it 'returns if a file is empty' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,52 +9,30 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70120123039860 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0
|
21
|
+
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70120123039860
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70120123039440 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ! '>='
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
32
|
+
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: yard
|
38
|
-
requirement: &2158693380 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - =
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 0.7.2
|
44
|
-
type: :development
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *2158693380
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: bluecloth
|
49
|
-
requirement: &2158692920 !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
|
-
requirements:
|
52
|
-
- - =
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: 2.2.0
|
55
|
-
type: :development
|
56
|
-
prerelease: false
|
57
|
-
version_requirements: *2158692920
|
35
|
+
version_requirements: *70120123039440
|
58
36
|
description: FS gathers the cluttered methods for working with files and dirs. Internally
|
59
37
|
using the good old standard library, but providing simple methods in a single place.
|
60
38
|
email:
|
@@ -95,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
73
|
version: '0'
|
96
74
|
segments:
|
97
75
|
- 0
|
98
|
-
hash:
|
76
|
+
hash: -2961947820199174561
|
99
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
78
|
none: false
|
101
79
|
requirements:
|
@@ -104,10 +82,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
82
|
version: '0'
|
105
83
|
segments:
|
106
84
|
- 0
|
107
|
-
hash:
|
85
|
+
hash: -2961947820199174561
|
108
86
|
requirements: []
|
109
87
|
rubyforge_project:
|
110
|
-
rubygems_version: 1.8.
|
88
|
+
rubygems_version: 1.8.11
|
111
89
|
signing_key:
|
112
90
|
specification_version: 3
|
113
91
|
summary: Work with your filesystem!
|