folder_template 0.1.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.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +11 -0
  3. data/LICENSE +22 -0
  4. data/README.md +55 -0
  5. data/folder_template.gemspec +38 -0
  6. data/lib/folder_template.rb +20 -0
  7. data/lib/folder_template/fs_adapter.rb +64 -0
  8. data/lib/folder_template/template_folder.rb +58 -0
  9. data/lib/folder_template/template_folder_entry.rb +59 -0
  10. data/lib/folder_template/template_string.rb +68 -0
  11. data/lib/folder_template/version.rb +12 -0
  12. data/rakefile.rb +42 -0
  13. data/test/_test_env.rb +31 -0
  14. data/test/data/test1/actual_output/lib/project_aaa.rb +1 -0
  15. data/test/data/test1/actual_output/lib/project_aaa/cls_fn.rb +13 -0
  16. data/test/data/test1/actual_output/test_/_test_cls_fn.rb +20 -0
  17. data/test/data/test1/expected_output/lib/project_aaa.rb +1 -0
  18. data/test/data/test1/expected_output/lib/project_aaa/cls_fn.rb +13 -0
  19. data/test/data/test1/expected_output/test_/_test_cls_fn.rb +20 -0
  20. data/test/data/test1/template/lib/>>{{project_name}}.rb +1 -0
  21. data/test/data/test1/template/lib/{{project_name}}/{{class_filename}}.rb +13 -0
  22. data/test/data/test1/template/test_/_test_{{class_filename}}.rb +20 -0
  23. data/test/data/test_append/actual_output/lib/project_aaa.rb +3 -0
  24. data/test/data/test_append/actual_output/lib/project_aaa/cls1_fn.rb +13 -0
  25. data/test/data/test_append/actual_output/lib/project_aaa/cls2_fn.rb +13 -0
  26. data/test/data/test_append/actual_output/lib/project_aaa/cls3_fn.rb +13 -0
  27. data/test/data/test_append/actual_output/test_/_test_cls1_fn.rb +20 -0
  28. data/test/data/test_append/actual_output/test_/_test_cls2_fn.rb +20 -0
  29. data/test/data/test_append/actual_output/test_/_test_cls3_fn.rb +20 -0
  30. data/test/data/test_append/expected_output/lib/project_aaa.rb +3 -0
  31. data/test/data/test_append/expected_output/lib/project_aaa/cls1_fn.rb +13 -0
  32. data/test/data/test_append/expected_output/lib/project_aaa/cls2_fn.rb +13 -0
  33. data/test/data/test_append/expected_output/lib/project_aaa/cls3_fn.rb +13 -0
  34. data/test/data/test_append/expected_output/test_/_test_cls1_fn.rb +20 -0
  35. data/test/data/test_append/expected_output/test_/_test_cls2_fn.rb +20 -0
  36. data/test/data/test_append/expected_output/test_/_test_cls3_fn.rb +20 -0
  37. data/test/data/test_append/template/lib/>>{{project_name}}.rb +1 -0
  38. data/test/data/test_append/template/lib/{{project_name}}/{{class_filename}}.rb +13 -0
  39. data/test/data/test_append/template/test_/_test_{{class_filename}}.rb +20 -0
  40. data/test/test_fs_adapter.rb +56 -0
  41. data/test/test_template_folder.rb +62 -0
  42. data/test/test_template_folder_entry.rb +147 -0
  43. data/test/test_template_string.rb +94 -0
  44. metadata +175 -0
@@ -0,0 +1,62 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : lib/ruby_project_generator/version.rb
4
+ # PROJECT : RubyProjectGenerator
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+
11
+ require '_test_env.rb'
12
+
13
+
14
+ module FolderTemplate
15
+
16
+ describe TemplateFolder do
17
+ let( :data_base_path ) { File.join( FolderTemplate::BASE_PATH, 'test', 'data' ) }
18
+ let( :template ) { TemplateFolder.new( File.join( data_base_path, data_test_name, 'template' ) ) }
19
+ let( :expected_output_path ) { File.join( data_base_path, data_test_name, 'expected_output' ) }
20
+ let( :actual_output_path ) { File.join( data_base_path, data_test_name, 'actual_output' ) }
21
+ let( :base_env ) { Hash.new.merge( project_name: 'project_aaa',
22
+ project_namespace: 'ProjectAaa',
23
+ copyright_owner: 'Me',
24
+ copyright_year: 'YEAR' ) }
25
+
26
+ before do
27
+ FileUtils.rm_rf( actual_output_path )
28
+ FileUtils.makedirs( actual_output_path )
29
+ end
30
+
31
+ describe "test1" do
32
+ let( :data_test_name ) { 'test1' }
33
+ let( :env ) { base_env.merge( class_filename:'cls_fn', class_name:'ClsName' ) }
34
+
35
+ it "" do
36
+ fs = FsAdapter.new( actual_output_path, verbose:false )
37
+ template.generate( fs, env )
38
+
39
+ assert_folders_match( expected_output_path, actual_output_path )
40
+ end
41
+ end
42
+
43
+ describe "test_append" do
44
+ let( :data_test_name ) { 'test_append' }
45
+ let( :env ) { base_env }
46
+
47
+ it "" do
48
+ fs = FsAdapter.new( actual_output_path, verbose:false )
49
+ template.generate( fs, env.merge( class_filename:'cls1_fn', class_name:'Cls1Name') )
50
+ template.generate( fs, env.merge( class_filename:'cls2_fn', class_name:'Cls2Name') )
51
+ template.generate( fs, env.merge( class_filename:'cls3_fn', class_name:'Cls3Name') )
52
+
53
+ assert_folders_match( expected_output_path, actual_output_path )
54
+ end
55
+ end
56
+
57
+
58
+ end # describe TemplateFolder
59
+
60
+ end
61
+
62
+
@@ -0,0 +1,147 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : test/ruby_project_generator/template_string.rb
4
+ # PROJECT : RubyProjectGenerator
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+
11
+ require '_test_env.rb'
12
+
13
+
14
+ module FolderTemplate
15
+ describe TemplateFolderEntry do
16
+
17
+ describe "when initialized for directory entry" do
18
+ let(:filename) { "lib/{{project_name}}" }
19
+ let(:content) { nil }
20
+ subject { TemplateFolderEntry.new( filename, content ) }
21
+
22
+ it "parses filename into a TemplateString" do
23
+ subject.filename_template.must_be_kind_of TemplateString
24
+ end
25
+
26
+ it "parses filename correctly" do
27
+ "#{subject.filename_template}".must_equal filename
28
+ end
29
+
30
+ it "sets content_template to nil" do
31
+ subject.content_template.must_be_nil
32
+ end
33
+
34
+
35
+ describe "when asking for variables" do
36
+ it "returns filename variables" do
37
+ subject.variables.must_equal subject.filename_template.variables
38
+ end
39
+ end
40
+
41
+ describe "when calling generate" do
42
+ let(:fs) { MiniTest::Mock.new }
43
+ let(:env) { Hash.new.merge( project_name:"aaa", class_filename:"bbb" ) }
44
+
45
+ it "expands content and filename and invokes fs.write_to_file()" do
46
+ expected_filename = subject.filename_template.expand( env ).to_s
47
+ fs.expect :makedirs, nil, [expected_filename]
48
+
49
+ subject.generate( fs, env )
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ describe "when initialized for file entry" do
56
+ let(:filename) { "lib/{{project_name}}/{{project_name}}.rb" }
57
+ let(:content) { "# {{filename}}\n\nrequire_relative 'lib/{{project_name}}/{{class_filename}}.rb'" }
58
+ subject { TemplateFolderEntry.new( filename, content ) }
59
+
60
+ it "parses filename into a TemplateString" do
61
+ subject.filename_template.must_be_kind_of TemplateString
62
+ end
63
+
64
+ it "parses filename correctly" do
65
+ "#{subject.filename_template}".must_equal filename
66
+ end
67
+
68
+ it "prases content into TemplateString" do
69
+ subject.content_template.must_be_kind_of TemplateString
70
+ end
71
+
72
+ it "parses content correctly" do
73
+ "#{subject.content_template}".must_equal content
74
+ end
75
+
76
+ describe "when asking for variables" do
77
+ it "returns a union set of filename and content variables" do
78
+ expected_variables = subject.filename_template.variables + subject.content_template.variables
79
+ subject.variables.must_equal expected_variables
80
+ end
81
+ end
82
+
83
+ describe "when calling generate" do
84
+ let(:fs) { MiniTest::Mock.new }
85
+ let(:env) { Hash.new.merge( project_name:"aaa", class_filename:"bbb" ) }
86
+
87
+ it "expands content and filename and invokes fs.write_to_file()" do
88
+ expected_filename = subject.filename_template.expand( env ).to_s
89
+ expected_content = subject.content_template.expand( env.merge( filename:expected_filename) ).to_s
90
+ fs.expect :write_to_file, nil, [expected_filename, expected_content]
91
+
92
+ subject.generate( fs, env )
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "when initialized for append entry" do
98
+ let(:filename) { "lib/>>{{project_name}}.rb" }
99
+ let(:content) { "# {{filename}}\n\nrequire_relative 'lib/{{project_name}}/{{class_filename}}.rb'" }
100
+ subject { TemplateFolderEntry.new( filename, content ) }
101
+
102
+ it "parses filename into a TemplateString" do
103
+ subject.filename_template.must_be_kind_of TemplateString
104
+ end
105
+
106
+ it "removes append marker from filename template" do
107
+ "#{subject.filename_template}".must_equal filename.gsub( ">>", "" )
108
+ end
109
+
110
+ it "marks entry as append" do
111
+ subject.append.must_equal true
112
+ end
113
+
114
+ it "prases content into TemplateString" do
115
+ subject.content_template.must_be_kind_of TemplateString
116
+ end
117
+
118
+ it "parses content correctly" do
119
+ "#{subject.content_template}".must_equal content
120
+ end
121
+
122
+
123
+ describe "when asking for variables" do
124
+ it "returns a union set of filename and content variables" do
125
+ expected_variables = subject.filename_template.variables + subject.content_template.variables
126
+ subject.variables.must_equal expected_variables
127
+ end
128
+ end
129
+
130
+ describe "when calling generate" do
131
+ let(:fs) { MiniTest::Mock.new }
132
+ let(:env) { Hash.new.merge( project_name:"aaa", class_filename:"bbb" ) }
133
+
134
+ it "expands content and filename and invokes fs.write_to_file()" do
135
+ expected_filename = subject.filename_template.expand( env ).to_s
136
+ expected_content = subject.content_template.expand( env.merge( filename:expected_filename) ).to_s
137
+ fs.expect :append_to_file, nil, [expected_filename, expected_content]
138
+
139
+ subject.generate( fs, env )
140
+ end
141
+ end
142
+ end
143
+
144
+ end # describe TemplateString
145
+ end
146
+
147
+
@@ -0,0 +1,94 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : test/ruby_project_generator/template_string.rb
4
+ # PROJECT : RubyProjectGenerator
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+
11
+ require '_test_env.rb'
12
+
13
+
14
+ module FolderTemplate
15
+ describe TemplateString do
16
+
17
+ describe "when initialized with a string" do
18
+ let(:template_string) { "prefix {{arg1}} infix {{arg2}} suffix" }
19
+ subject { TemplateString.new( template_string )}
20
+
21
+ it "parses template string into an Array" do
22
+ subject.content.must_be_kind_of Array
23
+ end
24
+
25
+ it "splits definition into Strings and Symbols" do
26
+ content_types = Set.new( subject.content.map { |e| e.class } )
27
+ content_types.must_equal Set.new( [String, Symbol] )
28
+ end
29
+
30
+ it "splits definition into constituant parts" do
31
+ subject.content.must_equal ["prefix ", :arg1, " infix ", :arg2, " suffix"]
32
+ end
33
+
34
+ it "extracts variables from definition string" do
35
+ subject.variables.must_equal Set.new( [:arg1, :arg2] )
36
+ end
37
+
38
+ it "evaluates to original template string" do
39
+ "#{subject}".must_equal template_string
40
+ end
41
+ end
42
+
43
+
44
+ describe "when initialized special string" do
45
+ it "finds all variables" do
46
+ template = TemplateString.new( "{{arg0}}{{arg1}} prefix {{arg2}}{{arg3}} suffix {{arg4}}{{arg5}}" )
47
+ template.variables.must_equal Set.new( [:arg0, :arg1, :arg2, :arg3, :arg4, :arg5] )
48
+ end
49
+
50
+ it "keeps only non empty strings" do
51
+ template = TemplateString.new( "{{arg0}}{{arg1}} prefix {{arg2}}{{arg3}} suffix {{arg4}}{{arg5}}" )
52
+ strings = template.content.select{ |s| String === s }
53
+ strings.must_equal( [" prefix ", " suffix "])
54
+ end
55
+ end
56
+
57
+ describe "calling expand" do
58
+ let(:template_string) { "prefix {{arg1}} infix {{arg2}} suffix" }
59
+ subject { TemplateString.new( template_string )}
60
+
61
+ it "expands variables using environment" do
62
+ "#{subject.expand( arg1:"aaa", arg2:"bbb")}".must_equal "prefix aaa infix bbb suffix"
63
+ end
64
+
65
+ it "preserves undefined variables" do
66
+ "#{subject.expand( arg1:"aaa" )}".must_equal "prefix aaa infix {{arg2}} suffix"
67
+ end
68
+
69
+ it "passes undefined variables to block" do
70
+ result = subject.expand( arg1:"aaa" ) do |variable|
71
+ "<#{variable}>"
72
+ end
73
+ "#{result}".must_equal "prefix aaa infix <arg2> suffix"
74
+ end
75
+
76
+ it "preserves template variable when block returns nil" do
77
+ result = subject.expand( arg1:"aaa" ) do |variable|
78
+ nil
79
+ end
80
+ "#{result}".must_equal "prefix aaa infix {{arg2}} suffix"
81
+ end
82
+
83
+ it "removes undefined variables when block returns empty string" do
84
+ result = subject.expand( arg1:"aaa" ) do |variable|
85
+ ""
86
+ end
87
+ "#{result}".must_equal "prefix aaa infix suffix"
88
+ end
89
+ end
90
+
91
+ end # describe TemplateString
92
+ end
93
+
94
+
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: folder_template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Marc-Antoine Argenton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-10 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: watch
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description: FolderTemplate is a minimalistic template engine that generates files
70
+ and folders structure from a template folder layout. It includes a simple variable
71
+ expansion syntax, automatically injects variables for filename and basename, and
72
+ can optionally append content to existing files.
73
+ email:
74
+ - maargenton.dev@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - folder_template.gemspec
83
+ - lib/folder_template.rb
84
+ - lib/folder_template/fs_adapter.rb
85
+ - lib/folder_template/template_folder.rb
86
+ - lib/folder_template/template_folder_entry.rb
87
+ - lib/folder_template/template_string.rb
88
+ - lib/folder_template/version.rb
89
+ - rakefile.rb
90
+ - test/_test_env.rb
91
+ - test/data/test1/actual_output/lib/project_aaa.rb
92
+ - test/data/test1/actual_output/lib/project_aaa/cls_fn.rb
93
+ - test/data/test1/actual_output/test_/_test_cls_fn.rb
94
+ - test/data/test1/expected_output/lib/project_aaa.rb
95
+ - test/data/test1/expected_output/lib/project_aaa/cls_fn.rb
96
+ - test/data/test1/expected_output/test_/_test_cls_fn.rb
97
+ - test/data/test1/template/lib/>>{{project_name}}.rb
98
+ - test/data/test1/template/lib/{{project_name}}/{{class_filename}}.rb
99
+ - test/data/test1/template/test_/_test_{{class_filename}}.rb
100
+ - test/data/test_append/actual_output/lib/project_aaa.rb
101
+ - test/data/test_append/actual_output/lib/project_aaa/cls1_fn.rb
102
+ - test/data/test_append/actual_output/lib/project_aaa/cls2_fn.rb
103
+ - test/data/test_append/actual_output/lib/project_aaa/cls3_fn.rb
104
+ - test/data/test_append/actual_output/test_/_test_cls1_fn.rb
105
+ - test/data/test_append/actual_output/test_/_test_cls2_fn.rb
106
+ - test/data/test_append/actual_output/test_/_test_cls3_fn.rb
107
+ - test/data/test_append/expected_output/lib/project_aaa.rb
108
+ - test/data/test_append/expected_output/lib/project_aaa/cls1_fn.rb
109
+ - test/data/test_append/expected_output/lib/project_aaa/cls2_fn.rb
110
+ - test/data/test_append/expected_output/lib/project_aaa/cls3_fn.rb
111
+ - test/data/test_append/expected_output/test_/_test_cls1_fn.rb
112
+ - test/data/test_append/expected_output/test_/_test_cls2_fn.rb
113
+ - test/data/test_append/expected_output/test_/_test_cls3_fn.rb
114
+ - test/data/test_append/template/lib/>>{{project_name}}.rb
115
+ - test/data/test_append/template/lib/{{project_name}}/{{class_filename}}.rb
116
+ - test/data/test_append/template/test_/_test_{{class_filename}}.rb
117
+ - test/test_fs_adapter.rb
118
+ - test/test_template_folder.rb
119
+ - test/test_template_folder_entry.rb
120
+ - test/test_template_string.rb
121
+ homepage: ''
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.5
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Simple and generic folder structure template engine
144
+ test_files:
145
+ - test/_test_env.rb
146
+ - test/data/test1/actual_output/lib/project_aaa.rb
147
+ - test/data/test1/actual_output/lib/project_aaa/cls_fn.rb
148
+ - test/data/test1/actual_output/test_/_test_cls_fn.rb
149
+ - test/data/test1/expected_output/lib/project_aaa.rb
150
+ - test/data/test1/expected_output/lib/project_aaa/cls_fn.rb
151
+ - test/data/test1/expected_output/test_/_test_cls_fn.rb
152
+ - test/data/test1/template/lib/>>{{project_name}}.rb
153
+ - test/data/test1/template/lib/{{project_name}}/{{class_filename}}.rb
154
+ - test/data/test1/template/test_/_test_{{class_filename}}.rb
155
+ - test/data/test_append/actual_output/lib/project_aaa.rb
156
+ - test/data/test_append/actual_output/lib/project_aaa/cls1_fn.rb
157
+ - test/data/test_append/actual_output/lib/project_aaa/cls2_fn.rb
158
+ - test/data/test_append/actual_output/lib/project_aaa/cls3_fn.rb
159
+ - test/data/test_append/actual_output/test_/_test_cls1_fn.rb
160
+ - test/data/test_append/actual_output/test_/_test_cls2_fn.rb
161
+ - test/data/test_append/actual_output/test_/_test_cls3_fn.rb
162
+ - test/data/test_append/expected_output/lib/project_aaa.rb
163
+ - test/data/test_append/expected_output/lib/project_aaa/cls1_fn.rb
164
+ - test/data/test_append/expected_output/lib/project_aaa/cls2_fn.rb
165
+ - test/data/test_append/expected_output/lib/project_aaa/cls3_fn.rb
166
+ - test/data/test_append/expected_output/test_/_test_cls1_fn.rb
167
+ - test/data/test_append/expected_output/test_/_test_cls2_fn.rb
168
+ - test/data/test_append/expected_output/test_/_test_cls3_fn.rb
169
+ - test/data/test_append/template/lib/>>{{project_name}}.rb
170
+ - test/data/test_append/template/lib/{{project_name}}/{{class_filename}}.rb
171
+ - test/data/test_append/template/test_/_test_{{class_filename}}.rb
172
+ - test/test_fs_adapter.rb
173
+ - test/test_template_folder.rb
174
+ - test/test_template_folder_entry.rb
175
+ - test/test_template_string.rb