Module: Ruflet::Rails::DesktopLauncher
- Defined in:
- lib/ruflet/rails/desktop_launcher.rb
Class Method Summary collapse
- .backend_url(root:, argv: ARGV, env: ENV) ⇒ Object
- .desktop_enabled?(root:, argv: ARGV, env: ENV) ⇒ Boolean
- .launch(url, root:) ⇒ Object
- .launch_once(root:, argv: ARGV, env: ENV, wait: 1.0) ⇒ Object
- .launched? ⇒ Boolean
- .rails_server_command?(argv) ⇒ Boolean
- .rails_server_port(argv) ⇒ Object
- .replace_url_port(url, port) ⇒ Object
- .reset! ⇒ Object
- .ruflet_yaml_backend_url(root) ⇒ Object
- .wait_for_backend(url) ⇒ Object
Class Method Details
.backend_url(root:, argv: ARGV, env: ENV) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 42 def backend_url(root:, argv: ARGV, env: ENV) explicit = env["RUFLET_BACKEND_URL"].to_s.strip return explicit unless explicit.empty? config_url = ruflet_yaml_backend_url(root) cli_port = rails_server_port(argv) return replace_url_port(config_url, cli_port) if cli_port return config_url if config_url "http://localhost:#{env["PORT"].to_s.empty? ? 3000 : env["PORT"]}" end |
.desktop_enabled?(root:, argv: ARGV, env: ENV) ⇒ Boolean
54 55 56 57 58 59 60 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 54 def desktop_enabled?(root:, argv: ARGV, env: ENV) return true if env["RUFLET_RAILS_DESKTOP_SERVER"].to_s.downcase == "true" return true if env["RUFLET_RAILS_DESKTOP"].to_s.downcase == "true" return true if Array(argv).map(&:to_s).include?("--desktop") false end |
.launch(url, root:) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 25 def launch(url, root:) require "ruflet/cli" Dir.chdir(root.to_s) do wait_for_backend(url) if Ruflet::CLI.respond_to?(:launch_desktop_client, true) Ruflet::CLI.send(:launch_desktop_client, url) else warn "Ruflet desktop launcher is unavailable in this ruflet version." nil end end rescue StandardError => e warn "Failed to launch Ruflet desktop client: #{e.class}: #{e.}" nil end |
.launch_once(root:, argv: ARGV, env: ENV, wait: 1.0) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 10 def launch_once(root:, argv: ARGV, env: ENV, wait: 1.0) return false unless desktop_enabled?(root: root, argv: argv, env: env) return false unless env["RUFLET_RAILS_DESKTOP_SERVER"].to_s.downcase == "true" || rails_server_command?(argv) return false if env["RUFLET_RAILS_DESKTOP"].to_s.downcase == "false" return false if launched? @launched = true url = backend_url(root: root, argv: argv, env: env) Thread.new do sleep wait.to_f if wait.to_f.positive? launch(url, root: root) end true end |
.launched? ⇒ Boolean
107 108 109 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 107 def launched? !!@launched end |
.rails_server_command?(argv) ⇒ Boolean
62 63 64 65 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 62 def rails_server_command?(argv) command = Array(argv).find { |value| !value.to_s.start_with?("-") }.to_s %w[server s].include?(command) end |
.rails_server_port(argv) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 67 def rails_server_port(argv) values = Array(argv).map(&:to_s) values.each_with_index do |value, index| return values[index + 1].to_i if %w[-p --port].include?(value) && values[index + 1].to_i.positive? return value.split("=", 2).last.to_i if value.start_with?("--port=") && value.split("=", 2).last.to_i.positive? end nil end |
.replace_url_port(url, port) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 88 def replace_url_port(url, port) return "http://localhost:#{port}" if url.to_s.strip.empty? uri = URI.parse(url) uri.port = port.to_i uri.to_s rescue URI::InvalidURIError "http://localhost:#{port}" end |
.reset! ⇒ Object
111 112 113 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 111 def reset! @launched = false end |
.ruflet_yaml_backend_url(root) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 76 def ruflet_yaml_backend_url(root) path = %w[ruflet.yaml ruflet.yml].map { |name| File.join(root.to_s, name) }.find { |candidate| File.file?(candidate) } return nil unless path require "yaml" config = YAML.safe_load(File.read(path), aliases: true) || {} value = config.dig("app", "backend_url") || config["backend_url"] || config["server_url"] value.to_s.strip.empty? ? nil : value.to_s.strip rescue StandardError nil end |
.wait_for_backend(url) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/ruflet/rails/desktop_launcher.rb', line 98 def wait_for_backend(url) return unless Ruflet::CLI.respond_to?(:wait_for_server_boot, true) uri = URI.parse(url) Ruflet::CLI.send(:wait_for_server_boot, uri.port) if uri.port rescue URI::InvalidURIError nil end |