Skip to content

Commit

Permalink
Merge branch 'main' into swap-active-support-core-exts
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed May 13, 2024
2 parents 91c3592 + c428f02 commit dc12f35
Show file tree
Hide file tree
Showing 62 changed files with 781 additions and 862 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix: Lock Liquid to version < 5.5 (unresolved data leakage and generation errors otherwise)
- Fix: esbuild file endings from previous release
- Use bundler binstubs to create `bin/bt` shortcut instead of by `cp`

## [1.3.3] - 2024-03-16

Expand Down
13 changes: 9 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ PATH
liquid (>= 5.0, < 5.5)
listen (~> 3.0)
rack (>= 3.0)
rackup (~> 2.0)
rake (>= 13.0)
roda (~> 3.46)
rouge (>= 3.0, < 5.0)
Expand Down Expand Up @@ -129,10 +130,10 @@ GEM
racc (~> 1.4)
nokogiri (1.16.4-x86_64-linux)
racc (~> 1.4)
nokolexbor (0.5.3)
nokolexbor (0.5.3-arm64-darwin)
nokolexbor (0.5.3-x86_64-darwin)
nokolexbor (0.5.3-x86_64-linux)
nokolexbor (0.5.4)
nokolexbor (0.5.4-arm64-darwin)
nokolexbor (0.5.4-x86_64-darwin)
nokolexbor (0.5.4-x86_64-linux)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
Expand All @@ -142,6 +143,9 @@ GEM
rack (3.0.10)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rainbow (3.1.1)
rake (13.2.1)
rb-fsevent (0.11.2)
Expand Down Expand Up @@ -223,6 +227,7 @@ GEM
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
uri (0.13.0)
webrick (1.8.1)
yard (0.9.36)
zeitwerk (2.6.13)

Expand Down
6 changes: 6 additions & 0 deletions bridgetown-core/bin/bt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# `bt` is a shortcut to `bridgetown`

load File.join(File.dirname(__FILE__), 'bridgetown')
3 changes: 2 additions & 1 deletion bridgetown-core/bridgetown-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r!^(benchmark|features|script|test)/!)
end
s.executables = ["bridgetown"]
s.executables = ["bridgetown", "bt"] # `bt` is a shortcut to `bridgetown` command
s.bindir = "bin"
s.require_paths = ["lib"]

Expand Down Expand Up @@ -46,6 +46,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("liquid", [">= 5.0", "< 5.5"])
s.add_runtime_dependency("listen", "~> 3.0")
s.add_runtime_dependency("rack", ">= 3.0")
s.add_runtime_dependency("rackup", "~> 2.0")
s.add_runtime_dependency("rake", ">= 13.0")
s.add_runtime_dependency("roda", "~> 3.46")
s.add_runtime_dependency("rouge", [">= 3.0", "< 5.0"])
Expand Down
10 changes: 7 additions & 3 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ module Bridgetown
autoload :Slot, "bridgetown-core/slot"
autoload :StaticFile, "bridgetown-core/static_file"
autoload :Transformable, "bridgetown-core/concerns/transformable"
autoload :URL, "bridgetown-core/url"
autoload :Utils, "bridgetown-core/utils"
autoload :VERSION, "bridgetown-core/version"
autoload :Watcher, "bridgetown-core/watcher"
Expand Down Expand Up @@ -375,9 +374,7 @@ def build_errors_path
)
end
end
end

module Bridgetown
module Model; end

module Resource
Expand All @@ -390,6 +387,13 @@ def self.register_extension(mod)
end
end
end

# mixin for identity so Roda knows to call renderable objects
module RodaCallable
def self.===(other)
other.class < self
end
end
end

Zeitwerk.with_loader do |l|
Expand Down
30 changes: 8 additions & 22 deletions bridgetown-core/lib/bridgetown-core/commands/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,22 @@ def self.print_startup_message
def build
Bridgetown.logger.adjust_verbosity(options)

unless caller_locations.find do |loc|
loc.to_s.include?("bridgetown-core/commands/start.rb")
end
self.class.print_startup_message
end

# @type [Bridgetown::Configuration]
config_options = configuration_with_overrides(
options, Bridgetown::Current.preloaded_configuration
)

config_options.run_initializers! context: :static
self.class.print_startup_message unless config_options["start_command"]

config_options["serving"] = false unless config_options["serving"]
Process.setproctitle(
"bridgetown #{Bridgetown::VERSION} " \
"(#{config_options["start_command"] ? "start" : "build"}) [#{File.basename(Dir.pwd)}]"
)

config_options.run_initializers! context: :static

if !Bridgetown.env.production? &&
!config_options[:skip_frontend] && config_options["using_puma"]
!config_options[:skip_frontend] && config_options["start_command"]
if Bridgetown::Utils.frontend_bundler_type(config_options[:root_dir]) == :esbuild
Bridgetown::Utils.update_esbuild_autogenerated_config config_options
end
Expand Down Expand Up @@ -96,19 +95,6 @@ def build_site(config_options)
@site.process
Bridgetown.logger.info "Done! 🎉", "#{"Completed".bold.green} in less than " \
"#{(Time.now - t).ceil(2)} seconds."

return unless config_options[:using_puma]

require "socket"
external_ip = Socket.ip_address_list.find do |ai|
ai.ipv4? && !ai.ipv4_loopback?
end&.ip_address
scheme = config_options.bind&.split("://")&.first == "ssl" ? "https" : "http"
port = config_options.bind&.split(":")&.last || ENV["BRIDGETOWN_PORT"] || 4000
Bridgetown.logger.info ""
Bridgetown.logger.info "Now serving at:", "#{scheme}://localhost:#{port}".magenta
Bridgetown.logger.info "", "#{scheme}://#{external_ip}:#{port}".magenta if external_ip
Bridgetown.logger.info ""
end

# Watch for file changes and rebuild the site.
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/commands/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ def bundle_install(path)
Bridgetown.with_unbundled_env do
inside(path) do
run "bundle install", abort_on_failure: true
# create binstubs to `bin/bridgetown` and `bin/bt`
run "bundle binstubs bridgetown-core"
run "cp bin/bridgetown bin/bt"
end
end
end
Expand Down
135 changes: 75 additions & 60 deletions bridgetown-core/lib/bridgetown-core/commands/start.rb
Original file line number Diff line number Diff line change
@@ -1,103 +1,118 @@
# frozen_string_literal: true

require "rackup/server"

module Bridgetown
class Server < Rackup::Server
def start(after_stop_callback = nil)
trap(:INT) { exit }
super()
ensure
after_stop_callback&.call
end

def name
server.to_s.split("::").last
end

def using_puma?
name == "Puma"
end

def serveable?
server
true
rescue LoadError, NameError
false
end
end

module Commands
class Start < Thor::Group
extend BuildOptions
extend Summarizable
include ConfigurationOverridable
include Bridgetown::Utils::PidTracker

Registrations.register do
register(Start, "start", "start", Start.summary)
register(Start, "dev", "dev", "Alias of start")
end

class_option :bind, aliases: "-B", desc: "URI for Puma to bind to (start with tcp://)"
class_option :port,
aliases: "-P",
type: :numeric,
default: 4000,
desc: "Serve your site on the specified port. Defaults to 4000."
class_option :bind,
aliases: "-B",
type: :string,
default: "0.0.0.0",
desc: "URL for the server to bind to."
class_option :skip_frontend,
type: :boolean,
desc: "Don't load the frontend bundler (always true for production)"
desc: "Don't load the frontend bundler (always true for production)."
class_option :skip_live_reload,
type: :boolean,
desc: "Don't use the live reload functionality (always true for production)"
desc: "Don't use the live reload functionality (always true for production)."

def self.banner
"bridgetown start [options]"
end
summary "Start the Puma server, frontend bundler, and Bridgetown watcher"
summary "Start the web server, frontend bundler, and Bridgetown watcher"

def start # rubocop:todo Metrics/PerceivedComplexity
def start
Bridgetown.logger.writer.enable_prefix
Bridgetown::Commands::Build.print_startup_message
sleep 0.25

begin
require("puma/detect")
rescue LoadError
raise "** Puma server gem not found. Check your Gemfile and Bundler env? **"
end

options = Thor::CoreExt::HashWithIndifferentAccess.new(self.options)
options[:using_puma] = true
options[:start_command] = true

# Load Bridgetown configuration into thread memory
bt_options = configuration_with_overrides(options)
port = ENV.fetch("BRIDGETOWN_PORT", bt_options.port)
# TODO: support Puma serving HTTPS directly?
bt_bound_url = "http://#{bt_options.bind}:#{port}"

# Set a local site URL in the config if one is not available
if Bridgetown.env.development? && !options["url"]
scheme = bt_options.bind&.split("://")&.first == "ssl" ? "https" : "http"
port = bt_options.bind&.split(":")&.last || ENV["BRIDGETOWN_PORT"] || 4000
bt_options.url = "#{scheme}://localhost:#{port}"
end
bt_options.url = bt_bound_url if Bridgetown.env.development? && !options["url"]

puma_pid =
Process.fork do
require "puma/cli"

Puma::Runner.class_eval do
def output_header(mode)
log "* Puma version: #{Puma::Const::PUMA_VERSION} (#{ruby_engine}) (\"#{Puma::Const::CODE_NAME}\")" # rubocop:disable Layout/LineLength
if mode == "cluster"
log "* Cluster Master PID: #{Process.pid}"
else
log "* PID: #{Process.pid}"
end
end
end
Bridgetown::Server.new({
Host: bt_options.bind,
Port: port,
config: "config.ru",
}).tap do |server|
if server.serveable?
create_pid_dir

puma_args = []
if bt_options[:bind]
puma_args << "--bind"
puma_args << bt_options[:bind]
end
bt_options.skip_live_reload = !server.using_puma?

cli = Puma::CLI.new puma_args
cli.launcher.events.on_stopped do
build_args = ["-w"] + ARGV.reject { |arg| arg == "start" }
build_pid = Process.fork { Bridgetown::Commands::Build.start(build_args) }
add_pid(build_pid, file: :bridgetown)

after_stop_callback = -> {
say "Stopping Bridgetown server..."
Bridgetown::Hooks.trigger :site, :server_shutdown
end
cli.run
end
Process.kill "SIGINT", build_pid
remove_pidfile :bridgetown

begin
Signal.trap("TERM") do
Process.kill "SIGINT", puma_pid
sleep 0.5 # let it breathe
exit 0 # this runs the ensure block below
end
# Shut down the frontend bundler etc. if they're running
unless Bridgetown.env.production? || bt_options[:skip_frontend]
Bridgetown::Utils::Aux.kill_processes
end
}

Process.setproctitle("bridgetown #{Bridgetown::VERSION} [#{File.basename(Dir.pwd)}]")

build_args = ["-w"] + ARGV.reject { |arg| arg == "start" }
Bridgetown::Commands::Build.start(build_args)
rescue StandardError => e
Process.kill "SIGINT", puma_pid
sleep 0.5
raise e
ensure
# Shut down esbuild, etc. if they're running
Bridgetown::Utils::Aux.kill_processes
end
Bridgetown.logger.info ""
Bridgetown.logger.info "Booting #{server.name} at:", bt_bound_url.to_s.magenta
Bridgetown.logger.info ""

sleep 0.5 # finish cleaning up
server.start(after_stop_callback)
else
say "Unable to find a Rack server."
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def render_in(view_context, &block)
# Subclasses can override this method to return a string from their own
# template handling.
def template
call || _renderer.render(self)
(method(:call).arity.zero? ? call : nil) || _renderer.render(self)
end

# Typically not used but here as a compatibility nod toward ViewComponent.
Expand Down
4 changes: 2 additions & 2 deletions bridgetown-core/lib/bridgetown-core/concerns/site/ssr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ def ssr?

def enable_ssr
Bridgetown.logger.info "SSR:", "enabled."
config.fast_refresh = false # SSR mode and Fast Refresh mode are mututally exclusive
@ssr_enabled = true
end

def ssr_setup(&block)
config.serving = true

Bridgetown::Hooks.trigger :site, :pre_read, self
ssr_first_read
Bridgetown::Hooks.trigger :site, :post_read, self
Expand All @@ -46,6 +45,7 @@ def ssr_setup(&block)
end

def ssr_first_read
# TODO: this shouldn't be running twice, right?!
Bridgetown::Hooks.trigger :site, :pre_read, self
defaults_reader.tap do |d|
d.path_defaults.clear
Expand Down
Loading

0 comments on commit dc12f35

Please sign in to comment.