Merge pull request #541 from middleman/load_paths

Extract load_paths so they aren't locked into the binary
This commit is contained in:
Thomas Reynolds 2012-07-19 18:22:04 -07:00
commit 3f21f7bc62
9 changed files with 85 additions and 58 deletions

View file

@ -1,6 +1,8 @@
Master 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 * Use centralized Logger and add benchmark methods
3.0.0.rc.3 3.0.0.rc.3

View file

@ -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")) 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) $LOAD_PATH.unshift(moredir) unless $LOAD_PATH.include?(moredir)
# Core Pathname library used for traversal require "middleman-core/load_paths"
require "pathname" Middleman.setup_load_paths
# 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/cli" require "middleman-core/cli"
# Change flag to a module # Change directory to the root
ARGV.unshift("help") if ARGV.delete("--help") Dir.chdir(ENV["MM_ROOT"]) if ENV["MM_ROOT"]
# Default command is server
if ARGV[0] != "help" && (ARGV.length < 1 || ARGV.first.include?("-"))
ARGV.unshift("server")
end
# Start the CLI # Start the CLI
if ENV["MM_ROOT"] Middleman::Cli::Base.start
# Change directory to the root
Dir.chdir(ENV["MM_ROOT"])
Middleman::Cli::Base.start
else
Middleman::Cli::Base.start
end

View file

@ -26,6 +26,7 @@ module Middleman
define_hook :ready define_hook :ready
class << self 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]

View file

@ -10,6 +10,20 @@ module Middleman
# The base task from which everything else etends # The base task from which everything else etends
class Base < Thor 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" desc "version", "Show version"
def version def version
require 'middleman-core/version' require 'middleman-core/version'

View file

@ -145,7 +145,7 @@ module Middleman
# #
# @return [Class] # @return [Class]
def server(&block) def server(&block)
::Middleman::Application.server(&block) ::Middleman::Application.server
end end
end end

View file

@ -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

View file

@ -1,4 +1,4 @@
require 'rubygems' require 'rubygems'
require 'middleman' require 'middleman/rack'
run Middleman.server run Middleman.server

View file

@ -11,6 +11,7 @@ require "thor"
require "pathname" require "pathname"
module Middleman module Middleman
module Util module Util
# The logger # The logger

View file

@ -0,0 +1,4 @@
require "middleman-core/load_paths"
::Middleman.setup_load_paths
require "middleman-core"