avm-tools 0.22.0 → 0.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/avm/docker/container.rb +6 -5
- data/lib/avm/docker/registry.rb +1 -1
- data/lib/avm/docker/runner.rb +13 -2
- data/lib/avm/git/commit/deploy.rb +2 -2
- data/lib/avm/patches/object/template.rb +4 -21
- data/lib/avm/self.rb +16 -0
- data/lib/avm/self/docker_image.rb +16 -0
- data/lib/avm/self/instance.rb +19 -0
- data/lib/avm/stereotypes/eac_webapp_base0/apache_host.rb +3 -4
- data/lib/avm/tools/runner/self.rb +30 -0
- data/lib/avm/tools/runner/self/docker.rb +14 -0
- data/lib/avm/tools/version.rb +1 -1
- data/template/avm/self/docker_image/Dockerfile +17 -0
- data/template/avm/self/docker_image/entrypoint.sh +12 -0
- metadata +11 -7
- data/lib/avm/templates.rb +0 -51
- data/lib/avm/templates/directory.rb +0 -96
- data/lib/avm/templates/file.rb +0 -101
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cc8ec766645c9ba79f3bbee342a271a47dfbc9921b433b0de06c2dbdac82470
|
4
|
+
data.tar.gz: f2efe1291c604c06320b386de40ab0035641017c040ebc0fa3ff5a56a1b57b3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31f9530c35e93e73fa80ea12e25c1301dadeda7028624cea0ca58253b201f7c3f7b4f3c015f6e9bc6f4fd7db183010be9adb6c89fe8d858f3e38e97d600e90d7
|
7
|
+
data.tar.gz: 3aaa766a741baf3d4e3dd5d33219bc1342fe9c0683802e33ca79472327d7b7909c0e45c4d1fde482fbae3a3f817d9a5315958dac26a02ee9e534b02b23fb10e0
|
data/lib/avm/docker/container.rb
CHANGED
@@ -22,13 +22,13 @@ module Avm
|
|
22
22
|
if instance.docker_container_exist?
|
23
23
|
run_start
|
24
24
|
else
|
25
|
-
run_run
|
25
|
+
run_run(options)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
def run_run
|
29
|
+
def run_run(options)
|
30
30
|
infom "\"docker run #{instance.docker_container_name}...\""
|
31
|
-
::Avm::Executables.docker.command.append(run_run_arguments).system!
|
31
|
+
::Avm::Executables.docker.command.append(run_run_arguments(options)).system!
|
32
32
|
end
|
33
33
|
|
34
34
|
def run_start
|
@@ -36,9 +36,10 @@ module Avm
|
|
36
36
|
::Avm::Executables.docker.command.append(run_start_arguments).system!
|
37
37
|
end
|
38
38
|
|
39
|
-
def run_run_arguments
|
39
|
+
def run_run_arguments(options)
|
40
|
+
entrypoint_args = options[:entrypoint_args].if_present([])
|
40
41
|
['run', '-it', '--name', instance.docker_container_name] + instance.docker_run_arguments +
|
41
|
-
[instance.docker_image.tag]
|
42
|
+
[instance.docker_image.tag] + entrypoint_args
|
42
43
|
end
|
43
44
|
|
44
45
|
def run_start_arguments
|
data/lib/avm/docker/registry.rb
CHANGED
data/lib/avm/docker/runner.rb
CHANGED
@@ -14,7 +14,7 @@ module Avm
|
|
14
14
|
Manipulate Docker images.
|
15
15
|
|
16
16
|
Usage:
|
17
|
-
__PROGRAM__ [options] [-B <build-arg>...]
|
17
|
+
__PROGRAM__ [options] [-B <build-arg>...] [-E <run-arg>...]
|
18
18
|
__PROGRAM__ -h | --help
|
19
19
|
|
20
20
|
Options:
|
@@ -23,6 +23,7 @@ module Avm
|
|
23
23
|
-p --push Push the image to Docker registry.
|
24
24
|
-r --run Run or start a container with builded image.
|
25
25
|
-B --build-arg=<build-arg> Argument for "docker build".
|
26
|
+
-E --entrypoint-arg=<run-arg> Argument for entrypoint on "docker run"
|
26
27
|
-c --clear Remove container if exist before run.
|
27
28
|
DOCOPT
|
28
29
|
|
@@ -44,6 +45,7 @@ module Avm
|
|
44
45
|
infov 'Registry name', registry
|
45
46
|
infov 'Image name', instance.docker_image.tag
|
46
47
|
infov 'Build arguments', build_args
|
48
|
+
infov 'Entrypoint arguments', entrypoint_args
|
47
49
|
end
|
48
50
|
|
49
51
|
def build
|
@@ -54,12 +56,21 @@ module Avm
|
|
54
56
|
options.fetch('--build-arg')
|
55
57
|
end
|
56
58
|
|
59
|
+
def entrypoint_args
|
60
|
+
options.fetch('--entrypoint-arg')
|
61
|
+
end
|
62
|
+
|
57
63
|
def push
|
58
64
|
instance.docker_image.push if options.fetch('--push')
|
59
65
|
end
|
60
66
|
|
61
67
|
def container_run
|
62
|
-
|
68
|
+
return unless options.fetch('--run')
|
69
|
+
|
70
|
+
instance.docker_container.run(
|
71
|
+
entrypoint_args: entrypoint_args,
|
72
|
+
clear: options.fetch('--clear')
|
73
|
+
)
|
63
74
|
end
|
64
75
|
|
65
76
|
def registry_uncached
|
@@ -4,7 +4,7 @@ require 'addressable'
|
|
4
4
|
require 'eac_ruby_utils/simple_cache'
|
5
5
|
require 'eac_ruby_utils/require_sub'
|
6
6
|
::EacRubyUtils.require_sub(__FILE__)
|
7
|
-
require 'avm/
|
7
|
+
require 'avm/patches/object/template'
|
8
8
|
|
9
9
|
module Avm
|
10
10
|
module Git
|
@@ -92,7 +92,7 @@ module Avm
|
|
92
92
|
def copy_appended_directory(directory)
|
93
93
|
raise 'Variables source not set' if variables_source.blank?
|
94
94
|
|
95
|
-
::
|
95
|
+
::EacRubyUtils::Templates::Directory.new(directory).apply(variables_source, build_dir)
|
96
96
|
end
|
97
97
|
|
98
98
|
def mkdir_target
|
@@ -1,24 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
require 'avm/templates'
|
3
|
+
require 'eac_ruby_utils/patches/object/template'
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@template ||= ::Avm::Templates.template(name.underscore)
|
10
|
-
end
|
11
|
-
|
12
|
-
def template_path
|
13
|
-
@template_path ||= ::Avm::Templates.template_path(name.underscore)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def template
|
18
|
-
self.class.template
|
19
|
-
end
|
20
|
-
|
21
|
-
def template_path
|
22
|
-
self.class.template_path
|
23
|
-
end
|
24
|
-
end
|
5
|
+
::EacRubyUtils::Templates::Searcher.default.included_paths << ::File.expand_path(
|
6
|
+
('../' * 4) + 'template', __dir__
|
7
|
+
)
|
data/lib/avm/self.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/require_sub'
|
4
|
+
require 'avm/instances/base'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Self
|
8
|
+
::EacRubyUtils.require_sub(__FILE__)
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def instance
|
12
|
+
@instance ||= ::Avm::Self::Instance.by_id('avm-tools_self')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'avm/docker/image'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Self
|
8
|
+
class DockerImage < ::Avm::Docker::Image
|
9
|
+
common_constructor :registry
|
10
|
+
|
11
|
+
def tag
|
12
|
+
registry.name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/instances/base'
|
4
|
+
require 'avm/self/docker_image'
|
5
|
+
require 'avm/stereotypes/eac_ubuntu_base0/docker_image'
|
6
|
+
|
7
|
+
module Avm
|
8
|
+
module Self
|
9
|
+
class Instance < ::Avm::Instances::Base
|
10
|
+
def docker_image_class
|
11
|
+
::Avm::Self::DockerImage
|
12
|
+
end
|
13
|
+
|
14
|
+
def docker_run_arguments
|
15
|
+
['-e', "LOCAL_USER_ID=#{::Process.uid}"]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'eac_ruby_utils/core_ext'
|
4
4
|
require 'avm/patches/object/template'
|
5
5
|
require 'avm/stereotypes/eac_ubuntu_base0/apache'
|
6
|
-
require 'avm/
|
6
|
+
require 'avm/patches/object/template'
|
7
7
|
|
8
8
|
module Avm
|
9
9
|
module Stereotypes
|
@@ -25,9 +25,8 @@ module Avm
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def no_ssl_site_content
|
28
|
-
::Avm::
|
29
|
-
|
30
|
-
).apply(EntriesReader.new(self, instance))
|
28
|
+
::Avm::Stereotypes::EacWebappBase0::ApacheHost.template.child('no_ssl.conf')
|
29
|
+
.apply(EntriesReader.new(self, instance))
|
31
30
|
end
|
32
31
|
|
33
32
|
private
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/console/docopt_runner'
|
4
|
+
require 'eac_ruby_utils/require_sub'
|
5
|
+
require 'avm/self'
|
6
|
+
|
7
|
+
module Avm
|
8
|
+
module Tools
|
9
|
+
class Runner < ::EacRubyUtils::Console::DocoptRunner
|
10
|
+
class Self < ::EacRubyUtils::Console::DocoptRunner
|
11
|
+
::EacRubyUtils.require_sub(__FILE__)
|
12
|
+
|
13
|
+
DOC = <<~DOCOPT
|
14
|
+
Utilities for self avm-tools.
|
15
|
+
|
16
|
+
Usage:
|
17
|
+
__PROGRAM__ [options] __SUBCOMMANDS__
|
18
|
+
__PROGRAM__ -h | --help
|
19
|
+
|
20
|
+
Options:
|
21
|
+
-h --help Show this screen.
|
22
|
+
DOCOPT
|
23
|
+
|
24
|
+
def instance
|
25
|
+
::Avm::Self.instance
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/docker/runner'
|
4
|
+
|
5
|
+
module Avm
|
6
|
+
module Tools
|
7
|
+
class Runner < ::EacRubyUtils::Console::DocoptRunner
|
8
|
+
class Self < ::EacRubyUtils::Console::DocoptRunner
|
9
|
+
class Docker < ::Avm::Docker::Runner
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/avm/tools/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
FROM ruby:2.4
|
2
|
+
RUN gem install avm-tools
|
3
|
+
|
4
|
+
RUN apt-get update && apt-get -y --no-install-recommends install \
|
5
|
+
ca-certificates \
|
6
|
+
curl
|
7
|
+
|
8
|
+
RUN gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4
|
9
|
+
RUN curl -o /usr/local/bin/gosu -SL "https://github.com/tianon/gosu/releases/download/1.4/gosu-$(dpkg --print-architecture)" \
|
10
|
+
&& curl -o /usr/local/bin/gosu.asc -SL "https://github.com/tianon/gosu/releases/download/1.4/gosu-$(dpkg --print-architecture).asc" \
|
11
|
+
&& gpg --verify /usr/local/bin/gosu.asc \
|
12
|
+
&& rm /usr/local/bin/gosu.asc \
|
13
|
+
&& chmod +x /usr/local/bin/gosu
|
14
|
+
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
|
15
|
+
RUN chmod +x /usr/local/bin/entrypoint.sh
|
16
|
+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
|
17
|
+
CMD []
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# Add local user
|
4
|
+
# Either use the LOCAL_USER_ID if passed in at runtime or
|
5
|
+
# fallback
|
6
|
+
|
7
|
+
USER_ID=${LOCAL_USER_ID:-9001}
|
8
|
+
|
9
|
+
echo "Starting with UID : $USER_ID"
|
10
|
+
useradd --shell /bin/bash -u $USER_ID -o -c "" -m user
|
11
|
+
export HOME=/home/user
|
12
|
+
exec /usr/local/bin/gosu user avm "$@"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: avm-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.23.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Esquilo Azul Company
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aranha-parsers
|
@@ -76,14 +76,14 @@ dependencies:
|
|
76
76
|
requirements:
|
77
77
|
- - "~>"
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: '0.
|
79
|
+
version: '0.17'
|
80
80
|
type: :runtime
|
81
81
|
prerelease: false
|
82
82
|
version_requirements: !ruby/object:Gem::Requirement
|
83
83
|
requirements:
|
84
84
|
- - "~>"
|
85
85
|
- !ruby/object:Gem::Version
|
86
|
-
version: '0.
|
86
|
+
version: '0.17'
|
87
87
|
- !ruby/object:Gem::Dependency
|
88
88
|
name: filesize
|
89
89
|
requirement: !ruby/object:Gem::Requirement
|
@@ -222,6 +222,9 @@ files:
|
|
222
222
|
- lib/avm/patches/eac_launcher_git_base.rb
|
223
223
|
- lib/avm/patches/object/template.rb
|
224
224
|
- lib/avm/result.rb
|
225
|
+
- lib/avm/self.rb
|
226
|
+
- lib/avm/self/docker_image.rb
|
227
|
+
- lib/avm/self/instance.rb
|
225
228
|
- lib/avm/stereotypes.rb
|
226
229
|
- lib/avm/stereotypes/eac_rails_base0.rb
|
227
230
|
- lib/avm/stereotypes/eac_rails_base0/apache_host.rb
|
@@ -244,9 +247,6 @@ files:
|
|
244
247
|
- lib/avm/stereotypes/postgresql/instance.rb
|
245
248
|
- lib/avm/stereotypes/postgresql/instance/data_unit.rb
|
246
249
|
- lib/avm/stereotypes/postgresql/instance_with.rb
|
247
|
-
- lib/avm/templates.rb
|
248
|
-
- lib/avm/templates/directory.rb
|
249
|
-
- lib/avm/templates/file.rb
|
250
250
|
- lib/avm/tools.rb
|
251
251
|
- lib/avm/tools/git.rb
|
252
252
|
- lib/avm/tools/runner.rb
|
@@ -270,7 +270,11 @@ files:
|
|
270
270
|
- lib/avm/tools/runner/git/dirty_files.rb
|
271
271
|
- lib/avm/tools/runner/git/issue.rb
|
272
272
|
- lib/avm/tools/runner/git/issue/complete.rb
|
273
|
+
- lib/avm/tools/runner/self.rb
|
274
|
+
- lib/avm/tools/runner/self/docker.rb
|
273
275
|
- lib/avm/tools/version.rb
|
276
|
+
- template/avm/self/docker_image/Dockerfile
|
277
|
+
- template/avm/self/docker_image/entrypoint.sh
|
274
278
|
- template/avm/stereotypes/eac_rails_base0/deploy/config/database.yml.template
|
275
279
|
- template/avm/stereotypes/eac_ubuntu_base0/docker_image/Dockerfile
|
276
280
|
- template/avm/stereotypes/eac_webapp_base0/apache_host/no_ssl.conf
|
data/lib/avm/templates.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'eac_ruby_utils/require_sub'
|
4
|
-
::EacRubyUtils.require_sub(__FILE__)
|
5
|
-
|
6
|
-
module Avm
|
7
|
-
module Templates
|
8
|
-
class << self
|
9
|
-
def template(subpath, required = true)
|
10
|
-
path = template_path(subpath)
|
11
|
-
if path.blank?
|
12
|
-
return nil unless required
|
13
|
-
|
14
|
-
raise "Template not found for subpath \"#{subpath}\" (Included paths: #{included_paths})"
|
15
|
-
end
|
16
|
-
return ::Avm::Templates::File.new(path) if ::File.file?(path)
|
17
|
-
return ::Avm::Templates::Directory.new(path) if ::File.directory?(path)
|
18
|
-
|
19
|
-
raise 'Invalid branching'
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return The absolute path of template if found, +nil+ otherwise.
|
23
|
-
def template_path(subpath)
|
24
|
-
included_paths.each do |included_path|
|
25
|
-
r = search_template_in_included_path(included_path, subpath)
|
26
|
-
return r if r
|
27
|
-
end
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def included_paths
|
32
|
-
@included_paths ||= ::Set.new([::File.expand_path('../../template', __dir__)])
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def search_template_in_included_path(included_path, subpath)
|
38
|
-
path = ::File.join(included_path, subpath)
|
39
|
-
dir = ::File.dirname(path)
|
40
|
-
Dir.entries(dir).each do |entry|
|
41
|
-
next if %w[. ..].include?(entry)
|
42
|
-
return path if template_basename(entry) == ::File.basename(subpath)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def template_basename(entry)
|
47
|
-
entry.gsub(/(?:\.[a-z0-9]+)+\z/i, '')
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'avm/templates/file'
|
4
|
-
|
5
|
-
module Avm
|
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
|
-
private
|
21
|
-
|
22
|
-
def apply_fs_object(source_relative, target)
|
23
|
-
if ::File.directory?(source_absolute(source_relative))
|
24
|
-
apply_directory(source_relative, target)
|
25
|
-
elsif ::File.file?(source_absolute(source_relative))
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def source_absolute(source_relative)
|
30
|
-
::File.expand_path(source_relative, path)
|
31
|
-
end
|
32
|
-
|
33
|
-
class TemplateNode
|
34
|
-
attr_reader :source_directory, :source_relative, :target_root_directory, :variables_source
|
35
|
-
|
36
|
-
def initialize(source_directory, source_relative, target_root_directory, variables_source)
|
37
|
-
@source_directory = source_directory
|
38
|
-
@source_relative = source_relative
|
39
|
-
@target_root_directory = target_root_directory
|
40
|
-
@variables_source = variables_source
|
41
|
-
end
|
42
|
-
|
43
|
-
def apply
|
44
|
-
if file?
|
45
|
-
apply_file
|
46
|
-
elsif directory?
|
47
|
-
apply_directory
|
48
|
-
else
|
49
|
-
raise "Unknown filesystem type: #{source_absolute}"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def apply_directory
|
56
|
-
::FileUtils.mkdir_p(target_absolute)
|
57
|
-
Dir.entries(source_absolute).each do |entry|
|
58
|
-
child(entry).apply unless %w[. ..].include?(entry)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def apply_file
|
63
|
-
if ::File.extname(source_absolute) == TEMPLATE_EXTNAME
|
64
|
-
::Avm::Templates::File.new(source_absolute).apply_to_file(
|
65
|
-
variables_source, target_absolute
|
66
|
-
)
|
67
|
-
else
|
68
|
-
::FileUtils.cp(source_absolute, target_absolute)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def child(entry)
|
73
|
-
TemplateNode.new(source_directory, ::File.join(source_relative, entry),
|
74
|
-
target_root_directory, variables_source)
|
75
|
-
end
|
76
|
-
|
77
|
-
def file?
|
78
|
-
::File.file?(source_absolute)
|
79
|
-
end
|
80
|
-
|
81
|
-
def directory?
|
82
|
-
::File.directory?(source_absolute)
|
83
|
-
end
|
84
|
-
|
85
|
-
def source_absolute
|
86
|
-
::File.expand_path(source_relative, source_directory.path)
|
87
|
-
end
|
88
|
-
|
89
|
-
def target_absolute
|
90
|
-
::File.expand_path(source_relative, target_root_directory)
|
91
|
-
.gsub(/#{::Regexp.quote(TEMPLATE_EXTNAME)}\z/, '')
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
data/lib/avm/templates/file.rb
DELETED
@@ -1,101 +0,0 @@
|
|
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 Avm
|
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
|