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::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

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



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

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



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

def config
  @config
end

#config_contextObject (readonly)

Returns the value of attribute config_context



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

def config_context
  @config_context
end

#extensionsObject (readonly)

Returns the value of attribute extensions



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

def extensions
  @extensions
end

#mappingsObject (readonly)

Returns the value of attribute mappings



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

def mappings
  @mappings
end

#middlewareObject (readonly)

Returns the value of attribute middleware



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

def middleware
  @middleware
end

#set_of_extensions_with_layoutObject (readonly)

Returns the value of attribute set_of_extensions_with_layout



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

def set_of_extensions_with_layout
  @set_of_extensions_with_layout
end

#sitemapObject (readonly)

Returns the value of attribute sitemap



51
52
53
# File 'middleman-core/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 'middleman-core/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 'middleman-core/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 'middleman-core/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 'middleman-core/lib/middleman-core/application.rb', line 42

def root_path
  Pathname(root)
end

Instance Method Details

#apply_cli_optionsObject



308
309
310
311
312
313
314
315
316
317
# File 'middleman-core/lib/middleman-core/application.rb', line 308

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:



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

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:



81
# File 'middleman-core/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)


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

Contract Bool

#build?Boolean

Returns:

  • (Boolean)


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

def build?
  mode?(:build)
end

#build_dirString

Where to build output files

Returns:



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

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

#css_dirString

Location of stylesheets within source.

Returns:



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

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

#development?Boolean

Returns:

  • (Boolean)


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

def development?
  environment?(:development)
end

#disable_sitemapBoolean

If we should not run the sitemap.

Returns:

  • (Boolean)


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

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

#encodingString

Default string encoding for templates and output.

Returns:



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

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

#environmentString

Middleman environment. Defaults to :development

Returns:



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

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)


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

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

#evaluate_configuration!Object

Eval config



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

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)


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

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:



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

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:



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

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

#http_prefixString

Default prefix for building paths. Used by HTML helpers.

Returns:



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

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

#httpsBoolean

Whether to serve the preview server over HTTPS.

Returns:

  • (Boolean)


85
# File 'middleman-core/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:



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

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

#index_fileString

Which file should be used for directory indexes

Returns:



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

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

#js_dirString

Location of javascripts within source.

Returns:



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

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

#layoutString

Default layout name

Returns:



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

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

#layouts_dirString

Location of layouts within source. Used by renderers.

Returns:



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

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

#map(map, &block) ⇒ Object



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

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

#modeString

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

Returns:



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

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

#mode?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#portFixnum

Which port preview should start on.

Returns:

  • (Fixnum)


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

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

#production?Boolean

Returns:

  • (Boolean)


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

def production?
  environment?(:production)
end

#protect_from_csrfBoolean

Should Padrino include CRSF tag

Returns:

  • (Boolean)


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

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

#prune_tilt_templates!Object

Clean up missing Tilt exts



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

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

#server?Boolean

Returns:

  • (Boolean)


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

def server?
  mode?(:server)
end

#server_nameNilClass, String

Which server name should be used

Returns:



77
# File 'middleman-core/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 attribute

  • value (defaults to: nil)

    Attribute value



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

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.



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

def shutdown!
  execute_callbacks(:before_shutdown)
end

#sourceString

Name of the source directory

Returns:



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

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

#source_dirObject



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

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 'middleman-core/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 'middleman-core/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



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

Contract String, Proc => Any

#strip_index_fileBoolean

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

Returns:

  • (Boolean)


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

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:



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

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.



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

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

#trailing_slashBoolean

Whether to include a trailing slash when stripping the index file

Returns:

  • (Boolean)


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

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



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

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