diff --git a/bridgetown-core/lib/bridgetown-core/commands/build.rb b/bridgetown-core/lib/bridgetown-core/commands/build.rb index 0b5194ca0..1dc6e0adc 100644 --- a/bridgetown-core/lib/bridgetown-core/commands/build.rb +++ b/bridgetown-core/lib/bridgetown-core/commands/build.rb @@ -50,11 +50,7 @@ def build if Bridgetown::Utils.frontend_bundler_type(config_options[:root_dir]) == :esbuild Bridgetown::Utils.update_esbuild_autogenerated_config config_options end - require "rake" - Rake.with_application do |rake| - rake.load_rakefile - rake["frontend:watcher"].invoke(true) - end + invoke_frontend_watcher_from_rake end @site = Bridgetown::Site.new(config_options) @@ -125,6 +121,21 @@ def display_folder_paths(config_options) plugins_dir = File.expand_path(config_options["plugins_dir"]) Bridgetown.logger.info "Custom Plugins:", plugins_dir if Dir.exist?(plugins_dir) end + + def invoke_frontend_watcher_from_rake + require "rake" + Rake.with_application do |rake| + begin + rake.raw_load_rakefile + rescue StandardError => e + unless e.message.include?("No Rakefile found") + Bridgetown.logger.error "Error Running Rake:", "#{e.message} (#{e.class})" + e.backtrace[0..1].each { |backtrace_line| Bridgetown.logger.info backtrace_line } + end + end + rake["frontend:watcher"].invoke(true) if rake.tasks.any? { _1.name == "frontend:watcher" } + end + end end end end diff --git a/bridgetown-core/lib/bridgetown-core/commands/start.rb b/bridgetown-core/lib/bridgetown-core/commands/start.rb index 0dca6c21c..e7c47f73f 100644 --- a/bridgetown-core/lib/bridgetown-core/commands/start.rb +++ b/bridgetown-core/lib/bridgetown-core/commands/start.rb @@ -84,7 +84,7 @@ def start Bridgetown::Server.new({ Host: bt_options.bind, Port: port, - config: "config.ru", + config: rack_config_file, }).tap do |server| if server.serveable? pid_tracker.create_pid_dir @@ -117,6 +117,14 @@ def start end end end + + protected + + def rack_config_file + File.exist?("config.ru") ? + "config.ru" : + File.expand_path("../rack/default_config.ru", __dir__) + end end end end diff --git a/bridgetown-core/lib/bridgetown-core/configuration.rb b/bridgetown-core/lib/bridgetown-core/configuration.rb index dec848e8c..68bbd2ca7 100644 --- a/bridgetown-core/lib/bridgetown-core/configuration.rb +++ b/bridgetown-core/lib/bridgetown-core/configuration.rb @@ -129,7 +129,10 @@ def from(user_config, starting_defaults = DEFAULTS) def run_initializers!(context:) # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity initializers_file = File.join(root_dir, "config", "initializers.rb") - return unless File.file?(initializers_file) + unless File.file?(initializers_file) + setup_load_paths! appending: true + return + end load initializers_file @@ -287,13 +290,13 @@ def merge_environment_specific_options! end def setup_load_paths!(appending: false) # rubocop:todo Metrics - unless appending - self[:root_dir] = File.expand_path(self[:root_dir]) - self[:source] = File.expand_path(self[:source], self[:root_dir]) - self[:destination] = File.expand_path(self[:destination], self[:root_dir]) + self[:root_dir] = File.expand_path(self[:root_dir]) + self[:source] = File.expand_path(self[:source], self[:root_dir]) + self[:destination] = File.expand_path(self[:destination], self[:root_dir]) + unless appending autoload_paths.unshift({ - path: self[:plugins_dir], + path: File.expand_path(self[:plugins_dir], self[:root_dir]), eager: true, }) autoload_paths.unshift({ diff --git a/bridgetown-core/lib/bridgetown-core/rack/default_config.ru b/bridgetown-core/lib/bridgetown-core/rack/default_config.ru new file mode 100644 index 000000000..7b959c7f4 --- /dev/null +++ b/bridgetown-core/lib/bridgetown-core/rack/default_config.ru @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "bridgetown-core/rack/boot" + +Bridgetown::Rack.boot + +unless defined?(RodaApp) + class RodaApp < Roda + plugin :bridgetown_server + route(&:bridgetown) + end +end + +run RodaApp.freeze.app diff --git a/bridgetown-foundation/lib/bridgetown/foundation/packages/pid_tracker.rb b/bridgetown-foundation/lib/bridgetown/foundation/packages/pid_tracker.rb index 39994d090..a4c569262 100644 --- a/bridgetown-foundation/lib/bridgetown/foundation/packages/pid_tracker.rb +++ b/bridgetown-foundation/lib/bridgetown/foundation/packages/pid_tracker.rb @@ -13,10 +13,13 @@ def add_pid(pid, file:) def read_pidfile(file) File.readlines pidfile_for(file), chomp: true + rescue SystemCallError + [] end def remove_pidfile(file) File.delete pidfile_for(file) + rescue SystemCallError # rubocop:disable Lint/SuppressedException end private diff --git a/bridgetown-website/config.ru b/bridgetown-website/config.ru deleted file mode 100644 index 80ee3495e..000000000 --- a/bridgetown-website/config.ru +++ /dev/null @@ -1,7 +0,0 @@ -# This file is used by Rack-based servers during the Bridgetown boot process. - -require "bridgetown-core/rack/boot" - -Bridgetown::Rack.boot - -run RodaApp.freeze.app # see server/roda_app.rb diff --git a/bridgetown-website/server/roda_app.rb b/bridgetown-website/server/roda_app.rb index 921e63ebc..6b495ed56 100644 --- a/bridgetown-website/server/roda_app.rb +++ b/bridgetown-website/server/roda_app.rb @@ -7,7 +7,5 @@ class RodaApp < Roda plugin :bridgetown_server - route do |r| - r.bridgetown - end + route(&:bridgetown) end