Class: Middleman::Application

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Contracts
Defined in:
lib/middleman-core/application.rb

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(&block) ⇒ Application

Initialize the Middleman project



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/middleman-core/application.rb', line 206

def initialize(&block)
  # Search the root of the project for required files
  $LOAD_PATH.unshift(root) unless $LOAD_PATH.include?(root)

  ::Middleman::Util.instrument 'application.setup' do
    @callbacks = ::Middleman::CallbackManager.new
    @callbacks.install_methods!(self, [
                                  :initialized,
                                  :configure,
                                  :before_extensions,
                                  :before_instance_block,
                                  :before_sitemap,
                                  :before_configuration,
                                  :after_configuration,
                                  :after_configuration_eval,
                                  :ready,
                                  :before_build,
                                  :after_build,
                                  :before_shutdown,
                                  :before, # Before Rack requests
                                  :before_render,
                                  :after_render,
                                  :before_server
                                ])

    @middleware = Set.new
    @mappings = Set.new

    @template_context_class = Class.new(Middleman::TemplateContext)
    @generic_template_context = @template_context_class.new(self)
    @config_context = ConfigContext.new(self, @template_context_class)

    # Setup the default values from calls to set before initialization
    @config = ::Middleman::Configuration::ConfigurationManager.new
    @config.load_settings(self.class.config.all_settings)

    config[:source] = ENV['MM_SOURCE'] if ENV['MM_SOURCE']

    # TODO, make this less global
    ::Middleman::FileRenderer.cache.clear
    ::Middleman::TemplateRenderer.cache.clear
  end

  execute_callbacks(:before_extensions)

  @extensions = ::Middleman::ExtensionManager.new(self)

  execute_callbacks(:before_instance_block)

  # Evaluate a passed block if given
  config_context.instance_exec(&block) if block_given?

  execute_callbacks(:before_sitemap)

  # Initialize the Sitemap
  @sitemap = ::Middleman::Sitemap::Store.new(self)

  ::Middleman::Extension.clear_after_extension_callbacks

  # Before config is parsed, before extensions get to it.
  execute_callbacks(:initialized)

  # Before config is parsed. Mostly used for extensions.
  execute_callbacks(:before_configuration)

  # Eval config.
  evaluate_configuration!

  # Run any `configure` blocks for the current environment.
  execute_callbacks([:configure, config[:environment]])

  # Run any `configure` blocks for the current mode.
  execute_callbacks([:configure, config[:mode]])

  # Post parsing, pre-extension callback
  execute_callbacks(:after_configuration_eval)

  if Object.const_defined?(:Encoding)
    Encoding.default_external = config[:encoding]
  end

  prune_tilt_templates!

  # After extensions have worked after_config
  execute_callbacks(:after_configuration)

  # Everything is stable
  execute_callbacks(:ready) unless config[:exit_before_ready]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



60
61
62
# File 'lib/middleman-core/application.rb', line 60

def config
  @config
end

#config_contextObject (readonly)

Returns the value of attribute config_context.



48
49
50
# File 'lib/middleman-core/application.rb', line 48

def config_context
  @config_context
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



63
64
65
# File 'lib/middleman-core/application.rb', line 63

def extensions
  @extensions
end

#generic_template_contextObject (readonly)

An instance of the above anonymouse class.



57
58
59
# File 'lib/middleman-core/application.rb', line 57

def generic_template_context
  @generic_template_context
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



69
70
71
# File 'lib/middleman-core/application.rb', line 69

def mappings
  @mappings
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



66
67
68
# File 'lib/middleman-core/application.rb', line 66

def middleware
  @middleware
end

#sitemapObject (readonly)

Returns the value of attribute sitemap.



51
52
53
# File 'lib/middleman-core/application.rb', line 51

def sitemap
  @sitemap
end

#template_context_classObject (readonly)

An anonymous subclass of ::Middleman::TemplateContext



54
55
56
# File 'lib/middleman-core/application.rb', line 54

def template_context_class
  @template_context_class
end

Class Method Details

.configConfigurationManager

Global configuration for the whole Middleman project.

Returns:

  • (ConfigurationManager)


29
30
31
# File 'lib/middleman-core/application.rb', line 29

def config
  @config ||= ::Middleman::Configuration::ConfigurationManager.new
end

.rootString

Root project directory (overwritten in middleman build/server)

Returns:



35
36
37
38
39
# File 'lib/middleman-core/application.rb', line 35

def root
  r = ENV['MM_ROOT'] ? ENV['MM_ROOT'].dup : ::Middleman::Util.current_directory
  r.encode!('UTF-8', 'UTF-8-MAC') if RUBY_PLATFORM =~ /darwin/
  r
end

.root_pathObject

Pathname-addressed root



42
43
44
# File 'lib/middleman-core/application.rb', line 42

def root_path
  Pathname(root)
end

Instance Method Details

#bind_addressNilClass, String

Which bind address the preview server should use

Returns:



81
# File 'lib/middleman-core/application.rb', line 81

define_setting :bind_address, nil, 'The bind address of the preview server'

#BoolBoolean

Backwards compatible helper. Whether we're in production mode.

Returns:

  • (Boolean)


340
# File 'lib/middleman-core/application.rb', line 340

Contract Bool

#build?Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/middleman-core/application.rb', line 348

def build?
  mode?(:build)
end

#build_dirString

Where to build output files

Returns:



145
# File 'lib/middleman-core/application.rb', line 145

define_setting :build_dir,   'build', 'Where to build output files'

#css_dirString

Location of stylesheets within source.

Returns:



129
# File 'lib/middleman-core/application.rb', line 129

define_setting :css_dir,     'stylesheets', 'Location of stylesheets within source'

#development?Boolean

Returns:

  • (Boolean)


370
371
372
# File 'lib/middleman-core/application.rb', line 370

def development?
  environment?(:development)
end

#encodingString

Default string encoding for templates and output.

Returns:



161
# File 'lib/middleman-core/application.rb', line 161

define_setting :encoding, 'utf-8', 'Default string encoding for templates and output'

#environmentString

Middleman environment. Defaults to :development

Returns:



109
# File 'lib/middleman-core/application.rb', line 109

define_setting :environment, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development), 'Middleman environment. Defaults to :development'

#environment?(key) ⇒ Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/middleman-core/application.rb', line 356

def environment?(key)
  config[:environment] == key
end

#evaluate_configuration!Object

Eval config



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/middleman-core/application.rb', line 297

def evaluate_configuration!
  # Check for and evaluate local configuration in `config.rb`
  config_rb = File.join(root, 'config.rb')
  if File.exist? config_rb
    logger.debug '== Reading: Local config: config.rb'
    config_context.instance_eval File.read(config_rb), config_rb, 1
  else
    # Check for and evaluate local configuration in `middleman.rb`
    middleman_rb = File.join(root, 'middleman.rb')
    if File.exist? middleman_rb
      logger.debug '== Reading: Local middleman: middleman.rb'
      config_context.instance_eval File.read(middleman_rb), middleman_rb, 1
    end
  end

  env_config = File.join(root, 'environments', "#{config[:environment]}.rb")
  return unless File.exist? env_config

  logger.debug "== Reading: #{config[:environment]} config"
  config_context.instance_eval File.read(env_config), env_config, 1
end

#exit_before_readyBoolean

If we should exit before ready event.

Returns:

  • (Boolean)


101
# File 'lib/middleman-core/application.rb', line 101

define_setting :exit_before_ready, false, 'If we should exit before ready event.'

#extensions_with_layoutArray.<String>

Which file extensions have a layout by default.

Returns:



157
# File 'lib/middleman-core/application.rb', line 157

define_setting :extensions_with_layout, %w(.htm .html .xhtml .php), 'Which file extensions have a layout by default.'

#fonts_dirString

Location of fonts within source.

Returns:



137
# File 'lib/middleman-core/application.rb', line 137

define_setting :fonts_dir,   'fonts', 'Location of fonts within source'

#http_prefixString

Default prefix for building paths. Used by HTML helpers.

Returns:



149
# File 'lib/middleman-core/application.rb', line 149

define_setting :http_prefix, '/', 'Default prefix for building paths'

#httpsBoolean

Whether to serve the preview server over HTTPS.

Returns:

  • (Boolean)


85
# File 'lib/middleman-core/application.rb', line 85

define_setting :https, false, 'Serve the preview server over SSL/TLS'

#images_dirString

Location of images within source. Used by HTML helpers.

Returns:



133
# File 'lib/middleman-core/application.rb', line 133

define_setting :images_dir,  'images', 'Location of images within source'

#index_fileString

Which file should be used for directory indexes

Returns:



113
# File 'lib/middleman-core/application.rb', line 113

define_setting :index_file,  'index.html', 'Which file should be used for directory indexes'

#js_dirString

Location of javascripts within source.

Returns:



125
# File 'lib/middleman-core/application.rb', line 125

define_setting :js_dir,      'javascripts', 'Location of javascripts within source'

#layoutString

Default layout name

Returns:



153
# File 'lib/middleman-core/application.rb', line 153

define_setting :layout, :_auto_layout, 'Default layout name'

#layouts_dirString

Location of layouts within source. Used by renderers.

Returns:



141
# File 'lib/middleman-core/application.rb', line 141

define_setting :layouts_dir, 'layouts', 'Location of layouts within source'

#map(map, &block) ⇒ Object



401
402
403
# File 'lib/middleman-core/application.rb', line 401

def map(map, &block)
  @mappings << MapDescriptor.new(map, block)
end

#modeString

Middleman mode. Defaults to :server, set to :build by the build process

Returns:



105
# File 'lib/middleman-core/application.rb', line 105

define_setting :mode, :server, 'Middleman mode. Defaults to :server'

#mode?(key) ⇒ Boolean

Returns:

  • (Boolean)


334
335
336
# File 'lib/middleman-core/application.rb', line 334

def mode?(key)
  config[:mode] == key
end

#portFixnum

Which port preview should start on.

Returns:

  • (Fixnum)


73
# File 'lib/middleman-core/application.rb', line 73

define_setting :port, 4567, 'The preview server port'

#production?Boolean

Returns:

  • (Boolean)


377
378
379
# File 'lib/middleman-core/application.rb', line 377

def production?
  environment?(:production)
end

#protect_from_csrfBoolean

Should Padrino include CRSF tag

Returns:

  • (Boolean)


165
# File 'lib/middleman-core/application.rb', line 165

define_setting :protect_from_csrf, false, 'Should Padrino include CRSF tag'

#prune_tilt_templates!Object

Clean up missing Tilt exts



320
321
322
323
324
325
326
327
328
# File 'lib/middleman-core/application.rb', line 320

def prune_tilt_templates!
  ::Tilt.mappings.each_key do |key|
    begin
      ::Tilt[".#{key}"]
    rescue LoadError, NameError
      ::Tilt.mappings.delete(key)
    end
  end
end

#server?Boolean

Returns:

  • (Boolean)


341
342
343
# File 'lib/middleman-core/application.rb', line 341

def server?
  mode?(:server)
end

#server_nameNilClass, String

Which server name should be used

Returns:



77
# File 'lib/middleman-core/application.rb', line 77

define_setting :server_name, nil, 'The server name of preview server'

#set(key, value = nil, &block)

Deprecated.

Prefer accessing settings through "config".

This method returns an undefined value.

Set attributes (global variables)

Parameters:

  • key (Symbol)

    Name of the attribue

  • value (defaults to: nil)

    Attribute value



417
418
419
420
421
422
# File 'lib/middleman-core/application.rb', line 417

def set(key, value=nil, &block)
  logger.warn "Warning: `set :#{key}` is deprecated. Use `config[:#{key}] =` instead."

  value = block if block_given?
  config[key] = value
end

#shutdown!Object

Let everyone know we're shutting down.



406
407
408
# File 'lib/middleman-core/application.rb', line 406

def shutdown!
  execute_callbacks(:before_shutdown)
end

#sourceString

Name of the source directory

Returns:



97
# File 'lib/middleman-core/application.rb', line 97

define_setting :source, 'source', 'Name of the source directory'

#source_dirObject



383
384
385
# File 'lib/middleman-core/application.rb', line 383

def source_dir
  Pathname(File.join(root, config[:source]))
end

#ssl_certificateString

The (optional) path to the SSL cert to use for the preview server.

Returns:



89
# File 'lib/middleman-core/application.rb', line 89

define_setting :ssl_certificate, nil, 'Path to an X.509 certificate to use for the preview server'

#ssl_private_keyString

The (optional) private key for the certificate in :ssl_certificate.

Returns:



93
# File 'lib/middleman-core/application.rb', line 93

define_setting :ssl_private_key, nil, "Path to an RSA private key for the preview server's certificate"

#String

This method returns an undefined value.

Add Rack App mapped to specific path

Parameters:

  • map (String)

    Path to map



400
# File 'lib/middleman-core/application.rb', line 400

Contract String, Proc => Any

#strip_index_fileBoolean

Whether to strip the index file name off links to directory indexes

Returns:

  • (Boolean)


117
# File 'lib/middleman-core/application.rb', line 117

define_setting :strip_index_file, true, 'Whether to strip the index file name off links to directory indexes'

#SymbolSymbol

Backwards compatible helper. What the current environment is.

Returns:



362
# File 'lib/middleman-core/application.rb', line 362

Contract Symbol

#to_sObject Also known as: inspect

Work around this bug: http://bugs.ruby-lang.org/issues/4521 where Ruby will call to_s/inspect while printing exception messages, which can take a long time (minutes at full CPU) if the object is huge or has cyclic references, like this.



428
429
430
# File 'lib/middleman-core/application.rb', line 428

def to_s
  "#<Middleman::Application:0x#{object_id}>"
end

#trailing_slashBoolean

Whether to include a trailing slash when stripping the index file

Returns:

  • (Boolean)


121
# File 'lib/middleman-core/application.rb', line 121

define_setting :trailing_slash, true, 'Whether to include a trailing slash when stripping the index file'

#use(middleware, *args, &block)

This method returns an undefined value.

Use Rack middleware

Contract Any, Args[Any], Maybe[Proc] => Any

Parameters:

  • middleware (Class)

    Middleware module



392
393
394
# File 'lib/middleman-core/application.rb', line 392

def use(middleware, *args, &block)
  @middleware << MiddlewareDescriptor.new(middleware, args, block)
end