eac_ruby_utils 0.37.0 → 0.42.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68c46afaacfcbdd7bb63906f6d7ec668edfaeb5da500d46c68e8ee82e7ee6d20
4
- data.tar.gz: 184a7551426a3ba8dc7117254119ad49f53713fa420a67f9b7c80f8697b92f7d
3
+ metadata.gz: aae61dcf00f56fd19422516fdeddc796f6f18cb592bfda3d994ad91651f6ed9a
4
+ data.tar.gz: cec8de39973f51c39a7466249756105e3c37b80ffba2ab373f8a9a29f9eea8ca
5
5
  SHA512:
6
- metadata.gz: 3236d13fa87bba223b8d88387fb2e3f55de304a933f9e91f5919f51a9ab9fd69fd2f5dc6e355e172d24d37cb435d245e105344a4510f0aa6e6145230fd335c79
7
- data.tar.gz: ad219efa4bef8a2dc636f36c9e4a8535d37fe7ea2b90bb6633cf104860fa357cd4248896e70ea8f8894aa3dfd0ce793d559dd880e650327cb48302599db8d2cf
6
+ metadata.gz: 7c8ec736251746dabf77d8d97b35c0768995cc83817830ec9af8c7261ce3d100d74ae87143ba7a3fbd82ca81e2618c8b0fbf13367820babb29341a1bfcf28efc
7
+ data.tar.gz: 98ce82f43d3f029d947e368dc1cd519d69249182e73fb13d0ee683050e0b93e4770d029eda3bc02f07b80909ab16788bec35b6086fb44f6daa82ec38255ebc37
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class Boolean
5
+ class << self
6
+ def parse(value)
7
+ return parse_string(value) if value.is_a?(::String)
8
+ return parse_string(value.to_s) if value.is_a?(::Symbol)
9
+ return parse_number(value) if value.is_a?(::Number)
10
+
11
+ value ? true : false
12
+ end
13
+
14
+ private
15
+
16
+ def parse_string(value)
17
+ ['', 'n', 'no', 'f', 'false'].include?(value.strip.downcase) ? false : true
18
+ end
19
+
20
+ def parse_number(value)
21
+ value.zero?
22
+ end
23
+ end
24
+
25
+ attr_reader :value
26
+
27
+ def initialize(value)
28
+ @value = self.class.parse(value)
29
+ end
30
+ end
31
+ end
@@ -54,6 +54,7 @@ module EacRubyUtils
54
54
  end
55
55
 
56
56
  def execute!(options = {})
57
+ options[:exit_outputs] = status_results.merge(options[:exit_outputs].presence || {})
57
58
  er = ExecuteResult.new(execute(options), options)
58
59
  return er.result if er.success?
59
60
 
@@ -16,6 +16,10 @@ module EacRubyUtils
16
16
  duplicate_by_extra_options(envvars: envvars.merge(name => value))
17
17
  end
18
18
 
19
+ def status_result(status_code, result)
20
+ duplicate_by_extra_options(status_results: status_results.merge(status_code => result))
21
+ end
22
+
19
23
  def pipe(other_command)
20
24
  duplicate_by_extra_options(pipe: other_command)
21
25
  end
@@ -28,6 +32,10 @@ module EacRubyUtils
28
32
  extra_options[:envvars] ||= {}.with_indifferent_access
29
33
  end
30
34
 
35
+ def status_results
36
+ extra_options[:status_results] ||= {}.with_indifferent_access
37
+ end
38
+
31
39
  def append_envvars(command)
32
40
  e = envvars.map { |k, v| "#{Shellwords.escape(k)}=#{Shellwords.escape(v)}" }.join(' ')
33
41
  e.present? ? "#{e} #{command}" : command
@@ -2,12 +2,16 @@
2
2
 
3
3
  require 'addressable'
4
4
  require 'eac_ruby_utils/envs/base_env'
5
+ require 'eac_ruby_utils/patches/object/if_present'
6
+ require 'eac_ruby_utils/patches/module/require_sub'
5
7
  require 'net/ssh'
6
8
  require 'shellwords'
7
9
 
8
10
  module EacRubyUtils
9
11
  module Envs
10
12
  class SshEnv < ::EacRubyUtils::Envs::BaseEnv
13
+ require_sub __FILE__, include_modules: true
14
+
11
15
  USER_PATTERN = /[a-z_][a-z0-9_-]*/.freeze
12
16
  HOSTNAME_PATTERN = /[^@]+/.freeze
13
17
  USER_HOSTNAME_PATTERN = /\A(?:(#{USER_PATTERN})@)?(#{HOSTNAME_PATTERN})\z/.freeze
@@ -49,17 +53,16 @@ module EacRubyUtils
49
53
  private
50
54
 
51
55
  def ssh_command_line
52
- r = %w[ssh]
53
- r += ['-p', uri.port] if uri.port.present?
54
- r += ssh_command_line_options
55
- r << user_hostname_uri
56
- r.map { |a| Shellwords.escape(a) }.join(' ')
56
+ (%w[ssh] +
57
+ %w[nodasho dasho port].flat_map { |m| send("ssh_command_line_#{m}_args") } +
58
+ [user_hostname_uri])
59
+ .map { |a| Shellwords.escape(a) }.join(' ')
57
60
  end
58
61
 
59
- def ssh_command_line_options
60
- r = []
61
- uri.query_values&.each { |k, v| r += ['-o', "#{k}=#{v}"] }
62
- r
62
+ def ssh_command_line_port_args
63
+ uri.port.if_present([]) do |v|
64
+ ['-p', v]
65
+ end
63
66
  end
64
67
 
65
68
  def user_hostname_uri
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/module/common_concern'
4
+ require 'eac_ruby_utils/patches/module/simple_cache'
5
+
6
+ module EacRubyUtils
7
+ module Envs
8
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
9
+ module DashoOptions
10
+ common_concern
11
+
12
+ module ClassMethods
13
+ def add_nodasho_option(name)
14
+ return if nodasho_options.include?(name)
15
+
16
+ nodasho_options << name
17
+ const_set("#{name.underscore}_option".upcase, name)
18
+ end
19
+
20
+ def nodasho_options
21
+ @nodasho_options ||= []
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def ssh_command_line_dasho_args
27
+ r = []
28
+ uri.query_values&.each do |k, v|
29
+ r += ['-o', "#{k}=#{v}"] unless nodasho_options.include?(k)
30
+ end
31
+ r
32
+ end
33
+
34
+ def ssh_command_line_nodasho_args
35
+ nodasho_options.flat_map do |option_name|
36
+ uri_query_value(option_name).if_present([]) do |option_value|
37
+ send("ssh_command_line_#{option_name.underscore}_args", option_value)
38
+ end
39
+ end
40
+ end
41
+
42
+ def nodasho_options
43
+ self.class.nodasho_options
44
+ end
45
+
46
+ def uri_query_value(name)
47
+ uri.query_values.if_present { |v| v[name] }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'eac_ruby_utils/listable'
5
+ require 'eac_ruby_utils/patches/object/if_present'
6
+
7
+ module EacRubyUtils
8
+ module Envs
9
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
10
+ module IdentityFile
11
+ extend ::ActiveSupport::Concern
12
+
13
+ included do
14
+ add_nodasho_option('IdentityFile')
15
+ end
16
+
17
+ def ssh_command_line_identity_file_args(value)
18
+ ['-i', value]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'eac_ruby_utils/boolean'
5
+
6
+ module EacRubyUtils
7
+ module Envs
8
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
9
+ module Quiet
10
+ extend ::ActiveSupport::Concern
11
+
12
+ included do
13
+ add_nodasho_option('Quiet')
14
+ end
15
+
16
+ def ssh_command_line_quiet_args(value)
17
+ ::EacRubyUtils::Boolean.parse(value) ? ['-q'] : []
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'eac_ruby_utils/listable'
5
+ require 'eac_ruby_utils/patches/object/if_present'
6
+
7
+ module EacRubyUtils
8
+ module Envs
9
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
10
+ module Terminal
11
+ extend ::ActiveSupport::Concern
12
+
13
+ included do
14
+ add_nodasho_option('Terminal')
15
+ include ::EacRubyUtils::Listable
16
+ lists.add_string :terminal_option, :auto, :disable, :enable, :force
17
+ end
18
+
19
+ def ssh_command_line_terminal_args(value)
20
+ self.class.lists.terminal_option.value_validate!(value)
21
+ case value
22
+ when TERMINAL_OPTION_AUTO then ENV['TERM'].present? ? %w[-t] : []
23
+ when TERMINAL_OPTION_DISABLE then ['-T']
24
+ when TERMINAL_OPTION_ENABLE then ['-t']
25
+ when TERMINAL_OPTION_FORCE then ['-tt']
26
+ else raise "Invalid conditional branch: #{value}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/filesystem_cache'
4
+ require 'tmpdir'
4
5
 
5
6
  module EacRubyUtils
6
7
  class << self
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/module/common_concern'
4
+ require 'eac_ruby_utils/require_sub'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ ::EacRubyUtils.require_sub __FILE__
9
+
10
+ common_concern do
11
+ include ::EacRubyUtils::Listable
12
+ lists.add_symbol :type, :array, :boolean, :common, :hash
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/inflector'
4
+ require 'eac_ruby_utils/immutable/base_accessor'
5
+ require 'eac_ruby_utils/patches/class/common_constructor'
6
+
7
+ module EacRubyUtils
8
+ module Immutable
9
+ class ArrayAccessor < ::EacRubyUtils::Immutable::BaseAccessor
10
+ def apply(klass)
11
+ accessor = self
12
+ klass.send(:define_method, name) do |value|
13
+ accessor.immutable_value_set(self, value)
14
+ end
15
+
16
+ klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do
17
+ accessor.immutable_value_get(self)
18
+ end
19
+ end
20
+
21
+ def immutable_value_get(object)
22
+ super || []
23
+ end
24
+
25
+ def immutable_value_set(object, value)
26
+ duplicate_object(object) do |old_value|
27
+ (old_value || []) + [value]
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ class BaseAccessor
6
+ common_constructor :name do
7
+ self.name = name.to_sym
8
+ end
9
+
10
+ def duplicate_object(object)
11
+ accessor_new_value = yield(immutable_value_get(object))
12
+ new_values = object.send(:immutable_values_get).merge(name => accessor_new_value)
13
+ r = object.class.new(*object.immutable_constructor_args)
14
+ r.send(:immutable_values_set, new_values)
15
+ r
16
+ end
17
+
18
+ def immutable_value_get(object)
19
+ object.send(:immutable_values_get)[name]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/common_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class BooleanAccessor < ::EacRubyUtils::Immutable::CommonAccessor
9
+ def apply(klass)
10
+ super
11
+ accessor = self
12
+ klass.send(:define_method, "#{name}?") { send(accessor.name) }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ module ClassMethods
6
+ def immutable_accessor(*accessors)
7
+ options = accessors.extract_options!
8
+ options[:type] ||= const_get('TYPE_COMMON')
9
+ accessors.each do |name|
10
+ class_name = options.fetch(:type).to_s.camelize + 'Accessor'
11
+ ::EacRubyUtils::Immutable.const_get(class_name).new(name).apply(self)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def imutable_single_accessor(name, options); end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/base_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class CommonAccessor < ::EacRubyUtils::Immutable::BaseAccessor
9
+ def apply(klass)
10
+ accessor = self
11
+ klass.send(:define_method, name) do |*args|
12
+ case args.count
13
+ when 0 then next accessor.immutable_value_get(self)
14
+ when 1 then next accessor.immutable_value_set(self, args.first)
15
+ else
16
+ raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 0..1)"
17
+ end
18
+ end
19
+ end
20
+
21
+ def immutable_value_get(object)
22
+ object.send(:immutable_values_get)[name]
23
+ end
24
+
25
+ def immutable_value_set(object, value)
26
+ duplicate_object(object) { |_old_value| value }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/base_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class HashAccessor < ::EacRubyUtils::Immutable::BaseAccessor
9
+ def apply(klass)
10
+ apply_get(klass)
11
+ apply_set(klass)
12
+ end
13
+
14
+ def immutable_value_get(object)
15
+ super || {}
16
+ end
17
+
18
+ def immutable_value_set(object, key, value)
19
+ duplicate_object(object) do |old_value|
20
+ (old_value || {}).merge(key => value)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def apply_get(klass)
27
+ accessor = self
28
+ klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do
29
+ accessor.immutable_value_get(self)
30
+ end
31
+ end
32
+
33
+ def apply_set(klass)
34
+ accessor = self
35
+ klass.send(:define_method, name) do |*args|
36
+ case args.count
37
+ when 1 then next accessor.immutable_value_get(self, args[0])
38
+ when 2 then next accessor.immutable_value_set(self, *args[0..1])
39
+ else
40
+ raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 1..2)"
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ module InstanceMethods
6
+ def immutable_constructor_args
7
+ []
8
+ end
9
+
10
+ private
11
+
12
+ def immutable_values_get
13
+ @immutable_values || {}
14
+ end
15
+
16
+ def immutable_values_set(new_values)
17
+ @immutable_values = new_values
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patch'
4
+ require 'eac_ruby_utils/immutable'
5
+
6
+ class Module
7
+ def enable_immutable
8
+ ::EacRubyUtils.patch(self, ::EacRubyUtils::Immutable)
9
+ end
10
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/inflector'
4
+
3
5
  module EacRubyUtils
4
6
  class << self
5
7
  def require_sub(file, options = {})
@@ -26,6 +28,23 @@ module EacRubyUtils
26
28
 
27
29
  private
28
30
 
31
+ def active_support_require(path)
32
+ return false unless options[REQUIRE_DEPENDENCY_OPTION_KEY]
33
+
34
+ ::Kernel.require_dependency(path)
35
+ true
36
+ end
37
+
38
+ def autoload_require(path)
39
+ return false unless base?
40
+
41
+ basename = ::File.basename(path, '.*')
42
+ return false if basename.start_with?('_')
43
+
44
+ base.autoload ::ActiveSupport::Inflector.camelize(basename), path
45
+ true
46
+ end
47
+
29
48
  def include_modules
30
49
  return unless options[INCLUDE_MODULES_OPTION_KEY]
31
50
 
@@ -41,14 +60,22 @@ module EacRubyUtils
41
60
  options[BASE_OPTION_KEY] || raise('Option :base not setted')
42
61
  end
43
62
 
63
+ def base?
64
+ options[BASE_OPTION_KEY] ? true : false
65
+ end
66
+
67
+ def kernel_require(path)
68
+ ::Kernel.require(path)
69
+ end
70
+
44
71
  def require_sub_files
45
72
  Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].sort.each do |path|
46
- if options[REQUIRE_DEPENDENCY_OPTION_KEY]
47
- require_dependency path
48
- else
49
- require path
50
- end
73
+ require_sub_file(path)
51
74
  end
52
75
  end
76
+
77
+ def require_sub_file(path)
78
+ active_support_require(path) || autoload_require(path) || kernel_require(path)
79
+ end
53
80
  end
54
81
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.37.0'
4
+ VERSION = '0.42.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.37.0
4
+ version: 0.42.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: 2020-06-20 00:00:00.000000000 Z
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -109,6 +109,7 @@ files:
109
109
  - README.rdoc
110
110
  - lib/eac_ruby_utils.rb
111
111
  - lib/eac_ruby_utils/arguments_consumer.rb
112
+ - lib/eac_ruby_utils/boolean.rb
112
113
  - lib/eac_ruby_utils/by_reference.rb
113
114
  - lib/eac_ruby_utils/common_concern.rb
114
115
  - lib/eac_ruby_utils/common_constructor.rb
@@ -139,6 +140,10 @@ files:
139
140
  - lib/eac_ruby_utils/envs/process.rb
140
141
  - lib/eac_ruby_utils/envs/spawn.rb
141
142
  - lib/eac_ruby_utils/envs/ssh_env.rb
143
+ - lib/eac_ruby_utils/envs/ssh_env/dasho_options.rb
144
+ - lib/eac_ruby_utils/envs/ssh_env/identity_file.rb
145
+ - lib/eac_ruby_utils/envs/ssh_env/quiet.rb
146
+ - lib/eac_ruby_utils/envs/ssh_env/terminal.rb
142
147
  - lib/eac_ruby_utils/filesystem_cache.rb
143
148
  - lib/eac_ruby_utils/fs.rb
144
149
  - lib/eac_ruby_utils/fs/extname.rb
@@ -148,6 +153,14 @@ files:
148
153
  - lib/eac_ruby_utils/fs/traversable.rb
149
154
  - lib/eac_ruby_utils/fs/traverser.rb
150
155
  - lib/eac_ruby_utils/fs_cache.rb
156
+ - lib/eac_ruby_utils/immutable.rb
157
+ - lib/eac_ruby_utils/immutable/array_accessor.rb
158
+ - lib/eac_ruby_utils/immutable/base_accessor.rb
159
+ - lib/eac_ruby_utils/immutable/boolean_accessor.rb
160
+ - lib/eac_ruby_utils/immutable/class_methods.rb
161
+ - lib/eac_ruby_utils/immutable/common_accessor.rb
162
+ - lib/eac_ruby_utils/immutable/hash_accessor.rb
163
+ - lib/eac_ruby_utils/immutable/instance_methods.rb
151
164
  - lib/eac_ruby_utils/listable.rb
152
165
  - lib/eac_ruby_utils/listable/class_methods.rb
153
166
  - lib/eac_ruby_utils/listable/instance_methods.rb
@@ -171,6 +184,7 @@ files:
171
184
  - lib/eac_ruby_utils/patches/module.rb
172
185
  - lib/eac_ruby_utils/patches/module/common_concern.rb
173
186
  - lib/eac_ruby_utils/patches/module/console_speaker.rb
187
+ - lib/eac_ruby_utils/patches/module/immutable.rb
174
188
  - lib/eac_ruby_utils/patches/module/listable.rb
175
189
  - lib/eac_ruby_utils/patches/module/patch.rb
176
190
  - lib/eac_ruby_utils/patches/module/require_sub.rb
@@ -228,8 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
242
  - !ruby/object:Gem::Version
229
243
  version: '0'
230
244
  requirements: []
231
- rubyforge_project:
232
- rubygems_version: 2.7.7
245
+ rubygems_version: 3.0.6
233
246
  signing_key:
234
247
  specification_version: 4
235
248
  summary: Utilities for E.A.C.'s Ruby projects.