fs 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.mdown CHANGED
@@ -49,8 +49,20 @@ commands (unsorted).
49
49
 
50
50
  ## Todo
51
51
 
52
- - Aliases
53
- - list(dir, '*.rb')
52
+ Here is my mind … here is my mind …
53
+
54
+ - maybe use underscores (remove_dir)
55
+ - remove! to force something
56
+ - maybe makedir! to mkdir -p
57
+ - use Find#find in FS#find ;)
58
+ - some kind of #tree output
59
+ - file type
60
+ - exist?
61
+ - list_dirs
62
+ - list_files
63
+ - find_dirs
64
+ - find_files
65
+ - FS.link('a.txt' => 'b.txt') with a hash
54
66
 
55
67
  ## BTW
56
68
 
data/lib/fs.rb CHANGED
@@ -3,8 +3,10 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) unless $LOAD_PATH.include?(File.dirna
3
3
  require 'fileutils'
4
4
  require 'etc'
5
5
  require 'fs/base'
6
+ require 'fs/alias'
6
7
 
7
8
  module FS
9
+ include FS::Base
10
+ include FS::Alias
8
11
  extend self
9
- extend FS::Base
10
12
  end
data/lib/fs/alias.rb ADDED
@@ -0,0 +1,26 @@
1
+ module FS
2
+ module Alias
3
+ ALIASES = {
4
+ :ls => :list,
5
+ :mkdir => :makedir,
6
+ :mkdir_p => :makedirs,
7
+ :rmdir => :removedir,
8
+ :rm_r => :removedirs,
9
+ :cd => :changedir,
10
+ :mv => :move,
11
+ :cp => :copy,
12
+ :rm => :remove,
13
+ :cat => :read,
14
+ :ln => :link,
15
+ }
16
+
17
+ def self.included(base)
18
+ base.class_eval do
19
+ ALIASES.each do |shortcut, original|
20
+ alias_method shortcut, original
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+ end
data/lib/fs/base.rb CHANGED
@@ -1,20 +1,34 @@
1
1
  module FS
2
2
  module Base
3
+
3
4
  # FileUtils.touch
4
- def touch(files)
5
+ def touch(*files)
5
6
  FileUtils.touch(files)
6
7
  end
7
8
 
8
9
  # FileUtils#mkdir
9
- def makedir(dirs)
10
+ def makedir(*dirs)
10
11
  FileUtils.mkdir(dirs)
11
12
  end
12
13
 
13
14
  # FileUtils#mkdir_p
14
- def makedirs(dirs)
15
+ def makedirs(*dirs)
15
16
  FileUtils.mkdir_p(dirs)
16
17
  end
17
18
 
19
+ # FileUtils#rmdir
20
+ def removedir(*dirs)
21
+ dirs.each do |dir|
22
+ raise Errno::ENOTEMPTY unless list(dir).empty?
23
+ end
24
+ FileUtils.rmdir(dirs)
25
+ end
26
+
27
+ # FileUtils#rm_r
28
+ def removedirs(*dirs)
29
+ FileUtils.rm_r(dirs)
30
+ end
31
+
18
32
  # Dir#glob
19
33
  def list(dir='.', pattern='*')
20
34
  glob(dir, pattern)
@@ -26,13 +40,33 @@ module FS
26
40
  glob(dir, '**', pattern)
27
41
  end
28
42
 
43
+ # TODO: find time to make this cool, not work only
29
44
  # FileUtils#mv
30
45
  def move(*froms, to)
31
46
  froms.each do |from|
32
- FileUtils.mv(from, to)
47
+ FileUtils.mv(from, to)
33
48
  end
34
49
  end
35
50
 
51
+ # TODO: find time to make this cool, not work only
52
+ # FileUtils#cp
53
+ def copy(*froms, to)
54
+ froms.each do |from|
55
+ FileUtils.cp(from, to)
56
+ end
57
+ end
58
+
59
+ # Creates hard links for files and symbolic links for dirs
60
+ #
61
+ # FileUtils#ln or FileUtils#ln_s
62
+ def link(from, to)
63
+ if File.directory?(from)
64
+ FileUtils.ln_s(from, to)
65
+ else
66
+ FileUtils.ln(from, to)
67
+ end
68
+ end
69
+
36
70
  # FileUtils#rm
37
71
  def remove(*pathes)
38
72
  FileUtils.rm(pathes)
@@ -68,6 +102,16 @@ module FS
68
102
  '/'
69
103
  end
70
104
 
105
+ # Dir#chdir
106
+ def changedir(dir)
107
+ Dir.chdir(dir)
108
+ end
109
+
110
+ # Dir#pwd
111
+ def currentdir
112
+ Dir.pwd
113
+ end
114
+
71
115
  private
72
116
 
73
117
  def assert_dir(path)
@@ -75,9 +119,11 @@ module FS
75
119
  end
76
120
 
77
121
  def glob(dir, *patterns)
78
- Dir.glob(File.join(dir, patterns)).map do |path|
79
- path.gsub(/^\.?#{dir}\/?/, '')
122
+ fulldir = File.expand_path(dir)
123
+ Dir.glob(File.join(fulldir, patterns)).map do |path|
124
+ path.gsub(/^#{fulldir}\/?/, '')
80
125
  end
81
126
  end
127
+
82
128
  end
83
129
  end
data/lib/fs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module FS
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe FS::Alias do
4
+
5
+ FS::Alias::ALIASES.each do |shortcut, original|
6
+ it "#{shortcut} exists for #{original}" do
7
+ FS.respond_to?(shortcut).should be_true
8
+ FS.respond_to?(original).should be_true
9
+ s = FS.method(shortcut)
10
+ o = FS.method(original)
11
+ s.receiver.should eql(o.receiver)
12
+ s.parameters.should eql(o.parameters)
13
+ end
14
+ end
15
+
16
+ end
17
+
data/spec/base_spec.rb CHANGED
@@ -9,7 +9,13 @@ describe FS::Base do
9
9
  File.exist?(file).should be_true
10
10
  end
11
11
 
12
- it 'touches a list of files' do
12
+ it 'accepts multiple files' do
13
+ FS.touch('foo.txt', 'bar.txt')
14
+ File.exist?('foo.txt').should be_true
15
+ File.exist?('bar.txt').should be_true
16
+ end
17
+
18
+ it 'accepts a list of files' do
13
19
  files = ['foo.txt', 'bar.txt']
14
20
  FS.touch(files)
15
21
  files.each do |file|
@@ -24,6 +30,12 @@ describe FS::Base do
24
30
  File.directory?('foo').should be_true
25
31
  end
26
32
 
33
+ it 'accepts multiple dirs' do
34
+ FS.makedir('foo', 'bar')
35
+ File.directory?('foo').should be_true
36
+ File.directory?('bar').should be_true
37
+ end
38
+
27
39
  it 'fails if a parent dir is missing' do
28
40
  lambda {FS.makedir('foo/bar')}.should raise_error
29
41
  end
@@ -36,6 +48,43 @@ describe FS::Base do
36
48
  File.directory?('foo/bar').should be_true
37
49
  File.directory?('foo/bar/baz').should be_true
38
50
  end
51
+
52
+ it 'accepts multiple dirs' do
53
+ FS.makedirs('foo/bar', 'baz/yep')
54
+ File.directory?('foo').should be_true
55
+ File.directory?('foo/bar').should be_true
56
+ File.directory?('baz').should be_true
57
+ File.directory?('baz/yep').should be_true
58
+ end
59
+ end
60
+
61
+ describe 'removedir' do
62
+ it 'removes a dir' do
63
+ FS.makedir('foo')
64
+ FS.removedir('foo')
65
+ File.exist?('foo').should be_false
66
+ end
67
+
68
+ it 'fails if dir not empty' do
69
+ FS.makedirs('foo/dir')
70
+ FS.touch('foo/file')
71
+ lambda {FS.removedir('foo')}.should raise_error
72
+ end
73
+ end
74
+
75
+ describe 'removedirs' do
76
+ it 'removes a dir' do
77
+ FS.makedir('foo')
78
+ FS.removedirs('foo')
79
+ File.exist?('foo').should be_false
80
+ end
81
+
82
+ it 'removes a dir even if something is inside' do
83
+ FS.makedirs('foo/dir')
84
+ FS.touch('foo/file')
85
+ FS.removedirs('foo')
86
+ File.exist?('foo').should be_false
87
+ end
39
88
  end
40
89
 
41
90
  describe 'list' do
@@ -120,7 +169,7 @@ describe FS::Base do
120
169
  end
121
170
 
122
171
  describe 'move' do
123
- it 'rename a file' do
172
+ it 'renames a file' do
124
173
  FS.touch('foo.txt')
125
174
  FS.move('foo.txt', 'bar.txt')
126
175
  FS.list.should eql(['bar.txt'])
@@ -144,6 +193,46 @@ describe FS::Base do
144
193
  end
145
194
  end
146
195
 
196
+ describe 'copy' do
197
+ it 'copies a file' do
198
+ FS.write('foo.txt', 'lala')
199
+ FS.copy('foo.txt', 'bar.txt')
200
+ File.exist?('foo.txt').should be_true
201
+ File.exist?('bar.txt').should be_true
202
+ end
203
+
204
+ it 'copies the content' do
205
+ FS.write('foo.txt', 'lala')
206
+ FS.copy('foo.txt', 'bar.txt')
207
+ FS.read('foo.txt').should eql('lala')
208
+ FS.read('bar.txt').should eql('lala')
209
+ end
210
+
211
+ it 'copies a file to a dir' do
212
+ FS.write('foo.txt', 'lala')
213
+ FS.makedir('dir')
214
+ FS.copy('foo.txt', 'dir/bar.txt')
215
+ File.exist?('foo.txt').should be_true
216
+ File.exist?('dir/bar.txt').should be_true
217
+ end
218
+ end
219
+
220
+ describe 'link' do
221
+ it 'links to files' do
222
+ FS.touch('foo.txt')
223
+ FS.link('foo.txt', 'bar.txt')
224
+ FS.write('foo.txt', 'lala')
225
+ FS.read('bar.txt').should eql('lala')
226
+ end
227
+
228
+ it 'links to dirs' do
229
+ FS.makedir('foo')
230
+ FS.link('foo', 'bar')
231
+ FS.touch('foo/file')
232
+ FS.list('bar').should eql(['file'])
233
+ end
234
+ end
235
+
147
236
  describe 'remove' do
148
237
  it 'removes files' do
149
238
  FS.touch('file')
@@ -210,12 +299,27 @@ describe FS::Base do
210
299
  FS.home('root').should match(/\/root$/)
211
300
  end
212
301
  end
213
-
214
- # describe 'removedir'
215
- # describe 'removedirs'
216
- # describe 'currentdir'
217
- # describe 'changedir'
218
- # describe 'copy'
219
- # describe 'link'
220
302
 
303
+ describe 'currentdir' do
304
+ it 'returns the current dir' do
305
+ FS.currentdir.should eql(@test_dir)
306
+ end
307
+
308
+ it 'works after dir was changed' do
309
+ here = FS.currentdir
310
+ FS.makedir('foo')
311
+ Dir.chdir('foo')
312
+ FS.currentdir.should eql(File.join(here, 'foo'))
313
+ end
314
+ end
315
+
316
+ describe 'changedir' do
317
+ it 'change the current dir' do
318
+ here = Dir.pwd
319
+ FS.makedir('foo')
320
+ FS.changedir('foo')
321
+ Dir.pwd.should eql(File.join(here, 'foo'))
322
+ end
323
+ end
324
+
221
325
  end
@@ -25,6 +25,7 @@ module FS
25
25
  end
26
26
  end
27
27
  end
28
+
28
29
  end
29
30
  end
30
31
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: fs
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Bernd J\xC3\xBCnger"
@@ -62,8 +62,10 @@ files:
62
62
  - Rakefile
63
63
  - fs.gemspec
64
64
  - lib/fs.rb
65
+ - lib/fs/alias.rb
65
66
  - lib/fs/base.rb
66
67
  - lib/fs/version.rb
68
+ - spec/alias_spec.rb
67
69
  - spec/base_spec.rb
68
70
  - spec/spec_helper.rb
69
71
  - spec/support/test_dir_support.rb
@@ -97,6 +99,7 @@ signing_key:
97
99
  specification_version: 3
98
100
  summary: Work with your filesystem!
99
101
  test_files:
102
+ - spec/alias_spec.rb
100
103
  - spec/base_spec.rb
101
104
  - spec/spec_helper.rb
102
105
  - spec/support/test_dir_support.rb