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



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
307
# File 'middleman-core/lib/middleman-core/application.rb', line 217

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



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

def config
  @config
end

#config_contextObject (readonly)

Returns the value of attribute config_context



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

def config_context
  @config_context
end

#extensionsObject (readonly)

Returns the value of attribute extensions



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

def extensions
  @extensions
end

#mappingsObject (readonly)

Returns the value of attribute mappings



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

def mappings
  @mappings
end

#middlewareObject (readonly)

Returns the value of attribute middleware



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

def middleware
  @middleware
end

#set_of_extensions_with_layoutObject (readonly)

Returns the value of attribute set_of_extensions_with_layout



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

def set_of_extensions_with_layout
  @set_of_extensions_with_layout
end

#sitemapObject (readonly)

Returns the value of attribute sitemap



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

def sitemap
  @sitemap
end

#template_context_classObject (readonly)

An anonymous subclass of ::Middleman::TemplateContext



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

def template_context_class
  @template_context_class
end

Class Method Details

.configConfigurationManager

Global configuration for the whole Middleman project.

Returns:

  • (ConfigurationManager)


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

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

.rootString

Root project directory (overwritten in middleman build/server)

Returns:



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

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



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

def root_path
  Pathname(root)
end

Instance Method Details

#apply_cli_optionsObject



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

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:



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

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:



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

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:



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

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

#css_dirString

Location of stylesheets within source.

Returns:



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

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)


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

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

#encodingString

Default string encoding for templates and output.

Returns:



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

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

#environmentString

Middleman environment. Defaults to :development

Returns:



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

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



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

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)


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

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:



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

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:



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

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

#http_prefixString

Default prefix for building paths. Used by HTML helpers.

Returns:



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

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

#httpsBoolean

Whether to serve the preview server over HTTPS.

Returns:

  • (Boolean)


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

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

#images_dirString

Location of images within source. Used by HTML helpers.

Returns:



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

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

#index_fileString

Which file should be used for directory indexes

Returns:



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

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

#js_dirString

Location of javascripts within source.

Returns:



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

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

#layoutString

Default layout name

Returns:



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

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

#layouts_dirString

Location of layouts within source. Used by renderers.

Returns:



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

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:



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

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)


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

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)


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

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

#prune_tilt_templates!Object

Clean up missing Tilt exts



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

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)


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:



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

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:



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

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:



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

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:



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

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)


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

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)


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

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