ehbrs-tools 0.3.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 +7 -0
- data/Gemfile +13 -0
- data/exe/ehbrs +6 -0
- data/lib/ehbrs.rb +7 -0
- data/lib/ehbrs/executables.rb +28 -0
- data/lib/ehbrs/tools.rb +9 -0
- data/lib/ehbrs/tools/runner.rb +35 -0
- data/lib/ehbrs/tools/runner/vg.rb +26 -0
- data/lib/ehbrs/tools/runner/vg/ips.rb +90 -0
- data/lib/ehbrs/tools/runner/videos.rb +25 -0
- data/lib/ehbrs/tools/runner/videos/unsupported.rb +65 -0
- data/lib/ehbrs/tools/version.rb +7 -0
- data/lib/ehbrs/videos.rb +9 -0
- data/lib/ehbrs/videos/convert_job.rb +99 -0
- data/lib/ehbrs/videos/file.rb +70 -0
- data/lib/ehbrs/videos/profiles/base.rb +35 -0
- data/lib/ehbrs/videos/profiles/same_quality.rb +21 -0
- data/lib/ehbrs/videos/track.rb +27 -0
- data/lib/ehbrs/videos/unsupported/check_result.rb +24 -0
- data/lib/ehbrs/videos/unsupported/check_set.rb +43 -0
- data/lib/ehbrs/videos/unsupported/check_support.rb +68 -0
- data/lib/ehbrs/videos/unsupported/checks.rb +13 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_extra_unlisted.rb +27 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_extra_unsupported.rb +26 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_unlisted.rb +23 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_unsupported.rb +25 -0
- data/lib/ehbrs/videos/unsupported/checks/invalid_extension.rb +25 -0
- data/lib/ehbrs/videos/unsupported/file.rb +50 -0
- data/lib/ehbrs/videos/unsupported/file/fix.rb +45 -0
- data/lib/ehbrs/videos/unsupported/fix_profile.rb +56 -0
- data/lib/ehbrs/videos/unsupported/fixes/supported_codec.rb +36 -0
- data/lib/ehbrs/videos/unsupported/fixes/supported_container.rb +17 -0
- data/lib/ehbrs/videos/unsupported/profiles.rb +13 -0
- data/lib/ehbrs/videos/unsupported/profiles/base.rb +102 -0
- data/lib/ehbrs/videos/unsupported/profiles/philco.rb +27 -0
- data/lib/ehbrs/videos/unsupported/profiles/samsung.rb +32 -0
- data/lib/ehbrs/videos/unsupported/search.rb +72 -0
- data/lib/ehbrs/videos/unsupported/track.rb +32 -0
- data/vendor/eac_cli/Gemfile +5 -0
- data/vendor/eac_cli/eac_cli.gemspec +18 -0
- data/vendor/eac_cli/lib/eac_cli.rb +7 -0
- data/vendor/eac_cli/lib/eac_cli/default_runner.rb +22 -0
- data/vendor/eac_cli/lib/eac_cli/runner.rb +30 -0
- data/vendor/eac_cli/lib/eac_cli/runner/argument_option.rb +13 -0
- data/vendor/eac_cli/lib/eac_cli/runner/base_option.rb +22 -0
- data/vendor/eac_cli/lib/eac_cli/runner/boolean_option.rb +13 -0
- data/vendor/eac_cli/lib/eac_cli/runner/definition.rb +60 -0
- data/vendor/eac_cli/lib/eac_cli/runner/docopt_doc.rb +65 -0
- data/vendor/eac_cli/lib/eac_cli/runner/positional_argument.rb +15 -0
- data/vendor/eac_cli/lib/eac_cli/version.rb +5 -0
- data/vendor/eac_cli/spec/rubocop_spec.rb +7 -0
- data/vendor/eac_cli/spec/spec_helper.rb +100 -0
- data/vendor/eac_ruby_utils/Gemfile +16 -0
- data/vendor/eac_ruby_utils/MIT-LICENCE +16 -0
- data/vendor/eac_ruby_utils/README.rdoc +3 -0
- data/vendor/eac_ruby_utils/eac_ruby_utils.gemspec +25 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/arguments_consumer.rb +81 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/common_constructor.rb +133 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb +82 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb +66 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs.rb +135 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner.rb +39 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb +18 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_doc.rb +25 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_settings.rb +19 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb +139 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker.rb +134 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker/_class_methods.rb +39 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker/list.rb +63 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker/node.rb +25 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/contextualizable.rb +16 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/core_ext.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/base_env.rb +28 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/command.rb +155 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/command/extra_options.rb +46 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb +51 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/file.rb +22 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/local_env.rb +17 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/process.rb +19 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/ssh_env.rb +72 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb +53 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_traverser.rb +56 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/fs_cache.rb +11 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable.rb +17 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/class_methods.rb +14 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/instance_methods.rb +30 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/integer_list.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/list.rb +89 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb +61 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/string_list.rb +19 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/value.rb +38 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/on_clean_ruby_environment.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/options_consumer.rb +62 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patch.rb +11 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches.rb +4 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/class.rb +4 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/class/common_constructor.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/hash.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/hash/options_consumer.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module.rb +4 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/console_speaker.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/listable.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/patch.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/require_sub.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/simple_cache.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/asserts.rb +14 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/if_present.rb +12 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/template.rb +16 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb +95 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb +11 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/rspec.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/rspec/conditional.rb +38 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/rspec/stubbed_ssh.rb +44 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/settings_provider.rb +65 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb +42 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/directory.rb +104 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/file.rb +101 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb +55 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/version.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/yaml.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/arguments_consumer_spec.rb +17 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/common_constructor_spec.rb +36 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/configs_spec.rb +31 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/configs_spec.rb +14 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/docopt_runner_spec.rb +136 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/speaker_spec.rb +36 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/envs/executable_spec.rb +33 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/envs/ssh_env_spec.rb +25 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/filesystem_cache_spec.rb +32 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/listable_spec.rb +180 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/options_consumer_spec.rb +45 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/hash/options_consumer_spec.rb +17 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/hash/sym_keys_hash_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/module/console_speaker_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/module/listable_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/module/simple_cache_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/if_present_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/template_spec.rb +23 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/template_spec_files/path/my_stub_with_template +1 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/paths_hash_spec.rb +49 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/settings_provider_spec.rb +46 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/simple_cache_spec.rb +102 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/file_spec.rb +34 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/file_spec_files/expected_content +2 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/file_spec_files/source.template +2 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec.rb +30 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec_files/path1/subdir1/file1.template +1 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec_files/path1/subdir1/file2 +1 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec_files/path2/subdir1/file3.template +1 -0
- data/vendor/eac_ruby_utils/spec/locales/pt-BR.yml +38 -0
- data/vendor/eac_ruby_utils/spec/spec_helper.rb +12 -0
- metadata +227 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/object/blank'
|
|
4
|
+
require 'eac_ruby_utils/console/speaker'
|
|
5
|
+
|
|
6
|
+
module EacRubyUtils
|
|
7
|
+
module Rspec
|
|
8
|
+
class Conditional
|
|
9
|
+
include ::EacRubyUtils::Console::Speaker
|
|
10
|
+
|
|
11
|
+
def self.default
|
|
12
|
+
@default ||= new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize
|
|
16
|
+
@tags = {}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def add(tag, &condition)
|
|
20
|
+
tags[tag] = condition
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def configure(rspec_config)
|
|
24
|
+
tags.each do |tag, condition|
|
|
25
|
+
message = condition.call
|
|
26
|
+
if message.present?
|
|
27
|
+
warn("Excluded tag: #{tag}: #{message}")
|
|
28
|
+
rspec_config.filter_run_excluding tag
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
attr_reader :tags
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/object/blank'
|
|
4
|
+
require 'eac_ruby_utils/envs/ssh_env'
|
|
5
|
+
require 'eac_ruby_utils/patches/object/if_present'
|
|
6
|
+
|
|
7
|
+
module EacRubyUtils
|
|
8
|
+
module Rspec
|
|
9
|
+
class StubbedSsh
|
|
10
|
+
DEFAULT_ENVVAR_NAME = 'STUBBED_SSH_URL'
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def default
|
|
14
|
+
@default ||= new(DEFAULT_ENVVAR_NAME)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
attr_reader :envvar_name
|
|
19
|
+
|
|
20
|
+
def initialize(envvar_name)
|
|
21
|
+
@envvar_name = envvar_name
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def validate
|
|
25
|
+
return nil if provided_url.present?
|
|
26
|
+
|
|
27
|
+
"Environment variable \"#{envvar_name}\" unprovided or blank"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def validate!
|
|
31
|
+
validate.if_present { |v| raise v }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def provided_url
|
|
35
|
+
ENV[envvar_name]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def build_env
|
|
39
|
+
validate!
|
|
40
|
+
::EacRubyUtils::Envs::SshEnv.new(provided_url)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/string/inflections'
|
|
4
|
+
require 'eac_ruby_utils/options_consumer'
|
|
5
|
+
|
|
6
|
+
module EacRubyUtils
|
|
7
|
+
# Provide a option by constant, method or options object.
|
|
8
|
+
module SettingsProvider
|
|
9
|
+
def setting_constant_name(key, fullname = false)
|
|
10
|
+
name = key.to_s.underscore.upcase
|
|
11
|
+
name = "#{self.class.name}::#{name}" if fullname
|
|
12
|
+
name
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def setting_search_order
|
|
16
|
+
%w[settings_object method constant]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def settings_object
|
|
20
|
+
respond_to?(settings_object_name) ? send(settings_object_name) : {}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def settings_object_name
|
|
24
|
+
'settings'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def setting_value(key, options = {})
|
|
28
|
+
options = parse_setting_value_options(options)
|
|
29
|
+
options.order.each do |method|
|
|
30
|
+
value = send("setting_value_by_#{method}", key)
|
|
31
|
+
return value if value
|
|
32
|
+
end
|
|
33
|
+
return nil unless options.required
|
|
34
|
+
|
|
35
|
+
raise "Setting \"#{key}\" not found. Supply in #{settings_object_name}, implement a " \
|
|
36
|
+
"\"#{key}\" method or declare a #{setting_constant_name(key, true)} constant."
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def setting_value_by_constant(key)
|
|
40
|
+
constant_name = setting_constant_name(key)
|
|
41
|
+
begin
|
|
42
|
+
self.class.const_get(constant_name)
|
|
43
|
+
rescue NameError
|
|
44
|
+
nil
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def setting_value_by_method(key)
|
|
49
|
+
respond_to?(key) ? send(key) : nil
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def setting_value_by_settings_object(key)
|
|
53
|
+
settings_object[key.to_s] || settings_object[key.to_sym]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def parse_setting_value_options(options)
|
|
59
|
+
r = ::EacRubyUtils::OptionsConsumer.new(options).consume_all(:required, :order, ostruct: true)
|
|
60
|
+
r.required = true if r.required.nil?
|
|
61
|
+
r.order = setting_search_order if r.order.nil?
|
|
62
|
+
r
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module EacRubyUtils
|
|
4
|
+
module SimpleCache
|
|
5
|
+
UNCACHED_METHOD_PATTERN = /\A(\s+)_uncached\z/.freeze
|
|
6
|
+
|
|
7
|
+
def method_missing(method, *args, &block)
|
|
8
|
+
uncached_method = "#{method}_uncached"
|
|
9
|
+
if respond_to?(uncached_method, true)
|
|
10
|
+
call_method_with_cache(uncached_method, args, &block)
|
|
11
|
+
else
|
|
12
|
+
super
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def respond_to_missing?(method, include_all = false)
|
|
17
|
+
if method.to_s.end_with?('_uncached')
|
|
18
|
+
super
|
|
19
|
+
else
|
|
20
|
+
respond_to?("#{method}_uncached", true) || super
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def reset_cache
|
|
25
|
+
@cache_keys = nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def call_method_with_cache(method, args, &block)
|
|
31
|
+
raise 'Não é possível realizar o cache de métodos com bloco' if block
|
|
32
|
+
|
|
33
|
+
key = ([method] + args).join('@@@')
|
|
34
|
+
cache_keys[key] = send(method, *args) unless cache_keys.key?(key)
|
|
35
|
+
cache_keys[key]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def cache_keys
|
|
39
|
+
@cache_keys ||= {}
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
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
|