middleman-core 3.0.2 → 3.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -223,7 +223,7 @@ module Middleman
223
223
  # messages, which can take a long time (minutes at full CPU)
224
224
  # if the object is huge or has cyclic references, like this.
225
225
  def to_s
226
- "#<Middleman::Application>"
226
+ "#<Middleman::Application:0x#{object_id}>"
227
227
  end
228
228
 
229
229
  # Expand a path to include the index file if it's a directory
@@ -37,6 +37,10 @@ module Middleman::Cli
37
37
  :type => :boolean,
38
38
  :default => false,
39
39
  :desc => 'Generate profiling report for server startup'
40
+ method_option :reload_paths,
41
+ :type => :string,
42
+ :default => false,
43
+ :desc => 'Additional paths to auto-reload when files change'
40
44
 
41
45
  # Start the server
42
46
  def server
@@ -56,7 +60,8 @@ module Middleman::Cli
56
60
  :environment => options["environment"],
57
61
  :debug => options["verbose"],
58
62
  :instrumenting => options["instrument"],
59
- :"disable-watcher" => options["disable-watcher"]
63
+ :"disable-watcher" => options["disable-watcher"],
64
+ :reload_paths => options["reload_paths"]
60
65
  }
61
66
 
62
67
  puts "== The Middleman is loading"
@@ -9,43 +9,26 @@ module Middleman
9
9
  DEFAULT_PORT = 4567
10
10
 
11
11
  class << self
12
- attr_reader :app
12
+ attr_reader :app, :port
13
13
  delegate :logger, :to => :app
14
14
 
15
15
  # Start an instance of Middleman::Application
16
16
  # @return [void]
17
- def start(options={})
18
- @app = ::Middleman::Application.server.inst do
19
- if options[:environment]
20
- set :environment, options[:environment].to_sym
21
- end
22
-
23
- logger(options[:debug] ? 0 : 1, options[:instrumenting] || false)
24
- end
25
-
26
- port = options[:port] || DEFAULT_PORT
17
+ def start(opts={})
18
+ @options = opts
19
+ @port = @options[:port] || DEFAULT_PORT
27
20
 
21
+ mount_instance
28
22
  logger.info "== The Middleman is standing watch on port #{port}"
29
23
 
30
- @webrick ||= setup_webrick(
31
- options[:host] || "0.0.0.0",
32
- port,
33
- options[:debug] || false
34
- )
35
-
36
- mount_instance(app)
37
-
38
- start_file_watcher unless options[:"disable-watcher"]
39
-
40
24
  @initialized ||= false
41
25
  unless @initialized
42
26
  @initialized = true
43
27
 
44
28
  register_signal_handlers unless ::Middleman::WINDOWS
45
29
 
46
- # Save the last-used options so it may be re-used when
30
+ # Save the last-used @options so it may be re-used when
47
31
  # reloading later on.
48
- @last_options = options
49
32
  ::Middleman::Profiling.report("server_start")
50
33
 
51
34
  @webrick.start
@@ -66,8 +49,12 @@ module Middleman
66
49
  # Simply stop, then start the server
67
50
  # @return [void]
68
51
  def reload
69
- stop
70
- start @last_options
52
+ logger.info "== The Middleman is reloading"
53
+
54
+ unmount_instance
55
+ mount_instance
56
+
57
+ logger.info "== The Middleman is standing watch on port #{port}"
71
58
  end
72
59
 
73
60
  # Stop the current instance, exit Webrick
@@ -78,47 +65,47 @@ module Middleman
78
65
  end
79
66
 
80
67
  private
68
+ def new_app
69
+ opts = @options
70
+ @app =::Middleman::Application.server.inst do
71
+ if opts[:environment]
72
+ set :environment, opts[:environment].to_sym
73
+ end
74
+
75
+ logger(opts[:debug] ? 0 : 1, opts[:instrumenting] || false)
76
+ end
77
+ end
81
78
 
82
79
  def start_file_watcher
83
- # Watcher Library
84
- require "listen"
85
-
86
- return if @listener
87
-
88
- @listener = Listen.to(Dir.pwd, :relative_paths => true)
89
-
80
+ return if @options[:"disable-watcher"]
81
+
82
+ first_run = !@listener
83
+
84
+ if first_run
85
+ # Watcher Library
86
+ require "listen"
87
+ @listener = Listen.to(Dir.pwd, :relative_paths => true)
88
+ end
89
+
90
90
  @listener.change do |modified, added, removed|
91
91
  added_and_modified = (modified + added)
92
92
 
93
- unless added_and_modified.empty?
94
- # See if the changed file is config.rb or lib/*.rb
95
- if needs_to_reload?(added_and_modified)
96
- reload
97
- return
98
- end
99
-
100
- # Otherwise forward to Middleman
93
+ # See if the changed file is config.rb or lib/*.rb
94
+ if needs_to_reload?(added_and_modified) || needs_to_reload?(removed)
95
+ reload
96
+ else
101
97
  added_and_modified.each do |path|
102
- @app.files.did_change(path)
98
+ app.files.did_change(path)
103
99
  end
104
- end
105
100
 
106
- unless removed.empty?
107
- # See if the changed file is config.rb or lib/*.rb
108
- if needs_to_reload?(removed)
109
- reload
110
- return
111
- end
112
-
113
- # Otherwise forward to Middleman
114
101
  removed.each do |path|
115
- @app.files.did_delete(path)
102
+ app.files.did_delete(path)
116
103
  end
117
104
  end
118
105
  end
119
106
 
120
107
  # Don't block this thread
121
- @listener.start(false)
108
+ @listener.start(false) if first_run
122
109
  end
123
110
 
124
111
  # Trap the interupt signal and shut down smoothly
@@ -131,13 +118,12 @@ module Middleman
131
118
 
132
119
  # Initialize webrick
133
120
  # @return [void]
134
- def setup_webrick(host, port, is_logging)
121
+ def setup_webrick(host, is_logging)
135
122
  @host = host
136
- @port = port
137
123
 
138
124
  http_opts = {
139
125
  :BindAddress => @host,
140
- :Port => @port,
126
+ :Port => port,
141
127
  :AccessLog => []
142
128
  }
143
129
 
@@ -147,15 +133,28 @@ module Middleman
147
133
  http_opts[:Logger] = ::WEBrick::Log.new(nil, 0)
148
134
  end
149
135
 
150
- ::WEBrick::HTTPServer.new(http_opts)
136
+ begin
137
+ ::WEBrick::HTTPServer.new(http_opts)
138
+ rescue Errno::EADDRINUSE => e
139
+ logger.error "== Port #{port} is unavailable. Either close the instance of Middleman already running on #{port} or start this Middleman on a new port with: --port=#{port.to_i+1}"
140
+ exit(1)
141
+ end
151
142
  end
152
143
 
153
144
  # Attach a new Middleman::Application instance
154
145
  # @param [Middleman::Application] app
155
146
  # @return [void]
156
- def mount_instance(app)
157
- @app = app
158
- @webrick.mount "/", ::Rack::Handler::WEBrick, @app.class.to_rack_app
147
+ def mount_instance
148
+ @app = new_app
149
+ @webrick ||= setup_webrick(
150
+ @options[:host] || "0.0.0.0",
151
+ @options[:debug] || false
152
+ )
153
+
154
+ @app = new_app
155
+ start_file_watcher
156
+
157
+ @webrick.mount "/", ::Rack::Handler::WEBrick, app.class.to_rack_app
159
158
  end
160
159
 
161
160
  # Detach the current Middleman::Application instance
@@ -169,8 +168,22 @@ module Middleman
169
168
  # @param [Array<String>] paths Array of paths to check
170
169
  # @return [Boolean] Whether the server needs to reload
171
170
  def needs_to_reload?(paths)
171
+ match_against = [
172
+ %r{^config\.rb},
173
+ %r{^lib/^[^\.](.*)\.rb$},
174
+ %r{^helpers/^[^\.](.*)_helper\.rb$}
175
+ ]
176
+
177
+ if @options[:reload_paths]
178
+ @options[:reload_paths].split(',').each do |part|
179
+ match_against << %r{^#{part}}
180
+ end
181
+ end
182
+
172
183
  paths.any? do |path|
173
- path.match(%{^config\.rb}) || path.match(%r{^lib/^[^\.](.*)\.rb$}) || path.match(%r{^helpers/^[^\.](.*)_helper\.rb$})
184
+ match_against.any? do |matcher|
185
+ path.match(matcher)
186
+ end
174
187
  end
175
188
  end
176
189
  end
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  # Current Version
3
3
  # @return [String]
4
- VERSION = '3.0.2' unless const_defined?(:VERSION)
4
+ VERSION = '3.0.4' unless const_defined?(:VERSION)
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-05 00:00:00.000000000 Z
13
+ date: 2012-09-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -585,7 +585,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
585
585
  version: '0'
586
586
  segments:
587
587
  - 0
588
- hash: -1472293550739068315
588
+ hash: -1143395816558407601
589
589
  required_rubygems_version: !ruby/object:Gem::Requirement
590
590
  none: false
591
591
  requirements:
@@ -594,7 +594,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
594
594
  version: '0'
595
595
  segments:
596
596
  - 0
597
- hash: -1472293550739068315
597
+ hash: -1143395816558407601
598
598
  requirements: []
599
599
  rubyforge_project:
600
600
  rubygems_version: 1.8.23