diff --git a/CHANGELOG.md b/CHANGELOG.md index b15d736e..29f8b213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Master === +* Extract load_paths so they aren't locked into the binary +* Add middleman/rack for better config.ru support * Use centralized Logger and add benchmark methods 3.0.0.rc.3 diff --git a/middleman-core/bin/middleman b/middleman-core/bin/middleman index 5b67ff9f..e7ccc0d5 100755 --- a/middleman-core/bin/middleman +++ b/middleman-core/bin/middleman @@ -7,64 +7,13 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) moredir = File.expand_path(File.join(File.dirname(File.dirname(libdir)), "middleman-more", "lib", "middleman-more")) $LOAD_PATH.unshift(moredir) unless $LOAD_PATH.include?(moredir) -# Core Pathname library used for traversal -require "pathname" - -# Recursive method to find config.rb -def locate_root(cwd = Pathname.new(Dir.pwd)) - return cwd.to_s if (cwd + 'config.rb').exist? - return false if cwd.root? - locate_root(cwd.parent) -end - -# Only look for config.rb if MM_ROOT isn't set -if !ENV["MM_ROOT"] && found_path = locate_root - ENV["MM_ROOT"] = found_path -end - -is_bundler_setup = false - -# If we've found the root, try to setup Bundler -if ENV["MM_ROOT"] - - root_gemfile = File.expand_path('Gemfile', ENV["MM_ROOT"]) - ENV['BUNDLE_GEMFILE'] ||= root_gemfile - - if !File.exists?(ENV['BUNDLE_GEMFILE']) - git_gemfile = Pathname.new(__FILE__).expand_path.parent.parent.parent + "Gemfile" - ENV['BUNDLE_GEMFILE'] = git_gemfile.to_s - end - - if File.exists?(ENV['BUNDLE_GEMFILE']) - is_bundler_setup = true - require 'bundler/setup' - end -end - -# Automatically discover extensions in RubyGems -require "middleman-core/extensions" - -if is_bundler_setup - Bundler.require -else - ::Middleman.load_extensions_in_path -end +require "middleman-core/load_paths" +Middleman.setup_load_paths require "middleman-core/cli" -# Change flag to a module -ARGV.unshift("help") if ARGV.delete("--help") - -# Default command is server -if ARGV[0] != "help" && (ARGV.length < 1 || ARGV.first.include?("-")) - ARGV.unshift("server") -end +# Change directory to the root +Dir.chdir(ENV["MM_ROOT"]) if ENV["MM_ROOT"] # Start the CLI -if ENV["MM_ROOT"] - # Change directory to the root - Dir.chdir(ENV["MM_ROOT"]) - Middleman::Cli::Base.start -else - Middleman::Cli::Base.start -end +Middleman::Cli::Base.start \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index cd35f07e..c33f6681 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -26,6 +26,7 @@ module Middleman define_hook :ready class << self + # Mix-in helper methods. Accepts either a list of Modules # and/or a block to be evaluated # @return [void] diff --git a/middleman-core/lib/middleman-core/cli.rb b/middleman-core/lib/middleman-core/cli.rb index 56cc6927..284df439 100644 --- a/middleman-core/lib/middleman-core/cli.rb +++ b/middleman-core/lib/middleman-core/cli.rb @@ -10,6 +10,20 @@ module Middleman # The base task from which everything else etends class Base < Thor + class << self + def start(*args) + # Change flag to a module + ARGV.unshift("help") if ARGV.delete("--help") + + # Default command is server + if ARGV[0] != "help" && (ARGV.length < 1 || ARGV.first.include?("-")) + ARGV.unshift("server") + end + + super + end + end + desc "version", "Show version" def version require 'middleman-core/version' diff --git a/middleman-core/lib/middleman-core/core_extensions/request.rb b/middleman-core/lib/middleman-core/core_extensions/request.rb index de654a9f..c3491327 100644 --- a/middleman-core/lib/middleman-core/core_extensions/request.rb +++ b/middleman-core/lib/middleman-core/core_extensions/request.rb @@ -145,7 +145,7 @@ module Middleman # # @return [Class] def server(&block) - ::Middleman::Application.server(&block) + ::Middleman::Application.server end end diff --git a/middleman-core/lib/middleman-core/load_paths.rb b/middleman-core/lib/middleman-core/load_paths.rb new file mode 100644 index 00000000..89e8260e --- /dev/null +++ b/middleman-core/lib/middleman-core/load_paths.rb @@ -0,0 +1,56 @@ +# Core Pathname library used for traversal +require "pathname" + +module Middleman + + class << self + def setup_load_paths + @_is_setup ||= begin + + # Only look for config.rb if MM_ROOT isn't set + if !ENV["MM_ROOT"] && found_path = locate_root + ENV["MM_ROOT"] = found_path + end + + is_bundler_setup = false + + # If we've found the root, try to setup Bundler + if ENV["MM_ROOT"] + + root_gemfile = File.expand_path('Gemfile', ENV["MM_ROOT"]) + ENV['BUNDLE_GEMFILE'] ||= root_gemfile + + if !File.exists?(ENV['BUNDLE_GEMFILE']) + git_gemfile = Pathname.new(__FILE__).expand_path.parent.parent.parent + "Gemfile" + ENV['BUNDLE_GEMFILE'] = git_gemfile.to_s + end + + if File.exists?(ENV['BUNDLE_GEMFILE']) + is_bundler_setup = true + require 'bundler/setup' + end + end + + # Automatically discover extensions in RubyGems + require "middleman-core/extensions" + + if is_bundler_setup + Bundler.require + else + ::Middleman.load_extensions_in_path + end + + true + end + end + + # Recursive method to find config.rb + def locate_root(cwd = Pathname.new(Dir.pwd)) + return cwd.to_s if (cwd + 'config.rb').exist? + return false if cwd.root? + locate_root(cwd.parent) + end + + end + +end \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/templates/shared/config.ru b/middleman-core/lib/middleman-core/templates/shared/config.ru index 21f98c27..e091b492 100644 --- a/middleman-core/lib/middleman-core/templates/shared/config.ru +++ b/middleman-core/lib/middleman-core/templates/shared/config.ru @@ -1,4 +1,4 @@ require 'rubygems' -require 'middleman' +require 'middleman/rack' run Middleman.server \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 2b035c7d..2163df18 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -11,6 +11,7 @@ require "thor" require "pathname" module Middleman + module Util # The logger diff --git a/middleman-core/lib/middleman/rack.rb b/middleman-core/lib/middleman/rack.rb new file mode 100644 index 00000000..8dde64bd --- /dev/null +++ b/middleman-core/lib/middleman/rack.rb @@ -0,0 +1,4 @@ +require "middleman-core/load_paths" +::Middleman.setup_load_paths + +require "middleman-core" \ No newline at end of file