Class: Middleman::Application

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Contracts
Defined in:
middleman-core/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


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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'middleman-core/lib/middleman-core/application.rb', line 219

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_server,
                                  :reload
                                ])

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

    @template_context_class = Class.new(Middleman::TemplateContext)
    @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?

  apply_cli_options

  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]])

  apply_cli_options

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

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

  prune_tilt_templates!

  # After extensions have worked after_config
  execute_callbacks(:after_configuration)

  @set_of_extensions_with_layout = ::Hamster::Set.new config.extensions_with_layout

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.


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

def config
  @config
end

#config_contextObject (readonly)

Returns the value of attribute config_context.


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

def config_context
  @config_context
end

#extensionsObject (readonly)

Returns the value of attribute extensions.


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

def extensions
  @extensions
end

#mappingsObject (readonly)

Returns the value of attribute mappings.


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

def mappings
  @mappings
end

#middlewareObject (readonly)

Returns the value of attribute middleware.


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

def middleware
  @middleware
end

#set_of_extensions_with_layoutObject (readonly)

Returns the value of attribute set_of_extensions_with_layout.


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

def set_of_extensions_with_layout
  @set_of_extensions_with_layout
end

#sitemapObject (readonly)

Returns the value of attribute sitemap.


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

def sitemap
  @sitemap
end

#template_context_classObject (readonly)

An anonymous subclass of ::Middleman::TemplateContext


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

def template_context_class
  @template_context_class
end

Class Method Details

.configConfigurationManager

Global configuration for the whole Middleman project.

Returns:

  • (ConfigurationManager)

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

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

.rootString

Root project directory (overwritten in middleman build/server)

Returns:


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

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

.root_pathObject

Pathname-addressed root


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

def root_path
  Pathname(root)
end

Instance Method Details

#apply_cli_optionsObject


311
312
313
314
315
316
317
318
319
320
# File 'middleman-core/lib/middleman-core/application.rb', line 311

def apply_cli_options
  config[:cli_options].each do |k, v|
    setting = config.setting(k.to_sym)
    next unless setting

    v = setting.options[:import].call(v) if setting.options[:import]

    config[k.to_sym] = v
  end
end

#asset_extensionsArray.<String>

Which file extensions are "assets."

Returns:


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

define_setting :asset_extensions, %w[.css .png .jpg .jpeg .webp .svg .svgz .js .gif .ttf .otf .woff .woff2 .eot .ico .map], 'Which file extensions are treated as assets.'

#bind_addressNilClass, String

Which bind address the preview server should use

Returns:


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

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

#BoolBoolean

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

Returns:

  • (Boolean)

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

Contract Bool

#build?Boolean

Returns:

  • (Boolean)

373
374
375
# File 'middleman-core/lib/middleman-core/application.rb', line 373

def build?
  mode?(:build)
end

#build_dirString

Where to build output files

Returns:


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

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

#css_dirString

Location of stylesheets within source.

Returns:


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

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

#development?Boolean

Returns:

  • (Boolean)

395
396
397
# File 'middleman-core/lib/middleman-core/application.rb', line 395

def development?
  environment?(:development)
end

#disable_sitemapBoolean

If we should not run the sitemap.

Returns:

  • (Boolean)

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

define_setting :disable_sitemap, false, 'If we should not run the sitemap.'

#encodingString

Default string encoding for templates and output.

Returns:


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

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

#environmentString

Middleman environment. Defaults to :development

Returns:


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

define_setting :environment, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development), 'Middleman environment. Defaults to :development', import: proc { |s| s.to_sym }

#environment?(key) ⇒ Boolean

Returns:

  • (Boolean)

381
382
383
# File 'middleman-core/lib/middleman-core/application.rb', line 381

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

#evaluate_configuration!Object

Eval config


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'middleman-core/lib/middleman-core/application.rb', line 323

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 ::Middleman::Util.read_file(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 ::Middleman::Util.read_file(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 ::Middleman::Util.read_file(env_config), env_config, 1
end

#exit_before_readyBoolean

If we should exit before ready event.

Returns:

  • (Boolean)

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

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:


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

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:


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

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

#http_prefixString

Default prefix for building paths. Used by HTML helpers.

Returns:


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

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

#httpsBoolean

Whether to serve the preview server over HTTPS.

Returns:

  • (Boolean)

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

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

#images_dirString

Location of images within source. Used by HTML helpers.

Returns:


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

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

#index_fileString

Which file should be used for directory indexes

Returns:


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

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

#js_dirString

Location of javascripts within source.

Returns:


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

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

#layoutString

Default layout name

Returns:


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

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

#layouts_dirString

Location of layouts within source. Used by renderers.

Returns:


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

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

#map(map, &block) ⇒ Object


426
427
428
# File 'middleman-core/lib/middleman-core/application.rb', line 426

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

#modeString

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

Returns:


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

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

#mode?(key) ⇒ Boolean

Returns:

  • (Boolean)

359
360
361
# File 'middleman-core/lib/middleman-core/application.rb', line 359

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

#portFixnum

Which port preview should start on.

Returns:

  • (Fixnum)

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

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

#production?Boolean

Returns:

  • (Boolean)

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

def production?
  environment?(:production)
end

#protect_from_csrfBoolean

Should Padrino include CRSF tag

Returns:

  • (Boolean)

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

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

#prune_tilt_templates!Object

Clean up missing Tilt exts


346
347
348
349
350
351
352
353
# File 'middleman-core/lib/middleman-core/application.rb', line 346

def prune_tilt_templates!
  mapping = ::Tilt.default_mapping
  mapping.lazy_map.each_key do |key|
    mapping[key]
  rescue LoadError, NameError
  end
  mapping.lazy_map.clear
end

#server?Boolean

Returns:

  • (Boolean)

366
367
368
# File 'middleman-core/lib/middleman-core/application.rb', line 366

def server?
  mode?(:server)
end

#server_nameNilClass, String

Which server name should be used

Returns:


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

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 attribute

  • value (defaults to: nil)

    Attribute value


442
443
444
445
446
447
# File 'middleman-core/lib/middleman-core/application.rb', line 442

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.


431
432
433
# File 'middleman-core/lib/middleman-core/application.rb', line 431

def shutdown!
  execute_callbacks(:before_shutdown)
end

#sourceString

Name of the source directory

Returns:


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

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

#source_dirObject


408
409
410
# File 'middleman-core/lib/middleman-core/application.rb', line 408

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:


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

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:


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

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


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

Contract String, Proc => Any

#strip_index_fileBoolean

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

Returns:

  • (Boolean)

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

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:


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

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.


453
454
455
# File 'middleman-core/lib/middleman-core/application.rb', line 453

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

#trailing_slashBoolean

Whether to include a trailing slash when stripping the index file

Returns:

  • (Boolean)

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

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


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

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