class Middleman::Application

Attributes

config[R]
config_context[R]
extensions[R]
generic_template_context[R]

An instance of the above anonymouse class.

mappings[R]
middleware[R]
sitemap[R]
template_context_class[R]

An anonymous subclass of ::Middleman::TemplateContext

Public Class Methods

config() click to toggle source

Global configuration for the whole Middleman project. @return [ConfigurationManager]

# File lib/middleman-core/application.rb, line 29
def config
  @config ||= ::Middleman::Configuration::ConfigurationManager.new
end
new(&block) click to toggle source

Initialize the Middleman project

# File 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_render,
                                  :after_render,
                                  :before_server,
                                  :reload
                                ])

    @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?

  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)

  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
root() click to toggle source

Root project directory (overwritten in middleman build/server) @return [String]

# 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_path() click to toggle source

Pathname-addressed root

# File lib/middleman-core/application.rb, line 42
def root_path
  Pathname(root)
end

Public Instance Methods

apply_cli_options() click to toggle source
# File lib/middleman-core/application.rb, line 312
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
build?() click to toggle source
# File lib/middleman-core/application.rb, line 375
def build?
  mode?(:build)
end
development?() click to toggle source
# File lib/middleman-core/application.rb, line 397
def development?
  environment?(:development)
end
environment() click to toggle source
# File lib/middleman-core/application.rb, line 390
def environment
  config[:environment]
end
environment?(key) click to toggle source
# File lib/middleman-core/application.rb, line 383
def environment?(key)
  config[:environment] == key
end
evaluate_configuration!() click to toggle source

Eval config

# File lib/middleman-core/application.rb, line 324
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
inspect()
Alias for: to_s
map(map, &block) click to toggle source
# File lib/middleman-core/application.rb, line 428
def map(map, &block)
  @mappings << MapDescriptor.new(map, block)
end
mode?(key) click to toggle source
# File lib/middleman-core/application.rb, line 361
def mode?(key)
  config[:mode] == key
end
production?() click to toggle source
# File lib/middleman-core/application.rb, line 404
def production?
  environment?(:production)
end
prune_tilt_templates!() click to toggle source

Clean up missing Tilt exts

# File lib/middleman-core/application.rb, line 347
def prune_tilt_templates!
  ::Tilt.default_mapping.lazy_map.each_key do |key|
    begin
      ::Tilt[".#{key}"]
    rescue LoadError, NameError
      ::Tilt.default_mapping.lazy_map.delete(key)
    end
  end
end
server?() click to toggle source
# File lib/middleman-core/application.rb, line 368
def server?
  mode?(:server)
end
set(key, value=nil, &block) click to toggle source

Set attributes (global variables)

@deprecated Prefer accessing settings through “config”.

@param [Symbol] key Name of the attribue @param value Attribute value @return [void]

# File lib/middleman-core/application.rb, line 444
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!() click to toggle source

Let everyone know we’re shutting down.

# File lib/middleman-core/application.rb, line 433
def shutdown!
  execute_callbacks(:before_shutdown)
end
source_dir() click to toggle source
# File lib/middleman-core/application.rb, line 410
def source_dir
  Pathname(File.join(root, config[:source]))
end
to_s() click to toggle source

Work around this bug: 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.

# File lib/middleman-core/application.rb, line 455
def to_s
  "#<Middleman::Application:0x#{object_id}>"
end
Also aliased as: inspect
use(middleware, *args, &block) click to toggle source

Use Rack middleware

@param [Class] middleware Middleware module @return [void] Contract Any, Args, Maybe => Any

# File lib/middleman-core/application.rb, line 419
def use(middleware, *args, &block)
  @middleware << MiddlewareDescriptor.new(middleware, args, block)
end