Switch over to new config methods

This commit is contained in:
Ben Hollis 2012-10-13 19:54:55 -07:00
parent 781fa1c65f
commit a50ca56fbc
22 changed files with 119 additions and 115 deletions

View file

@ -44,69 +44,69 @@ module Middleman
# Root project directory (overwritten in middleman build/server)
# @return [String]
set :root, ENV["MM_ROOT"] || Dir.pwd
config.define_setting :root, (ENV["MM_ROOT"] || Dir.pwd), 'Root project directory'
# Pathname-addressed root
def self.root_path
Pathname(root)
Pathname(config[:root])
end
delegate :root_path, :to => :"self.class"
# Name of the source directory
# @return [String]
set :source, "source"
config.define_setting :source, "source", 'Name of the source directory'
# Middleman environment. Defaults to :development, set to :build by the build process
# @return [String]
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
config.define_setting :environment, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development), 'Middleman environment. Defaults to :development, set to :build by the build process'
# Which file should be used for directory indexes
# @return [String]
set :index_file, "index.html"
config.define_setting :index_file, "index.html", 'Which file should be used for directory indexes'
# Whether to strip the index file name off links to directory indexes
# @return [Boolean]
set :strip_index_file, true
config.define_setting :strip_index_file, true, 'Whether to strip the index file name off links to directory indexes'
# Whether to include a trailing slash when stripping the index file
# @return [Boolean]
set :trailing_slash, true
config.define_setting :trailing_slash, true, 'Whether to include a trailing slash when stripping the index file'
# Location of javascripts within source.
# @return [String]
set :js_dir, "javascripts"
config.define_setting :js_dir, "javascripts", 'Location of javascripts within source'
# Location of stylesheets within source. Used by Compass.
# @return [String]
set :css_dir, "stylesheets"
config.define_setting :css_dir, "stylesheets", 'Location of stylesheets within source'
# Location of images within source. Used by HTML helpers and Compass.
# @return [String]
set :images_dir, "images"
config.define_setting :images_dir, "images", 'Location of images within source'
# Location of fonts within source. Used by Compass.
# @return [String]
set :fonts_dir, "fonts"
config.define_setting :fonts_dir, "fonts", 'Location of fonts within source'
# Where to build output files
# @return [String]
set :build_dir, "build"
config.define_setting :build_dir, "build", 'Where to build output files'
# Default prefix for building paths. Used by HTML helpers and Compass.
# @return [String]
set :http_prefix, "/"
config.define_setting :http_prefix, "/", 'Default prefix for building paths'
# Default string encoding for templates and output.
# @return [String]
set :encoding, "utf-8"
config.define_setting :encoding, "utf-8", 'Default string encoding for templates and output'
# Whether to catch and display exceptions
# @return [Boolean]
set :show_exceptions, true
config.define_setting :show_exceptions, true, 'Whether to catch and display exceptions'
# Default layout name
# @return [String, Symbold]
set :layout, :_auto_layout
config.define_setting :layout, :_auto_layout, 'Default layout name'
# Activate custom features and extensions
include Middleman::CoreExtensions::Extensions
@ -151,12 +151,12 @@ module Middleman
cache.clear
# Setup the default values from calls to set before initialization
self.class.superclass.config.to_h.each { |k,v| self.class.set(k,v) }
self.class.superclass.config.to_h.each { |k,v| self.class.config.define_setting(k,v) }
# Evaluate a passed block if given
instance_exec(&block) if block_given?
set :source, ENV["MM_SOURCE"] if ENV["MM_SOURCE"]
config[:source] = ENV["MM_SOURCE"] if ENV["MM_SOURCE"]
super
end
@ -172,17 +172,17 @@ module Middleman
# Whether we're in development mode
# @return [Boolean] If we're in dev mode
def development?; environment == :development; end
def development?; config[:environment] == :development; end
# Whether we're in build mode
# @return [Boolean] If we're in build mode
def build?; environment == :build; end
def build?; config[:environment] == :build; end
# The full path to the source directory
#
# @return [String]
def source_dir
File.join(root, source)
File.join(config[:root], config[:source])
end
delegate :logger, :instrument, :to => ::Middleman::Util
@ -205,7 +205,7 @@ module Middleman
if !resource
# Try it with /index.html at the end
indexed_path = File.join(path.sub(%r{/$}, ''), index_file)
indexed_path = File.join(path.sub(%r{/$}, ''), config[:index_file])
resource = sitemap.find_resource_by_destination_path(indexed_path)
end

View file

@ -83,7 +83,7 @@ module Middleman::Cli
# @return [Middleman::Application]
def shared_instance(verbose=false, instrument=false)
@_shared_instance ||= ::Middleman::Application.server.inst do
set :environment, :build
config[:environment] = :build
logger(verbose ? 0 : 1, instrument)
end
end

View file

@ -30,7 +30,7 @@ module Middleman
# old Middleman.
#
# @deprecated Prefer accessing settings through "config".
def method_missing(method)
def method_missing(method, *args)
if config.defines_setting? method
config[method]
else
@ -71,7 +71,7 @@ module Middleman
# old Middleman.
#
# @deprecated Prefer accessing settings through "config".
def method_missing(method)
def method_missing(method, *args)
if config.defines_setting? method
config[method]
else
@ -128,7 +128,7 @@ module Middleman
if defines_setting?(method) && args.size == 0
self[method]
elsif method =~ /^(\w+)=$/ && args.size == 1
self[$1] = args[0]
self[$1.to_sym] = args[0]
else
super
end
@ -190,9 +190,17 @@ module Middleman
end
end
# An individual configuration setting, with an optional default and description.
# Also models whether or not a value has been set.
class ConfigSetting
attr_accessor :key, :default, :description
attr_writer :value
# The name of this setting
attr_accessor :key
# The default value for this setting
attr_accessor :default
# A human-friendly description of the setting
attr_accessor :description
def initialize(key, default, description)
@value_set = false
@ -201,15 +209,20 @@ module Middleman
self.description = description
end
# The user-supplied value for this setting, overriding the default
def value=(value)
@value = value
@value_set = true
end
# The effective value of the setting, which may be the default
# if the user has not set a value themselves. Note that even if the
# user sets the value to nil it will override the default.
def value
value_set? ? @value : default
end
# Whether or not there has been a value set beyond the default
def value_set?
@value_set
end

View file

@ -13,7 +13,7 @@ module Middleman
require "yaml"
require "active_support/json"
app.set :data_dir, "data"
app.config.define_setting :data_dir, "data", "The directory data files are stored in"
app.send :include, InstanceMethods
end
alias :included :registered
@ -24,12 +24,12 @@ module Middleman
# Setup data files before anything else so they are available when
# parsing config.rb
def initialize
self.files.changed DataStore.matcher do |file|
self.data.touch_file(file) if file.match(%r{^#{self.data_dir}\/})
files.changed DataStore.matcher do |file|
data.touch_file(file) if file.match(%r{^#{config[:data_dir]}\/})
end
self.files.deleted DataStore.matcher do |file|
self.data.remove_file(file) if file.match(%r{^#{self.data_dir}\/})
files.deleted DataStore.matcher do |file|
data.remove_file(file) if file.match(%r{^#{config[data_dir]}\/})
end
super

View file

@ -42,11 +42,8 @@ module Middleman
app.define_hook :build_config
app.define_hook :development_config
if ENV["AUTOLOAD_SPROCKETS"]
app.set :autoload_sprockets, (ENV["AUTOLOAD_SPROCKETS"] == "true")
else
app.set :autoload_sprockets, true
end
app.config.define_setting :autoload_sprockets, true, 'Automatically load sprockets at startup?'
app.config[:autoload_sprockets] = (ENV["AUTOLOAD_SPROCKETS"] == "true") if ENV["AUTOLOAD_SPROCKETS"]
app.extend ClassMethods
app.send :include, InstanceMethods
@ -134,7 +131,7 @@ module Middleman
instance_eval File.read(local_config), local_config, 1
end
if autoload_sprockets
if config[:autoload_sprockets]
begin
require "middleman-sprockets"
activate(:sprockets)

View file

@ -9,20 +9,20 @@ module Middleman
# once registered
def registered(app)
# Setup a default helpers paths
app.set :helpers_dir, "helpers"
app.set :helpers_filename_glob, "**{,/*/**}/*.rb"
app.set :helpers_filename_to_module_name_proc, Proc.new { |filename|
app.config.define_setting :helpers_dir, "helpers", 'Directory to autoload helper modules from'
app.config.define_setting :helpers_filename_glob, "**.rb", 'Glob pattern for matching helper ruby files'
app.config.define_setting :helpers_filename_to_module_name_proc, Proc.new { |filename|
basename = File.basename(filename, File.extname(filename))
basename.camelcase
}
}, 'Proc implementing the conversion from helper filename to module name'
# After config
app.after_configuration do
helpers_path = File.expand_path(helpers_dir, root)
helpers_path = File.expand_path(config[:helpers_dir], config[:root])
next unless File.exists?(helpers_path)
Dir[File.join(helpers_path, helpers_filename_glob)].each do |filename|
module_name = helpers_filename_to_module_name_proc.call(filename)
Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename|
module_name = config[:helpers_filename_to_module_name_proc].call(filename)
next unless module_name
require filename

View file

@ -257,7 +257,7 @@ module Middleman
extension_class = ::Tilt[ext]
::Tilt.mappings.each do |ext, engines|
next unless engines.include? extension_class
engine_options = respond_to?(ext.to_sym) ? send(ext.to_sym) : {}
engine_options = config[ext.to_sym] || {}
options.merge!(engine_options)
end

View file

@ -30,10 +30,10 @@ module Middleman
def with_layout(layout_name, &block)
old_layout = layout
set :layout, layout_name
config[:layout] = layout_name
instance_exec(&block) if block_given?
ensure
set :layout, old_layout
config[:layout] = old_layout
end
# The page method allows the layout to be set on a specific path

View file

@ -71,7 +71,7 @@ module Middleman
opts = @options
@app =::Middleman::Application.server.inst do
if opts[:environment]
set :environment, opts[:environment].to_sym
config[:environment] = opts[:environment].to_sym
end
logger(opts[:debug] ? 0 : 1, opts[:instrumenting] || false)

View file

@ -8,8 +8,8 @@ module Middleman
# once registered
def registered(app)
# Setup a default ERb engine
app.set :erb_engine, :erb
app.set :erb_engine_prefix, ::Tilt
app.config.define_setting :erb_engine, :erb, 'The engine to use for rendering ERb templates'
app.config.define_setting :erb_engine_prefix, ::Tilt, 'The parent module for ERb template engines'
app.before_configuration do
template_extensions :erb => :html
@ -19,14 +19,14 @@ module Middleman
app.after_configuration do
# Find the user's prefered engine
# Convert symbols to classes
if erb_engine.is_a? Symbol
if config[:erb_engine].is_a? Symbol
engine = engine.to_s
engine = engine == "erb" ? "ERB" : engine.camelize
erb_engine = erb_engine_prefix.const_get("#{engine}Template")
config[:erb_engine] = config[:erb_engine_prefix].const_get("#{engine}Template")
end
# Tell Tilt to use the preferred engine
::Tilt.prefer(erb_engine)
::Tilt.prefer(config[:erb_engine])
end
end
alias :included :registered

View file

@ -11,8 +11,8 @@ module Middleman
# Once registered
def registered(app)
# Default sass options
app.set :less, {}
# Default less options
app.config.define_setting :less, {}, 'LESS compiler options'
app.before_configuration do
template_extensions :less => :css

View file

@ -10,8 +10,8 @@ module Middleman
# Once registered
def registered(app)
# Set our preference for a markdown engine
app.set :markdown_engine, :maruku
app.set :markdown_engine_prefix, ::Tilt
app.config.define_setting :markdown_engine, :maruku, 'Preferred markdown engine'
app.config.define_setting :markdown_engine_prefix, ::Tilt, 'The parent module for markdown template engines'
app.before_configuration do
template_extensions :markdown => :html,
@ -26,24 +26,24 @@ module Middleman
begin
# Look for the user's preferred engine
if markdown_engine == :redcarpet
if config[:markdown_engine] == :redcarpet
require "middleman-core/renderers/redcarpet"
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate)
elsif !markdown_engine.nil?
elsif !config[:markdown_engine].nil?
# Map symbols to classes
markdown_engine_klass = if markdown_engine.is_a? Symbol
engine = markdown_engine.to_s
markdown_engine_klass = if config[:markdown_engine].is_a? Symbol
engine = config[:markdown_engine].to_s
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
markdown_engine_prefix.const_get("#{engine}Template")
config[:markdown_engine_prefix].const_get("#{engine}Template")
else
markdown_engine_prefix
config[:markdown_engine_prefix]
end
# Tell tilt to use that engine
::Tilt.prefer(markdown_engine_klass)
end
rescue LoadError
logger.warn "Requested Markdown engine (#{markdown_engine}) not found. Maybe the gem needs to be installed and required?"
logger.warn "Requested Markdown engine (#{config[:markdown_engine]}) not found. Maybe the gem needs to be installed and required?"
end
end
end

View file

@ -12,16 +12,16 @@ module Middleman
# Once registered
def registered(app)
# Default sass options
app.set :sass, {}
app.config.define_setting :sass, {}, 'Sass engine options'
# Location of SASS .sass_cache directory.
# @return [String]
# set :sass_cache_path, "/tmp/middleman-app-name/sass_cache"
app.set(:sass_cache_path) { File.join(app.root_path, '.sass_cache') } # runtime compile of path
app.config.define_setting :sass_cache_path, nil, 'Location of sass cache' # runtime compile of path
app.before_configuration do
template_extensions :scss => :css,
:sass => :css
config[:sass_cache_path] = File.join(app.root_path, '.sass_cache')
end
# Tell Tilt to use it as well (for inline sass blocks)
@ -76,11 +76,11 @@ module Middleman
more_opts = { :filename => eval_file, :line => line, :syntax => syntax }
if @context.is_a?(::Middleman::Application) && file
location_of_sass_file = File.expand_path(@context.source, @context.root)
location_of_sass_file = @context.source_dir
parts = basename.split('.')
parts.pop
more_opts[:css_filename] = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
more_opts[:css_filename] = File.join(location_of_sass_file, @context.config[:css_dir], parts.join("."))
end
options.merge(more_opts)

View file

@ -20,10 +20,10 @@ module Middleman
app.register Middleman::Sitemap::Extensions::Ignores
# Set to automatically convert some characters into a directory
app.set :automatic_directory_matcher, nil
app.config.define_setting :automatic_directory_matcher, nil, 'Set to automatically convert some characters into a directory'
# Setup callbacks which can exclude paths from the sitemap
app.set :ignored_sitemap_matchers, {
app.config.define_setting :ignored_sitemap_matchers, {
# dotfiles and folders in the root
:root_dotfiles => proc { |file| file.match(%r{^\.}) },
@ -38,7 +38,7 @@ module Middleman
:layout => proc { |file|
file.match(%r{^source/layout\.}) || file.match(%r{^source/layouts/})
}
}
}, 'Callbacks that can exclude paths from the sitemap'
# Include instance methods
app.send :include, InstanceMethods

View file

@ -7,9 +7,6 @@ module MyExtension
# Called when user `activate`s your extension
def registered(app, options={})
# Setup extension-specific config
app.set :config_variable, false
# Include class methods
# app.extend ClassMethods
@ -18,9 +15,6 @@ module MyExtension
app.after_configuration do
# Do something
# config_variable is now either the default or the user's
# setting from config.rb
end
end
alias :included :registered

View file

@ -33,7 +33,7 @@ module Middleman
if resource = sitemap.find_resource_by_path(path)
resource.url
else
File.join(http_prefix, path)
File.join(config[:http_prefix], path)
end
end
end

View file

@ -19,38 +19,38 @@ module Middleman
# Location of SASS/SCSS files external to source directory.
# @return [Array]
# set :sass_assets_paths, ["#{root}/assets/sass/", "/path/2/external/sass/repository/"]
app.set :sass_assets_paths, []
# config[:sass_assets_paths] = ["#{root}/assets/sass/", "/path/2/external/sass/repository/"]
app.config.define_setting :sass_assets_paths, [], 'Paths to extra SASS/SCSS files'
app.after_configuration do
::Compass.configuration do |config|
config.project_path = source_dir
config.environment = :development
config.cache_path = sass_cache_path
config.sass_dir = css_dir
config.additional_import_paths = sass_assets_paths
config.css_dir = css_dir
config.javascripts_dir = js_dir
config.fonts_dir = fonts_dir
config.images_dir = images_dir
config.http_path = http_prefix
::Compass.configuration do |compass_config|
compass_config.project_path = source_dir
compass_config.environment = :development
compass_config.cache_path = config[:sass_cache_path]
compass_config.sass_dir = config[:css_dir]
compass_config.additional_import_paths = config[:sass_assets_paths]
compass_config.css_dir = config[:css_dir]
compass_config.javascripts_dir = config[:js_dir]
compass_config.fonts_dir = config[:fonts_dir]
compass_config.images_dir = config[:images_dir]
compass_config.http_path = config[:http_prefix]
# Disable this initially, the cache_buster extension will
# re-enable it if requested.
config.asset_cache_buster :none
compass_config.asset_cache_buster :none
# Disable this initially, the relative_assets extension will
# re-enable it if requested.
config.relative_assets = false
compass_config.relative_assets = false
# Default output style
config.output_style = :nested
compass_config.output_style = :nested
# No line-comments in test mode (changing paths mess with sha1)
config.line_comments = false if ENV["TEST"]
compass_config.line_comments = false if ENV["TEST"]
if respond_to?(:asset_host) && asset_host.is_a?(Proc)
config.asset_host(&asset_host)
if config.defines_setting?(:asset_host) && config[:asset_host].is_a?(Proc)
compass_config.asset_host(&config[:asset_host])
end
end

View file

@ -20,7 +20,7 @@ module Middleman
app.helpers Helpers
app.set :relative_links, false
app.config.define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones'
end
alias :included :registered
end
@ -110,7 +110,7 @@ module Middleman
# :relative option which, if set to true, will produce
# relative URLs instead of absolute URLs. You can also add
#
# set :relative_links, true
# config[:relative_links] = true
#
# to config.rb to have all links default to relative.
def link_to(*args, &block)
@ -136,10 +136,10 @@ module Middleman
resource = sitemap.find_resource_by_path(url)
# Allow people to turn on relative paths for all links with set :relative_links, true
# Allow people to turn on relative paths for all links with config[:relative_links] = true
# but still override on a case by case basis with the :relative parameter.
effective_relative = relative || false
if relative.nil? && relative_links
if relative.nil? && config[:relative_links]
effective_relative = true
end

View file

@ -9,11 +9,11 @@ module Middleman
# Once registerd
def registered(app, options={})
app.set :locales_dir, "locales"
app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations'
# Needed for helpers as well
app.after_configuration do
locales_glob = File.join(locales_dir, "*.yml");
locales_glob = File.join(config[:locales_dir], "*.yml");
::I18n.load_path += Dir[File.join(root, locales_glob)]
::I18n.reload!

View file

@ -11,7 +11,7 @@ module Middleman
# Once registered
def registered(app)
# Default to no host
app.set :asset_host, false
app.config.define_setting :asset_host, false, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
# Include methods
app.send :include, InstanceMethods
@ -30,12 +30,12 @@ module Middleman
# @return [String]
def asset_url(path, prefix="")
original_output = super
return original_output unless asset_host
return original_output unless config[:asset_host]
asset_prefix = if asset_host.is_a?(Proc)
asset_host.call(original_output)
elsif asset_host.is_a?(String)
asset_host
asset_prefix = if config[:asset_host].is_a?(Proc)
config[:asset_host].call(original_output)
elsif config[:asset_host].is_a?(String)
config[:asset_host]
end
File.join(asset_prefix, original_output)

View file

@ -10,13 +10,13 @@ module Middleman
# Once registered
def registered(app, options={})
app.set :css_compressor, false
app.config.define_setting :css_compressor, nil, 'Set the CSS compressor to use. Deprecated in favor of the :compressor option when activating :minify_css'
ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false
app.after_configuration do
chosen_compressor = css_compressor || options[:compressor] || begin
chosen_compressor = config[:css_compressor] || options[:compressor] || begin
require "middleman-more/extensions/minify_css/rainpress"
::Rainpress
end

View file

@ -10,7 +10,7 @@ module Middleman
# Once registered
def registered(app, options={})
app.set :js_compressor, false
app.config.define_setting :js_compressor, nil, 'Set the JS compressor to use. Deprecated in favor of the :compressor option when activating :minify_js'
ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false