Re-arrange CLI loads for hopefully faster startup time

This commit is contained in:
Thomas Reynolds 2012-05-24 11:03:58 -07:00
parent fffa80a987
commit 94c5f32853
5 changed files with 98 additions and 96 deletions

View file

@ -4,21 +4,13 @@
libdir = File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), "lib"))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
# Setup RubyGems
require "rubygems"
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_middleman_root!(cwd = Pathname.new(Dir.pwd))
return cwd.to_s if File.exists?(File.join(cwd, 'config.rb'))
return false if cwd.root?
locate_middleman_root!(cwd.parent)
end
require "middleman-core/cli"
# Only look for config.rb if MM_ROOT isn't set
if !ENV["MM_ROOT"] && found_path = locate_middleman_root!
if !ENV["MM_ROOT"] && found_path = ::Middleman.locate_root
ENV["MM_ROOT"] = found_path
end
@ -30,30 +22,19 @@ if ENV["MM_ROOT"]
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
end
# Automatically discover extensions in RubyGems
require "middleman-core/extensions"
::Middleman.load_extensions_in_path
# Default command is server
ARGV.unshift("server") if ARGV.length < 1 || ARGV.first.include?("-")
begin
# Local
require File.expand_path(File.join(File.dirname(File.dirname(libdir)), "middleman-more", "lib", "middleman-more"))
rescue LoadError
begin
# Rubygems
require "middleman-more"
rescue LoadError
# Require Middleman
require 'middleman-core'
# Start the CLI
if ENV["MM_ROOT"]
# Change directory to the root
Dir.chdir(ENV["MM_ROOT"]) do
Middleman::Cli::Base.start
end
end
# Automatically discover extensions in RubyGems
Middleman.load_extensions_in_path
require "middleman-core/cli"
# Change directory to the root
Dir.chdir(ENV["MM_ROOT"] || Dir.pwd) do
# Start the CLI
else
Middleman::Cli::Base.start
end

View file

@ -2,8 +2,20 @@
require 'thor'
require "thor/group"
# Core Pathname library used for traversal
require "pathname"
# CLI Module
module Middleman::Cli
module Middleman
# Recursive method to find config.rb
def self.locate_root(cwd = Pathname.new(Dir.pwd))
return cwd.to_s if File.exists?(File.join(cwd, 'config.rb'))
return false if cwd.root?
locate_root(cwd.parent)
end
module Cli
# The base task from which everything else etends
class Base < Thor
@ -63,6 +75,7 @@ module Middleman::Cli
end
end
end
end
end
# Include the core CLI items

View file

@ -1,9 +1,3 @@
# Use Rack::Test for inspecting a running server for output
require "rack"
require "rack/test"
require 'find'
# CLI Module
module Middleman::Cli
@ -38,6 +32,12 @@ module Middleman::Cli
raise Thor::Error, "Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
end
# Use Rack::Test for inspecting a running server for output
require "rack"
require "rack/test"
require 'find'
self.class.shared_instance(options["verbose"] || false)
self.class.shared_rack

View file

@ -1,5 +1,3 @@
require "middleman-core/preview_server"
# CLI Module
module Middleman::Cli
@ -34,6 +32,9 @@ module Middleman::Cli
# Start the server
def server
require "middleman-core"
require "middleman-core/preview_server"
if !ENV["MM_ROOT"]
puts "== Could not find a Middleman project config.rb"
puts "== Treating directory as a static site to be served"
@ -50,7 +51,7 @@ module Middleman::Cli
}
puts "== The Middleman is loading"
Middleman::PreviewServer.start(params)
::Middleman::PreviewServer.start(params)
end
end

View file

@ -78,6 +78,13 @@ module Middleman
if defined?(Bundler)
Bundler.require
else
require "rubygems"
begin
require "middleman-more"
rescue LoadError
end
extensions = rubygems_latest_specs.select do |spec|
spec_has_file?(spec, EXTENSION_FILE)
end