Class: Middleman::Application
- Inherits:
-
Object
- Object
- Middleman::Application
- Extended by:
- Forwardable
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/application.rb
Constant Summary
Constants included from Contracts
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#config_context ⇒ Object
readonly
Returns the value of attribute config_context.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#generic_template_context ⇒ Object
readonly
An instance of the above anonymouse class.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#middleware ⇒ Object
readonly
Returns the value of attribute middleware.
-
#sitemap ⇒ Object
readonly
Returns the value of attribute sitemap.
-
#template_context_class ⇒ Object
readonly
An anonymous subclass of ::Middleman::TemplateContext.
Class Method Summary collapse
-
.config ⇒ ConfigurationManager
Global configuration for the whole Middleman project.
-
.root ⇒ String
Root project directory (overwritten in middleman build/server).
-
.root_path ⇒ Object
Pathname-addressed root.
Instance Method Summary collapse
- #apply_cli_options ⇒ Object
-
#asset_extensions ⇒ Array.<String>
Which file extensions are "assets.".
-
#bind_address ⇒ NilClass, String
Which bind address the preview server should use.
-
#Bool ⇒ Boolean
Backwards compatible helper.
- #build? ⇒ Boolean
-
#build_dir ⇒ String
Where to build output files.
-
#css_dir ⇒ String
Location of stylesheets within source.
- #development? ⇒ Boolean
-
#disable_sitemap ⇒ Boolean
If we should not run the sitemap.
-
#encoding ⇒ String
Default string encoding for templates and output.
-
#environment ⇒ String
Middleman environment.
- #environment?(key) ⇒ Boolean
-
#evaluate_configuration! ⇒ Object
Eval config.
-
#exit_before_ready ⇒ Boolean
If we should exit before ready event.
-
#extensions_with_layout ⇒ Array.<String>
Which file extensions have a layout by default.
-
#fonts_dir ⇒ String
Location of fonts within source.
-
#http_prefix ⇒ String
Default prefix for building paths.
-
#https ⇒ Boolean
Whether to serve the preview server over HTTPS.
-
#images_dir ⇒ String
Location of images within source.
-
#index_file ⇒ String
Which file should be used for directory indexes.
-
#initialize(&block) ⇒ Application
constructor
Initialize the Middleman project.
-
#js_dir ⇒ String
Location of javascripts within source.
-
#layout ⇒ String
Default layout name.
-
#layouts_dir ⇒ String
Location of layouts within source.
- #map(map, &block) ⇒ Object
-
#mode ⇒ String
Middleman mode.
- #mode?(key) ⇒ Boolean
-
#port ⇒ Fixnum
Which port preview should start on.
- #production? ⇒ Boolean
-
#protect_from_csrf ⇒ Boolean
Should Padrino include CRSF tag.
-
#prune_tilt_templates! ⇒ Object
Clean up missing Tilt exts.
- #server? ⇒ Boolean
-
#server_name ⇒ NilClass, String
Which server name should be used.
-
#set(key, value = nil, &block)
deprecated
Deprecated.
Prefer accessing settings through "config".
-
#shutdown! ⇒ Object
Let everyone know we're shutting down.
-
#source ⇒ String
Name of the source directory.
- #source_dir ⇒ Object
-
#ssl_certificate ⇒ String
The (optional) path to the SSL cert to use for the preview server.
-
#ssl_private_key ⇒ String
The (optional) private key for the certificate in :ssl_certificate.
-
#String
Add Rack App mapped to specific path.
-
#strip_index_file ⇒ Boolean
Whether to strip the index file name off links to directory indexes.
-
#Symbol ⇒ Symbol
Backwards compatible helper.
-
#to_s ⇒ Object
(also: #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.
-
#trailing_slash ⇒ Boolean
Whether to include a trailing slash when stripping the index file.
-
#use(middleware, *args, &block)
Use Rack middleware.
Methods included from Contracts
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 307 308 309 |
# File '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_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) # 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]]) # 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 |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
60 61 62 |
# File 'lib/middleman-core/application.rb', line 60 def config @config end |
#config_context ⇒ Object (readonly)
Returns the value of attribute config_context.
48 49 50 |
# File 'lib/middleman-core/application.rb', line 48 def config_context @config_context end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
63 64 65 |
# File 'lib/middleman-core/application.rb', line 63 def extensions @extensions end |
#generic_template_context ⇒ Object (readonly)
An instance of the above anonymouse class.
57 58 59 |
# File 'lib/middleman-core/application.rb', line 57 def generic_template_context @generic_template_context end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
69 70 71 |
# File 'lib/middleman-core/application.rb', line 69 def mappings @mappings end |
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
66 67 68 |
# File 'lib/middleman-core/application.rb', line 66 def middleware @middleware end |
#sitemap ⇒ Object (readonly)
Returns the value of attribute sitemap.
51 52 53 |
# File 'lib/middleman-core/application.rb', line 51 def sitemap @sitemap end |
#template_context_class ⇒ Object (readonly)
An anonymous subclass of ::Middleman::TemplateContext
54 55 56 |
# File 'lib/middleman-core/application.rb', line 54 def template_context_class @template_context_class end |
Class Method Details
.config ⇒ ConfigurationManager
Global configuration for the whole Middleman project.
29 30 31 |
# File 'lib/middleman-core/application.rb', line 29 def config @config ||= ::Middleman::Configuration::ConfigurationManager.new end |
.root ⇒ String
Root project directory (overwritten in middleman build/server)
35 36 37 38 39 |
# 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 ⇒ Object
Pathname-addressed root
42 43 44 |
# File 'lib/middleman-core/application.rb', line 42 def root_path Pathname(root) end |
Instance Method Details
#apply_cli_options ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/middleman-core/application.rb', line 311 def config[:cli_options].each do |k, v| setting = config.setting(k.to_sym) next unless setting v = setting.[:import].call(v) if setting.[:import] config[k.to_sym] = v end end |
#asset_extensions ⇒ Array.<String>
Which file extensions are "assets."
165 |
# File '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_address ⇒ NilClass, String
Which bind address the preview server should use
81 |
# File 'lib/middleman-core/application.rb', line 81 define_setting :bind_address, nil, 'The bind address of the preview server' |
#Bool ⇒ Boolean
Backwards compatible helper. Whether we're in production mode.
366 |
# File 'lib/middleman-core/application.rb', line 366 Contract Bool |
#build? ⇒ Boolean
374 375 376 |
# File 'lib/middleman-core/application.rb', line 374 def build? mode?(:build) end |
#build_dir ⇒ String
Where to build output files
149 |
# File 'lib/middleman-core/application.rb', line 149 define_setting :build_dir, 'build', 'Where to build output files' |
#css_dir ⇒ String
Location of stylesheets within source.
133 |
# File 'lib/middleman-core/application.rb', line 133 define_setting :css_dir, 'stylesheets', 'Location of stylesheets within source' |
#development? ⇒ Boolean
396 397 398 |
# File 'lib/middleman-core/application.rb', line 396 def development? environment?(:development) end |
#disable_sitemap ⇒ Boolean
If we should not run the sitemap.
101 |
# File 'lib/middleman-core/application.rb', line 101 define_setting :disable_sitemap, false, 'If we should not run the sitemap.' |
#encoding ⇒ String
Default string encoding for templates and output.
169 |
# File 'lib/middleman-core/application.rb', line 169 define_setting :encoding, 'utf-8', 'Default string encoding for templates and output' |
#environment ⇒ String
Middleman environment. Defaults to :development
113 |
# File '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
382 383 384 |
# File 'lib/middleman-core/application.rb', line 382 def environment?(key) config[:environment] == key end |
#evaluate_configuration! ⇒ Object
Eval config
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/middleman-core/application.rb', line 323 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 |
#exit_before_ready ⇒ Boolean
If we should exit before ready event.
105 |
# File 'lib/middleman-core/application.rb', line 105 define_setting :exit_before_ready, false, 'If we should exit before ready event.' |
#extensions_with_layout ⇒ Array.<String>
Which file extensions have a layout by default.
161 |
# File '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_dir ⇒ String
Location of fonts within source.
141 |
# File 'lib/middleman-core/application.rb', line 141 define_setting :fonts_dir, 'fonts', 'Location of fonts within source' |
#http_prefix ⇒ String
Default prefix for building paths. Used by HTML helpers.
153 |
# File 'lib/middleman-core/application.rb', line 153 define_setting :http_prefix, '/', 'Default prefix for building paths' |
#https ⇒ Boolean
Whether to serve the preview server over HTTPS.
85 |
# File 'lib/middleman-core/application.rb', line 85 define_setting :https, false, 'Serve the preview server over SSL/TLS' |
#images_dir ⇒ String
Location of images within source. Used by HTML helpers.
137 |
# File 'lib/middleman-core/application.rb', line 137 define_setting :images_dir, 'images', 'Location of images within source' |
#index_file ⇒ String
Which file should be used for directory indexes
117 |
# File 'lib/middleman-core/application.rb', line 117 define_setting :index_file, 'index.html', 'Which file should be used for directory indexes' |
#js_dir ⇒ String
Location of javascripts within source.
129 |
# File 'lib/middleman-core/application.rb', line 129 define_setting :js_dir, 'javascripts', 'Location of javascripts within source' |
#layout ⇒ String
Default layout name
157 |
# File 'lib/middleman-core/application.rb', line 157 define_setting :layout, :_auto_layout, 'Default layout name' |
#layouts_dir ⇒ String
Location of layouts within source. Used by renderers.
145 |
# File 'lib/middleman-core/application.rb', line 145 define_setting :layouts_dir, 'layouts', 'Location of layouts within source' |
#map(map, &block) ⇒ Object
427 428 429 |
# File 'lib/middleman-core/application.rb', line 427 def map(map, &block) @mappings << MapDescriptor.new(map, block) end |
#mode ⇒ String
Middleman mode. Defaults to :server, set to :build by the build process
109 |
# File 'lib/middleman-core/application.rb', line 109 define_setting :mode, :server, 'Middleman mode. Defaults to :server' |
#mode?(key) ⇒ Boolean
360 361 362 |
# File 'lib/middleman-core/application.rb', line 360 def mode?(key) config[:mode] == key end |
#port ⇒ Fixnum
Which port preview should start on.
73 |
# File 'lib/middleman-core/application.rb', line 73 define_setting :port, 4567, 'The preview server port' |
#production? ⇒ Boolean
403 404 405 |
# File 'lib/middleman-core/application.rb', line 403 def production? environment?(:production) end |
#protect_from_csrf ⇒ Boolean
Should Padrino include CRSF tag
173 |
# File '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
346 347 348 349 350 351 352 353 354 |
# File 'lib/middleman-core/application.rb', line 346 def prune_tilt_templates! ::Tilt.mappings.each_key do |key| begin ::Tilt[".#{key}"] rescue LoadError, NameError ::Tilt.mappings.delete(key) end end end |
#server? ⇒ Boolean
367 368 369 |
# File 'lib/middleman-core/application.rb', line 367 def server? mode?(:server) end |
#server_name ⇒ NilClass, String
Which server name should be used
77 |
# File 'lib/middleman-core/application.rb', line 77 define_setting :server_name, nil, 'The server name of preview server' |
#set(key, value = nil, &block)
Prefer accessing settings through "config".
This method returns an undefined value.
Set attributes (global variables)
443 444 445 446 447 448 |
# File 'lib/middleman-core/application.rb', line 443 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.
432 433 434 |
# File 'lib/middleman-core/application.rb', line 432 def shutdown! execute_callbacks(:before_shutdown) end |
#source ⇒ String
Name of the source directory
97 |
# File 'lib/middleman-core/application.rb', line 97 define_setting :source, 'source', 'Name of the source directory' |
#source_dir ⇒ Object
409 410 411 |
# File 'lib/middleman-core/application.rb', line 409 def source_dir Pathname(File.join(root, config[:source])) end |
#ssl_certificate ⇒ String
The (optional) path to the SSL cert to use for the preview server.
89 |
# File '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_key ⇒ String
The (optional) private key for the certificate in :ssl_certificate.
93 |
# File '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
426 |
# File 'lib/middleman-core/application.rb', line 426 Contract String, Proc => Any |
#strip_index_file ⇒ Boolean
Whether to strip the index file name off links to directory indexes
121 |
# File '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' |
#Symbol ⇒ Symbol
Backwards compatible helper. What the current environment is.
388 |
# File 'lib/middleman-core/application.rb', line 388 Contract Symbol |
#to_s ⇒ Object 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.
454 455 456 |
# File 'lib/middleman-core/application.rb', line 454 def to_s "#<Middleman::Application:0x#{object_id}>" end |
#trailing_slash ⇒ Boolean
Whether to include a trailing slash when stripping the index file
125 |
# File '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
418 419 420 |
# File 'lib/middleman-core/application.rb', line 418 def use(middleware, *args, &block) @middleware << MiddlewareDescriptor.new(middleware, args, block) end |