Required options for extensions

This commit is contained in:
Thomas Reynolds 2014-07-23 21:08:20 -07:00
parent 21c38b707c
commit 5e20fca73e
3 changed files with 24 additions and 24 deletions

View file

@ -70,13 +70,14 @@ module Middleman
# @param [Symbol] key The name of the option # @param [Symbol] key The name of the option
# @param [Object] default The default value for the option # @param [Object] default The default value for the option
# @param [String] description A human-readable description of what the option does # @param [String] description A human-readable description of what the option does
# @param [Hash] options Additional options.
# @return [ConfigSetting] # @return [ConfigSetting]
def define_setting(key, default=nil, description=nil) def define_setting(key, default=nil, description=nil, options={})
raise "Setting #{key} doesn't exist" if @finalized raise "Setting #{key} doesn't exist" if @finalized
raise "Setting #{key} already defined" if @settings.key?(key) raise "Setting #{key} already defined" if @settings.key?(key)
raise 'Setting key must be a Symbol' unless key.is_a? Symbol raise 'Setting key must be a Symbol' unless key.is_a? Symbol
@settings[key] = ConfigSetting.new(key, default, description) @settings[key] = ConfigSetting.new(key, default, description, options)
end end
# Switch the configuration manager is finalized, it switches to read-only # Switch the configuration manager is finalized, it switches to read-only
@ -94,7 +95,7 @@ module Middleman
# Load in a list of settings # Load in a list of settings
def load_settings(other_settings) def load_settings(other_settings)
other_settings.each do |setting| other_settings.each do |setting|
new_setting = define_setting(setting.key, setting.default, setting.description) new_setting = define_setting(setting.key, setting.default, setting.description, setting.options)
new_setting.value = setting.value if setting.value_set? new_setting.value = setting.value if setting.value_set?
end end
end end
@ -124,11 +125,15 @@ module Middleman
# A human-friendly description of the setting # A human-friendly description of the setting
attr_accessor :description attr_accessor :description
def initialize(key, default, description) # Additional config.
attr_accessor :options
def initialize(key, default, description, options={})
@value_set = false @value_set = false
self.key = key self.key = key
self.default = default self.default = default
self.description = description self.description = description
self.options = options
end end
# The user-supplied value for this setting, overriding the default # The user-supplied value for this setting, overriding the default

View file

@ -106,8 +106,8 @@ module Middleman
# @param [Symbol] key The name of the option # @param [Symbol] key The name of the option
# @param [Object] default The default value for the option # @param [Object] default The default value for the option
# @param [String] description A human-readable description of what the option does # @param [String] description A human-readable description of what the option does
def option(key, default=nil, description=nil) def option(key, default=nil, description=nil, options={})
config.define_setting(key, default, description) config.define_setting(key, default, description, options)
end end
# Declare helpers to be added the global Middleman application. # Declare helpers to be added the global Middleman application.
@ -239,6 +239,13 @@ module Middleman
end end
yield @options if block_given? yield @options if block_given?
@options.all_settings.each do |o|
next unless o.options[:required] && !o.value_set?
logger.error "The `:#{o.key}` option of the `#{self.class.ext_name}` extension is required."
exit(1)
end
end end
def bind_before_configuration def bind_before_configuration

View file

@ -1,31 +1,19 @@
class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
self.supports_multiple_instances = true self.supports_multiple_instances = true
option :name, nil, 'The name of the pipeline' option :name, nil, 'The name of the pipeline', required: true
option :command, nil, 'The command to initialize' option :command, nil, 'The command to initialize', required: true
option :source, nil, 'Path to merge into sitemap' option :source, nil, 'Path to merge into sitemap', required: true
option :latency, 0.25, 'Latency between refreshes of source' option :latency, 0.25, 'Latency between refreshes of source'
def initialize(app, config={}, &block) def initialize(app, config={}, &block)
super super
if options[:name].nil?
throw "Name is required"
end
if options[:command].nil?
throw "Command is required"
end
if options[:source].nil?
throw "Source is required"
end
require 'thread' require 'thread'
app.files.watch :source, app.files.watch :source,
path: File.expand_path(options[:source], app.root), path: File.expand_path(options[:source], app.root),
latency: options[:latency] latency: options[:latency]
end end
def after_configuration def after_configuration
@ -41,7 +29,7 @@ class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
def watch_command! def watch_command!
::IO.popen(options[:command], 'r') do |pipe| ::IO.popen(options[:command], 'r') do |pipe|
while buf = pipe.gets while buf = pipe.gets
without_newline = buf.sub(/\n$/,'') without_newline = buf.sub(/\n$/, '')
logger.info "== External: #{without_newline}" if without_newline.length > 0 logger.info "== External: #{without_newline}" if without_newline.length > 0
end end
end end