class Middleman::Application
def apply_cli_options
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
def build?
def build? mode?(:build) end
def config
-
(ConfigurationManager)
-
def config @config ||= ::Middleman::Configuration::ConfigurationManager.new end
def development?
def development? environment?(:development) end
def environment
def environment config[:environment] end
def environment?(key)
def environment?(key) config[:environment] == key end
def evaluate_configuration!
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
def initialize(&block)
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? 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) apply_cli_options # 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
def map(map, &block)
def map(map, &block) @mappings << MapDescriptor.new(map, block) end
def mode?(key)
def mode?(key) config[:mode] == key end
def production?
def production? environment?(:production) end
def prune_tilt_templates!
def prune_tilt_templates! ::Tilt.mappings.each_key do |key| begin ::Tilt[".#{key}"] rescue LoadError, NameError ::Tilt.mappings.delete(key) end end end
def root
-
(String)
-
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
def root_path
def root_path Pathname(root) end
def server?
def server? mode?(:server) end
def set(key, value=nil, &block)
-
(void)
-
Parameters:
-
value
() -- Attribute value
-
key
(Symbol
) -- Name of the attribue
Deprecated:
- Prefer accessing settings through "config".
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
def shutdown!
def shutdown! execute_callbacks(:before_shutdown) end
def source_dir
def source_dir Pathname(File.join(root, config[:source])) end
def to_s
messages, which can take a long time (minutes at full CPU)
where Ruby will call to_s/inspect while printing exception
Work around this bug: http://bugs.ruby-lang.org/issues/4521
def to_s "#<Middleman::Application:0x#{object_id}>" end
def use(middleware, *args, &block)
-
(void)
-
Parameters:
-
middleware
(Class
) -- Middleware module
def use(middleware, *args, &block) @middleware << MiddlewareDescriptor.new(middleware, args, block) end