Class: Middleman::Apps::Extension
- Inherits:
-
Extension
- Object
- Extension
- Middleman::Apps::Extension
- Extended by:
- Forwardable
- Defined in:
- lib/middleman/apps/extension.rb
Overview
A Middleman extension to serve Rack applications (e.g. created via Sinatra) or other such apps when previewing using Middleman Preview server, as well as in the production (build) mode by creating an umbrella Rack app.
Usage examples can be seen in README for this extension.
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Options for Extension collapse
-
#app_dir(value) ⇒ Object
Extension Option - The directory child apps are stored in - Default: MM_APPS_DIR.
-
#map(value) ⇒ Object
Extension Option - Mappings for differently named child apps - Default: {}.
-
#mount_path(value) ⇒ Object
Extension Option - Root mount path for child apps - Default: /.
-
#namespace(value) ⇒ Object
Extension Option - Namespace for the child apps - Default: nil.
-
#not_found(value) ⇒ Object
Extension Option - Path to 404 error page - Default: 404.html.
-
#verbose(value) ⇒ Object
Extension Option - Displays list of child apps that were ignored - Default: false.
Instance Method Summary collapse
-
#after_configuration ⇒ nil
private
Run ‘after_configuration` hook passed on by MM.
-
#create_config_ru ⇒ nil
private
Create a ‘config.ru` file, if one does not exist, yet.
-
#initialize(app, options_hash = {}, &block) ⇒ Extension
constructor
A new instance of Extension.
-
#watch_child_apps ⇒ nil
Set a watcher to reload MM when files change in the directory for the child apps.
Constructor Details
#initialize(app, options_hash = {}, &block) ⇒ Extension
Returns a new instance of Extension.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/middleman/apps/extension.rb', line 36 def initialize(app, = {}, &block) super # useful for converting file names to ruby classes require 'active_support/core_ext/string/inflections' require 'middleman/sitemap/app_resource' require 'middleman/sitemap/app_collection' # get a reference to all the apps @collection = Sitemap::AppCollection.new(app, self, ) end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
16 17 18 |
# File 'lib/middleman/apps/extension.rb', line 16 def collection @collection end |
Instance Method Details
#after_configuration ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Run ‘after_configuration` hook passed on by MM
After configuration for middleman has been finalized, create a ‘config.ru` in the root directory, and mount all child apps, if we are on a preview server.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/middleman/apps/extension.rb', line 58 def after_configuration create_config_ru return if app.build? app.sitemap.register_resource_list_manipulator(:child_apps, @collection) return unless app.server? watch_child_apps @collection.mount_child_apps(app) end |
#app_dir(value) ⇒ Object
Extension Option - The directory child apps are stored in - Default: MM_APPS_DIR
31 32 |
# File 'lib/middleman/apps/extension.rb', line 31 option :app_dir, ENV['MM_APPS_DIR'] || 'apps', 'The directory child apps are stored in' |
#create_config_ru ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a ‘config.ru` file, if one does not exist, yet.
This is done whenever ‘middleman` cli is run for building, or previewing the static app.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/middleman/apps/extension.rb', line 93 def create_config_ru path = File.join(app.root, 'config.ru') return if File.exist?(path) content = <<-CONTENT.gsub(/^ {8}/, '') ENV['RACK_ENV'] ||= 'production' require 'middleman/apps' run Middleman::Apps.rack_app CONTENT File.open(path, 'wb') { |file| file.puts content } end |
#map(value) ⇒ Object
Extension Option - Mappings for differently named child apps - Default: {}
28 |
# File 'lib/middleman/apps/extension.rb', line 28 option :map, {}, 'Mappings for differently named child apps' |
#mount_path(value) ⇒ Object
Extension Option - Root mount path for child apps - Default: /
30 |
# File 'lib/middleman/apps/extension.rb', line 30 option :mount_path, '/', 'Root mount path for child apps' |
#namespace(value) ⇒ Object
Extension Option - Namespace for the child apps - Default: nil
27 |
# File 'lib/middleman/apps/extension.rb', line 27 option :namespace, nil, 'Namespace for the child apps' |
#not_found(value) ⇒ Object
Extension Option - Path to 404 error page - Default: 404.html
26 |
# File 'lib/middleman/apps/extension.rb', line 26 option :not_found, '404.html', 'Path to 404 error page' |
#verbose(value) ⇒ Object
Extension Option - Displays list of child apps that were ignored - Default: false
29 |
# File 'lib/middleman/apps/extension.rb', line 29 option :verbose, false, 'Displays list of child apps that were ignored' |
#watch_child_apps ⇒ nil
Set a watcher to reload MM when files change in the directory for the child apps.
74 75 76 77 78 79 80 81 |
# File 'lib/middleman/apps/extension.rb', line 74 def watch_child_apps # Make sure it exists, or `listen` will explode. app_path = File.(.app_dir, app.root) ::FileUtils.mkdir_p(app_path) watcher = app.files.watch :reload, path: app_path, only: /\.rb$/ list = @collection watcher.on_change { list.mount_child_apps(app) } end |