@@ -108,23 +108,26 @@ module Rails
108
108
module Command
109
109
class << self
110
110
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)
121
112
113
+ full_namespace = full_namespace.to_s
114
+ namespace, command_name = split_namespace(full_namespace)
122
115
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)
125
127
else
126
- find_by_namespace( " rake " ).perform(full_namespace, args, config)
128
+ puts error.detailed_message
127
129
end
130
+ exit (1 )
128
131
end
129
132
end
130
133
end
@@ -138,7 +141,6 @@ module Rails
138
141
module Command
139
142
class ServerCommand < Base # :nodoc:
140
143
def perform
141
- extract_environment_option_from_argument
142
144
set_application_directory!
143
145
prepare_restart
144
146
@@ -153,7 +155,7 @@ module Rails
153
155
after_stop_callback = -> { say " Exiting" unless options[:daemon ] }
154
156
server.start(after_stop_callback)
155
157
else
156
- say rack_server_suggestion(using )
158
+ say rack_server_suggestion(options[ : using] )
157
159
end
158
160
end
159
161
end
@@ -174,12 +176,12 @@ It adds functionality like routing, session, and common middlewares.
174
176
### ` rails/commands/server/server_command.rb `
175
177
176
178
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 `
178
180
method in ` rails/commands/server/server_command.rb ` :
179
181
180
182
``` ruby
181
183
module Rails
182
- class Server < :: Rack ::Server
184
+ class Server < Rackup ::Server
183
185
def initialize (options = nil )
184
186
@default_options = options || {}
185
187
super (@default_options )
@@ -189,16 +191,16 @@ module Rails
189
191
end
190
192
```
191
193
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` .
193
195
194
- ### Rack : ` lib/rack /server.rb `
196
+ ### Rackup : ` lib/rackup /server.rb `
195
197
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.
197
199
198
- The ` initialize ` method in ` Rack ::Server` simply sets several variables:
200
+ The ` initialize ` method in ` Rackup ::Server` simply sets several variables:
199
201
200
202
``` ruby
201
- module Rack
203
+ module Rackup
202
204
class Server
203
205
def initialize (options = nil )
204
206
@ignore_options = []
@@ -208,9 +210,8 @@ module Rack
208
210
@options = options
209
211
@app = options[:app ] if options[:app ]
210
212
else
211
- argv = defined? (SPEC_ARGV ) ? SPEC_ARGV : ARGV
212
213
@use_default_options = true
213
- @options = parse_options(argv )
214
+ @options = parse_options(ARGV )
214
215
end
215
216
end
216
217
end
@@ -225,12 +226,12 @@ When lines inside if statement is evaluated, a couple of instance variables will
225
226
``` ruby
226
227
module Rails
227
228
module Command
228
- class ServerCommand
229
+ class ServerCommand < Base # :nodoc:
229
230
no_commands do
230
231
def server_options
231
232
{
232
233
user_supplied_options: user_supplied_options,
233
- server: using ,
234
+ server: options[ : using] ,
234
235
log_stdout: log_to_stdout?,
235
236
Port: port,
236
237
Host: host,
252
253
253
254
The value will be assigned to instance variable ` @options ` .
254
255
255
- After ` super ` has finished in ` Rack ::Server` , we jump back to
256
+ After ` super ` has finished in ` Rackup ::Server` , we jump back to
256
257
` rails/commands/server/server_command.rb ` . At this point, ` set_environment `
257
258
is called within the context of the ` Rails::Server ` object.
258
259
@@ -282,7 +283,7 @@ defined like this:
282
283
283
284
``` ruby
284
285
module Rails
285
- class Server < ::Rack ::Server
286
+ class Server < ::Rackup ::Server
286
287
def start (after_stop_callback = nil )
287
288
trap (:INT ) { exit }
288
289
create_tmp_directories
@@ -313,8 +314,8 @@ module Rails
313
314
console.formatter = Rails .logger.formatter
314
315
console.level = Rails .logger.level
315
316
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)
318
319
end
319
320
end
320
321
end
@@ -328,12 +329,12 @@ if `bin/rails server` is called with `--dev-caching`. Finally, it calls `wrapped
328
329
responsible for creating the Rack app, before creating and assigning an instance
329
330
of ` ActiveSupport::Logger ` .
330
331
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:
332
333
333
334
``` ruby
334
- module Rack
335
+ module Rackup
335
336
class Server
336
- def start (& blk )
337
+ def start (& block )
337
338
if options[:warn ]
338
339
$- w = true
339
340
end
@@ -342,12 +343,13 @@ module Rack
342
343
$LOAD_PATH .unshift(* includes)
343
344
end
344
345
345
- if library = options[:require ]
346
+ Array ( options[:require ]).each do | library |
346
347
require library
347
348
end
348
349
349
350
if options[:debug ]
350
351
$DEBUG = true
352
+ require " pp"
351
353
p options[:server ]
352
354
pp wrapped_app
353
355
pp app
@@ -373,7 +375,7 @@ module Rack
373
375
end
374
376
end
375
377
376
- server.run wrapped_app, options, & blk
378
+ server.run( wrapped_app, ** options, & block)
377
379
end
378
380
end
379
381
end
@@ -384,7 +386,7 @@ we're going to explore more (even though it was executed before, and
384
386
thus memoized by now).
385
387
386
388
``` ruby
387
- module Rack
389
+ module Rackup
388
390
class Server
389
391
def wrapped_app
390
392
@wrapped_app ||= build_app app
396
398
The ` app ` method here is defined like so:
397
399
398
400
``` ruby
399
- module Rack
401
+ module Rackup
400
402
class Server
401
403
def app
402
404
@app ||= options[:builder ] ? build_app_from_string : build_app_and_options_from_config
@@ -410,9 +412,7 @@ module Rack
410
412
abort " configuration #{ options[:config ] } not found"
411
413
end
412
414
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 ])
416
416
end
417
417
418
418
def build_app_from_string
@@ -430,6 +430,7 @@ The `options[:config]` value defaults to `config.ru` which contains this:
430
430
require_relative " config/environment"
431
431
432
432
run Rails .application
433
+ Rails .application.load_server
433
434
```
434
435
435
436
@@ -438,17 +439,22 @@ The `Rack::Builder.parse_file` method here takes the content from this `config.r
438
439
``` ruby
439
440
module Rack
440
441
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)
444
443
# ...
444
+ new_from_string(config, path, ** options)
445
445
end
446
446
447
447
# ...
448
448
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
452
458
end
453
459
end
454
460
end
@@ -580,14 +586,14 @@ itself and are run between the `bootstrap` and `finisher`.
580
586
NOTE: Do not confuse Railtie initializers overall with the [ load_config_initializers] ( configuring.html#using-initializer-files )
581
587
initializer instance or its associated config initializers in ` config/initializers ` .
582
588
583
- After this is done we go back to ` Rack ::Server` .
589
+ After this is done we go back to ` Rackup ::Server` .
584
590
585
591
### Rack: lib/rack/server.rb
586
592
587
593
Last time we left when the ` app ` method was being defined:
588
594
589
595
``` ruby
590
- module Rack
596
+ module Rackup
591
597
class Server
592
598
def app
593
599
@app ||= options[:builder ] ? build_app_from_string : build_app_and_options_from_config
@@ -601,9 +607,7 @@ module Rack
601
607
abort " configuration #{ options[:config ] } not found"
602
608
end
603
609
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 ])
607
611
end
608
612
609
613
def build_app_from_string
@@ -617,7 +621,7 @@ At this point `app` is the Rails app itself (a middleware), and what
617
621
happens next is Rack will call all the provided middlewares:
618
622
619
623
``` ruby
620
- module Rack
624
+ module Rackup
621
625
class Server
622
626
private
623
627
def build_app (app )
@@ -633,11 +637,11 @@ module Rack
633
637
end
634
638
```
635
639
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` .
637
641
Here's how it looked like when we left:
638
642
639
643
``` ruby
640
- server.run wrapped_app, options, & blk
644
+ server.run( wrapped_app, ** options, & block)
641
645
```
642
646
643
647
At this point, the implementation of ` server.run ` will depend on the
@@ -650,11 +654,11 @@ module Rack
650
654
module Puma
651
655
# ...
652
656
def self .run (app , options = {})
653
- conf = self .config(app, options)
657
+ conf = self .config(app, options)
654
658
655
- events = options.delete(:Silent ) ? ::Puma ::Events .strings : ::Puma ::Events .stdio
659
+ log_writer = options.delete(:Silent ) ? ::Puma ::LogWriter .strings : ::Puma ::LogWriter .stdio
656
660
657
- launcher = ::Puma ::Launcher .new (conf, events: events)
661
+ launcher = ::Puma ::Launcher .new (conf, log_writer: log_writer, events: @ events )
658
662
659
663
yield launcher if block_given?
660
664
begin
0 commit comments