eac_ruby_utils 0.16.0 → 0.17.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c932e97f7a56a87ac479e6897edc7726b810e7031c88049bcdaa674e3a47d8c
4
- data.tar.gz: cf4eb48992f065f1ba1bc0fbd5c76951679c30f8133b3f2c36f7d82e162e27fb
3
+ metadata.gz: bb7610983cbff65f045d8b281a7bb753f715dcdc7c5e4025852d8e44752f09d9
4
+ data.tar.gz: d496ac3d43969ac58e14a4e4f57f47bae6e5c07ccc721b440dc3095f5f34c290
5
5
  SHA512:
6
- metadata.gz: dde3dde0b173519bc0fcd6d799cd6896895087cce78310bd733fde0848b137674d5d11ff4cda4cc523a917ce7a0ca4228727c8c7e3d660c68ecbee13da02c93c
7
- data.tar.gz: 9f4e4f8f8a9f48d2817f0496be6617860256888fc2a50abb37306837872e3f23aef35eb1f4bf654667b352379d90cd6312f64177e700d06670439772dbfb643e
6
+ metadata.gz: 74516a7e43ac2a023a9e94c9f898b9ff2a8841778535225b0876707a7008ea46463ceb31296b67fb059d457afe5a5076b7eca03a7125cdeb753d90d059c4a2d4
7
+ data.tar.gz: 1d3c7f9c36ea92386781aab18ee5cf92e80e6f92305fa54a663f467b857b24f786ee55c1eff1e8d33cacf0f8c01f92c643df79c1aa0443b5dc4583150310de42
@@ -19,6 +19,10 @@ module EacRubyUtils
19
19
  load
20
20
  end
21
21
 
22
+ def clear
23
+ self.data = ::EacRubyUtils::PathsHash.new({})
24
+ end
25
+
22
26
  def save
23
27
  ::File.write(storage_path, data.to_h.to_yaml)
24
28
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'eac_ruby_utils/templates/searcher'
5
+
6
+ class Object
7
+ class << self
8
+ def template
9
+ @template ||= ::EacRubyUtils::Templates::Searcher.default.template(name.underscore)
10
+ end
11
+ end
12
+
13
+ def template
14
+ self.class.template
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+
5
+ module Avm
6
+ module Templates
7
+ ::EacRubyUtils.require_sub(__FILE__)
8
+ end
9
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/templates/file'
4
+
5
+ module EacRubyUtils
6
+ module Templates
7
+ class Directory
8
+ TEMPLATE_EXTNAME = '.template'
9
+
10
+ attr_reader :path
11
+
12
+ def initialize(path)
13
+ @path = path
14
+ end
15
+
16
+ def apply(variables_source, directory)
17
+ TemplateNode.new(self, '.', directory, variables_source).apply
18
+ end
19
+
20
+ def child(subpath)
21
+ child_path = ::File.join(path, subpath)
22
+ return ::EacRubyUtils::Templates::File.new(child_path) if ::File.file?(child_path)
23
+ return ::EacRubyUtils::Templates::Directory.new(child_path) if ::File.directory?(child_path)
24
+
25
+ raise "Child \"#{subpath}\" from \"#{path}\" not found"
26
+ end
27
+
28
+ private
29
+
30
+ def apply_fs_object(source_relative, target)
31
+ if ::File.directory?(source_absolute(source_relative))
32
+ apply_directory(source_relative, target)
33
+ elsif ::File.file?(source_absolute(source_relative))
34
+ end
35
+ end
36
+
37
+ def source_absolute(source_relative)
38
+ ::File.expand_path(source_relative, path)
39
+ end
40
+
41
+ class TemplateNode
42
+ attr_reader :source_directory, :source_relative, :target_root_directory, :variables_source
43
+
44
+ def initialize(source_directory, source_relative, target_root_directory, variables_source)
45
+ @source_directory = source_directory
46
+ @source_relative = source_relative
47
+ @target_root_directory = target_root_directory
48
+ @variables_source = variables_source
49
+ end
50
+
51
+ def apply
52
+ if file?
53
+ apply_file
54
+ elsif directory?
55
+ apply_directory
56
+ else
57
+ raise "Unknown filesystem type: #{source_absolute}"
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def apply_directory
64
+ ::FileUtils.mkdir_p(target_absolute)
65
+ Dir.entries(source_absolute).each do |entry|
66
+ child(entry).apply unless %w[. ..].include?(entry)
67
+ end
68
+ end
69
+
70
+ def apply_file
71
+ if ::File.extname(source_absolute) == TEMPLATE_EXTNAME
72
+ ::EacRubyUtils::Templates::File.new(source_absolute).apply_to_file(
73
+ variables_source, target_absolute
74
+ )
75
+ else
76
+ ::FileUtils.cp(source_absolute, target_absolute)
77
+ end
78
+ end
79
+
80
+ def child(entry)
81
+ TemplateNode.new(source_directory, ::File.join(source_relative, entry),
82
+ target_root_directory, variables_source)
83
+ end
84
+
85
+ def file?
86
+ ::File.file?(source_absolute)
87
+ end
88
+
89
+ def directory?
90
+ ::File.directory?(source_absolute)
91
+ end
92
+
93
+ def source_absolute
94
+ ::File.expand_path(source_relative, source_directory.path)
95
+ end
96
+
97
+ def target_absolute
98
+ ::File.expand_path(source_relative, target_root_directory)
99
+ .gsub(/#{::Regexp.quote(TEMPLATE_EXTNAME)}\z/, '')
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'eac_ruby_utils/simple_cache'
5
+
6
+ module EacRubyUtils
7
+ module Templates
8
+ class File
9
+ include ::EacRubyUtils::SimpleCache
10
+
11
+ VARIABLE_DELIMITER = ::Regexp.quote('%%')
12
+ VARIABLE_PATTERN = /#{VARIABLE_DELIMITER}([a-z0-9\._]*)#{VARIABLE_DELIMITER}/i.freeze
13
+
14
+ attr_reader :path
15
+
16
+ def initialize(path)
17
+ @path = path
18
+ end
19
+
20
+ # +variables_provider+ A [Hash] or object which responds to +read_entry(entry_name)+.
21
+ def apply(variables_source)
22
+ variables_provider = build_variables_provider(variables_source)
23
+ variables.inject(content) do |a, e|
24
+ a.gsub(variable_pattern(e), variables_provider.variable_value(e).to_s)
25
+ end
26
+ end
27
+
28
+ def apply_to_file(variables_source, output_file_path)
29
+ ::File.write(output_file_path, apply(variables_source))
30
+ end
31
+
32
+ private
33
+
34
+ def variables_uncached
35
+ content.scan(VARIABLE_PATTERN).map(&:first).map do |name|
36
+ sanitize_variable_name(name)
37
+ end.to_set
38
+ end
39
+
40
+ def content_uncached
41
+ ::File.read(path)
42
+ end
43
+
44
+ def sanitize_variable_name(variable_name)
45
+ variable_name.to_s.downcase
46
+ end
47
+
48
+ def build_variables_provider(variables_source)
49
+ return HashVariablesProvider.new(variables_source) if variables_source.is_a?(::Hash)
50
+ return EntriesReaderVariablesProvider.new(variables_source) if
51
+ variables_source.respond_to?(:read_entry)
52
+
53
+ raise "Variables provider not found for #{variables_source}"
54
+ end
55
+
56
+ def variable_pattern(name)
57
+ /#{VARIABLE_DELIMITER}#{::Regexp.quote(name)}#{VARIABLE_DELIMITER}/i
58
+ end
59
+
60
+ class BaseVariablesProvider
61
+ attr_reader :source
62
+
63
+ def initialize(source)
64
+ @source = source
65
+ end
66
+
67
+ def variable_value(name)
68
+ return variable_fetch(name) if variable_exist?(name)
69
+
70
+ raise VariableNotFoundError, "Variable \"#{name}\" not found in #{source}"
71
+ end
72
+ end
73
+
74
+ class HashVariablesProvider < BaseVariablesProvider
75
+ def initialize(source)
76
+ super(source.with_indifferent_access)
77
+ end
78
+
79
+ def variable_exist?(name)
80
+ source.key?(name)
81
+ end
82
+
83
+ def variable_fetch(name)
84
+ source.fetch(name)
85
+ end
86
+ end
87
+
88
+ class EntriesReaderVariablesProvider < BaseVariablesProvider
89
+ def variable_exist?(_name)
90
+ true
91
+ end
92
+
93
+ def variable_fetch(name)
94
+ source.read_entry(name)
95
+ end
96
+ end
97
+
98
+ class VariableNotFoundError < StandardError; end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'eac_ruby_utils/templates/directory'
5
+ require 'eac_ruby_utils/templates/file'
6
+
7
+ module EacRubyUtils
8
+ module Templates
9
+ class Searcher
10
+ class << self
11
+ def default
12
+ @default ||= new
13
+ end
14
+ end
15
+
16
+ def template(subpath, required = true)
17
+ path = template_path(subpath)
18
+ if path.blank?
19
+ return nil unless required
20
+
21
+ raise_template_not_found(subpath)
22
+ end
23
+ return ::EacRubyUtils::Templates::File.new(path) if ::File.file?(path)
24
+ return ::EacRubyUtils::Templates::Directory.new(path) if ::File.directory?(path)
25
+
26
+ raise 'Invalid branching'
27
+ end
28
+
29
+ # @return The absolute path of template if found, +nil+ otherwise.
30
+ def template_path(subpath)
31
+ included_paths.each do |included_path|
32
+ r = search_template_in_included_path(included_path, subpath)
33
+ return r if r
34
+ end
35
+ nil
36
+ end
37
+
38
+ def included_paths
39
+ @included_paths ||= ::Set.new
40
+ end
41
+
42
+ private
43
+
44
+ def raise_template_not_found(subpath)
45
+ raise "Template not found for subpath \"#{subpath}\"" \
46
+ " (Included paths: #{included_paths.to_a.join(::File::PATH_SEPARATOR)})"
47
+ end
48
+
49
+ def search_template_in_included_path(included_path, subpath)
50
+ path = ::File.join(included_path, subpath)
51
+ ::File.exist?(path) ? path : nil
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.16.0'
4
+ VERSION = '0.17.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_ruby_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-05 00:00:00.000000000 Z
11
+ date: 2019-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -166,12 +166,17 @@ files:
166
166
  - lib/eac_ruby_utils/patches/object.rb
167
167
  - lib/eac_ruby_utils/patches/object/asserts.rb
168
168
  - lib/eac_ruby_utils/patches/object/if_present.rb
169
+ - lib/eac_ruby_utils/patches/object/template.rb
169
170
  - lib/eac_ruby_utils/paths_hash.rb
170
171
  - lib/eac_ruby_utils/require_sub.rb
171
172
  - lib/eac_ruby_utils/rspec.rb
172
173
  - lib/eac_ruby_utils/rspec/conditional.rb
173
174
  - lib/eac_ruby_utils/rspec/stubbed_ssh.rb
174
175
  - lib/eac_ruby_utils/simple_cache.rb
176
+ - lib/eac_ruby_utils/templates.rb
177
+ - lib/eac_ruby_utils/templates/directory.rb
178
+ - lib/eac_ruby_utils/templates/file.rb
179
+ - lib/eac_ruby_utils/templates/searcher.rb
175
180
  - lib/eac_ruby_utils/version.rb
176
181
  - lib/eac_ruby_utils/yaml.rb
177
182
  homepage: