eac_ruby_utils 0.107.1 → 0.109.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: eeb6daf99db3dddb24c607bd5e7dc957075ec1988015aa6871d5cc52df66e6cd
4
- data.tar.gz: 0d7126d4dbd98c296fdfa13c7e398cba361540fc46ed6a491eb5461cf9791666
3
+ metadata.gz: f49997c361144d891925c3596cdc2ca37bcc485aa26aa287c4f7bfe529f3c553
4
+ data.tar.gz: 50f784d1c96e7432f5bcec9823899cb4407d1e2fe7465aee7bd605a6dfbfe6ea
5
5
  SHA512:
6
- metadata.gz: 5dbbb58d1c41ea8e5cbbffcd01f5d7c2c51fc3abe83e615bcec30b3360e66ba24e8d7311b81b49a9d8ef3a43f3ae73e4396303efa84e6810fd4f9f615cd8b84f
7
- data.tar.gz: 8a11abf8ab6d9c3c817b7df22ec6e2b3f449c371b595852e19ed12605b97ba1f4a8ffed449a0c122ec45c193980c3acf2245c9424e258c5b8f0d5347f51c822b
6
+ metadata.gz: 2cee869d6232e3b1b7027dc0a0927c9d3ae272e0177d4c90718ebaa279374b60cf42171006eae06df51a0c09967019ba7e9b175fd5635051ed196c4cc012bed8
7
+ data.tar.gz: fc6b4ed8fb837382ed816326d8930ce6fbb04a90fad68d5c48acf3516bd30f51eb4570ae04216129aa870627c8551500f480855a11981726e4dbb1ff05023cff
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'shellwords'
5
+
6
+ module EacRubyUtils
7
+ module Envs
8
+ module BaseCommand
9
+ class AppendCommandOptions
10
+ enable_method_class
11
+ common_constructor :command, :command_line, :options
12
+
13
+ def result
14
+ r = command_line
15
+ r = input.command + ' | ' + r if input
16
+ r = "cat #{Shellwords.escape(input_file)} | #{r}" if input_file
17
+ r += ' > ' + Shellwords.escape(output_file) if output_file
18
+ r
19
+ end
20
+
21
+ # @return [EacRubyUtils::Envs::Command, nil]
22
+ def input
23
+ options[:input]
24
+ end
25
+
26
+ # @return [Pathname, nil]
27
+ def input_file
28
+ options[:input_file].if_present(&:to_pathname)
29
+ end
30
+
31
+ # @return [Pathname, nil]
32
+ def output_file
33
+ options[:output_file].if_present(&:to_pathname)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/struct'
4
+
5
+ module EacRubyUtils
6
+ module Envs
7
+ module BaseCommand
8
+ module Concat
9
+ AND_OPERATOR = '&&'
10
+ BEFORE_OPERATOR = ';'
11
+ OR_OPERATOR = '||'
12
+ PIPE_OPERATOR = '|'
13
+
14
+ # @param operator [Symbol]
15
+ # @return [EacRubyUtils::Envs::CompositeCommand]
16
+ def concat(operator, other_command)
17
+ require 'eac_ruby_utils/envs/composite_command'
18
+ ::EacRubyUtils::Envs::CompositeCommand.new(operator, self, other_command)
19
+ end
20
+
21
+ # @return [EacRubyUtils::Envs::Command]
22
+ def and(other_command)
23
+ concat(AND_OPERATOR, other_command)
24
+ end
25
+
26
+ # @return [EacRubyUtils::Envs::Command]
27
+ def before(other_command)
28
+ concat(BEFORE_OPERATOR, other_command)
29
+ end
30
+
31
+ def or(other_command)
32
+ concat(OR_OPERATOR, other_command)
33
+ end
34
+
35
+ def pipe(other_command)
36
+ concat(PIPE_OPERATOR, other_command)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Envs
5
+ module BaseCommand
6
+ module Debugging
7
+ def debug?
8
+ ENV['DEBUG'].to_s.strip != ''
9
+ end
10
+
11
+ # Print a message if debugging is enabled.
12
+ def debug_print(message)
13
+ message = message.to_s
14
+ puts message.if_respond(:light_red, message) if debug?
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/envs/execution_error'
4
+ require 'eac_ruby_utils/envs/execution_result'
5
+ require 'eac_ruby_utils/envs/process'
6
+ require 'eac_ruby_utils/envs/spawn'
7
+ require 'pp'
8
+
9
+ module EacRubyUtils
10
+ module Envs
11
+ module BaseCommand
12
+ module Execution
13
+ def execute!(options = {})
14
+ options[:exit_outputs] = status_results.merge(options[:exit_outputs].presence || {})
15
+ er = ::EacRubyUtils::Envs::ExecutionResult.new(execute(options), options)
16
+ return er.result if er.success?
17
+
18
+ raise ::EacRubyUtils::Envs::ExecutionError,
19
+ "execute! command failed: #{self}\n#{er.r.pretty_inspect}"
20
+ end
21
+
22
+ def execute(options = {})
23
+ c = command(options)
24
+ debug_print("BEFORE: #{c}")
25
+ t1 = Time.now
26
+ r = ::EacRubyUtils::Envs::Process.new(c).to_h
27
+ i = Time.now - t1
28
+ debug_print("AFTER [#{i}]: #{c}")
29
+ r
30
+ end
31
+
32
+ def spawn(options = {})
33
+ c = command(options)
34
+ debug_print("SPAWN: #{c}")
35
+ ::EacRubyUtils::Envs::Spawn.new(c)
36
+ end
37
+
38
+ def system!(options = {})
39
+ return if system(options)
40
+
41
+ raise ::EacRubyUtils::Envs::ExecutionError, "system! command failed: #{self}"
42
+ end
43
+
44
+ def system(options = {})
45
+ c = command(options)
46
+ debug_print(c)
47
+ Kernel.system(c)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'shellwords'
6
+
7
+ module EacRubyUtils
8
+ module Envs
9
+ module BaseCommand
10
+ module ExtraOptions
11
+ # @return [ActiveSupport::HashWithIndifferentAccess]
12
+ def extra_options
13
+ @extra_options ||= {}.with_indifferent_access
14
+ end
15
+
16
+ def status_result(status_code, result)
17
+ duplicate_by_extra_options(status_results: status_results.merge(status_code => result))
18
+ end
19
+
20
+ private
21
+
22
+ def status_results
23
+ extra_options[:status_results] ||= {}.with_indifferent_access
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'shellwords'
5
+
6
+ module EacRubyUtils
7
+ module Envs
8
+ module BaseCommand
9
+ require_sub __FILE__
10
+
11
+ common_concern do
12
+ enable_abstract_methods
13
+ include ::EacRubyUtils::Envs::BaseCommand::Concat
14
+ include ::EacRubyUtils::Envs::BaseCommand::Debugging
15
+ include ::EacRubyUtils::Envs::BaseCommand::Execution
16
+ include ::EacRubyUtils::Envs::BaseCommand::ExtraOptions
17
+ end
18
+
19
+ # @return [EacRubyUtils::Envs::BaseEnv]
20
+ def env
21
+ raise_abstract_method __method__
22
+ end
23
+
24
+ def command(options = {})
25
+ append_command_options(
26
+ env.command_line(command_line_without_env),
27
+ options
28
+ )
29
+ end
30
+
31
+ # @return [String]
32
+ def command_line_without_env(_options = {})
33
+ raise_abstract_method __method__
34
+ end
35
+
36
+ private
37
+
38
+ def escape(arg)
39
+ arg = arg.to_s
40
+ m = /^\@ESC_(.+)$/.match(arg)
41
+ m ? m[1] : Shellwords.escape(arg)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'shellwords'
5
+
3
6
  module EacRubyUtils
4
7
  module Envs
5
8
  class Command
@@ -12,18 +12,8 @@ module EacRubyUtils
12
12
  duplicate_by_extra_options(chdir: dir)
13
13
  end
14
14
 
15
- def status_result(status_code, result)
16
- duplicate_by_extra_options(status_results: status_results.merge(status_code => result))
17
- end
18
-
19
15
  private
20
16
 
21
- attr_reader :extra_options
22
-
23
- def status_results
24
- extra_options[:status_results] ||= {}.with_indifferent_access
25
- end
26
-
27
17
  def append_chdir(command)
28
18
  extra_options[:chdir].present? ? "(cd '#{extra_options[:chdir]}' ; #{command} )" : command
29
19
  end
@@ -1,134 +1,66 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/core_ext'
4
- require 'eac_ruby_utils/envs/process'
5
- require 'eac_ruby_utils/envs/spawn'
6
- require 'pp'
7
- require 'shellwords'
4
+ require 'eac_ruby_utils/envs/base_command'
8
5
 
9
6
  module EacRubyUtils
10
7
  module Envs
11
8
  class Command
12
- require_sub __FILE__, include_modules: true
13
-
14
- def initialize(env, command, extra_options = {})
15
- @env = env
16
- @extra_options = extra_options.with_indifferent_access
17
- if command.count == 1 && command.first.is_a?(Array)
18
- @command = command.first
19
- elsif command.is_a?(Array)
20
- @command = command
21
- else
22
- raise "Invalid argument command: #{command}|#{command.class}"
9
+ require_sub __FILE__, include_modules: true, require_dependency: true
10
+ include ::EacRubyUtils::Envs::BaseCommand
11
+
12
+ class << self
13
+ # @param command [Array]
14
+ # @return [Array]
15
+ def sanitize_initialize_arguments(arguments)
16
+ if arguments.count == 1 && arguments.first.is_a?(Array)
17
+ arguments.first
18
+ elsif arguments.is_a?(Array)
19
+ arguments
20
+ else
21
+ raise "Invalid argument command: #{arguments}|#{arguments.class}"
22
+ end
23
23
  end
24
24
  end
25
25
 
26
- def args
27
- @command
26
+ common_constructor :env, :args, :extra_options, default: [{}] do
27
+ self.extra_options = extra_options.with_indifferent_access
28
+ self.args = self.class.sanitize_initialize_arguments(args)
28
29
  end
29
30
 
30
31
  def append(args)
31
- duplicate_by_command(@command + args)
32
+ duplicate_by_command(self.args + args)
32
33
  end
33
34
 
34
35
  def prepend(args)
35
- duplicate_by_command(args + @command)
36
+ duplicate_by_command(args + self.args)
36
37
  end
37
38
 
38
39
  def to_s
39
- "#{@command} [ENV: #{@env}]"
40
+ "#{args} [ENV: #{env}]"
40
41
  end
41
42
 
42
- def command(options = {})
43
- c = @command
43
+ # @return [String]
44
+ def command_line_without_env
45
+ c = args
44
46
  c = c.map { |x| escape(x) }.join(' ') if c.is_a?(Enumerable)
45
- append_command_options(
46
- @env.command_line(
47
- append_chdir(append_concat(append_envvars(c)))
48
- ),
49
- options
50
- )
51
- end
52
-
53
- def execute!(options = {})
54
- options[:exit_outputs] = status_results.merge(options[:exit_outputs].presence || {})
55
- er = ExecuteResult.new(execute(options), options)
56
- return er.result if er.success?
57
-
58
- raise ::EacRubyUtils::Envs::Command::ExecError,
59
- "execute! command failed: #{self}\n#{er.r.pretty_inspect}"
60
- end
61
-
62
- def execute(options = {})
63
- c = command(options)
64
- debug_print("BEFORE: #{c}")
65
- t1 = Time.now
66
- r = ::EacRubyUtils::Envs::Process.new(c).to_h
67
- i = Time.now - t1
68
- debug_print("AFTER [#{i}]: #{c}")
69
- r
70
- end
71
-
72
- def spawn(options = {})
73
- c = command(options)
74
- debug_print("SPAWN: #{c}")
75
- ::EacRubyUtils::Envs::Spawn.new(c)
76
- end
77
-
78
- def system!(options = {})
79
- return if system(options)
80
-
81
- raise ::EacRubyUtils::Envs::Command::ExecError, "system! command failed: #{self}"
82
- end
83
-
84
- def system(options = {})
85
- c = command(options)
86
- debug_print(c)
87
- Kernel.system(c)
47
+ append_chdir(append_envvars(c))
88
48
  end
89
49
 
90
50
  protected
91
51
 
92
52
  def duplicate(command, extra_options)
93
- self.class.new(@env, command, extra_options)
53
+ self.class.new(env, command, extra_options)
94
54
  end
95
55
 
96
56
  private
97
57
 
98
- attr_reader :extra_options
99
-
100
58
  def duplicate_by_command(new_command)
101
- duplicate(new_command, @extra_options)
59
+ duplicate(new_command, extra_options)
102
60
  end
103
61
 
104
62
  def duplicate_by_extra_options(set_extra_options)
105
- duplicate(@command, @extra_options.merge(set_extra_options))
106
- end
107
-
108
- def debug?
109
- ENV['DEBUG'].to_s.strip != ''
110
- end
111
-
112
- # Print a message if debugging is enabled.
113
- def debug_print(message)
114
- message = message.to_s
115
- puts message.if_respond(:light_red, message) if debug?
116
- end
117
-
118
- def append_command_options(command, options)
119
- command = options[:input].command + ' | ' + command if options[:input]
120
- if options[:input_file]
121
- command = "cat #{Shellwords.escape(options[:input_file])}" \
122
- " | #{command}"
123
- end
124
- command += ' > ' + Shellwords.escape(options[:output_file]) if options[:output_file]
125
- command
126
- end
127
-
128
- def escape(arg)
129
- arg = arg.to_s
130
- m = /^\@ESC_(.+)$/.match(arg)
131
- m ? m[1] : Shellwords.escape(arg)
63
+ duplicate(args, extra_options.merge(set_extra_options))
132
64
  end
133
65
  end
134
66
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'eac_ruby_utils/envs/base_command'
5
+
6
+ module EacRubyUtils
7
+ module Envs
8
+ class CompositeCommand
9
+ include ::EacRubyUtils::Envs::BaseCommand
10
+
11
+ enable_listable
12
+ lists.add_string :operator, '&&' => :and, ';' => :before, '||' => :or, '|' => :pipe
13
+
14
+ common_constructor :operator, :left_command, :right_command do
15
+ self.operator = self.class.lists.operator.value_validate!(operator.to_s)
16
+ end
17
+
18
+ # @return [EacRubyUtils::Envs::BaseEnv]
19
+ delegate :env, to: :left_command
20
+
21
+ # @return [String]
22
+ def command_line_without_env(_options = {})
23
+ ::Shellwords.join(['bash', '-c', bash_command])
24
+ end
25
+
26
+ # @return [String]
27
+ def bash_command
28
+ ['set', '-euo', 'pipefail', OPERATOR_BEFORE, left_command.command, operator,
29
+ right_command.command].join(' ')
30
+ end
31
+ end
32
+ end
33
+ end
@@ -2,9 +2,7 @@
2
2
 
3
3
  module EacRubyUtils
4
4
  module Envs
5
- class Command
6
- class ExecError < ::RuntimeError
7
- end
5
+ class ExecutionError < ::RuntimeError
8
6
  end
9
7
  end
10
8
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/envs/execution_error'
4
+
5
+ module EacRubyUtils
6
+ module Envs
7
+ class ExecutionResult
8
+ attr_reader :r, :options
9
+
10
+ def initialize(result, options)
11
+ @r = result
12
+ @options = options
13
+ end
14
+
15
+ def result
16
+ return exit_code_zero_result if exit_code_zero?
17
+ return expected_error_result if expected_error?
18
+
19
+ raise ::EacRubyUtils::Envs::ExecutionError, 'Failed!'
20
+ end
21
+
22
+ def success?
23
+ exit_code_zero? || expected_error?
24
+ end
25
+
26
+ private
27
+
28
+ def exit_code_zero?
29
+ r[:exit_code]&.zero?
30
+ end
31
+
32
+ def exit_code_zero_result
33
+ r[options[:output] || :stdout]
34
+ end
35
+
36
+ def expected_error_result
37
+ options[:exit_outputs][r[:exit_code]]
38
+ end
39
+
40
+ def expected_error?
41
+ options[:exit_outputs].is_a?(Hash) && options[:exit_outputs].key?(r[:exit_code])
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.107.1'
4
+ VERSION = '0.109.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.107.1
4
+ version: 0.109.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: 2022-11-07 00:00:00.000000000 Z
11
+ date: 2022-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -139,14 +139,20 @@ files:
139
139
  - lib/eac_ruby_utils/custom_format.rb
140
140
  - lib/eac_ruby_utils/enum.rb
141
141
  - lib/eac_ruby_utils/envs.rb
142
+ - lib/eac_ruby_utils/envs/base_command.rb
143
+ - lib/eac_ruby_utils/envs/base_command/append_command_options.rb
144
+ - lib/eac_ruby_utils/envs/base_command/concat.rb
145
+ - lib/eac_ruby_utils/envs/base_command/debugging.rb
146
+ - lib/eac_ruby_utils/envs/base_command/execution.rb
147
+ - lib/eac_ruby_utils/envs/base_command/extra_options.rb
142
148
  - lib/eac_ruby_utils/envs/base_env.rb
143
149
  - lib/eac_ruby_utils/envs/command.rb
144
- - lib/eac_ruby_utils/envs/command/concat.rb
145
150
  - lib/eac_ruby_utils/envs/command/envvars.rb
146
- - lib/eac_ruby_utils/envs/command/exec_error.rb
147
- - lib/eac_ruby_utils/envs/command/execute_result.rb
148
151
  - lib/eac_ruby_utils/envs/command/extra_options.rb
152
+ - lib/eac_ruby_utils/envs/composite_command.rb
149
153
  - lib/eac_ruby_utils/envs/executable.rb
154
+ - lib/eac_ruby_utils/envs/execution_error.rb
155
+ - lib/eac_ruby_utils/envs/execution_result.rb
150
156
  - lib/eac_ruby_utils/envs/file.rb
151
157
  - lib/eac_ruby_utils/envs/local_env.rb
152
158
  - lib/eac_ruby_utils/envs/process.rb
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_ruby_utils/struct'
4
-
5
- module EacRubyUtils
6
- module Envs
7
- class Command
8
- module Concat
9
- def concat(operator, other_command)
10
- duplicate_by_extra_options(concat: ::EacRubyUtils::Struct.new(
11
- operator: operator, command: other_command
12
- ))
13
- end
14
-
15
- def or(other_command)
16
- concat('||', other_command)
17
- end
18
-
19
- def pipe(other_command)
20
- concat('|', other_command)
21
- end
22
-
23
- private
24
-
25
- def append_concat(command)
26
- extra_options[:concat].if_present(command) do |v|
27
- "#{command} #{v.operator} #{v.command.command}"
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_ruby_utils/envs/command/exec_error'
4
-
5
- module EacRubyUtils
6
- module Envs
7
- class Command
8
- class ExecuteResult
9
- attr_reader :r, :options
10
-
11
- def initialize(result, options)
12
- @r = result
13
- @options = options
14
- end
15
-
16
- def result
17
- return exit_code_zero_result if exit_code_zero?
18
- return expected_error_result if expected_error?
19
-
20
- raise ::EacRubyUtils::Envs::Command::ExecError, 'Failed!'
21
- end
22
-
23
- def success?
24
- exit_code_zero? || expected_error?
25
- end
26
-
27
- private
28
-
29
- def exit_code_zero?
30
- r[:exit_code]&.zero?
31
- end
32
-
33
- def exit_code_zero_result
34
- r[options[:output] || :stdout]
35
- end
36
-
37
- def expected_error_result
38
- options[:exit_outputs][r[:exit_code]]
39
- end
40
-
41
- def expected_error?
42
- options[:exit_outputs].is_a?(Hash) && options[:exit_outputs].key?(r[:exit_code])
43
- end
44
- end
45
- end
46
- end
47
- end