Move more things over to new configs. Don't let root be configured

This commit is contained in:
Ben Hollis 2012-10-13 22:37:24 -07:00
parent a50ca56fbc
commit 4fe22e3fb5
14 changed files with 42 additions and 39 deletions

View file

@ -29,26 +29,25 @@ module Middleman
# Ready (all loading and parsing of extensions complete) hook # Ready (all loading and parsing of extensions complete) hook
define_hook :ready define_hook :ready
class << self
# Mix-in helper methods. Accepts either a list of Modules # Mix-in helper methods. Accepts either a list of Modules
# and/or a block to be evaluated # and/or a block to be evaluated
# @return [void] # @return [void]
def helpers(*extensions, &block) def self.helpers(*extensions, &block)
class_eval(&block) if block_given? class_eval(&block) if block_given?
include(*extensions) if extensions.any? include(*extensions) if extensions.any?
end end
end
delegate :helpers, :to => :"self.class" delegate :helpers, :to => :"self.class"
# Root project directory (overwritten in middleman build/server) # Root project directory (overwritten in middleman build/server)
# @return [String] # @return [String]
config.define_setting :root, (ENV["MM_ROOT"] || Dir.pwd), 'Root project directory' def self.root
ENV["MM_ROOT"] || Dir.pwd
end
delegate :root, :to => :"self.class"
# Pathname-addressed root # Pathname-addressed root
def self.root_path def self.root_path
Pathname(config[:root]) Pathname(root)
end end
delegate :root_path, :to => :"self.class" delegate :root_path, :to => :"self.class"
@ -96,14 +95,6 @@ module Middleman
# @return [String] # @return [String]
config.define_setting :http_prefix, "/", 'Default prefix for building paths' config.define_setting :http_prefix, "/", 'Default prefix for building paths'
# Default string encoding for templates and output.
# @return [String]
config.define_setting :encoding, "utf-8", 'Default string encoding for templates and output'
# Whether to catch and display exceptions
# @return [Boolean]
config.define_setting :show_exceptions, true, 'Whether to catch and display exceptions'
# Default layout name # Default layout name
# @return [String, Symbold] # @return [String, Symbold]
config.define_setting :layout, :_auto_layout, 'Default layout name' config.define_setting :layout, :_auto_layout, 'Default layout name'
@ -182,7 +173,7 @@ module Middleman
# #
# @return [String] # @return [String]
def source_dir def source_dir
File.join(config[:root], config[:source]) File.join(root, config[:source])
end end
delegate :logger, :instrument, :to => ::Middleman::Util delegate :logger, :instrument, :to => ::Middleman::Util

View file

@ -114,7 +114,7 @@ module Middleman::Cli
# @param [Middleman::Sitemap::Resource] resource # @param [Middleman::Sitemap::Resource] resource
# @return [String] The full path of the file that was written # @return [String] The full path of the file that was written
def render_to_file(resource) def render_to_file(resource)
build_dir = self.class.shared_instance.build_dir build_dir = self.class.shared_instance.config[:build_dir]
output_file = File.join(build_dir, resource.destination_path) output_file = File.join(build_dir, resource.destination_path)
begin begin

View file

@ -18,7 +18,7 @@ module Middleman
# After config # After config
app.after_configuration do app.after_configuration do
helpers_path = File.expand_path(config[:helpers_dir], config[:root]) helpers_path = File.join(root, config[:helpers_dir])
next unless File.exists?(helpers_path) next unless File.exists?(helpers_path)
Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename| Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename|

View file

@ -29,7 +29,7 @@ module Middleman
# Before parsing config, load the data/ directory # Before parsing config, load the data/ directory
app.before_configuration do app.before_configuration do
files.reload_path(data_dir) files.reload_path(config[:data_dir])
end end
# After config, load everything else # After config, load everything else

View file

@ -62,7 +62,7 @@ module Middleman::CoreExtensions
def clear_data(file) def clear_data(file)
# Copied from Sitemap::Store#file_to_path, but without # Copied from Sitemap::Store#file_to_path, but without
# removing the file extension # removing the file extension
file = File.expand_path(file, @app.root) file = File.join(@app.root, file)
prefix = @app.source_dir.sub(/\/$/, "") + "/" prefix = @app.source_dir.sub(/\/$/, "") + "/"
return unless file.include?(prefix) return unless file.include?(prefix)
path = file.sub(prefix, "") path = file.sub(prefix, "")

View file

@ -272,7 +272,7 @@ module Middleman
# @return [String] # @return [String]
def fetch_layout(engine, opts) def fetch_layout(engine, opts)
# The layout name comes from either the system default or the options # The layout name comes from either the system default or the options
local_layout = opts.has_key?(:layout) ? opts[:layout] : layout local_layout = opts.has_key?(:layout) ? opts[:layout] : config[:layout]
return false unless local_layout return false unless local_layout
# Look for engine-specific options # Look for engine-specific options

View file

@ -28,7 +28,7 @@ module Middleman
# @param [String, Symbol] layout_name # @param [String, Symbol] layout_name
# @return [void] # @return [void]
def with_layout(layout_name, &block) def with_layout(layout_name, &block)
old_layout = layout old_layout = config[:layout]
config[:layout] = layout_name config[:layout] = layout_name
instance_exec(&block) if block_given? instance_exec(&block) if block_given?
@ -50,7 +50,7 @@ module Middleman
blocks << block if block_given? blocks << block if block_given?
# Default layout # Default layout
opts[:layout] = layout if opts[:layout].nil? opts[:layout] = config[:layout] if opts[:layout].nil?
# If the url is a regexp # If the url is a regexp
if url.is_a?(Regexp) || url.include?("*") if url.is_a?(Regexp) || url.include?("*")
@ -66,7 +66,7 @@ module Middleman
# Normalized path # Normalized path
url = '/' + Middleman::Util.normalize_path(url) url = '/' + Middleman::Util.normalize_path(url)
if url.end_with?('/') || File.directory?(File.join(source_dir, url)) if url.end_with?('/') || File.directory?(File.join(source_dir, url))
url = File.join(url, index_file) url = File.join(url, config[:index_file])
end end
# Setup proxy # Setup proxy

View file

@ -6,6 +6,10 @@ module Middleman::CoreExtensions::RubyEncoding
# Once registerd # Once registerd
def registered(app) def registered(app)
# Default string encoding for templates and output.
# @return [String]
app.config.define_setting :encoding, "utf-8", 'Default string encoding for templates and output'
app.send :include, InstanceMethods app.send :include, InstanceMethods
end end
@ -15,8 +19,8 @@ module Middleman::CoreExtensions::RubyEncoding
module InstanceMethods module InstanceMethods
def initialize def initialize
if Object.const_defined?(:Encoding) if Object.const_defined?(:Encoding)
Encoding.default_internal = encoding Encoding.default_internal = config[:encoding]
Encoding.default_external = encoding Encoding.default_external = config[:encoding]
end end
super super

View file

@ -11,10 +11,14 @@ module Middleman
# Once registered # Once registered
def registered(app) def registered(app)
# Whether to catch and display exceptions
# @return [Boolean]
app.config.define_setting :show_exceptions, true, 'Whether to catch and display exceptions'
# When in dev # When in dev
app.configure :development do app.configure :development do
# Include middlemare # Include middlemare
if show_exceptions if config[:show_exceptions]
use ::Middleman::CoreExtensions::ShowExceptions::Middleware use ::Middleman::CoreExtensions::ShowExceptions::Middleware
end end
end end

View file

@ -19,7 +19,7 @@ module Middleman
end end
app.after_configuration do app.after_configuration do
::Less.paths << File.expand_path(css_dir, source_dir) ::Less.paths << File.join(source_dir, config[:css_dir])
end end
# Tell Tilt to use it as well (for inline sass blocks) # Tell Tilt to use it as well (for inline sass blocks)

View file

@ -45,7 +45,7 @@ module Middleman
path = @sitemap.file_to_path(file) path = @sitemap.file_to_path(file)
return false unless path return false unless path
ignored = @app.ignored_sitemap_matchers.any? do |name, callback| ignored = @app.config[:ignored_sitemap_matchers].any? do |name, callback|
callback.call(file) callback.call(file)
end end
@ -85,7 +85,7 @@ module Middleman
::Middleman::Sitemap::Resource.new( ::Middleman::Sitemap::Resource.new(
@sitemap, @sitemap,
@sitemap.file_to_path(file), @sitemap.file_to_path(file),
File.expand_path(file, @app.root) File.join(@app.root, file)
) )
end end
end end

View file

@ -164,7 +164,7 @@ module Middleman
# @param [String] file # @param [String] file
# @return [String] # @return [String]
def file_to_path(file) def file_to_path(file)
file = File.expand_path(file, @app.root) file = File.join(@app.root, file)
prefix = @app.source_dir.sub(/\/$/, "") + "/" prefix = @app.source_dir.sub(/\/$/, "") + "/"
return false unless file.start_with?(prefix) return false unless file.start_with?(prefix)
@ -172,8 +172,8 @@ module Middleman
path = file.sub(prefix, "") path = file.sub(prefix, "")
# Replace a file name containing automatic_directory_matcher with a folder # Replace a file name containing automatic_directory_matcher with a folder
unless @app.automatic_directory_matcher.nil? unless @app.config[:automatic_directory_matcher].nil?
path = path.gsub(@app.automatic_directory_matcher, "/") path = path.gsub(@app.config[:automatic_directory_matcher], "/")
end end
extensionless_path(path) extensionless_path(path)

View file

@ -10,9 +10,12 @@ end
Given /^an empty app$/ do Given /^an empty app$/ do
step %Q{a directory named "empty_app"} step %Q{a directory named "empty_app"}
step %Q{I cd to "empty_app"} step %Q{I cd to "empty_app"}
ENV["MM_ROOT"] = nil
end end
Given /^a fixture app "([^\"]*)"$/ do |path| Given /^a fixture app "([^\"]*)"$/ do |path|
ENV["MM_ROOT"] = nil
# This step can be reentered from several places but we don't want # This step can be reentered from several places but we don't want
# to keep re-copying and re-cd-ing into ever-deeper directories # to keep re-copying and re-cd-ing into ever-deeper directories
next if File.basename(current_dir) == path next if File.basename(current_dir) == path

View file

@ -40,9 +40,10 @@ Given /^the Server is running$/ do
ENV["MM_SOURCE"] = "" ENV["MM_SOURCE"] = ""
end end
ENV["MM_ROOT"] = root_dir
initialize_commands = @initialize_commands || [] initialize_commands = @initialize_commands || []
initialize_commands.unshift lambda { initialize_commands.unshift lambda {
set :root, root_dir
set :environment, @current_env || :development set :environment, @current_env || :development
set :show_exceptions, false set :show_exceptions, false
} }