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
|
# File 'lib/ruflet/cli/run_command.rb', line 10
def command_run(args)
options = { target: "mobile" }
parser = OptionParser.new do |o|
o.on("--web") { options[:target] = "web" }
o.on("--mobile") { options[:target] = "mobile" }
o.on("--desktop") { options[:target] = "desktop" }
end
parser.parse!(args)
script_token = args.shift || "main"
script_path = resolve_script(script_token)
unless script_path
warn "Script not found: #{script_token}"
warn "Expected: ./#{script_token}.rb, ./#{script_token}, or explicit file path."
return 1
end
selected_port = find_available_port(8550)
env = {
"RUFLET_TARGET" => options[:target],
"RUFLET_SUPPRESS_SERVER_BANNER" => "1",
"RUFLET_PORT" => selected_port.to_s
}
puts "Requested port 8550 is busy; bound to #{selected_port}" if selected_port != 8550
print_mobile_qr_hint(port: selected_port) if options[:target] == "mobile"
cmd =
if File.file?(File.expand_path("Gemfile", Dir.pwd))
env["BUNDLE_PATH"] = "vendor/bundle"
env["BUNDLE_DISABLE_SHARED_GEMS"] = "true"
bundle_ready = system(env, "bundle", "check", out: File::NULL, err: File::NULL)
return 1 unless bundle_ready || system(env, "bundle", "install")
["bundle", "exec", RbConfig.ruby, script_path]
else
[RbConfig.ruby, script_path]
end
child_pid = Process.spawn(env, *cmd, pgroup: true)
forward_signal = lambda do |signal|
begin
Process.kill(signal, -child_pid)
rescue Errno::ESRCH
nil
end
end
previous_int = Signal.trap("INT") { forward_signal.call("INT") }
previous_term = Signal.trap("TERM") { forward_signal.call("TERM") }
_pid, status = Process.wait2(child_pid)
status.success? ? 0 : (status.exitstatus || 1)
ensure
Signal.trap("INT", previous_int) if defined?(previous_int) && previous_int
Signal.trap("TERM", previous_term) if defined?(previous_term) && previous_term
if defined?(child_pid) && child_pid
begin
Process.kill("TERM", -child_pid)
rescue Errno::ESRCH
nil
end
end
end
|