#!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'pathname' ENV['RAILS_ENV'] = 'standalone' ENV['SMALRUBY_EDITOR_HOME'] ||= Pathname('~/.smalruby-editor').expand_path.to_s APP_PATH = File.expand_path('../../config/application', __FILE__) require_relative '../config/boot' lib = File.expand_path('../../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'smalruby_editor' Dir.chdir(File.expand_path('../../', APP_PATH)) require 'rails/commands/server' # rubocop:disable all module Rails class Server < ::Rack::Server def start url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" puts "=> smalruby-editor #{SmalrubyEditor::VERSION} starting in #{Rails.env} on #{url}" puts "=> Run `smalruby-editor -h` for more startup options" trap(:INT) { exit } puts "=> Ctrl-C to shutdown server" unless options[:daemonize] unless options[:daemonize] wrapped_app # touch the app so the logger is set up console = ActiveSupport::Logger.new($stdout) console.formatter = Rails.logger.formatter console.level = Rails.logger.level Rails.logger.extend(ActiveSupport::Logger.broadcast(console)) if ActiveRecord::Migrator.needs_migration? ActiveRecord::Migration.verbose = true begin ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths) ensure ActiveRecord::Migration.verbose = false end end end super ensure # The '-h' option calls exit before @options is set. # If we call 'options' with it unset, we get double help banners. puts 'Exiting' unless @options && options[:daemonize] end def default_options super.merge({ Port: 8087, DoNotReverseLookup: true, environment: (ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development").dup, daemonize: false, debugger: false, pid: Pathname(ENV['SMALRUBY_EDITOR_HOME']).join("tmp/pids/server.pid").to_s, config: File.expand_path("config.ru") }) end private def parse_options_with_start_callback(args) options = parse_options_without_start_callback(args) options[:StartCallback] = -> { uri = "http://localhost:#{options[:Port]}" launchy = Launchy.open(uri) do |exception| puts "Attempted to open #{uri} and failed because #{exception}" end } options end alias_method_chain :parse_options, :start_callback end end Rails::Server.new.tap do |server| require APP_PATH Dir.chdir(Rails.application.root) server.start end # rubocop:enable all