Skip to content

Commit 80ffc86

Browse files
versunrafaelfranca
andauthored
docs: Update all code snippets to the latest version [ci skip] (rails#54118)
* docs: Update all code snippets to the latest version [ci skip] * $ bundle exec rubocop -A Co-authored-by: Rafael Mendonça França <[email protected]>
1 parent e978ef0 commit 80ffc86

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

guides/source/initialization.md

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,26 @@ module Rails
108108
module Command
109109
class << self
110110
def invoke(full_namespace, args = [], **config)
111-
namespace = full_namespace = full_namespace.to_s
112-
113-
if char = namespace =~ /:(\w+)$/
114-
command_name, namespace = $1, namespace.slice(0, char)
115-
else
116-
command_name = namespace
117-
end
118-
119-
command_name, namespace = "help", "help" if command_name.blank? || HELP_MAPPINGS.include?(command_name)
120-
command_name, namespace = "version", "version" if %w( -v --version ).include?(command_name)
111+
args = ["--help"] if rails_new_with_no_path?(args)
121112

113+
full_namespace = full_namespace.to_s
114+
namespace, command_name = split_namespace(full_namespace)
122115
command = find_by_namespace(namespace, command_name)
123-
if command && command.all_commands[command_name]
124-
command.perform(command_name, args, config)
116+
117+
with_argv(args) do
118+
if command && command.all_commands[command_name]
119+
command.perform(command_name, args, config)
120+
else
121+
invoke_rake(full_namespace, args, config)
122+
end
123+
end
124+
rescue UnrecognizedCommandError => error
125+
if error.name == full_namespace && command && command_name == full_namespace
126+
command.perform("help", [], config)
125127
else
126-
find_by_namespace("rake").perform(full_namespace, args, config)
128+
puts error.detailed_message
127129
end
130+
exit(1)
128131
end
129132
end
130133
end
@@ -138,7 +141,6 @@ module Rails
138141
module Command
139142
class ServerCommand < Base # :nodoc:
140143
def perform
141-
extract_environment_option_from_argument
142144
set_application_directory!
143145
prepare_restart
144146

@@ -153,7 +155,7 @@ module Rails
153155
after_stop_callback = -> { say "Exiting" unless options[:daemon] }
154156
server.start(after_stop_callback)
155157
else
156-
say rack_server_suggestion(using)
158+
say rack_server_suggestion(options[:using])
157159
end
158160
end
159161
end
@@ -174,12 +176,12 @@ It adds functionality like routing, session, and common middlewares.
174176
### `rails/commands/server/server_command.rb`
175177

176178
The `Rails::Server` class is defined in this file by inheriting from
177-
`Rack::Server`. When `Rails::Server.new` is called, this calls the `initialize`
179+
`Rackup::Server`. When `Rails::Server.new` is called, this calls the `initialize`
178180
method in `rails/commands/server/server_command.rb`:
179181

180182
```ruby
181183
module Rails
182-
class Server < ::Rack::Server
184+
class Server < Rackup::Server
183185
def initialize(options = nil)
184186
@default_options = options || {}
185187
super(@default_options)
@@ -189,16 +191,16 @@ module Rails
189191
end
190192
```
191193

192-
Firstly, `super` is called which calls the `initialize` method on `Rack::Server`.
194+
Firstly, `super` is called which calls the `initialize` method on `Rackup::Server`.
193195

194-
### Rack: `lib/rack/server.rb`
196+
### Rackup: `lib/rackup/server.rb`
195197

196-
`Rack::Server` is responsible for providing a common server interface for all Rack-based applications, which Rails is now a part of.
198+
`Rackup::Server` is responsible for providing a common server interface for all Rack-based applications, which Rails is now a part of.
197199

198-
The `initialize` method in `Rack::Server` simply sets several variables:
200+
The `initialize` method in `Rackup::Server` simply sets several variables:
199201

200202
```ruby
201-
module Rack
203+
module Rackup
202204
class Server
203205
def initialize(options = nil)
204206
@ignore_options = []
@@ -208,9 +210,8 @@ module Rack
208210
@options = options
209211
@app = options[:app] if options[:app]
210212
else
211-
argv = defined?(SPEC_ARGV) ? SPEC_ARGV : ARGV
212213
@use_default_options = true
213-
@options = parse_options(argv)
214+
@options = parse_options(ARGV)
214215
end
215216
end
216217
end
@@ -225,12 +226,12 @@ When lines inside if statement is evaluated, a couple of instance variables will
225226
```ruby
226227
module Rails
227228
module Command
228-
class ServerCommand
229+
class ServerCommand < Base # :nodoc:
229230
no_commands do
230231
def server_options
231232
{
232233
user_supplied_options: user_supplied_options,
233-
server: using,
234+
server: options[:using],
234235
log_stdout: log_to_stdout?,
235236
Port: port,
236237
Host: host,
@@ -252,7 +253,7 @@ end
252253

253254
The value will be assigned to instance variable `@options`.
254255

255-
After `super` has finished in `Rack::Server`, we jump back to
256+
After `super` has finished in `Rackup::Server`, we jump back to
256257
`rails/commands/server/server_command.rb`. At this point, `set_environment`
257258
is called within the context of the `Rails::Server` object.
258259

@@ -282,7 +283,7 @@ defined like this:
282283

283284
```ruby
284285
module Rails
285-
class Server < ::Rack::Server
286+
class Server < ::Rackup::Server
286287
def start(after_stop_callback = nil)
287288
trap(:INT) { exit }
288289
create_tmp_directories
@@ -313,8 +314,8 @@ module Rails
313314
console.formatter = Rails.logger.formatter
314315
console.level = Rails.logger.level
315316

316-
unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDOUT)
317-
Rails.logger.extend(ActiveSupport::Logger.broadcast(console))
317+
unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDERR, STDOUT)
318+
Rails.logger.broadcast_to(console)
318319
end
319320
end
320321
end
@@ -328,12 +329,12 @@ if `bin/rails server` is called with `--dev-caching`. Finally, it calls `wrapped
328329
responsible for creating the Rack app, before creating and assigning an instance
329330
of `ActiveSupport::Logger`.
330331

331-
The `super` method will call `Rack::Server.start` which begins its definition as follows:
332+
The `super` method will call `Rackup::Server.start` which begins its definition as follows:
332333

333334
```ruby
334-
module Rack
335+
module Rackup
335336
class Server
336-
def start(&blk)
337+
def start(&block)
337338
if options[:warn]
338339
$-w = true
339340
end
@@ -342,12 +343,13 @@ module Rack
342343
$LOAD_PATH.unshift(*includes)
343344
end
344345

345-
if library = options[:require]
346+
Array(options[:require]).each do |library|
346347
require library
347348
end
348349

349350
if options[:debug]
350351
$DEBUG = true
352+
require "pp"
351353
p options[:server]
352354
pp wrapped_app
353355
pp app
@@ -373,7 +375,7 @@ module Rack
373375
end
374376
end
375377

376-
server.run wrapped_app, options, &blk
378+
server.run(wrapped_app, **options, &block)
377379
end
378380
end
379381
end
@@ -384,7 +386,7 @@ we're going to explore more (even though it was executed before, and
384386
thus memoized by now).
385387

386388
```ruby
387-
module Rack
389+
module Rackup
388390
class Server
389391
def wrapped_app
390392
@wrapped_app ||= build_app app
@@ -396,7 +398,7 @@ end
396398
The `app` method here is defined like so:
397399

398400
```ruby
399-
module Rack
401+
module Rackup
400402
class Server
401403
def app
402404
@app ||= options[:builder] ? build_app_from_string : build_app_and_options_from_config
@@ -410,9 +412,7 @@ module Rack
410412
abort "configuration #{options[:config]} not found"
411413
end
412414

413-
app, options = Rack::Builder.parse_file(self.options[:config], opt_parser)
414-
@options.merge!(options) { |key, old, new| old }
415-
app
415+
Rack::Builder.parse_file(self.options[:config])
416416
end
417417

418418
def build_app_from_string
@@ -430,6 +430,7 @@ The `options[:config]` value defaults to `config.ru` which contains this:
430430
require_relative "config/environment"
431431

432432
run Rails.application
433+
Rails.application.load_server
433434
```
434435

435436

@@ -438,17 +439,22 @@ The `Rack::Builder.parse_file` method here takes the content from this `config.r
438439
```ruby
439440
module Rack
440441
class Builder
441-
def self.load_file(path, opts = Server::Options.new)
442-
# ...
443-
app = new_from_string cfgfile, config
442+
def self.load_file(path, **options)
444443
# ...
444+
new_from_string(config, path, **options)
445445
end
446446

447447
# ...
448448

449-
def self.new_from_string(builder_script, file = "(rackup)")
450-
eval "Rack::Builder.new {\n" + builder_script + "\n}.to_app",
451-
TOPLEVEL_BINDING, file, 0
449+
def self.new_from_string(builder_script, path = "(rackup)", **options)
450+
builder = self.new(**options)
451+
452+
# We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.
453+
# We cannot use instance_eval(String) as that would resolve constants differently.
454+
binding = BUILDER_TOPLEVEL_BINDING.call(builder)
455+
eval(builder_script, binding, path)
456+
457+
builder.to_app
452458
end
453459
end
454460
end
@@ -580,14 +586,14 @@ itself and are run between the `bootstrap` and `finisher`.
580586
NOTE: Do not confuse Railtie initializers overall with the [load_config_initializers](configuring.html#using-initializer-files)
581587
initializer instance or its associated config initializers in `config/initializers`.
582588

583-
After this is done we go back to `Rack::Server`.
589+
After this is done we go back to `Rackup::Server`.
584590

585591
### Rack: lib/rack/server.rb
586592

587593
Last time we left when the `app` method was being defined:
588594

589595
```ruby
590-
module Rack
596+
module Rackup
591597
class Server
592598
def app
593599
@app ||= options[:builder] ? build_app_from_string : build_app_and_options_from_config
@@ -601,9 +607,7 @@ module Rack
601607
abort "configuration #{options[:config]} not found"
602608
end
603609

604-
app, options = Rack::Builder.parse_file(self.options[:config], opt_parser)
605-
@options.merge!(options) { |key, old, new| old }
606-
app
610+
Rack::Builder.parse_file(self.options[:config])
607611
end
608612

609613
def build_app_from_string
@@ -617,7 +621,7 @@ At this point `app` is the Rails app itself (a middleware), and what
617621
happens next is Rack will call all the provided middlewares:
618622

619623
```ruby
620-
module Rack
624+
module Rackup
621625
class Server
622626
private
623627
def build_app(app)
@@ -633,11 +637,11 @@ module Rack
633637
end
634638
```
635639

636-
Remember, `build_app` was called (by `wrapped_app`) in the last line of `Rack::Server#start`.
640+
Remember, `build_app` was called (by `wrapped_app`) in the last line of `Rackup::Server#start`.
637641
Here's how it looked like when we left:
638642

639643
```ruby
640-
server.run wrapped_app, options, &blk
644+
server.run(wrapped_app, **options, &block)
641645
```
642646

643647
At this point, the implementation of `server.run` will depend on the
@@ -650,11 +654,11 @@ module Rack
650654
module Puma
651655
# ...
652656
def self.run(app, options = {})
653-
conf = self.config(app, options)
657+
conf = self.config(app, options)
654658

655-
events = options.delete(:Silent) ? ::Puma::Events.strings : ::Puma::Events.stdio
659+
log_writer = options.delete(:Silent) ? ::Puma::LogWriter.strings : ::Puma::LogWriter.stdio
656660

657-
launcher = ::Puma::Launcher.new(conf, events: events)
661+
launcher = ::Puma::Launcher.new(conf, log_writer: log_writer, events: @events)
658662

659663
yield launcher if block_given?
660664
begin

0 commit comments

Comments
 (0)