eac_templates 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: af93be7bf79c5e3ecf4cd7eb1ef371076cff21ee1a3df7a91f87214b4720f4cb
4
+ data.tar.gz: b52cb1e8a2aa0ccf991c649bc02cd601cb83f26c1d838e7f1046886f17517bc8
5
+ SHA512:
6
+ metadata.gz: a5950e2d66256f9f89b99419017911ebd66a9242afa40d1b6d107b07e9b006811ae50af006c08fbbf1beb555d5b7365843ecb50214884212f53b869aea72cceb
7
+ data.tar.gz: dae82810a933ba499b2027833cefb832ce2cb90d8e9e9ec022c6de859829244944f1b6e91f2636691a674037769985e7f090247cc43e00bed3f4c6dfa00574b6
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacTemplates
6
+ require_sub __FILE__
7
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_templates/file'
4
+
5
+ module EacTemplates
6
+ class Directory
7
+ TEMPLATE_EXTNAME = '.template'
8
+
9
+ attr_reader :path
10
+
11
+ def initialize(path)
12
+ @path = path.is_a?(::Pathname) ? path : ::Pathname.new(path.to_s)
13
+ end
14
+
15
+ def apply(variables_source, directory)
16
+ TemplateNode.new(self, '.', directory, variables_source).apply
17
+ end
18
+
19
+ def child(subpath)
20
+ child_path = ::File.join(path, subpath)
21
+ return ::EacTemplates::File.new(child_path) if ::File.file?(child_path)
22
+ return ::EacTemplates::Directory.new(child_path) if ::File.directory?(child_path)
23
+
24
+ raise "Child \"#{subpath}\" from \"#{path}\" not found"
25
+ end
26
+
27
+ def children
28
+ path.children.map do |path_child|
29
+ child(path_child.basename.to_path)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def apply_fs_object(source_relative, target)
36
+ if ::File.directory?(source_absolute(source_relative))
37
+ apply_directory(source_relative, target)
38
+ elsif ::File.file?(source_absolute(source_relative))
39
+ end
40
+ end
41
+
42
+ def source_absolute(source_relative)
43
+ ::File.expand_path(source_relative, path)
44
+ end
45
+
46
+ class TemplateNode
47
+ attr_reader :source_directory, :source_relative, :target_root_directory, :variables_source
48
+
49
+ def initialize(source_directory, source_relative, target_root_directory, variables_source)
50
+ @source_directory = source_directory
51
+ @source_relative = source_relative
52
+ @target_root_directory = target_root_directory
53
+ @variables_source = variables_source
54
+ end
55
+
56
+ def apply
57
+ if file?
58
+ apply_file
59
+ elsif directory?
60
+ apply_directory
61
+ else
62
+ raise "Unknown filesystem type: #{source_absolute}"
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def apply_directory
69
+ ::FileUtils.mkdir_p(target_absolute)
70
+ Dir.entries(source_absolute).each do |entry|
71
+ child(entry).apply unless %w[. ..].include?(entry)
72
+ end
73
+ end
74
+
75
+ def apply_file
76
+ if ::File.extname(source_absolute) == TEMPLATE_EXTNAME
77
+ ::EacTemplates::File.new(source_absolute).apply_to_file(
78
+ variables_source, target_absolute
79
+ )
80
+ else
81
+ ::FileUtils.cp(source_absolute, target_absolute)
82
+ end
83
+ end
84
+
85
+ def child(entry)
86
+ TemplateNode.new(source_directory, ::File.join(source_relative, entry),
87
+ target_root_directory, variables_source)
88
+ end
89
+
90
+ def file?
91
+ ::File.file?(source_absolute)
92
+ end
93
+
94
+ def directory?
95
+ ::File.directory?(source_absolute)
96
+ end
97
+
98
+ def source_absolute
99
+ ::File.expand_path(source_relative, source_directory.path)
100
+ end
101
+
102
+ def target_absolute
103
+ ::File.expand_path(source_relative, target_root_directory)
104
+ .gsub(/#{::Regexp.quote(TEMPLATE_EXTNAME)}\z/, '')
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'eac_templates/variable_providers'
5
+
6
+ module EacTemplates
7
+ class File
8
+ VARIABLE_DELIMITER = ::Regexp.quote('%%')
9
+ VARIABLE_PATTERN = /#{VARIABLE_DELIMITER}([a-z0-9\._]*)#{VARIABLE_DELIMITER}/i.freeze
10
+
11
+ enable_simple_cache
12
+ common_constructor :path do
13
+ self.path = path.to_pathname
14
+ end
15
+
16
+ # +variables_provider+ A [Hash] or object which responds to +read_entry(entry_name)+.
17
+ def apply(variables_source)
18
+ variables_provider = ::EacTemplates::VariableProviders.build(variables_source)
19
+ variables.inject(content) do |a, e|
20
+ a.gsub(variable_pattern(e), variables_provider.variable_value(e).to_s)
21
+ end
22
+ end
23
+
24
+ def apply_to_file(variables_source, output_file_path)
25
+ output_file_path.to_pathname.write(apply(variables_source))
26
+ end
27
+
28
+ private
29
+
30
+ def variables_uncached
31
+ content.scan(VARIABLE_PATTERN).map(&:first).map do |name|
32
+ sanitize_variable_name(name)
33
+ end.to_set
34
+ end
35
+
36
+ def content_uncached
37
+ path.read
38
+ end
39
+
40
+ def sanitize_variable_name(variable_name)
41
+ variable_name.to_s.downcase
42
+ end
43
+
44
+ def variable_pattern(name)
45
+ /#{VARIABLE_DELIMITER}#{::Regexp.quote(name)}#{VARIABLE_DELIMITER}/i
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'eac_templates/searcher'
5
+
6
+ class Module
7
+ def template
8
+ @template ||= ::EacTemplates::Searcher.default.template(name.underscore)
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_templates/patches/module/template'
4
+
5
+ class Object
6
+ def template
7
+ self.class.template
8
+ end
9
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'eac_templates/directory'
5
+ require 'eac_templates/file'
6
+
7
+ module EacTemplates
8
+ class Searcher
9
+ class << self
10
+ def default
11
+ @default ||= new
12
+ end
13
+ end
14
+
15
+ def template(subpath, required = true)
16
+ path = template_path(subpath)
17
+ if path.blank?
18
+ return nil unless required
19
+
20
+ raise_template_not_found(subpath)
21
+ end
22
+ return ::EacTemplates::File.new(path) if ::File.file?(path)
23
+ return ::EacTemplates::Directory.new(path) if ::File.directory?(path)
24
+
25
+ raise 'Invalid branching'
26
+ end
27
+
28
+ # @return The absolute path of template if found, +nil+ otherwise.
29
+ def template_path(subpath)
30
+ included_paths.each do |included_path|
31
+ r = search_template_in_included_path(included_path, subpath)
32
+ return r if r
33
+ end
34
+ nil
35
+ end
36
+
37
+ def included_paths
38
+ @included_paths ||= ::Set.new
39
+ end
40
+
41
+ private
42
+
43
+ def raise_template_not_found(subpath)
44
+ raise "Template not found for subpath \"#{subpath}\"" \
45
+ " (Included paths: #{included_paths.to_a.join(::File::PATH_SEPARATOR)})"
46
+ end
47
+
48
+ def search_template_in_included_path(included_path, subpath)
49
+ path = ::File.join(included_path, subpath)
50
+ ::File.exist?(path) ? path : nil
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacTemplates
4
+ class VariableNotFoundError < StandardError; end
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacTemplates
6
+ module VariableProviders
7
+ require_sub __FILE__
8
+
9
+ PROVIDERS = %w[entries_reader hash generic].map do |name|
10
+ "eac_templates/variable_providers/#{name}".camelize.constantize
11
+ end
12
+
13
+ class << self
14
+ def build(variables_source)
15
+ PROVIDERS.each do |provider|
16
+ return provider.new(variables_source) if provider.accept?(variables_source)
17
+ end
18
+
19
+ raise "Variables provider not found for #{variables_source}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_templates/variable_not_found_error'
4
+
5
+ module EacTemplates
6
+ module VariableProviders
7
+ class Base
8
+ attr_reader :source
9
+
10
+ def initialize(source)
11
+ @source = source
12
+ end
13
+
14
+ def variable_value(name)
15
+ return variable_fetch(name) if variable_exist?(name)
16
+
17
+ raise VariableNotFoundError, "Variable \"#{name}\" not found in #{source}"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_templates/variable_providers/base'
4
+
5
+ module EacTemplates
6
+ module VariableProviders
7
+ class EntriesReader < ::EacTemplates::VariableProviders::Base
8
+ class << self
9
+ def accept?(variables_source)
10
+ variables_source.respond_to?(:read_entry)
11
+ end
12
+ end
13
+
14
+ def variable_exist?(_name)
15
+ true
16
+ end
17
+
18
+ def variable_fetch(name)
19
+ source.read_entry(name)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_templates/variable_providers/base'
4
+
5
+ module EacTemplates
6
+ module VariableProviders
7
+ class Generic < ::EacTemplates::VariableProviders::Base
8
+ class << self
9
+ def accept?(variables_source)
10
+ variables_source.is_a?(::Object)
11
+ end
12
+ end
13
+
14
+ def variable_exist?(name)
15
+ source.respond_to?(name)
16
+ end
17
+
18
+ def variable_fetch(name)
19
+ source.send(name)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_templates/variable_providers/base'
4
+
5
+ module EacTemplates
6
+ module VariableProviders
7
+ class Hash < ::EacTemplates::VariableProviders::Base
8
+ class << self
9
+ def accept?(variables_source)
10
+ variables_source.is_a?(::Hash)
11
+ end
12
+ end
13
+
14
+ def initialize(source)
15
+ super(source.with_indifferent_access)
16
+ end
17
+
18
+ def variable_exist?(name)
19
+ source.key?(name)
20
+ end
21
+
22
+ def variable_fetch(name)
23
+ source.fetch(name)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacTemplates
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eac_templates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Put here the authors
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eac_config
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eac_ruby_utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.64'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.64'
41
+ - !ruby/object:Gem::Dependency
42
+ name: eac_ruby_gem_support
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.2'
55
+ description:
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/eac_templates.rb
62
+ - lib/eac_templates/directory.rb
63
+ - lib/eac_templates/file.rb
64
+ - lib/eac_templates/patches/module/template.rb
65
+ - lib/eac_templates/patches/object/template.rb
66
+ - lib/eac_templates/searcher.rb
67
+ - lib/eac_templates/variable_not_found_error.rb
68
+ - lib/eac_templates/variable_providers.rb
69
+ - lib/eac_templates/variable_providers/base.rb
70
+ - lib/eac_templates/variable_providers/entries_reader.rb
71
+ - lib/eac_templates/variable_providers/generic.rb
72
+ - lib/eac_templates/variable_providers/hash.rb
73
+ - lib/eac_templates/version.rb
74
+ homepage:
75
+ licenses: []
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
+ rubygems_version: 3.0.9
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Put here de description.
96
+ test_files: []