Module: SvelteOnRails::BootContext
- Defined in:
- lib/svelte_on_rails/lib/boot_context.rb
Class Method Summary collapse
-
.current_rails_command?(*names) ⇒ Boolean
rails <command>sets ARGV to the command name before Rails::Command dispatches. - .description ⇒ Object
- .program_basename ⇒ Object
- .rails_console? ⇒ Boolean
- .rails_dbconsole? ⇒ Boolean
- .rails_generator? ⇒ Boolean
- .rails_runner? ⇒ Boolean
-
.rake_task? ⇒ Boolean
--- detectors -----------------------------------------------------------.
- .server_program_name? ⇒ Boolean
-
.serving_http? ⇒ Boolean
We deliberately use a BLACKLIST + program-name check, not a whitelist of loaded constants.
- .test_runner? ⇒ Boolean
- .test_runner_name ⇒ Object
Class Method Details
.current_rails_command?(*names) ⇒ Boolean
rails <command> sets ARGV to the command name before Rails::Command
dispatches. This is stable across Rails 6/7/8.
92 93 94 95 96 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 92 def current_rails_command?(*names) argv0 = (defined?(ARGV) && ARGV.first) || nil return false unless argv0 names.include?(argv0.to_s) end |
.description ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 34 def description return "rake tasks: #{Rake.application.top_level_tasks.join(', ')}" if rake_task? return "rails console" if rails_console? return "rails runner" if rails_runner? return "rails dbconsole" if rails_dbconsole? return "rails generator" if rails_generator? return "test runner (#{test_runner_name})" if test_runner? return "rails server" if defined?(Rails::Server) "program=#{File.basename($PROGRAM_NAME)}" end |
.program_basename ⇒ Object
86 87 88 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 86 def program_basename File.basename($PROGRAM_NAME.to_s) end |
.rails_console? ⇒ Boolean
51 52 53 54 55 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 51 def rails_console? defined?(Rails::Console) || program_basename == "irb" || (defined?(IRB) && $PROGRAM_NAME.to_s.include?("irb")) end |
.rails_dbconsole? ⇒ Boolean
63 64 65 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 63 def rails_dbconsole? defined?(Rails::DBConsole) || current_rails_command?("dbconsole") end |
.rails_generator? ⇒ Boolean
67 68 69 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 67 def rails_generator? defined?(Rails::Generators) && current_rails_command?("generate", "destroy", "new") end |
.rails_runner? ⇒ Boolean
57 58 59 60 61 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 57 def rails_runner? defined?(Rails::Command::RunnerCommand) && Rails::Command.const_defined?(:RunnerCommand) && current_rails_command?("runner") end |
.rake_task? ⇒ Boolean
--- detectors -----------------------------------------------------------
47 48 49 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 47 def rake_task? defined?(Rake) && Rake.respond_to?(:application) && Rake.application.top_level_tasks.any? end |
.server_program_name? ⇒ Boolean
82 83 84 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 82 def server_program_name? %w[puma rackup unicorn unicorn_rails falcon passenger].include?(program_basename) end |
.serving_http? ⇒ Boolean
We deliberately use a BLACKLIST + program-name check, not a whitelist of
loaded constants. Reason: puma is typically in the app's Gemfile default
group, so defined?(::Puma::Launcher) is truthy even inside rails console
or rails assets:precompile.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 9 def serving_http? # Explicit overrides for tests / edge cases: return true if ENV["SVELTE_ON_RAILS_FORCE_SSR"] == "1" return false if ENV["SVELTE_ON_RAILS_DISABLE_SSR"] == "1" return false if rake_task? return false if rails_console? return false if rails_runner? return false if rails_dbconsole? return false if rails_generator? return false if test_runner? # Positive signal: `rails server` sets this constant during command dispatch. return true if defined?(Rails::Server) # Positive signal: launched directly via a server binary (bundle exec puma, # rackup, unicorn, falcon, passenger). $PROGRAM_NAME is set by the launcher # before it requires the app, so this is reliable. return true if server_program_name? # Default: assume this is NOT an HTTP-serving context. Better to be silent # than to spawn an unwanted Node process; users can force with the ENV var. false end |
.test_runner? ⇒ Boolean
71 72 73 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 71 def test_runner? !test_runner_name.nil? end |
.test_runner_name ⇒ Object
75 76 77 78 79 80 |
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 75 def test_runner_name return "rspec" if $PROGRAM_NAME.to_s.include?("rspec") return "minitest" if defined?(Minitest) && Minitest.respond_to?(:run) && !defined?(Rails::Server) return "cucumber" if $PROGRAM_NAME.to_s.include?("cucumber") nil end |