fs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fs.gemspec
4
+ gemspec
data/README.mdown ADDED
@@ -0,0 +1,58 @@
1
+ # FS (FileSystem)
2
+
3
+ Work with your filesystem!
4
+
5
+ ## Problem
6
+
7
+ This Gem shouldn't reinvent the wheel or be a replacement.
8
+ But in Ruby working with the filesystem really hurts!
9
+
10
+ In your toolbox are at least `File`, `Dir`, `FileUtils`, `Find`, and maybe some more.
11
+ Good tools, but to complicated for most cases.
12
+ It's not about piping, or copying the shell as it is.
13
+ But think about a simple `ls` in the shell, than how you would do this in Ruby.
14
+ Got the idea?
15
+
16
+ ## Solution
17
+
18
+ `FS` gathers the cluttered methods for working with files and dirs. Internally
19
+ using the good old standard library, but providing simple methods in a single place.
20
+
21
+ ## Examples
22
+
23
+ FS.changedir('~/Projects/fs')
24
+ ==> "/Users/bjuenger/Projects/fs"
25
+ FS.list('.')
26
+ ==> [".git", ".gitignore", ".rvmrc", "fs.gemspec", "Gemfile", "Gemfile.lock", "lib", "Rakefile", "README.mdown", "spec"]
27
+ FS.makedirs('tmp/demo')
28
+ FS.touch('tmp/demo/newfile.txt')
29
+ FS.list('tmp/demo')
30
+ ==> ["newfile.txt"]
31
+ FS.remove('tmp/demo/newfile.txt')
32
+ FS.list('tmp/demo')
33
+ ==> []
34
+
35
+ ## Aliases
36
+
37
+ Although verbose method names are good, there are some aliases for unix shell
38
+ commands (unsorted).
39
+
40
+ - ls => list
41
+ - mkdir => makedir
42
+ - mkdir_p => makedirs
43
+ - cd => changedir
44
+ - mv => move
45
+ - cp => copy
46
+ - rm => remove
47
+ - ln => link
48
+ - cat => read (no concatenate)
49
+
50
+ ## Todo
51
+
52
+ - Aliases
53
+ - list(dir, '*.rb')
54
+
55
+ ## BTW
56
+
57
+ If you need a replacement for the shell in pure ruby, than have a look at
58
+ [Rush](http://rush.heroku.com/).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+ task :default => :spec
data/fs.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fs/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fs"
7
+ s.version = FS::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bernd Jünger"]
10
+ s.email = ["blindgaenger@gmail.com"]
11
+ s.homepage = "http://github.com/blindgaenger/fs"
12
+ s.summary = %q{Work with your filesystem!}
13
+ s.description = %q{FS gathers the cluttered methods for working with files and dirs. Internally using the good old standard library, but providing simple methods in a single place.}
14
+
15
+ s.add_development_dependency 'rspec', '2.5.0'
16
+ s.add_development_dependency 'fakefs', '0.3.1'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
data/lib/fs.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'fileutils'
2
+
3
+ module FS
4
+ extend self
5
+
6
+ # FileUtils.touch
7
+ def touch(files)
8
+ FileUtils.touch(files)
9
+ end
10
+
11
+ # FileUtils#mkdir
12
+ def makedir(dirs)
13
+ FileUtils.mkdir(dirs)
14
+ end
15
+
16
+ # FileUtils#mkdir_p
17
+ def makedirs(dirs)
18
+ FileUtils.mkdir_p(dirs)
19
+ end
20
+
21
+ # Dir#entries
22
+ def list(dir, pattern='*')
23
+ glob(dir, pattern)
24
+ end
25
+
26
+ # Find#find
27
+ def find(dir, pattern='*')
28
+ glob(dir, '**', pattern)
29
+ end
30
+
31
+ # FileUtils#mv
32
+ def move(*froms, to)
33
+ froms.each do |from|
34
+ FileUtils.mv(from, to)
35
+ end
36
+ end
37
+
38
+ # FileUtils#rm
39
+ def remove(*pathes)
40
+ FileUtils.rm(pathes, :verbose => true)
41
+ end
42
+
43
+ # File#open(file, 'w')
44
+ def write(file, content=nil, &block)
45
+ if block_given?
46
+ File.open(file, 'w', &block)
47
+ else
48
+ File.open(file, 'w') {|f| f.write(content) }
49
+ end
50
+ end
51
+
52
+ # File#open(file, 'r')
53
+ def read(file, &block)
54
+ if block_given?
55
+ File.open(file, 'r', &block)
56
+ else
57
+ content = nil
58
+ File.open(file, 'r') {|f| content = f.read }
59
+ content
60
+ end
61
+ end
62
+
63
+
64
+ def root
65
+ '/'
66
+ end
67
+
68
+ def home(user=nil)
69
+ Dir.home(user)
70
+ end
71
+
72
+ private
73
+
74
+ def glob(dir, *patterns)
75
+ Dir.glob(File.join(dir, patterns)).map do |path|
76
+ path.gsub(/^\.?#{dir}\/?/, '')
77
+ end
78
+ end
79
+
80
+ end
data/lib/fs/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module FS
2
+ VERSION = "0.0.1"
3
+ end
data/spec/fs_spec.rb ADDED
@@ -0,0 +1,196 @@
1
+ require 'spec_helper'
2
+
3
+ describe FS do
4
+
5
+ describe 'touch' do
6
+ it 'touches a single file' do
7
+ file = '/foobar.txt'
8
+ FS.touch(file)
9
+ File.exist?(file).should be_true
10
+ end
11
+
12
+ it 'touches a list of files' do
13
+ files = ['/foo.txt', '/bar.txt']
14
+ FS.touch(files)
15
+ files.each do |file|
16
+ File.exist?(file).should be_true
17
+ end
18
+ end
19
+ end
20
+
21
+ describe 'makedir' do
22
+ it 'creates a dir' do
23
+ FS.makedir('/foo')
24
+ File.directory?('/foo').should be_true
25
+ end
26
+
27
+ it 'fails if a parent dir is missing' do
28
+ lambda {FS.makedir('/foo/bar')}.should raise_error
29
+ end
30
+ end
31
+
32
+ describe 'makedirs' do
33
+ it 'creates all missing parent dirs' do
34
+ FS.makedirs '/foo/bar/baz'
35
+ File.directory?('/foo').should be_true
36
+ File.directory?('/foo/bar').should be_true
37
+ File.directory?('/foo/bar/baz').should be_true
38
+ end
39
+ end
40
+
41
+ describe 'list' do
42
+ it 'returns an empty list if there are no files' do
43
+ FS.list('/').should be_empty
44
+ end
45
+
46
+ it 'lists all files and dirs (without . and ..)' do
47
+ FS.touch('/foo')
48
+ FS.makedir('/bar')
49
+ FS.list('/').should eql(['foo', 'bar'])
50
+ end
51
+
52
+ it 'globs files and dirs' do
53
+ FS.touch('/file.txt')
54
+ FS.touch('/file.rb')
55
+ FS.makedir('/dir.txt')
56
+ FS.makedir('/dir.rb')
57
+ FS.list('/', '*.txt').should eql(['file.txt', 'dir.txt'])
58
+ end
59
+
60
+ it 'lists files and dirs in a subdir' do
61
+ FS.makedir('/foo')
62
+ FS.makedir('/foo/dir')
63
+ FS.touch('/foo/file')
64
+ FS.list('/foo').should eql(['dir', 'file'])
65
+ end
66
+
67
+ it 'globs files and dirs in a subdir' do
68
+ FS.makedir('/foo')
69
+ FS.touch('/foo/file.txt')
70
+ FS.touch('/foo/file.rb')
71
+ FS.makedir('/foo/dir.txt')
72
+ FS.makedir('/foo/dir.rb')
73
+ FS.list('/foo', '*.txt').should eql(['dir.txt', 'file.txt'])
74
+ end
75
+ end
76
+
77
+ describe 'find' do
78
+ it 'returns an empty list if there are no files' do
79
+ FS.list('/').should be_empty
80
+ end
81
+
82
+ it 'finds files in all subdirs' do
83
+ FS.makedirs('/one/two/three')
84
+ FS.touch('/one/file.one')
85
+ FS.touch('/one/two/three/file.three')
86
+ FS.find('/').should eql([
87
+ 'one',
88
+ 'one/file.one',
89
+ 'one/two',
90
+ 'one/two/three',
91
+ 'one/two/three/file.three'
92
+ ])
93
+ end
94
+
95
+ it 'globs files in all subdirs' do
96
+ FS.makedirs('/one/two/three')
97
+ FS.touch('/one/file.one')
98
+ FS.touch('/one/two/three/file.three')
99
+ FS.find('/', 'file.*').should eql([
100
+ 'one/file.one',
101
+ 'one/two/three/file.three'
102
+ ])
103
+ end
104
+ end
105
+
106
+ describe 'move' do
107
+ it 'rename a file' do
108
+ FS.touch('/foo.txt')
109
+ FS.move('/foo.txt', '/bar.txt')
110
+ FS.list('/').should eql(['bar.txt'])
111
+ end
112
+
113
+ # FIXME: fakefs
114
+ # it 'moves a file' do
115
+ # FS.touch('/foo.txt')
116
+ # FS.makedirs('/tmp')
117
+ # FS.move('/foo.txt', '/tmp')
118
+ # FS.list('/').should eql(['tmp'])
119
+ # FS.list('/tmp').should eql(['foo.txt'])
120
+ # end
121
+
122
+ # FIXME: fakefs
123
+ # it 'moves files and dirs' do
124
+ # FS.touch('/file')
125
+ # FS.makedir('/dir')
126
+ # FS.makedir('/tmp')
127
+ # FS.move('/file', '/dir', '/tmp')
128
+ # FS.list('/').should eql(['tmp'])
129
+ # FS.list('/tmp').should eql(['file', 'dir'])
130
+ # end
131
+ end
132
+
133
+ describe 'remove' do
134
+ it 'removes files and dirs' do
135
+ FS.touch('/file')
136
+ FS.makedir('/dir')
137
+ FS.remove('/file', '/dir')
138
+ FS.list('/').should be_empty
139
+ end
140
+
141
+ # FIXME: fakefs
142
+ # it 'fails if the dir is not empty' do
143
+ # FS.makedir('/foo')
144
+ # FS.touch('/foo/bar')
145
+ # lambda {FS.remove('/foo')}.should raise_error
146
+ # end
147
+ end
148
+
149
+ describe 'write' do
150
+ it 'writes content from a string' do
151
+ FS.write('/foo.txt', 'bar')
152
+ File.open('/foo.txt').read.should eql('bar')
153
+ end
154
+
155
+ it 'writes content from a block' do
156
+ FS.write('/foo.txt') {|f| f.write 'bar' }
157
+ File.open('/foo.txt').read.should eql('bar')
158
+ end
159
+ end
160
+
161
+ describe 'read' do
162
+ it 'reads the content to a string' do
163
+ File.open('/foo.txt', 'w') {|f| f.write 'bar' }
164
+ FS.read('/foo.txt').should eql('bar')
165
+ end
166
+
167
+ it 'reads the content to a block' do
168
+ File.open('/foo.txt', 'w') {|f| f.write 'bar' }
169
+ FS.read('/foo.txt') {|f| f.read.should eql('bar')}
170
+ end
171
+ end
172
+
173
+ describe 'root' do
174
+ it 'always returns /' do
175
+ FS.root.should eql('/')
176
+ end
177
+ end
178
+
179
+ describe 'home' do
180
+ it 'returns the home of the current user' do
181
+ FS.home.should eql('/Users/me')
182
+ end
183
+
184
+ it 'returns the home of another user' do
185
+ FS.home('you').should eql('/Users/you')
186
+ end
187
+ end
188
+
189
+ # describe 'removedir'
190
+ # describe 'removedirs'
191
+ # describe 'currentdir'
192
+ # describe 'changedir'
193
+ # describe 'copy'
194
+ # describe 'link'
195
+
196
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec/core'
2
+ Dir['./spec/support/**/*.rb'].map {|f| require f}
3
+
4
+ require 'fs'
@@ -0,0 +1,32 @@
1
+ require 'fakefs/spec_helpers'
2
+
3
+ RSpec.configure do |config|
4
+ config.include FakeFS::SpecHelpers
5
+ end
6
+
7
+ # FileUtils.mkdir is not handled
8
+ # https://github.com/defunkt/fakefs/issues/closed#issue/37
9
+ #
10
+ # Patch:
11
+ # https://github.com/flavio/fakefs/blob/ea22773bc293ea3bce97317086b8171669780eab/lib/fakefs/fileutils.rb
12
+ module FakeFS
13
+ module FileUtils
14
+ extend self
15
+ def mkdir(path)
16
+ parent = path.split('/')
17
+ parent.pop
18
+ raise Errno::ENOENT, "No such file or directory - #{path}" unless parent.join == "" || FileSystem.find(parent.join('/'))
19
+ raise Errno::EEXIST, "File exists - #{path}" if FileSystem.find(path)
20
+ FileSystem.add(path, FakeDir.new)
21
+ end
22
+ end
23
+ end
24
+
25
+
26
+ module FakeFS
27
+ class Dir
28
+ def self.home(user=nil)
29
+ "/Users/#{user || 'me'}"
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fs
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - "Bernd J\xC3\xBCnger"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-28 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.5.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakefs
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - "="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.3.1
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: FS gathers the cluttered methods for working with files and dirs. Internally using the good old standard library, but providing simple methods in a single place.
39
+ email:
40
+ - blindgaenger@gmail.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - README.mdown
51
+ - Rakefile
52
+ - fs.gemspec
53
+ - lib/fs.rb
54
+ - lib/fs/version.rb
55
+ - spec/fs_spec.rb
56
+ - spec/spec_helper.rb
57
+ - spec/support/fakefs_support.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/blindgaenger/fs
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.6.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Work with your filesystem!
86
+ test_files:
87
+ - spec/fs_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/support/fakefs_support.rb