developwithpassion_expander 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.
- data/.gitignore +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/Rakefile +1 -0
- data/bin/dwp_expand +4 -0
- data/develpwithpassion_expander.gemspec +29 -0
- data/lib/developwithpassion_expander.rb +32 -0
- data/lib/developwithpassion_expander/array.rb +8 -0
- data/lib/developwithpassion_expander/array_dsl.rb +34 -0
- data/lib/developwithpassion_expander/array_dsl_module_factory.rb +14 -0
- data/lib/developwithpassion_expander/array_mutator.rb +12 -0
- data/lib/developwithpassion_expander/array_mutator_step.rb +17 -0
- data/lib/developwithpassion_expander/array_readable_step.rb +15 -0
- data/lib/developwithpassion_expander/array_visitor.rb +12 -0
- data/lib/developwithpassion_expander/array_visitor_step.rb +16 -0
- data/lib/developwithpassion_expander/array_writeable_step.rb +15 -0
- data/lib/developwithpassion_expander/cli_interface.rb +41 -0
- data/lib/developwithpassion_expander/copy.rb +29 -0
- data/lib/developwithpassion_expander/copy_to_target.rb +13 -0
- data/lib/developwithpassion_expander/enumerable_extensions.rb +11 -0
- data/lib/developwithpassion_expander/erb_template_file.rb +33 -0
- data/lib/developwithpassion_expander/expansion.rb +84 -0
- data/lib/developwithpassion_expander/file.rb +32 -0
- data/lib/developwithpassion_expander/file_merge.rb +54 -0
- data/lib/developwithpassion_expander/kernel.rb +35 -0
- data/lib/developwithpassion_expander/log.rb +17 -0
- data/lib/developwithpassion_expander/mustache_template_file.rb +11 -0
- data/lib/developwithpassion_expander/object_extensions.rb +28 -0
- data/lib/developwithpassion_expander/shell.rb +9 -0
- data/lib/developwithpassion_expander/shell_action_against_file.rb +12 -0
- data/lib/developwithpassion_expander/string.rb +5 -0
- data/lib/developwithpassion_expander/template_processors.rb +24 -0
- data/lib/developwithpassion_expander/template_visitor.rb +20 -0
- data/lib/developwithpassion_expander/version.rb +5 -0
- data/spec/spec_helper.rb +77 -0
- data/spec/specs/array_dsl_spec.rb +38 -0
- data/spec/specs/array_mutator_step_spec.rb +47 -0
- data/spec/specs/array_readable_step_spec.rb +26 -0
- data/spec/specs/array_spec.rb +22 -0
- data/spec/specs/array_visitor_step_spec.rb +78 -0
- data/spec/specs/array_writeable_step_spec.rb +30 -0
- data/spec/specs/copy_spec.rb +89 -0
- data/spec/specs/copy_to_target_spec.rb +24 -0
- data/spec/specs/enumerable_extensions_spec.rb +27 -0
- data/spec/specs/erb_template_file_spec.rb +65 -0
- data/spec/specs/expansion_spec.rb +146 -0
- data/spec/specs/file_merge_spec.rb +133 -0
- data/spec/specs/file_spec.rb +81 -0
- data/spec/specs/kernel_spec.rb +56 -0
- data/spec/specs/mustache_template_file_spec.rb +50 -0
- data/spec/specs/object_spec.rb +202 -0
- data/spec/specs/sample.rb +5 -0
- data/spec/specs/shell_action_against_file_spec.rb +24 -0
- data/spec/specs/string_spec.rb +13 -0
- data/spec/specs/template_processors_spec.rb +64 -0
- data/spec/specs/template_visitor_spec.rb +86 -0
- metadata +202 -0
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module DevelopWithPassion
|
4
|
+
module Expander
|
5
|
+
describe FileMerge do
|
6
|
+
context "when a file is added to the merge" do
|
7
|
+
let(:file){"file.rb"}
|
8
|
+
let(:output_file){"out_file.rb"}
|
9
|
+
let(:sut){FileMerge.new(output_file)}
|
10
|
+
before (:each) do
|
11
|
+
File.stub(:exists?).with(file).and_return(true)
|
12
|
+
end
|
13
|
+
context "and it is not already in the list" do
|
14
|
+
before (:each) do
|
15
|
+
sut.add(file)
|
16
|
+
end
|
17
|
+
it "should be added to the list of before files" do
|
18
|
+
sut.before_files[0].should == file
|
19
|
+
end
|
20
|
+
end
|
21
|
+
context "and it is already in the list" do
|
22
|
+
before (:each) do
|
23
|
+
sut.before_files << file
|
24
|
+
end
|
25
|
+
before (:each) do
|
26
|
+
sut.add(file)
|
27
|
+
end
|
28
|
+
it "should not be added to the list of before files" do
|
29
|
+
sut.before_files.count.should == 1
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when a file is added after the original contents" do
|
36
|
+
let(:file){"file.rb"}
|
37
|
+
let(:output_file){"out_file.rb"}
|
38
|
+
let(:sut){FileMerge.new(output_file)}
|
39
|
+
before (:each) do
|
40
|
+
File.stub(:exists?).with(file).and_return(true)
|
41
|
+
end
|
42
|
+
|
43
|
+
before (:each) do
|
44
|
+
sut.add_after_original_contents(file)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be added to the set of after files" do
|
48
|
+
sut.after_files[0].should == file
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when a set of files are merged to a target file" do
|
53
|
+
let(:target){RelativeFileSystem.file_name("merged.rb")}
|
54
|
+
let(:sut){FileMerge.new(target)}
|
55
|
+
before (:each) do
|
56
|
+
@file_system = RelativeFileSystem.new
|
57
|
+
@file_system.write_file("1.rb","first\n")
|
58
|
+
@file_system.write_file("2.rb","second\n")
|
59
|
+
@file_system.write_file("3.rb","third\n")
|
60
|
+
end
|
61
|
+
after (:each) do
|
62
|
+
@file_system.teardown
|
63
|
+
end
|
64
|
+
before (:each) do
|
65
|
+
files = [1,2,3].map{|item| RelativeFileSystem.file_name("#{item}.rb")}.to_a
|
66
|
+
File.open_for_write(target) do|file|
|
67
|
+
sut.merge_files(files,file)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
it "the target file should contain all of the file contents" do
|
71
|
+
IO.read(target).should == "first\nsecond\nthird\n"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "when a merge occurs" do
|
76
|
+
let(:target){RelativeFileSystem.file_name("original.rb")}
|
77
|
+
let(:sut){FileMerge.new(target)}
|
78
|
+
let(:first){"1.rb"}
|
79
|
+
let(:second){"2.rb"}
|
80
|
+
let(:third){"3.rb"}
|
81
|
+
let(:original){"original.rb"}
|
82
|
+
|
83
|
+
before (:each) do
|
84
|
+
@file_system = RelativeFileSystem.new
|
85
|
+
@file_system.write_file("1.rb","first\n")
|
86
|
+
@file_system.write_file("2.rb","second\n")
|
87
|
+
@file_system.write_file("3.rb","third\n")
|
88
|
+
@file_system.write_file("original.rb","original\n")
|
89
|
+
end
|
90
|
+
|
91
|
+
after (:each) do
|
92
|
+
@file_system.teardown
|
93
|
+
end
|
94
|
+
|
95
|
+
context "and original contents are flagged to be read" do
|
96
|
+
before (:each) do
|
97
|
+
sut.add_before_original_contents(RelativeFileSystem.file_name(first))
|
98
|
+
sut.add_before_original_contents(RelativeFileSystem.file_name(second))
|
99
|
+
sut.add_after_original_contents(RelativeFileSystem.file_name(third))
|
100
|
+
end
|
101
|
+
|
102
|
+
before (:each) do
|
103
|
+
sut.run
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should output the correct contents" do
|
107
|
+
contents = IO.read(target)
|
108
|
+
contents.should == "first\nsecond\noriginal\nthird\n"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "and original contents are not flagged to be read" do
|
113
|
+
before (:each) do
|
114
|
+
sut.add_before_original_contents(RelativeFileSystem.file_name(first))
|
115
|
+
sut.add_before_original_contents(RelativeFileSystem.file_name(second))
|
116
|
+
sut.add_after_original_contents(RelativeFileSystem.file_name(third))
|
117
|
+
sut.dont_read_original_file_contents
|
118
|
+
end
|
119
|
+
|
120
|
+
before (:each) do
|
121
|
+
sut.run
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should output the correct contents" do
|
125
|
+
contents = IO.read(target)
|
126
|
+
contents.should == "first\nsecond\nthird\n"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module DevelopWithPassion
|
4
|
+
module Expander
|
5
|
+
describe File do
|
6
|
+
let(:file_system){RelativeFileSystem.new}
|
7
|
+
|
8
|
+
context "when opening a file for read" do
|
9
|
+
let(:file){RelativeFileSystem.file_name("blah.rb")}
|
10
|
+
before (:each) do
|
11
|
+
file_system.write_file("blah.rb","sdf\nnsfsf")
|
12
|
+
@number_of_lines_visited = 0
|
13
|
+
end
|
14
|
+
|
15
|
+
after (:each) do
|
16
|
+
file_system.teardown
|
17
|
+
end
|
18
|
+
|
19
|
+
before (:each) do
|
20
|
+
File.open_for_read(file){|line| @number_of_lines_visited +=1}
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should open the file and run the block against each line in the file" do
|
24
|
+
@number_of_lines_visited.should == 2
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when reading all the text in a file" do
|
29
|
+
let(:file){RelativeFileSystem.file_name("blah.rb")}
|
30
|
+
let(:contents){<<-content
|
31
|
+
hello
|
32
|
+
this is
|
33
|
+
a file
|
34
|
+
content
|
35
|
+
}
|
36
|
+
before (:each) do
|
37
|
+
file_system.write_file("blah.rb",contents)
|
38
|
+
end
|
39
|
+
|
40
|
+
after (:each) do
|
41
|
+
file_system.teardown
|
42
|
+
end
|
43
|
+
|
44
|
+
before (:each) do
|
45
|
+
@result = File.read_all_text(file)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return the total contents" do
|
49
|
+
@result.should == contents
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "when reading all the text in a file after skipping lines" do
|
54
|
+
let(:file){RelativeFileSystem.file_name("blah.rb")}
|
55
|
+
let(:start){"this is the file\n"}
|
56
|
+
let(:contents){<<-content
|
57
|
+
hello
|
58
|
+
this is
|
59
|
+
a file
|
60
|
+
content
|
61
|
+
}
|
62
|
+
let(:all_contents){"#{start}#{contents}"}
|
63
|
+
before (:each) do
|
64
|
+
file_system.write_file("blah.rb",all_contents)
|
65
|
+
end
|
66
|
+
|
67
|
+
after (:each) do
|
68
|
+
file_system.teardown
|
69
|
+
end
|
70
|
+
|
71
|
+
before (:each) do
|
72
|
+
@result = File.read_all_text_after_skipping_lines(file,1)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return the contents except the skipped lines" do
|
76
|
+
@result.should == contents
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
describe Kernel do
|
5
|
+
context "when globbing for files" do
|
6
|
+
before (:each) do
|
7
|
+
@file_system = RelativeFileSystem.new
|
8
|
+
@file_system.write_file("1.rb","sdfsdfsdf")
|
9
|
+
@file_system.write_file("2.rb","sdfsdfsdf")
|
10
|
+
(1..2).each do|item|
|
11
|
+
@file_system.write_file("#{item}.rb","sdfsdf")
|
12
|
+
@file_system.write_file(".#{item}","sdfsdf")
|
13
|
+
(1..2).each do|folder|
|
14
|
+
new_folder = RelativeFileSystem.file_name("#{folder}")
|
15
|
+
file = File.join(new_folder,"#{folder}.rb")
|
16
|
+
@file_system.write_file(file,"sdfsd")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
after (:each) do
|
22
|
+
@file_system.teardown
|
23
|
+
end
|
24
|
+
|
25
|
+
context "and no block is given" do
|
26
|
+
before (:each) do
|
27
|
+
@result = glob(File.join(RelativeFileSystem.base_path,"**/*"))
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return all files in path including dotfiles" do
|
31
|
+
@result.count.should == 38
|
32
|
+
end
|
33
|
+
end
|
34
|
+
context "and a block is given" do
|
35
|
+
before (:each) do
|
36
|
+
@items_visited = 0
|
37
|
+
end
|
38
|
+
before (:each) do
|
39
|
+
@result = glob(File.join(RelativeFileSystem.base_path,"**/*")) do |file|
|
40
|
+
@items_visited += 1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return all files in path including dotfiles" do
|
45
|
+
@result.count.should == 38
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have run the block against each file" do
|
49
|
+
@items_visited.should == 38
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'configatron'
|
3
|
+
|
4
|
+
module DevelopWithPassion
|
5
|
+
module Expander
|
6
|
+
describe MustacheTemplateFile do
|
7
|
+
before (:each) do
|
8
|
+
@original_template = <<-original
|
9
|
+
This is the first line {{ item }}
|
10
|
+
This is the second line {{ item }}
|
11
|
+
original
|
12
|
+
end
|
13
|
+
context "when processing" do
|
14
|
+
let(:item){"yo"}
|
15
|
+
let(:sut){MustacheTemplateFile.new}
|
16
|
+
|
17
|
+
configatron.configure_from_hash :hello => "world"
|
18
|
+
|
19
|
+
before (:each) do
|
20
|
+
@filesystem = RelativeFileSystem.new
|
21
|
+
@output = RelativeFileSystem.file_name("out.rb")
|
22
|
+
@file_name = RelativeFileSystem.file_name("blah.rb")
|
23
|
+
|
24
|
+
@filesystem.write_file("blah.rb",@original_template)
|
25
|
+
|
26
|
+
configatron.stub(:to_hash).and_return(:item => item)
|
27
|
+
File.stub(:read_all_text).with(@file_name).and_return(@original_template)
|
28
|
+
end
|
29
|
+
|
30
|
+
after(:each) do
|
31
|
+
@filesystem.teardown
|
32
|
+
end
|
33
|
+
|
34
|
+
before (:each) do
|
35
|
+
sut.process(:input => @file_name,:output => @output)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should expand everything" do
|
39
|
+
@expected = <<-template
|
40
|
+
This is the first line #{item}
|
41
|
+
This is the second line #{item}
|
42
|
+
template
|
43
|
+
|
44
|
+
IO.read(@output).should == @expected
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Object do
|
4
|
+
context "when initializing a field with a set of default" do
|
5
|
+
it "should initialize based on the factory provided" do
|
6
|
+
|
7
|
+
class Item
|
8
|
+
attr_accessor :kids,:cars,:name
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
initialize_defaults lambda{[]},:kids,:cars
|
12
|
+
initialize_defaults lambda{""},:name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
item = Item.new
|
17
|
+
[item.kids,item.cars].each{|value| value.class.should == Array}
|
18
|
+
item.name.class.should == String
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when initializing simple arrays" do
|
23
|
+
it "should initialize all arrays specified on the instance" do
|
24
|
+
class Item
|
25
|
+
attr_accessor :kids,:cars,:bikes
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
initialize_arrays :kids,:cars,:bikes
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
item = Item.new
|
33
|
+
[item.kids,item.cars,item.bikes].each do|value|
|
34
|
+
value.class.should == Array
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when initializing arrays" do
|
40
|
+
context "using the dsl" do
|
41
|
+
it "should initialize all arrays specified on the instance and provide a method to expose addition" do
|
42
|
+
class Item
|
43
|
+
|
44
|
+
def initialize
|
45
|
+
array :kids do|a|
|
46
|
+
a.mutator :register_child
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
item = Item.new
|
52
|
+
item.methods.include?(:register_child).should be_true
|
53
|
+
item.register_child("hello")
|
54
|
+
item.kids.count.should == 1
|
55
|
+
end
|
56
|
+
it "should be able to expose a mutator with custom logic" do
|
57
|
+
class Item
|
58
|
+
attr_accessor :added,:item_added
|
59
|
+
|
60
|
+
def initialize
|
61
|
+
@added = 0
|
62
|
+
|
63
|
+
array :kids do|a|
|
64
|
+
a.mutator :register_child do|the_item|
|
65
|
+
@item_added = the_item
|
66
|
+
@added +=1
|
67
|
+
@kids.push(the_item)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
item = Item.new
|
74
|
+
item.register_child("hello")
|
75
|
+
item.kids.count.should == 1
|
76
|
+
item.item_added.should == "hello"
|
77
|
+
item.added.should == 1
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should be able to expose a processing visitor" do
|
81
|
+
class Item
|
82
|
+
attr_accessor :added
|
83
|
+
|
84
|
+
def initialize(visitor)
|
85
|
+
@added = 0
|
86
|
+
array :kids do|a|
|
87
|
+
a.mutator :register_child
|
88
|
+
a.process_using :register_kids,visitor
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class OurVisitor
|
94
|
+
attr_accessor :items
|
95
|
+
def initialize
|
96
|
+
@items = 0
|
97
|
+
end
|
98
|
+
def run_using(item)
|
99
|
+
@items +=1
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
our_visitor = OurVisitor.new
|
104
|
+
item = Item.new(our_visitor)
|
105
|
+
item.register_child("hello")
|
106
|
+
item.kids.count.should == 1
|
107
|
+
item.register_kids
|
108
|
+
our_visitor.items.should == 1
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should be able to expose a processing visitor by symbol" do
|
112
|
+
class Item
|
113
|
+
attr_accessor :added
|
114
|
+
|
115
|
+
def initialize
|
116
|
+
@added = 0
|
117
|
+
array :kids do|a|
|
118
|
+
a.mutator :register_child
|
119
|
+
a.process_using :register_kids,:speak
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class Kid
|
125
|
+
def initialize(array)
|
126
|
+
@array = array
|
127
|
+
end
|
128
|
+
def speak
|
129
|
+
@array.push("spoke")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
items = []
|
134
|
+
item = Item.new
|
135
|
+
item.register_child(Kid.new(items))
|
136
|
+
item.kids.count.should == 1
|
137
|
+
item.register_kids
|
138
|
+
items.count.should == 1
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
context "and no blocks are provided" do
|
143
|
+
it "should initialize all arrays specified on the instance and provide a method to expose addition" do
|
144
|
+
class Item
|
145
|
+
|
146
|
+
def initialize
|
147
|
+
array :kids do|a|
|
148
|
+
a.mutator :register_child
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
item = Item.new
|
154
|
+
item.methods.include?(:register_child).should be_true
|
155
|
+
item.register_child("hello")
|
156
|
+
item.kids.count.should == 1
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context "and a registration block is provided" do
|
161
|
+
it "should initialize all arrays specified on the instance and provide a method that delegates to the block" do
|
162
|
+
class Item
|
163
|
+
attr_accessor :added
|
164
|
+
|
165
|
+
def initialize
|
166
|
+
@added = 0
|
167
|
+
|
168
|
+
array :kids do|a|
|
169
|
+
a.mutator :register_child do|item|
|
170
|
+
@kids.push(item)
|
171
|
+
@added+=1
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
item = Item.new
|
178
|
+
item.register_child("hello")
|
179
|
+
item.kids.count.should == 1
|
180
|
+
item.added.should == 1
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "when initializing hashes" do
|
186
|
+
it "should initialize all hashes specified" do
|
187
|
+
class Item
|
188
|
+
attr_accessor :kids,:cars,:bikes
|
189
|
+
|
190
|
+
def initialize
|
191
|
+
initialize_hashes :kids,:cars,:bikes
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
item = Item.new
|
196
|
+
[item.kids,item.cars,item.bikes].each do|value|
|
197
|
+
value.class.should == Hash
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|