Class: Middleman::Apps::Extension

Inherits:
Extension
  • Object
show all
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

Options for Extension collapse

Instance Method Summary collapse

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, options_hash = {}, &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, options)
end

Instance Attribute Details

#collectionObject (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_configurationnil

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.

Returns:

  • (nil)


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

Parameters:

  • value

    value for this option - 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_runil

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.

Returns:

  • (nil)


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: {}

Parameters:

  • value

    value for this option - 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: /

Parameters:

  • value

    value for this option - 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

Parameters:

  • value

    value for this option - 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

Parameters:

  • value

    value for this option - 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

Parameters:

  • value

    value for this option - Default: ‘false`



29
# File 'lib/middleman/apps/extension.rb', line 29

option :verbose, false, 'Displays list of child apps that were ignored'

#watch_child_appsnil

Set a watcher to reload MM when files change in the directory for the child apps.

Returns:

  • (nil)


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.expand_path(options.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