eac_ruby_utils 0.107.1 → 0.108.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: 7e29ceb1b90c374898bd5a0ef48d3852bbd9704ecc46de6964610fec9aeb7e2f
4
+ data.tar.gz: 9dbb473fa3b24ac1354846da01f9c705f037fd59f4380500af97040953b8b69d
5
5
  SHA512:
6
- metadata.gz: 5dbbb58d1c41ea8e5cbbffcd01f5d7c2c51fc3abe83e615bcec30b3360e66ba24e8d7311b81b49a9d8ef3a43f3ae73e4396303efa84e6810fd4f9f615cd8b84f
7
- data.tar.gz: 8a11abf8ab6d9c3c817b7df22ec6e2b3f449c371b595852e19ed12605b97ba1f4a8ffed449a0c122ec45c193980c3acf2245c9424e258c5b8f0d5347f51c822b
6
+ metadata.gz: 13f74153fe16267d97f6edaa7aec5bef46d82f38fd17736b735bee01ae4669f71870b1da4c33f0827014a2dfb29c7eca8b59e44e71ebd2b89b8e1d7ae8243aab
7
+ data.tar.gz: 6ace755da4832ffcd7586dab3cb16d4ff6ef582bd8a3a3907cb16b7c773fcc708ffbd0e3ec39b5df48b47fb26253541158a4ee7594a38847826ee0219106f73c
@@ -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
+ class Command
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
@@ -6,18 +6,33 @@ module EacRubyUtils
6
6
  module Envs
7
7
  class Command
8
8
  module Concat
9
+ AND_OPERATOR = '&&'
10
+ BEFORE_OPERATOR = ';'
11
+ OR_OPERATOR = '||'
12
+ PIPE_OPERATOR = '|'
13
+
9
14
  def concat(operator, other_command)
10
15
  duplicate_by_extra_options(concat: ::EacRubyUtils::Struct.new(
11
16
  operator: operator, command: other_command
12
17
  ))
13
18
  end
14
19
 
20
+ # @return [EacRubyUtils::Envs::Command]
21
+ def and(other_command)
22
+ concat(AND_OPERATOR, other_command)
23
+ end
24
+
25
+ # @return [EacRubyUtils::Envs::Command]
26
+ def before(other_command)
27
+ concat(BEFORE_OPERATOR, other_command)
28
+ end
29
+
15
30
  def or(other_command)
16
- concat('||', other_command)
31
+ concat(OR_OPERATOR, other_command)
17
32
  end
18
33
 
19
34
  def pipe(other_command)
20
- concat('|', other_command)
35
+ concat(PIPE_OPERATOR, other_command)
21
36
  end
22
37
 
23
38
  private
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Envs
5
+ class Command
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
@@ -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
@@ -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
+ class Command
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
@@ -1,128 +1,72 @@
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
4
  require 'shellwords'
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
+
11
+ class << self
12
+ # @param command [Array]
13
+ # @return [Array]
14
+ def sanitize_initialize_arguments(arguments)
15
+ if arguments.count == 1 && arguments.first.is_a?(Array)
16
+ arguments.first
17
+ elsif arguments.is_a?(Array)
18
+ arguments
19
+ else
20
+ raise "Invalid argument command: #{arguments}|#{arguments.class}"
21
+ end
23
22
  end
24
23
  end
25
24
 
26
- def args
27
- @command
25
+ common_constructor :env, :args, :extra_options, default: [{}] do
26
+ self.extra_options = extra_options.with_indifferent_access
27
+ self.args = self.class.sanitize_initialize_arguments(args)
28
28
  end
29
29
 
30
30
  def append(args)
31
- duplicate_by_command(@command + args)
31
+ duplicate_by_command(self.args + args)
32
32
  end
33
33
 
34
34
  def prepend(args)
35
- duplicate_by_command(args + @command)
35
+ duplicate_by_command(args + self.args)
36
36
  end
37
37
 
38
38
  def to_s
39
- "#{@command} [ENV: #{@env}]"
39
+ "#{args} [ENV: #{env}]"
40
40
  end
41
41
 
42
42
  def command(options = {})
43
- c = @command
44
- c = c.map { |x| escape(x) }.join(' ') if c.is_a?(Enumerable)
45
43
  append_command_options(
46
- @env.command_line(
47
- append_chdir(append_concat(append_envvars(c)))
48
- ),
44
+ env.command_line(command_line_without_env),
49
45
  options
50
46
  )
51
47
  end
52
48
 
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)
49
+ # @return [String]
50
+ def command_line_without_env
51
+ c = args
52
+ c = c.map { |x| escape(x) }.join(' ') if c.is_a?(Enumerable)
53
+ append_chdir(append_concat(append_envvars(c)))
88
54
  end
89
55
 
90
56
  protected
91
57
 
92
58
  def duplicate(command, extra_options)
93
- self.class.new(@env, command, extra_options)
59
+ self.class.new(env, command, extra_options)
94
60
  end
95
61
 
96
62
  private
97
63
 
98
- attr_reader :extra_options
99
-
100
64
  def duplicate_by_command(new_command)
101
- duplicate(new_command, @extra_options)
65
+ duplicate(new_command, extra_options)
102
66
  end
103
67
 
104
68
  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
69
+ duplicate(args, extra_options.merge(set_extra_options))
126
70
  end
127
71
 
128
72
  def escape(arg)
@@ -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.108.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.108.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-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -141,12 +141,15 @@ files:
141
141
  - lib/eac_ruby_utils/envs.rb
142
142
  - lib/eac_ruby_utils/envs/base_env.rb
143
143
  - lib/eac_ruby_utils/envs/command.rb
144
+ - lib/eac_ruby_utils/envs/command/append_command_options.rb
144
145
  - lib/eac_ruby_utils/envs/command/concat.rb
146
+ - lib/eac_ruby_utils/envs/command/debugging.rb
145
147
  - 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
+ - lib/eac_ruby_utils/envs/command/execution.rb
148
149
  - lib/eac_ruby_utils/envs/command/extra_options.rb
149
150
  - lib/eac_ruby_utils/envs/executable.rb
151
+ - lib/eac_ruby_utils/envs/execution_error.rb
152
+ - lib/eac_ruby_utils/envs/execution_result.rb
150
153
  - lib/eac_ruby_utils/envs/file.rb
151
154
  - lib/eac_ruby_utils/envs/local_env.rb
152
155
  - lib/eac_ruby_utils/envs/process.rb
@@ -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