class Middleman::Application
Attributes
An instance of the above anonymouse class.
An anonymous subclass of ::Middleman::TemplateContext
Public Class Methods
Source
# File lib/middleman-core/application.rb, line 29 def config @config ||= ::Middleman::Configuration::ConfigurationManager.new end
Global configuration for the whole Middleman
project. @return [ConfigurationManager]
Source
# 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 # After extensions have worked after_config execute_callbacks(:after_configuration) # Everything is stable execute_callbacks(:ready) unless config[:exit_before_ready] end
Initialize the Middleman
project
Source
# 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 project directory (overwritten in middleman build/server) @return [String]
Source
# File lib/middleman-core/application.rb, line 42 def root_path Pathname(root) end
Pathname-addressed root
Public Instance Methods
Source
# File lib/middleman-core/application.rb, line 310 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
Source
# File lib/middleman-core/application.rb, line 384 def development? environment?(:development) end
Source
# File lib/middleman-core/application.rb, line 377 def environment config[:environment] end
Source
# File lib/middleman-core/application.rb, line 370 def environment?(key) config[:environment] == key end
Source
# File lib/middleman-core/application.rb, line 322 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
Eval config
Source
# File lib/middleman-core/application.rb, line 415 def map(map, &block) @mappings << MapDescriptor.new(map, block) end
Source
# File lib/middleman-core/application.rb, line 348 def mode?(key) config[:mode] == key end
Source
# File lib/middleman-core/application.rb, line 391 def production? environment?(:production) end
Source
# File lib/middleman-core/application.rb, line 431 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
Set attributes (global variables)
@deprecated Prefer accessing settings through “config”.
@param [Symbol] key Name of the attribue @param value Attribute value @return [void]
Source
# File lib/middleman-core/application.rb, line 420 def shutdown! execute_callbacks(:before_shutdown) end
Let everyone know we’re shutting down.
Source
# File lib/middleman-core/application.rb, line 397 def source_dir Pathname(File.join(root, config[:source])) end
Source
# File lib/middleman-core/application.rb, line 442 def to_s "#<Middleman::Application:0x#{object_id}>" end
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.