Module: Ruflet::CLI::RunCommand

Included in:
Ruflet::CLI, UpdateCommand
Defined in:
lib/ruflet/cli/run_command.rb

Constant Summary collapse

CLIENT_CHANNEL_MANIFEST =
"ruflet_client-manifest.json"
DEFAULT_CLIENT_UPDATE_INTERVAL =
6 * 60 * 60

Instance Method Summary collapse

Instance Method Details

#command_run(args) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruflet/cli/run_command.rb', line 22

def command_run(args)
  options = { target: "mobile", requested_port: 8550, reload: true }
  parser = OptionParser.new do |o|
    o.on("--web") { options[:target] = "web" }
    o.on("--desktop") { options[:target] = "desktop" }
    o.on("--port PORT", Integer) { |v| options[:requested_port] = v }
    o.on("--no-reload") { options[:reload] = false }
  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 = resolve_backend_port(options[:target], requested_port: options[:requested_port])
  return 1 unless selected_port
  env = {
    "RUFLET_TARGET" => options[:target],
    "RUFLET_SUPPRESS_SERVER_BANNER" => "1",
    "RUFLET_PORT" => selected_port.to_s
  }
  apply_local_ruflet_dev_overrides(env)
  assets_dir = File.join(File.dirname(script_path), "assets")
  env["RUFLET_ASSETS_DIR"] = assets_dir if File.directory?(assets_dir)

  # The backend serves the web client itself so both share one origin.
  if options[:target] == "web"
    web_client_dir = detect_web_client_dir
    if web_client_dir
      env["RUFLET_WEB_CLIENT_DIR"] = web_client_dir
    else
      warn "Web client build not found and prebuilt download failed."
      warn "Build one with `ruflet build web`, or set RUFLET_CLIENT_DIR."
      return 1
    end
  end

  print_run_banner(target: options[:target], requested_port: options[:requested_port], port: selected_port)
  print_mobile_qr_hint(port: selected_port) if options[:target] == "mobile"
  print_hot_reload_banner if options[:reload]

  gemfile_path = find_nearest_gemfile(Dir.pwd)
  cmd = build_runtime_command(script_path, gemfile_path: gemfile_path, env: env, reload: options[:reload])
  return 1 unless cmd

  run_state = { child_pid: Process.spawn(env, *cmd, pgroup: true), restart: false }
  reload_input_thread = options[:reload] ? start_reload_input_thread(run_state) : nil
  launched_client_pids = launch_target_client(options[:target], selected_port)
  forward_signal = lambda do |signal|
    begin
      Process.kill(signal, -run_state[: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") }

  loop do
    _pid, status = Process.wait2(run_state[:child_pid])
    return status.success? ? 0 : (status.exitstatus || 1) unless run_state[:restart]

    # Full restart requested ("R"): respawn the backend; connected
    # clients reconnect and re-register on their own (Flet-style).
    run_state[:restart] = false
    puts "Restarting app..."
    started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    run_state[:child_pid] = Process.spawn(env, *cmd, pgroup: true)
    wait_for_server_boot(selected_port)
    elapsed_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round
    puts "Restarted in #{elapsed_ms}ms"
  end
ensure
  reload_input_thread&.kill if defined?(reload_input_thread)
  Signal.trap("INT", previous_int) if defined?(previous_int) && previous_int
  Signal.trap("TERM", previous_term) if defined?(previous_term) && previous_term

  if defined?(run_state) && run_state && run_state[:child_pid]
    begin
      Process.kill("TERM", -run_state[:child_pid])
    rescue Errno::ESRCH
      nil
    end
  end

  Array(defined?(launched_client_pids) ? launched_client_pids : nil).compact.each do |pid|
    begin
      Process.kill("TERM", -pid)
    rescue Errno::ESRCH
      begin
        Process.kill("TERM", pid)
      rescue Errno::ESRCH
        nil
      end
    end
  end

end