directree 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d683e802122afc050f39ac35b866f7b25a23c8a
4
+ data.tar.gz: 1dcf82c56b663dab1e971b573eecd5ebecf51858
5
+ SHA512:
6
+ metadata.gz: 14d5690304204fb2aaa675dc3e7078d1bb148af954ee736ca462e29bee7a52d299749cff13422a0197a2600e5430ec98a663e18fc114ba863fe2bfe093de0ba3
7
+ data.tar.gz: 1603a39d2ebd3dffe74d848dbe52c5bd87435fcd904dedd2b27c64cb0ba775add3a45496b0d3cfd8de7373c4e80b766a2a53bfc9b7e4a6904231a2f35e7b977d
@@ -0,0 +1,23 @@
1
+ *.a
2
+ *.bundle
3
+ *.gem
4
+ *.o
5
+ *.rbc
6
+ *.so
7
+ .bundle
8
+ .config
9
+ .idea
10
+ .yardoc
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ Gemfile.lock
15
+ InstalledFiles
16
+ lib/bundler/man
17
+ mkmf.log
18
+ pkg
19
+ rdoc
20
+ spec/reports
21
+ test/tmp
22
+ test/version_tmp
23
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Kunal Dabir
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ directree.rb
2
+ ============
3
+
4
+ A simple DSL to create Directory Tree and Files with content.
5
+
6
+ A [Directree](https://github.com/kdabir/directree) implementation powered by ruby.
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'directree'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install directree
22
+
23
+ ## Usage
24
+
25
+ require 'directree'
26
+
27
+ Directree.create("mygem") {
28
+ dir("lib")
29
+ dir("spec"){
30
+ file("spec_helper.rb") {
31
+ <<-EOF
32
+ RSpec.configure do |config|
33
+ config.color_enabled = true
34
+ end
35
+ EOF
36
+ }
37
+ }
38
+ file("README.txt") {
39
+ "This is README of mygem"
40
+ }
41
+ }
42
+
43
+ That's it, Seriously !!
44
+
45
+ Verify it:
46
+
47
+ $ tree mygem
48
+ mygem
49
+ |-- README.txt
50
+ |-- lib
51
+ `-- spec
52
+ `-- spec_helper.rb
53
+
54
+ 2 directories, 2 files
55
+
56
+ And check the file content
57
+
58
+ $ cat mygem/spec/spec_helper.rb
59
+ RSpec.configure do |config|
60
+ config.color_enabled = true
61
+ end
62
+
63
+
64
+ See the [specs](tree/master/spec) for more details usage
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( https://github.com/kdabir/directree.rb/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'directree/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "directree"
8
+ spec.version = Directree::VERSION
9
+ spec.authors = ["Kunal Dabir"]
10
+ spec.email = ["kunal.dabir@gmail.com"]
11
+ spec.summary = %q{create directory trees and files with contents}
12
+ spec.description = %q{directree is convenient DSL for creating directory tree and text files with content}
13
+ spec.homepage = "http://github.com/kdabir/directree.rb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,84 @@
1
+ require "directree/version"
2
+ require 'fileutils'
3
+ require 'pathname'
4
+
5
+ module Directree
6
+
7
+ def self.create name, opts={}, &block
8
+ tree = build(name,opts,&block)
9
+ tree.create
10
+ tree
11
+ end
12
+
13
+ def self.build name, opts={}, &block
14
+ DirTree.new(name, opts, &block)
15
+ end
16
+
17
+ class DirTree
18
+ attr_reader :path, :opts, :children
19
+
20
+ def initialize name, opts={}, &block
21
+ @path = name
22
+ @opts = opts
23
+ @children ||= []
24
+ self.instance_eval &block if block_given?
25
+ end
26
+
27
+ def dir name, opts={}, &block
28
+ @children << DirTree.new(join_path(@path, name), opts, &block)
29
+ end
30
+
31
+ def file name, opts={}, &block
32
+ @children << WritableFile.new(join_path(@path, name), opts, &block)
33
+ end
34
+
35
+ def create
36
+ FileUtils::mkdir_p @path
37
+ @children.each{ |child| child.create}
38
+ end
39
+
40
+ def [](index_or_name)
41
+ if index_or_name.is_a? Integer
42
+ @children[index_or_name]
43
+ else
44
+ @children.find {|p| File.basename(p.path) == index_or_name}
45
+ end
46
+ end
47
+
48
+ def walk &block
49
+ [block.call(self), children.collect {|child|
50
+ if child.respond_to?(:walk)
51
+ child.walk(&block)
52
+ else
53
+ block.call(child)
54
+ end
55
+ }].flatten
56
+ end
57
+
58
+ private
59
+ def join_path parent, child
60
+ (Pathname(parent) + child).to_s
61
+ end
62
+ end
63
+
64
+
65
+ class WritableFile
66
+ attr_reader :path, :opts
67
+
68
+ def initialize name, opts={}, &block
69
+ @path = name
70
+ @opts = opts
71
+ @block = block if block_given?
72
+ end
73
+
74
+ def create
75
+ File.open(@path, 'w') {|f| f.write(content) }
76
+ end
77
+
78
+ def content
79
+ @block.call(@path, @opts) if @block
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,3 @@
1
+ module Directree
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,250 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+
5
+ describe Directree do
6
+
7
+ it "should give current version" do
8
+ (Directree::VERSION).should == "0.0.1"
9
+ end
10
+
11
+ it "should build tree from Module helper method" do
12
+ d = Directree.build("a", append:true) {
13
+ file("LICENSE.txt")
14
+ dir("bin")
15
+ dir("lib")
16
+ dir("spec") {
17
+ file("spec_helper.rb") {
18
+ <<-EOF
19
+ RSpec.configure do |config|
20
+ config.color_enabled = true
21
+ end
22
+ EOF
23
+ }
24
+ }
25
+
26
+ }
27
+ d.path.should == "a"
28
+ d["spec"]["spec_helper.rb"].path.should == "a/spec/spec_helper.rb"
29
+ d["spec"]["spec_helper.rb"].content.should include("RSpec.configure")
30
+ end
31
+
32
+ describe Directree::WritableFile do
33
+ context "new" do
34
+ it "should initialize with a File with name" do
35
+ f = Directree::WritableFile.new "a.txt"
36
+ f.path.should == "a.txt"
37
+ end
38
+
39
+ it "should init with dir name and options" do
40
+ f = Directree::WritableFile.new "a.txt", append:true
41
+ f.path.should == "a.txt"
42
+ f.opts.should == {append:true}
43
+ end
44
+
45
+ it "should init with dir name and block" do
46
+ f = Directree::WritableFile.new "a", overwrite:true do
47
+ "this will be the content of file"
48
+ end
49
+ f.path.should == "a"
50
+ f.opts.should == {overwrite:true}
51
+ end
52
+
53
+ it "should not call the block when initializing" do
54
+ called = false
55
+ Directree::WritableFile.new "a.txt", overwrite:true do
56
+ called = true
57
+ end
58
+ called.should be(false)
59
+ end
60
+ end
61
+
62
+ context "content" do
63
+
64
+ it "should be able to get content returned from block" do
65
+ f = Directree::WritableFile.new "a.txt", overwrite:true do |path, opts|
66
+ "this should be content"
67
+ end
68
+ f.content.should == "this should be content"
69
+ end
70
+
71
+ it "should call block with path and opts" do
72
+ called = false
73
+ f = Directree::WritableFile.new "a.txt", overwrite:true do |path, opts|
74
+ called = true
75
+ path.should == "a.txt"
76
+ opts.should == {overwrite:true}
77
+ end
78
+ f.content
79
+ called.should be(true)
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ describe Directree::DirTree do
86
+
87
+ context "new" do
88
+
89
+ it "should initialize with a directory name" do
90
+ d = Directree::DirTree.new "a"
91
+ d.path.should == "a"
92
+ end
93
+
94
+ it "should init with dir name and options" do
95
+ d = Directree::DirTree.new "a", overwrite:true
96
+ d.path.should == "a"
97
+ d.opts.should == {overwrite:true}
98
+ end
99
+
100
+ it "should init with dir name and block" do
101
+ d = Directree::DirTree.new "a", overwrite:true do
102
+ a = "something that doesn't matter"
103
+ end
104
+ d.path.should == "a"
105
+ d.opts.should == {overwrite:true}
106
+ end
107
+
108
+ it "should init with a block that can call method on self" do
109
+ called = false
110
+ Directree::DirTree.new "a", overwrite:true do
111
+ called = true
112
+ path.should == "a"
113
+ opts.should == {overwrite:true}
114
+ end
115
+ called.should be(true)
116
+ end
117
+ end
118
+
119
+ context "nesting" do
120
+
121
+ it "should be able to represent nested dir inside dir" do
122
+ d = Directree::DirTree.new("a")
123
+ d.dir("b", overwrite:true)
124
+ d.children.first.path.should == "a/b"
125
+ end
126
+
127
+ it "should be able to represent nested file inside dir" do
128
+ d = Directree::DirTree.new("a")
129
+ d.file("b.txt", append:true)
130
+ d.children.first.path.should == "a/b.txt"
131
+ end
132
+
133
+ it "should be able to represent n level nested dir inside dir" do
134
+ a = Directree::DirTree.new("a")
135
+ a.dir("b", overwrite:true) {
136
+ dir("c") {
137
+ dir("d")
138
+ }
139
+ }
140
+ a.children.first.path.should == "a/b"
141
+ a['b']['c']['d'].path.should == "a/b/c/d"
142
+ end
143
+
144
+ it "should be able to represent n level nested files and dir inside dir" do
145
+ a = Directree::DirTree.new("a")
146
+ a.dir("b", overwrite:true) {
147
+ dir("c") {
148
+ dir("d")
149
+ file('e.txt')
150
+ }
151
+ file "f.txt", create:false do
152
+ "this would be file's content"
153
+ end
154
+ }
155
+ a['b']['c']['d'].path.should == "a/b/c/d"
156
+ a['b']['c']['e.txt'].path.should == "a/b/c/e.txt"
157
+ a['b']['f.txt'].path.should == "a/b/f.txt"
158
+ end
159
+ end
160
+
161
+ context "access" do
162
+ a = Directree::DirTree.new("a") {
163
+ dir("b") {
164
+ dir("c")
165
+ file("d")
166
+ }
167
+ }
168
+
169
+ it "should be able to access by array index" do
170
+ a[0].path.should == "a/b"
171
+ a[0][0].path.should == "a/b/c"
172
+ a[0][1].path.should == "a/b/d"
173
+ end
174
+
175
+ it "should be able to access by name" do
176
+ a['b'].path.should == "a/b"
177
+ a['b']['c'].path.should == "a/b/c"
178
+ a['b']['d'].path.should == "a/b/d"
179
+ end
180
+ end
181
+
182
+
183
+ context "traversal" do
184
+ a = Directree::DirTree.new("a") {
185
+ dir("b") {
186
+ dir("c", required:true) {
187
+ file("1.txt")
188
+ }
189
+ dir("d")
190
+ file("2.txt", create:false)
191
+ }
192
+ }
193
+
194
+ it "should traverse every file" do
195
+ files = []
196
+ a.walk { |f| files << f.path }
197
+ files.should == ["a", "a/b", "a/b/c", "a/b/c/1.txt", "a/b/d", "a/b/2.txt"]
198
+ end
199
+
200
+ it "should traverse every file and get its opts" do
201
+ files = []
202
+ a.walk { |f| files << f.opts }
203
+ files.should == [{}, {}, {:required=>true}, {}, {}, {:create=>false}]
204
+ end
205
+ end
206
+ end
207
+
208
+ context "integration-testing" do
209
+
210
+ before(:each) do
211
+ @path = Dir.mktmpdir + "/directest"
212
+ FileUtils.mkdir_p(@path)
213
+ Dir.chdir(@path)
214
+ #puts "using #{@path} for integration tests"
215
+ end
216
+
217
+ after(:each) do
218
+ FileUtils.rm_rf("directest")
219
+ end
220
+
221
+ it "should create directories and files" do
222
+ a = Directree::DirTree.new("a") {
223
+ dir("b") {
224
+ dir("c", required:true) {
225
+ file("1.txt") {
226
+ "hello world"
227
+ }
228
+ }
229
+ dir("d")
230
+ file("2.txt", create:false)
231
+ }
232
+ }
233
+ a.create
234
+
235
+ Dir["**/*/"].should == ["a/", "a/b/", "a/b/c/", "a/b/d/"] # for directories
236
+ File.read("a/b/c/1.txt").should == "hello world"
237
+ File.read("a/b/2.txt").should == ""
238
+ end
239
+
240
+ it "should create from module" do
241
+ Directree.create("x") {
242
+ file "y.txt" do
243
+ "content of y.txt"
244
+ end
245
+ }
246
+ File.read("x/y.txt").should == "content of y.txt"
247
+ end
248
+ end
249
+
250
+ end
@@ -0,0 +1,5 @@
1
+ require 'directree'
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: directree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kunal Dabir
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: directree is convenient DSL for creating directory tree and text files
56
+ with content
57
+ email:
58
+ - kunal.dabir@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - directree.gemspec
69
+ - lib/directree.rb
70
+ - lib/directree/version.rb
71
+ - spec/directree_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/kdabir/directree.rb
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.0
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: create directory trees and files with contents
97
+ test_files:
98
+ - spec/directree_spec.rb
99
+ - spec/spec_helper.rb