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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 57f1ea5e547be1744282bff8cd884c09025ce012
4
+ data.tar.gz: 2aab075e18769cc2c8fecaf5207ca790e0e8f698
5
+ SHA512:
6
+ metadata.gz: b33c362595bc85d82e41bb541dff05d47f1d47a6ad995e414e46726b5dc8f5ba84e871e6d6c0df34be9cedb0780cfcbcdf6619da5386095a7d37be8675e44d2c
7
+ data.tar.gz: 561c584694d6c79071b507f7d06f6e47826aa3fd99695444e68c90aaf12b9e4f8f1abf382cdd2ef173d228a01f3ecc687b618fae4411d8088b5e25e84f049215
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : Gemfile
4
+ \# PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ source 'https://rubygems.org'
11
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # FolderTemplate
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/rr.svg)](http://badge.fury.io/rb/rr)
4
+ [![Build Status](https://travis-ci.org/rr/rr.svg?branch=master)](https://travis-ci.org/rr/rr)
5
+ [![Code Climate GPA](https://codeclimate.com/github/rr/rr.svg)](https://codeclimate.com/github/rr/rr)
6
+
7
+
8
+ FolderTemplate is a minimalistic template engine that generates files and
9
+ folders structure from a template folder layout. It includes a simple variable
10
+ expansion syntax, automatically injects variables for filename and basename,
11
+ and can optionally append content to existing files.
12
+
13
+
14
+ ## Template definitions
15
+
16
+ - Templates are defined as files and folders on disk
17
+ - Variables are surrounded with double curly: `{{variable}}`
18
+ - Variables can be part of filenames, folder names, and file content
19
+ - Template filename with a `>>` prefix will append their content to an existing file
20
+
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'folder_template'
26
+
27
+ template = FolderTemplate::TemplateFolder.new( template_path )
28
+ fs = FolderTemplate::FsAdapter.new( output_path, opts... )
29
+ env = Hash.new.merge( project_name:"my_project", ... )
30
+
31
+ template.generate( fs, env )
32
+ ```
33
+
34
+
35
+ ## FsAdapter
36
+
37
+ All filesystem operation are abstracted through an `FsAdapter` object that
38
+ performs final filename expansion, and execute all filesystem manipulations.
39
+
40
+ The `FsAdapter` interface is composed of 3 methods; any class implementing
41
+ those 3 methods can be used in place of the default `FsAdapter` object to
42
+ perform the necessary filesystem operations.
43
+
44
+ ```ruby
45
+ def makedirs( dirname )
46
+ def write_to_file( filename, content )
47
+ def append_to_file( filename, content )
48
+ ```
49
+
50
+ Default implementation of `FsAdapter` accepts a few options:
51
+
52
+ | Option | Default | Description |
53
+ | ------------------ |---------|-----------------------|
54
+ | `verbose` | `false` | When `true`, log each operation that is performed |
55
+ | `overwrite_files` | `false` | When `true`, replace existing files with content generated from template |
@@ -0,0 +1,38 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : folder_template.gemspec
4
+ # PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+
11
+ require_relative 'lib/folder_template/version.rb'
12
+
13
+ Gem::Specification.new do |spec|
14
+ spec.name = 'folder_template'
15
+ spec.version = FolderTemplate::VERSION
16
+ spec.authors = ["Marc-Antoine Argenton"]
17
+ spec.email = ["maargenton.dev@gmail.com"]
18
+ spec.summary = "Simple and generic folder structure template engine"
19
+ spec.description = %q{ FolderTemplate is a minimalistic template engine that generates files and
20
+ folders structure from a template folder layout. It includes a simple variable
21
+ expansion syntax, automatically injects variables for filename and basename,
22
+ and can optionally append content to existing files.
23
+ }.gsub( /\s+/, ' ').strip
24
+ spec.homepage = ""
25
+
26
+ spec.files = Dir['[A-Z]*', 'rakefile.rb', '*.gemspec'].reject { |f| f =~ /.lock/ }
27
+ spec.files += Dir['bin/**', 'lib/**/*.rb', 'test/**/*.rb', 'spec/**/*.rb', 'features/**/*.rb']
28
+ spec.executables = spec.files.grep( %r{^bin/} ) { |f| File.basename(f) }
29
+ spec.test_files = spec.files.grep( %r{^(test|spec|features)/} )
30
+
31
+ # spec.add_runtime_dependency 'facets', '~> 3.0'
32
+ # spec.add_runtime_dependency 'mustache', '~> 1.0'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.7'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'watch', '~> 0.1'
37
+ spec.add_development_dependency 'rr', '~> 1.1'
38
+ end
@@ -0,0 +1,20 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : lib/ruby_project_generator.rb
4
+ # PROJECT : RubyProjectGenerator
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ require 'fileutils'
11
+
12
+ module FolderTemplate
13
+ BASE_PATH = File.expand_path( File.join( File.dirname( __FILE__ ), ".." ) )
14
+ end
15
+
16
+ require_relative 'folder_template/version.rb'
17
+ require_relative 'folder_template/fs_adapter.rb'
18
+ require_relative 'folder_template/template_string.rb'
19
+ require_relative 'folder_template/template_folder_entry.rb'
20
+ require_relative 'folder_template/template_folder.rb'
@@ -0,0 +1,64 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : lib/folder_template/fs_adapter.rb
4
+ # PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ module FolderTemplate
11
+
12
+ class FsAdapter
13
+ attr_reader :base_path
14
+ attr_reader :opts
15
+
16
+ def initialize( base_path, **opts )
17
+ @base_path = base_path
18
+ @opts = opts
19
+ end
20
+
21
+ def makedirs( dirname )
22
+ target = File.join( base_path, dirname )
23
+ return if ( File.directory?( target ) )
24
+
25
+ puts "Creating diectory #{dirname} ..." if opts[:verbose]
26
+ FileUtils.makedirs( target )
27
+ end
28
+
29
+ def write_to_file( filename, content )
30
+ target = File.join( base_path, filename )
31
+
32
+ if ( File.exists?( target ) && ! opts[:overwrite_files] )
33
+ puts "Skiping file #{filename} ..." if opts[:verbose]
34
+ else
35
+ puts "Generating file #{filename} ..." if opts[:verbose]
36
+ FileUtils.makedirs( File.dirname( target ) )
37
+ File.write( target, content )
38
+ end
39
+ end
40
+
41
+ def append_to_file( filename, content )
42
+ target = File.join( base_path, filename )
43
+
44
+ puts "Appending content to file #{filename} ..." if opts[:verbose]
45
+ FileUtils.makedirs( File.dirname( target ) )
46
+ File.open( target, "a" ) { |f| f.write( content ) }
47
+ end
48
+ end # class FsAdapter
49
+
50
+ end
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
@@ -0,0 +1,58 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : lib/folder_template/template_folder.rb
4
+ # PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ module FolderTemplate
11
+
12
+ class TemplateFolder
13
+ attr_reader :entries
14
+
15
+ def initialize( path )
16
+ @entries = _load_template( path )
17
+ end
18
+
19
+ def variables
20
+ @variables ||= entries.each_with_object( Set.new ) do |e, variables|
21
+ variables.merge( e.variables )
22
+ end
23
+ end
24
+
25
+ def generate( fs, **env )
26
+ entries.each do |entry|
27
+ entry.generate( fs, env )
28
+ end
29
+ end
30
+
31
+
32
+ private
33
+ def _load_template( path )
34
+ prefix = File.join( path, "" )
35
+
36
+ Dir[File.join( path, "**", "*")].map do |source|
37
+ filename = source.gsub( prefix, '' )
38
+ content = File.read( source ) if !File.directory?( source )
39
+ TemplateFolderEntry.new( filename, content )
40
+ end
41
+ end
42
+ end # class TemplateFolder
43
+
44
+ end
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
@@ -0,0 +1,59 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : lib/folder_template/template_folder.rb
4
+ # PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ module FolderTemplate
11
+
12
+ class TemplateFolderEntry
13
+ attr_reader :filename_template
14
+ attr_reader :content_template
15
+ attr_reader :append
16
+
17
+ def initialize( filename, content )
18
+ @append, filename = _filter_append_marker( filename )
19
+ @filename_template = TemplateString.new( filename )
20
+ @content_template = TemplateString.new( content ) if !content.nil?
21
+ end
22
+
23
+ def variables
24
+ @variables ||= [filename_template, content_template].reject do |t|
25
+ t.nil?
26
+ end.each_with_object( Set.new ) do |t, variables|
27
+ variables.merge( t.variables )
28
+ end
29
+ end
30
+
31
+
32
+ def generate( fs, **env )
33
+ filename = filename_template.expand( env ).to_s
34
+ basename = File.basename( filename, File.extname( filename ) )
35
+
36
+ if ( content_template )
37
+ local_env = env.merge( filename:filename, basename:basename )
38
+ content = content_template.expand( local_env ).to_s
39
+ if ( append )
40
+ fs.append_to_file( filename, content )
41
+ else
42
+ fs.write_to_file( filename, content )
43
+ end
44
+ else
45
+ fs.makedirs( filename )
46
+ end
47
+ end
48
+
49
+ private
50
+ def _filter_append_marker( filename )
51
+ basename = File.basename( filename )
52
+ dirname = File.dirname( filename )
53
+
54
+ return false, filename if !basename.start_with?( ">>" )
55
+ return true, File.join( dirname, basename[2..-1] )
56
+ end
57
+ end # class TemplateFolderEntry
58
+
59
+ end
@@ -0,0 +1,68 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : lib/folder_template/template_string.rb
4
+ # PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ module FolderTemplate
11
+ class TemplateString
12
+ attr_reader :content
13
+
14
+ def initialize( template )
15
+ case template
16
+ when String
17
+ @content = _parse( template )
18
+ when Array
19
+ @content = template.select do |fragment|
20
+ !fragment.nil? && !fragment.empty?
21
+ end
22
+ when TemplateString
23
+ @content = template.content
24
+ end
25
+ end
26
+
27
+ def variables
28
+ @variables ||= _extract_variables( @content )
29
+ end
30
+
31
+ def expand( **env )
32
+ fragments = content.map do |fragment|
33
+ case fragment
34
+ when Symbol
35
+ env[fragment] || (yield fragment if block_given?)|| fragment
36
+ else
37
+ fragment
38
+ end
39
+ end.select { |fragment| !fragment.nil? && !fragment.empty? }
40
+ TemplateString.new( fragments )
41
+ end
42
+
43
+ def to_s
44
+ @content.map do |fragment|
45
+ ("{{#{fragment}}}" if Symbol === fragment) || fragment
46
+ end.join
47
+ end
48
+
49
+ private
50
+ VARIABLE_PATTERN = /{{(\w+)}}/
51
+
52
+ def _parse( template )
53
+ result = []
54
+ tail = template
55
+ loop do
56
+ head, token, tail = tail.partition( VARIABLE_PATTERN )
57
+ result << head if !head.empty?
58
+ break if $1.nil?
59
+ result << $1.to_sym
60
+ end
61
+ result
62
+ end
63
+
64
+ def _extract_variables( content )
65
+ Set.new( content.select { |s| Symbol === s } )
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,12 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : lib/folder_template/version.rb
4
+ # PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ module FolderTemplate
11
+ VERSION = '0.1.1'
12
+ end
data/rakefile.rb ADDED
@@ -0,0 +1,42 @@
1
+ # =============================================================================
2
+ #
3
+ # MODULE : rakefile.rb
4
+ # PROJECT : FolderTemplate
5
+ # DESCRIPTION :
6
+ #
7
+ # Copyright (c) 2016, Marc-Antoine Argenton. All rights reserved.
8
+ # =============================================================================
9
+
10
+ require 'bundler/gem_tasks'
11
+ require 'rake/testtask'
12
+
13
+ Rake::TestTask.new do |t|
14
+ t.libs << '.' << 'test'
15
+ t.test_files = FileList['test/**/test_*.rb']
16
+ t.verbose = false
17
+ end
18
+
19
+ begin
20
+ require 'watch'
21
+ w = `tput cols`.to_i || 80
22
+
23
+ def tty_red(str); "\e[31m#{str}\e[0m" end
24
+ def tty_green(str); "\e[32m#{str}\e[0m" end
25
+ def tty_blink(str); "\e[5m#{str}\e[25m" end
26
+ def tty_reverse_color(str); "\e[7m#{str}\e[27m" end
27
+
28
+
29
+ desc 'Run unit tests everytime a source or test file is changed'
30
+ task :autotest do
31
+ Watch.new( '**/*.rb' ) do
32
+ success = system "clear && rake test"
33
+
34
+ puts tty_green( "-" * w ) if success
35
+ puts tty_reverse_color(tty_red( "-" * w )) if !success
36
+ end
37
+ end
38
+
39
+ rescue Exception => e
40
+ end
41
+
42
+ task :default => [:test, :build]