/
shell.rb
98 lines (77 loc) · 2.62 KB
/
shell.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "childprocess"
require "itunes/store/transporter/errors"
module ITunes
module Store
class Transporter
class Shell # :nodoc:
attr :path
EXE_NAME = "iTMSTransporter"
WINDOWS_EXE = "#{EXE_NAME}.CMD"
DEFAULT_UNIX_PATH = "/usr/local/itms/bin/#{EXE_NAME}"
class << self
def windows?
# We just need to know where iTMSTransporter lives, though cygwin
# can crow when it receives a Windows path.
ChildProcess.windows? || ChildProcess.os == :cygwin
end
def default_path
if windows?
# The Transporter installer prefers x86
# But... I think ruby normalizes this to just PROGRAMFILES
root = ENV["PROGRAMFILES(x86)"] || ENV["PROGRAMFILES"] # Need C:\ in case?
File.join(root, "itms", WINDOWS_EXE)
else
DEFAULT_UNIX_PATH
end
end
end
def initialize(path = nil)
@path = path || self.class.default_path
end
def exec(argv, &block)
raise ArgumentError, "block required" unless block_given?
begin
process = ChildProcess.build(path, *argv)
stdout = IO.pipe
stderr = IO.pipe
stdout[1].sync = true
process.io.stdout = stdout[1]
stderr[1].sync = true
process.io.stderr = stderr[1]
process.start
stdout[1].close
stderr[1].close
poll(stdout[0], stderr[0], &block)
rescue ChildProcess::Error, SystemCallError => e
raise TransporterError, e.message
ensure
process.wait if process.alive?
[ stdout, stderr ].flatten.each { |io| io.close if !io.closed? }
end
process.exit_code
end
private
def poll(stdout, stderr)
read = [ stdout, stderr ]
loop do
# TODO: Not working on jruby
if ready = select(read, nil, nil, 1)
ready.each do |set|
next unless set.any?
set.each do |io|
if io.eof?
read.delete(io)
next
end
name = io == stdout ? :stdout : :stderr
yield(io.gets, name)
end
end
end
break unless read.any?
end
end
end
end
end
end