Skip to content

Commit

Permalink
Support minimalist installations with no config.ru or Rakefile (o…
Browse files Browse the repository at this point in the history
…r anything other than `Gemfile` really) (#942)

* Support minimalist installations with no `config.ru` or `Rakefile`

* fix cop

* Set key file path expansions on config load

* Ensure even with missing initializers files, load paths are expanded
  • Loading branch information
jaredcwhite authored Dec 17, 2024
1 parent db50735 commit e05a2ad
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
21 changes: 16 additions & 5 deletions bridgetown-core/lib/bridgetown-core/commands/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
10 changes: 9 additions & 1 deletion bridgetown-core/lib/bridgetown-core/commands/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
15 changes: 9 additions & 6 deletions bridgetown-core/lib/bridgetown-core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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({
Expand Down
14 changes: 14 additions & 0 deletions bridgetown-core/lib/bridgetown-core/rack/default_config.ru
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions bridgetown-website/config.ru

This file was deleted.

4 changes: 1 addition & 3 deletions bridgetown-website/server/roda_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
class RodaApp < Roda
plugin :bridgetown_server

route do |r|
r.bridgetown
end
route(&:bridgetown)
end

0 comments on commit e05a2ad

Please sign in to comment.