From 4fe22e3fb561bfc0a14d0dd05bd5cb8c38246a28 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 13 Oct 2012 22:37:24 -0700 Subject: [PATCH] Move more things over to new configs. Don't let root be configured --- .../lib/middleman-core/application.rb | 33 +++++++------------ .../lib/middleman-core/cli/build.rb | 2 +- .../core_extensions/external_helpers.rb | 2 +- .../core_extensions/file_watcher.rb | 2 +- .../core_extensions/front_matter.rb | 2 +- .../core_extensions/rendering.rb | 2 +- .../middleman-core/core_extensions/routing.rb | 6 ++-- .../core_extensions/ruby_encoding.rb | 8 +++-- .../core_extensions/show_exceptions.rb | 6 +++- .../lib/middleman-core/renderers/less.rb | 2 +- .../sitemap/extensions/on_disk.rb | 4 +-- .../lib/middleman-core/sitemap/store.rb | 6 ++-- .../step_definitions/builder_steps.rb | 3 ++ .../step_definitions/server_steps.rb | 3 +- 14 files changed, 42 insertions(+), 39 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index ea98a17d..38ba9ed2 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -29,26 +29,25 @@ module Middleman # Ready (all loading and parsing of extensions complete) hook define_hook :ready - class << self - - # Mix-in helper methods. Accepts either a list of Modules - # and/or a block to be evaluated - # @return [void] - def helpers(*extensions, &block) - class_eval(&block) if block_given? - include(*extensions) if extensions.any? - end + # Mix-in helper methods. Accepts either a list of Modules + # and/or a block to be evaluated + # @return [void] + def self.helpers(*extensions, &block) + class_eval(&block) if block_given? + include(*extensions) if extensions.any? end - delegate :helpers, :to => :"self.class" # Root project directory (overwritten in middleman build/server) # @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 def self.root_path - Pathname(config[:root]) + Pathname(root) end delegate :root_path, :to => :"self.class" @@ -96,14 +95,6 @@ module Middleman # @return [String] 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 # @return [String, Symbold] config.define_setting :layout, :_auto_layout, 'Default layout name' @@ -182,7 +173,7 @@ module Middleman # # @return [String] def source_dir - File.join(config[:root], config[:source]) + File.join(root, config[:source]) end delegate :logger, :instrument, :to => ::Middleman::Util diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 861c6458..6b9dcb5a 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -114,7 +114,7 @@ module Middleman::Cli # @param [Middleman::Sitemap::Resource] resource # @return [String] The full path of the file that was written 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) begin diff --git a/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb b/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb index 3e4716b0..6907740b 100644 --- a/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb +++ b/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb @@ -18,7 +18,7 @@ module Middleman # After config 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) Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename| diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index 1883b57b..a55471d9 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -29,7 +29,7 @@ module Middleman # Before parsing config, load the data/ directory app.before_configuration do - files.reload_path(data_dir) + files.reload_path(config[:data_dir]) end # After config, load everything else diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 9f55c60e..6fd3047d 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -62,7 +62,7 @@ module Middleman::CoreExtensions def clear_data(file) # Copied from Sitemap::Store#file_to_path, but without # removing the file extension - file = File.expand_path(file, @app.root) + file = File.join(@app.root, file) prefix = @app.source_dir.sub(/\/$/, "") + "/" return unless file.include?(prefix) path = file.sub(prefix, "") diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 3e040564..2836a105 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -272,7 +272,7 @@ module Middleman # @return [String] def fetch_layout(engine, opts) # 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 # Look for engine-specific options diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index 0416fec1..7b427acb 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -28,7 +28,7 @@ module Middleman # @param [String, Symbol] layout_name # @return [void] def with_layout(layout_name, &block) - old_layout = layout + old_layout = config[:layout] config[:layout] = layout_name instance_exec(&block) if block_given? @@ -50,7 +50,7 @@ module Middleman blocks << block if block_given? # Default layout - opts[:layout] = layout if opts[:layout].nil? + opts[:layout] = config[:layout] if opts[:layout].nil? # If the url is a regexp if url.is_a?(Regexp) || url.include?("*") @@ -66,7 +66,7 @@ module Middleman # Normalized path url = '/' + Middleman::Util.normalize_path(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 # Setup proxy diff --git a/middleman-core/lib/middleman-core/core_extensions/ruby_encoding.rb b/middleman-core/lib/middleman-core/core_extensions/ruby_encoding.rb index e9168386..3ff70b13 100644 --- a/middleman-core/lib/middleman-core/core_extensions/ruby_encoding.rb +++ b/middleman-core/lib/middleman-core/core_extensions/ruby_encoding.rb @@ -6,6 +6,10 @@ module Middleman::CoreExtensions::RubyEncoding # Once registerd 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 end @@ -15,8 +19,8 @@ module Middleman::CoreExtensions::RubyEncoding module InstanceMethods def initialize if Object.const_defined?(:Encoding) - Encoding.default_internal = encoding - Encoding.default_external = encoding + Encoding.default_internal = config[:encoding] + Encoding.default_external = config[:encoding] end super diff --git a/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb b/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb index 9dd27fb8..8c34612a 100644 --- a/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb @@ -11,10 +11,14 @@ module Middleman # Once registered 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 app.configure :development do # Include middlemare - if show_exceptions + if config[:show_exceptions] use ::Middleman::CoreExtensions::ShowExceptions::Middleware end end diff --git a/middleman-core/lib/middleman-core/renderers/less.rb b/middleman-core/lib/middleman-core/renderers/less.rb index 5f789980..6784e54c 100644 --- a/middleman-core/lib/middleman-core/renderers/less.rb +++ b/middleman-core/lib/middleman-core/renderers/less.rb @@ -19,7 +19,7 @@ module Middleman end app.after_configuration do - ::Less.paths << File.expand_path(css_dir, source_dir) + ::Less.paths << File.join(source_dir, config[:css_dir]) end # Tell Tilt to use it as well (for inline sass blocks) diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb index 52600ed7..d311973d 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb @@ -45,7 +45,7 @@ module Middleman path = @sitemap.file_to_path(file) 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) end @@ -85,7 +85,7 @@ module Middleman ::Middleman::Sitemap::Resource.new( @sitemap, @sitemap.file_to_path(file), - File.expand_path(file, @app.root) + File.join(@app.root, file) ) end end diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index 0b4a1f71..cf3779b2 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -164,7 +164,7 @@ module Middleman # @param [String] file # @return [String] def file_to_path(file) - file = File.expand_path(file, @app.root) + file = File.join(@app.root, file) prefix = @app.source_dir.sub(/\/$/, "") + "/" return false unless file.start_with?(prefix) @@ -172,8 +172,8 @@ module Middleman path = file.sub(prefix, "") # Replace a file name containing automatic_directory_matcher with a folder - unless @app.automatic_directory_matcher.nil? - path = path.gsub(@app.automatic_directory_matcher, "/") + unless @app.config[:automatic_directory_matcher].nil? + path = path.gsub(@app.config[:automatic_directory_matcher], "/") end extensionless_path(path) diff --git a/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb b/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb index be1bb082..e119568d 100644 --- a/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb @@ -10,9 +10,12 @@ end Given /^an empty app$/ do step %Q{a directory named "empty_app"} step %Q{I cd to "empty_app"} + ENV["MM_ROOT"] = nil end Given /^a fixture app "([^\"]*)"$/ do |path| + ENV["MM_ROOT"] = nil + # 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 next if File.basename(current_dir) == path diff --git a/middleman-core/lib/middleman-core/step_definitions/server_steps.rb b/middleman-core/lib/middleman-core/step_definitions/server_steps.rb index 8a0997f5..ac0a7c3c 100644 --- a/middleman-core/lib/middleman-core/step_definitions/server_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/server_steps.rb @@ -40,9 +40,10 @@ Given /^the Server is running$/ do ENV["MM_SOURCE"] = "" end + ENV["MM_ROOT"] = root_dir + initialize_commands = @initialize_commands || [] initialize_commands.unshift lambda { - set :root, root_dir set :environment, @current_env || :development set :show_exceptions, false }