middleman-core 4.0.0.beta.1 → 4.0.0.beta.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 02dd866c5282faf778b2e57669ad86197c26cc90
4
- data.tar.gz: 2402e62953aa0cb41f7cbe98078e7203b2bd98e4
3
+ metadata.gz: e102996c549a19735369428a6ca1fb6d2d9a735b
4
+ data.tar.gz: bd3b7074ef91dbef4d80491e1b67c41117ff0699
5
5
  SHA512:
6
- metadata.gz: 5d66bfdb93ea7353d9c0f3750f13f5df0b31712e6f7527a256f53116ba422ed7b93cd14e9544e57ac531345ca09d2fb73c1c1c285d6a7848624cb4297a339cc8
7
- data.tar.gz: 0653294a824cc10c51d0ecdb04fb0363d550e524ba04e58995d79dbd4d67fb7d751236e703a92c2f5a6860d970e2c879ce79bba92360c815242dc7e396cfbc55
6
+ metadata.gz: 52f6338f6da3da3c1f113826597376f95f0bf7948f6f01a512a924949eb9f8efc7784f4a082f6f32b3892e9f88d3ad6258e662ac297f14bf0b98547faaea2909
7
+ data.tar.gz: 44a55d6fcff77e9136d7701b15cfe4104eae02de1dc1f8d73698f02eddb5a5a089f2c201a7c2d36e680df845f2550edc9c6aa171258c61271856732fdb9fad32
@@ -1,29 +1,14 @@
1
- # i18n Built-in
2
- require 'i18n'
3
-
4
- # Don't fail on invalid locale, that's not what our current
5
- # users expect.
6
- ::I18n.enforce_available_locales = false
7
-
8
1
  # Use ActiveSupport JSON
9
2
  require 'active_support/json'
10
3
  require 'active_support/core_ext/integer/inflections'
11
4
 
12
- # Simple callback library
13
- require 'hooks'
14
-
15
- # Our custom logger
16
- require 'middleman-core/logger'
17
-
18
5
  require 'middleman-core/contracts'
19
-
6
+ require 'middleman-core/callback_manager'
7
+ require 'middleman-core/logger'
20
8
  require 'middleman-core/sitemap/store'
21
-
22
9
  require 'middleman-core/configuration'
23
-
24
10
  require 'middleman-core/extension_manager'
25
11
  require 'middleman-core/core_extensions'
26
-
27
12
  require 'middleman-core/config_context'
28
13
  require 'middleman-core/file_renderer'
29
14
  require 'middleman-core/template_renderer'
@@ -38,6 +23,9 @@ module Middleman
38
23
  include Contracts
39
24
 
40
25
  class << self
26
+ extend Forwardable
27
+ def_delegator :config, :define_setting
28
+
41
29
  # Global configuration for the whole Middleman project.
42
30
  # @return [ConfigurationManager]
43
31
  def config
@@ -56,108 +44,119 @@ module Middleman
56
44
  end
57
45
  end
58
46
 
59
- # Uses callbacks
60
- include Hooks
61
- include Hooks::InstanceHooks
47
+ Contract ::Middleman::ConfigContext
48
+ attr_reader :config_context
62
49
 
63
- define_hook :initialized
64
- define_hook :after_configuration
65
- define_hook :before_configuration
50
+ Contract ::Middleman::Sitemap::Store
51
+ attr_reader :sitemap
66
52
 
67
- # Before request hook
68
- define_hook :before
53
+ # An anonymous subclass of ::Middleman::TemplateContext
54
+ attr_reader :template_context_class
69
55
 
70
- # Ready (all loading and parsing of extensions complete) hook
71
- define_hook :ready
56
+ # An instance of the above anonymouse class.
57
+ attr_reader :generic_template_context
72
58
 
73
- # Runs before the build is started
74
- define_hook :before_build
59
+ Contract ::Middleman::Configuration::ConfigurationManager
60
+ attr_reader :config
75
61
 
76
- # Runs after the build is finished
77
- define_hook :after_build
62
+ Contract ::Middleman::ExtensionManager
63
+ attr_reader :extensions
78
64
 
79
- define_hook :before_shutdown
65
+ Contract SetOf[MiddlewareDescriptor]
66
+ attr_reader :middleware
80
67
 
81
- define_hook :before_render
82
- define_hook :after_render
68
+ Contract SetOf[MapDescriptor]
69
+ attr_reader :mappings
83
70
 
84
71
  # Which host preview should start on.
85
72
  # @return [Fixnum]
86
- config.define_setting :host, '0.0.0.0', 'The preview server host'
73
+ define_setting :host, '0.0.0.0', 'The preview server host'
87
74
 
88
75
  # Which port preview should start on.
89
76
  # @return [Fixnum]
90
- config.define_setting :port, 4567, 'The preview server port'
77
+ define_setting :port, 4567, 'The preview server port'
78
+
79
+ # Whether to serve the preview server over HTTPS.
80
+ # @return [Boolean]
81
+ config.define_setting :https, false, 'Serve the preview server over SSL/TLS'
82
+
83
+ # The (optional) path to the SSL cert to use for the preview server.
84
+ # @return [String]
85
+ config.define_setting :ssl_certificate, nil, 'Path to an X.509 certificate to use for the preview server'
86
+
87
+ # The (optional) private key for the certificate in :ssl_certificate.
88
+ # @return [String]
89
+ config.define_setting :ssl_private_key, nil, "Path to an RSA private key for the preview server's certificate"
91
90
 
92
91
  # Name of the source directory
93
92
  # @return [String]
94
- config.define_setting :source, 'source', 'Name of the source directory'
93
+ define_setting :source, 'source', 'Name of the source directory'
95
94
 
96
95
  # Middleman mode. Defaults to :server, set to :build by the build process
97
96
  # @return [String]
98
- config.define_setting :mode, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :server), 'Middleman mode. Defaults to :server'
97
+ define_setting :mode, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :server), 'Middleman mode. Defaults to :server'
99
98
 
100
99
  # Middleman environment. Defaults to :development
101
100
  # @return [String]
102
- config.define_setting :environment, :development, 'Middleman environment. Defaults to :development'
101
+ define_setting :environment, :development, 'Middleman environment. Defaults to :development'
103
102
 
104
103
  # Which file should be used for directory indexes
105
104
  # @return [String]
106
- config.define_setting :index_file, 'index.html', 'Which file should be used for directory indexes'
105
+ define_setting :index_file, 'index.html', 'Which file should be used for directory indexes'
107
106
 
108
107
  # Whether to strip the index file name off links to directory indexes
109
108
  # @return [Boolean]
110
- config.define_setting :strip_index_file, true, 'Whether to strip the index file name off links to directory indexes'
109
+ define_setting :strip_index_file, true, 'Whether to strip the index file name off links to directory indexes'
111
110
 
112
111
  # Whether to include a trailing slash when stripping the index file
113
112
  # @return [Boolean]
114
- config.define_setting :trailing_slash, true, 'Whether to include a trailing slash when stripping the index file'
113
+ define_setting :trailing_slash, true, 'Whether to include a trailing slash when stripping the index file'
115
114
 
116
115
  # Location of javascripts within source.
117
116
  # @return [String]
118
- config.define_setting :js_dir, 'javascripts', 'Location of javascripts within source'
117
+ define_setting :js_dir, 'javascripts', 'Location of javascripts within source'
119
118
 
120
119
  # Location of stylesheets within source. Used by Compass.
121
120
  # @return [String]
122
- config.define_setting :css_dir, 'stylesheets', 'Location of stylesheets within source'
121
+ define_setting :css_dir, 'stylesheets', 'Location of stylesheets within source'
123
122
 
124
123
  # Location of images within source. Used by HTML helpers and Compass.
125
124
  # @return [String]
126
- config.define_setting :images_dir, 'images', 'Location of images within source'
125
+ define_setting :images_dir, 'images', 'Location of images within source'
127
126
 
128
127
  # Location of fonts within source. Used by Compass.
129
128
  # @return [String]
130
- config.define_setting :fonts_dir, 'fonts', 'Location of fonts within source'
129
+ define_setting :fonts_dir, 'fonts', 'Location of fonts within source'
131
130
 
132
131
  # Location of layouts within source. Used by renderers.
133
132
  # @return [String]
134
- config.define_setting :layouts_dir, 'layouts', 'Location of layouts within source'
133
+ define_setting :layouts_dir, 'layouts', 'Location of layouts within source'
135
134
 
136
135
  # Where to build output files
137
136
  # @return [String]
138
- config.define_setting :build_dir, 'build', 'Where to build output files'
137
+ define_setting :build_dir, 'build', 'Where to build output files'
139
138
 
140
139
  # Default prefix for building paths. Used by HTML helpers and Compass.
141
140
  # @return [String]
142
- config.define_setting :http_prefix, '/', 'Default prefix for building paths'
141
+ define_setting :http_prefix, '/', 'Default prefix for building paths'
143
142
 
144
143
  # Default layout name
145
144
  # @return [String, Symbold]
146
- config.define_setting :layout, :_auto_layout, 'Default layout name'
145
+ define_setting :layout, :_auto_layout, 'Default layout name'
147
146
 
148
147
  # Default string encoding for templates and output.
149
148
  # @return [String]
150
- config.define_setting :encoding, 'utf-8', 'Default string encoding for templates and output'
149
+ define_setting :encoding, 'utf-8', 'Default string encoding for templates and output'
151
150
 
152
151
  # Should Padrino include CRSF tag
153
152
  # @return [Boolean]
154
- config.define_setting :protect_from_csrf, false, 'Should Padrino include CRSF tag'
153
+ define_setting :protect_from_csrf, false, 'Should Padrino include CRSF tag'
155
154
 
156
155
  # Set to automatically convert some characters into a directory
157
- config.define_setting :automatic_directory_matcher, nil, 'Set to automatically convert some characters into a directory'
156
+ define_setting :automatic_directory_matcher, nil, 'Set to automatically convert some characters into a directory'
158
157
 
159
158
  # Setup callbacks which can exclude paths from the sitemap
160
- config.define_setting :ignored_sitemap_matchers, {
159
+ define_setting :ignored_sitemap_matchers, {
161
160
  # Files starting with an underscore, but not a double-underscore
162
161
  partials: proc { |file|
163
162
  ignored = false
@@ -178,37 +177,40 @@ module Middleman
178
177
  }
179
178
  }, 'Callbacks that can exclude paths from the sitemap'
180
179
 
181
- config.define_setting :watcher_disable, false, 'If the Listen watcher should not run'
182
- config.define_setting :watcher_force_polling, false, 'If the Listen watcher should run in polling mode'
183
- config.define_setting :watcher_latency, nil, 'The Listen watcher latency'
184
-
185
- attr_reader :config_context
186
- attr_reader :sitemap
187
- attr_reader :cache
188
- attr_reader :template_context_class
189
- attr_reader :config
190
- attr_reader :generic_template_context
191
- attr_reader :extensions
192
- attr_reader :sources
193
-
194
- Contract SetOf[MiddlewareDescriptor]
195
- attr_reader :middleware
196
-
197
- Contract SetOf[MapDescriptor]
198
- attr_reader :mappings
180
+ define_setting :watcher_disable, false, 'If the Listen watcher should not run'
181
+ define_setting :watcher_force_polling, false, 'If the Listen watcher should run in polling mode'
182
+ define_setting :watcher_latency, nil, 'The Listen watcher latency'
199
183
 
200
- # Reference to Logger singleton
184
+ # Delegate convenience methods off to their implementations
201
185
  def_delegator :"::Middleman::Logger", :singleton, :logger
202
186
  def_delegator :"::Middleman::Util", :instrument
203
187
  def_delegators :"self.class", :root, :root_path
204
188
  def_delegators :@generic_template_context, :link_to, :image_tag, :asset_path
205
189
  def_delegators :@extensions, :activate
190
+ def_delegators :config, :define_setting
206
191
 
207
192
  # Initialize the Middleman project
208
193
  def initialize(&block)
209
194
  # Search the root of the project for required files
210
195
  $LOAD_PATH.unshift(root) unless $LOAD_PATH.include?(root)
211
196
 
197
+ @callbacks = ::Middleman::CallbackManager.new
198
+ @callbacks.install_methods!(self, [
199
+ :initialized,
200
+ :configure,
201
+ :before_sitemap,
202
+ :before_configuration,
203
+ :after_configuration,
204
+ :after_configuration_eval,
205
+ :ready,
206
+ :before_build,
207
+ :after_build,
208
+ :before_shutdown,
209
+ :before, # Before Rack requests
210
+ :before_render,
211
+ :after_render
212
+ ])
213
+
212
214
  @middleware = Set.new
213
215
  @mappings = Set.new
214
216
 
@@ -216,21 +218,22 @@ module Middleman
216
218
  @generic_template_context = @template_context_class.new(self)
217
219
  @config_context = ConfigContext.new(self, @template_context_class)
218
220
 
219
- ::Middleman::FileRenderer.cache.clear
220
- ::Middleman::TemplateRenderer.cache.clear
221
-
222
221
  # Setup the default values from calls to set before initialization
223
222
  @config = ::Middleman::Configuration::ConfigurationManager.new
224
223
  @config.load_settings(self.class.config.all_settings)
225
224
 
226
225
  config[:source] = ENV['MM_SOURCE'] if ENV['MM_SOURCE']
227
226
 
227
+ # TODO, make this less global
228
+ ::Middleman::FileRenderer.cache.clear
229
+ ::Middleman::TemplateRenderer.cache.clear
230
+
228
231
  @extensions = ::Middleman::ExtensionManager.new(self)
229
232
 
230
233
  # Evaluate a passed block if given
231
234
  config_context.instance_exec(&block) if block_given?
232
235
 
233
- @extensions.auto_activate(:before_sitemap)
236
+ execute_callbacks(:before_sitemap)
234
237
 
235
238
  # Initialize the Sitemap
236
239
  @sitemap = ::Middleman::Sitemap::Store.new(self)
@@ -242,42 +245,40 @@ module Middleman
242
245
 
243
246
  ::Middleman::Extension.clear_after_extension_callbacks
244
247
 
245
- @extensions.auto_activate(:before_configuration)
248
+ after_configuration_eval(&method(:prune_tilt_templates))
249
+
250
+ start_lifecycle
251
+ end
246
252
 
247
- run_hook :initialized
253
+ # Boot the app.
254
+ def start_lifecycle
255
+ # Before config is parsed, before extensions get to it.
256
+ execute_callbacks(:initialized)
248
257
 
249
- run_hook :before_configuration
258
+ # Before config is parsed. Mostly used for extensions.
259
+ execute_callbacks(:before_configuration)
250
260
 
251
- evaluate_configuration
261
+ # Eval config.
262
+ evaluate_configuration!
252
263
 
253
- # This is for making the tests work - since the tests
254
- # don't completely reload middleman, I18n.load_path can get
255
- # polluted with paths from other test app directories that don't
256
- # exist anymore.
257
- if ENV['TEST']
258
- ::I18n.load_path.delete_if { |path| path =~ %r{tmp/aruba} }
259
- ::I18n.reload!
260
- end
264
+ # Run any `configure` blocks for the current environment.
265
+ execute_callbacks([:configure, config[:environment]])
261
266
 
262
- # Clean up missing Tilt exts
263
- Tilt.mappings.each do |key, _|
264
- begin
265
- Tilt[".#{key}"]
266
- rescue LoadError, NameError
267
- Tilt.mappings.delete(key)
268
- end
269
- end
267
+ # Run any `configure` blocks for the current mode.
268
+ execute_callbacks([:configure, config[:mode]])
270
269
 
271
- @extensions.activate_all
270
+ # Post parsing, pre-extension callback
271
+ execute_callbacks(:after_configuration_eval)
272
272
 
273
- run_hook :after_configuration
274
- config_context.execute_after_configuration_callbacks
273
+ # After extensions have worked after_config
274
+ execute_callbacks(:after_configuration)
275
275
 
276
- run_hook :ready
277
- @config_context.execute_ready_callbacks
276
+ # Everything is stable
277
+ execute_callbacks(:ready)
278
278
  end
279
279
 
280
- def evaluate_configuration
280
+ # Eval config
281
+ def evaluate_configuration!
281
282
  # Check for and evaluate local configuration in `config.rb`
282
283
  config_rb = File.join(root, 'config.rb')
283
284
  if File.exist? config_rb
@@ -293,55 +294,67 @@ module Middleman
293
294
  end
294
295
 
295
296
  env_config = File.join(root, 'environments', "#{config[:environment]}.rb")
296
- if File.exist? env_config
297
- logger.debug "== Reading: #{config[:environment]} config"
298
- config_context.instance_eval File.read(env_config), env_config, 1
299
- end
297
+ return unless File.exist? env_config
300
298
 
301
- # Run any `configure` blocks for the current environment.
302
- config_context.execute_configure_callbacks(config[:environment])
299
+ logger.debug "== Reading: #{config[:environment]} config"
300
+ config_context.instance_eval File.read(env_config), env_config, 1
301
+ end
303
302
 
304
- # Run any `configure` blocks for the current mode.
305
- config_context.execute_configure_callbacks(config[:mode])
303
+ # Clean up missing Tilt exts
304
+ def prune_tilt_templates
305
+ ::Tilt.mappings.each do |key, _|
306
+ begin
307
+ ::Tilt[".#{key}"]
308
+ rescue LoadError, NameError
309
+ ::Tilt.mappings.delete(key)
310
+ end
311
+ end
306
312
  end
307
313
 
308
314
  # Whether we're in server mode
309
315
  # @return [Boolean] If we're in dev mode
316
+ Contract Bool
310
317
  def server?
311
318
  config[:mode] == :server
312
319
  end
313
320
 
314
321
  # Whether we're in build mode
315
322
  # @return [Boolean] If we're in dev mode
323
+ Contract Bool
316
324
  def build?
317
325
  config[:mode] == :build
318
326
  end
319
327
 
320
328
  # Whether we're in a specific environment
321
329
  # @return [Boolean]
330
+ Contract Bool
322
331
  def environment?(key)
323
332
  config[:environment] == key
324
333
  end
325
334
 
326
335
  # Backwards compatible helper. What the current environment is.
327
336
  # @return [Symbol]
337
+ Contract Symbol
328
338
  def environment
329
339
  config[:environment]
330
340
  end
331
341
 
332
342
  # Backwards compatible helper. Whether we're in dev mode.
333
343
  # @return [Boolean]
344
+ Contract Bool
334
345
  def development?
335
346
  environment?(:development)
336
347
  end
337
348
 
338
349
  # Backwards compatible helper. Whether we're in production mode.
339
350
  # @return [Boolean]
351
+ Contract Bool
340
352
  def production?
341
353
  environment?(:production)
342
354
  end
343
355
 
344
356
  # Backwards compatible helper. The full path to the default source dir.
357
+ Contract Pathname
345
358
  def source_dir
346
359
  Pathname(File.join(root, config[:source]))
347
360
  end
@@ -364,8 +377,9 @@ module Middleman
364
377
  @mappings << MapDescriptor.new(map, block)
365
378
  end
366
379
 
380
+ # Let everyone know we're shutting down.
367
381
  def shutdown!
368
- run_hook :before_shutdown
382
+ execute_callbacks(:before_shutdown)
369
383
  end
370
384
 
371
385
  # Work around this bug: http://bugs.ruby-lang.org/issues/4521
@@ -2,6 +2,7 @@ require 'pathname'
2
2
  require 'fileutils'
3
3
  require 'tempfile'
4
4
  require 'middleman-core/rack'
5
+ require 'middleman-core/callback_manager'
5
6
  require 'middleman-core/contracts'
6
7
 
7
8
  module Middleman
@@ -36,10 +37,11 @@ module Middleman
36
37
  @glob = opts.fetch(:glob)
37
38
  @cleaning = opts.fetch(:clean)
38
39
 
39
- @_event_callbacks = []
40
-
41
40
  rack_app = ::Middleman::Rack.new(@app).to_app
42
41
  @rack = ::Rack::MockRequest.new(rack_app)
42
+
43
+ @callbacks = ::Middleman::CallbackManager.new
44
+ @callbacks.install_methods!(self, [:on_build_event])
43
45
  end
44
46
 
45
47
  # Run the build phase.
@@ -49,7 +51,7 @@ module Middleman
49
51
  @has_error = false
50
52
  @events = {}
51
53
 
52
- @app.run_hook :before_build, self
54
+ @app.execute_callbacks(:before_build, [self])
53
55
 
54
56
  queue_current_paths if @cleaning
55
57
  prerender_css
@@ -60,21 +62,11 @@ module Middleman
60
62
 
61
63
  ::Middleman::Profiling.report('build')
62
64
 
63
- # Run hooks
64
- @app.run_hook :after_build, self
65
- @app.config_context.execute_after_build_callbacks(self)
65
+ @app.execute_callbacks(:after_build, [self])
66
66
 
67
67
  !@has_error
68
68
  end
69
69
 
70
- # Attach callbacks for build events.
71
- # @return [Array<Proc>] All the attached events.
72
- Contract Proc => ArrayOf[Proc]
73
- def on_build_event(&block)
74
- @_event_callbacks << block if block_given?
75
- @_event_callbacks
76
- end
77
-
78
70
  # Pre-request CSS to give Compass a chance to build sprites
79
71
  # @return [Array<Resource>] List of css resources that were output.
80
72
  Contract ResourceList
@@ -253,9 +245,7 @@ module Middleman
253
245
  @events[event_type] ||= []
254
246
  @events[event_type] << target
255
247
 
256
- @_event_callbacks.each do |callback|
257
- callback.call(event_type, target, extra)
258
- end
248
+ execute_callbacks(:on_build_event, [event_type, target, extra])
259
249
  end
260
250
  end
261
251
  end