@@ -52,30 +52,47 @@ def run(*args, **options, &block)
5252
5353 cmd = TTY ::Command . new ( printer : verbose ? :pretty : :null , uuid : false , **options )
5454
55+ args = prepare_args ( args , unsetenv_others )
56+
57+ result = cmd . run! ( *args ) do |out , err |
58+ # TTY::Command yields chunks, split them to match line-based expectation.
59+ ( out || err ) . each_line { |line | block . call ( line . chomp ) } if stream
60+ end
61+
62+ handle_result ( result , args , returns , options , stream , block )
63+ rescue TTY ::Command ::TimeoutExceeded
64+ raise TimeoutError , args . join ( ' ' )
65+ end
66+
67+ private
68+
69+ def prepare_args ( args , unsetenv_others )
5570 env = args . first . is_a? ( Hash ) ? args . shift : { }
5671
5772 if unsetenv_others
58- # Manually construct `env -i` command because TTY::Command unsetenv_others is unreliable
59- env_vars = env . map { |k , v | "#{ k } =#{ v } " }
60- args = if args . size == 1
61- [ 'env' , '-i' ] + env_vars + [ 'bash' , '-c' , args . first ]
62- else
63- [ 'env' , '-i' ] + env_vars + args
64- end
73+ construct_env_i_args ( args , env )
6574 else
66- # Normalize args to force bash for single strings (legacy support)
67- args = [ 'bash' , '-c' , args . first ] if args . size == 1
68- args . unshift ( env ) unless env . empty?
75+ construct_bash_args ( args , env )
6976 end
77+ end
7078
71- result = cmd . run! ( *args ) do |out , err |
72- next unless stream
73-
74- # Streaming mode: yield lines to block
75- # TTY::Command yields chunks, split them to match line-based expectation.
76- ( out || err ) . each_line { |line | block . call ( line . chomp ) }
79+ def construct_env_i_args ( args , env )
80+ env_vars = env . map { |k , v | "#{ k } =#{ v } " }
81+ base = [ 'env' , '-i' ] + env_vars
82+ if args . size == 1
83+ base + [ 'bash' , '-c' , args . first ]
84+ else
85+ base + args
7786 end
87+ end
88+
89+ def construct_bash_args ( args , env )
90+ args = [ 'bash' , '-c' , args . first ] if args . size == 1
91+ args . unshift ( env ) unless env . empty?
92+ args
93+ end
7894
95+ def handle_result ( result , args , returns , options , stream , block )
7996 # Legacy Bash.run behavior (Post-run yield) if NOT streaming
8097 # Note: legacy behavior requires yielding (stdout, stderr, exit_status)
8198 if block && !stream
@@ -86,8 +103,6 @@ def run(*args, **options, &block)
86103 end
87104
88105 result . out . chomp
89- rescue TTY ::Command ::TimeoutExceeded
90- raise TimeoutError . new ( args . join ( ' ' ) )
91106 end
92107 end
93108end
0 commit comments