Re-arrange CLI loads for hopefully faster startup time
This commit is contained in:
parent
fffa80a987
commit
94c5f32853
5 changed files with 98 additions and 96 deletions
|
@ -4,21 +4,13 @@
|
||||||
libdir = File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), "lib"))
|
libdir = File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), "lib"))
|
||||||
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
||||||
|
|
||||||
# Setup RubyGems
|
moredir = File.expand_path(File.join(File.dirname(File.dirname(libdir)), "middleman-more", "lib", "middleman-more"))
|
||||||
require "rubygems"
|
$LOAD_PATH.unshift(moredir) unless $LOAD_PATH.include?(moredir)
|
||||||
|
|
||||||
# Core Pathname library used for traversal
|
require "middleman-core/cli"
|
||||||
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
|
|
||||||
|
|
||||||
# Only look for config.rb if MM_ROOT isn't set
|
# 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
|
ENV["MM_ROOT"] = found_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -30,30 +22,19 @@ if ENV["MM_ROOT"]
|
||||||
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
|
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Automatically discover extensions in RubyGems
|
||||||
|
require "middleman-core/extensions"
|
||||||
|
::Middleman.load_extensions_in_path
|
||||||
|
|
||||||
# Default command is server
|
# Default command is server
|
||||||
ARGV.unshift("server") if ARGV.length < 1 || ARGV.first.include?("-")
|
ARGV.unshift("server") if ARGV.length < 1 || ARGV.first.include?("-")
|
||||||
|
|
||||||
begin
|
# Start the CLI
|
||||||
# Local
|
if ENV["MM_ROOT"]
|
||||||
require File.expand_path(File.join(File.dirname(File.dirname(libdir)), "middleman-more", "lib", "middleman-more"))
|
# Change directory to the root
|
||||||
rescue LoadError
|
Dir.chdir(ENV["MM_ROOT"]) do
|
||||||
begin
|
Middleman::Cli::Base.start
|
||||||
# Rubygems
|
|
||||||
require "middleman-more"
|
|
||||||
rescue LoadError
|
|
||||||
# Require Middleman
|
|
||||||
require 'middleman-core'
|
|
||||||
end
|
end
|
||||||
end
|
else
|
||||||
|
|
||||||
# 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
|
|
||||||
Middleman::Cli::Base.start
|
Middleman::Cli::Base.start
|
||||||
end
|
end
|
|
@ -2,64 +2,77 @@
|
||||||
require 'thor'
|
require 'thor'
|
||||||
require "thor/group"
|
require "thor/group"
|
||||||
|
|
||||||
|
# Core Pathname library used for traversal
|
||||||
|
require "pathname"
|
||||||
|
|
||||||
# CLI Module
|
# CLI Module
|
||||||
module Middleman::Cli
|
module Middleman
|
||||||
|
|
||||||
# The base task from which everything else etends
|
# Recursive method to find config.rb
|
||||||
class Base < Thor
|
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
|
||||||
|
|
||||||
desc "version", "Show version"
|
module Cli
|
||||||
def version
|
|
||||||
require 'middleman-core/version'
|
|
||||||
say "Middleman #{Middleman::VERSION}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Override the Thor help method to find help for subtasks
|
# The base task from which everything else etends
|
||||||
# @param [Symbol, String, nil] meth
|
class Base < Thor
|
||||||
# @param [Boolean] subcommand
|
|
||||||
# @return [void]
|
|
||||||
def help(meth = nil, subcommand = false)
|
|
||||||
if meth && !self.respond_to?(meth)
|
|
||||||
klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
|
|
||||||
klass.start(["-h", task].compact, :shell => self.shell)
|
|
||||||
else
|
|
||||||
list = []
|
|
||||||
Thor::Util.thor_classes_in(Middleman::Cli).each do |klass|
|
|
||||||
list += klass.printable_tasks(false)
|
|
||||||
end
|
|
||||||
list.sort!{ |a,b| a[0] <=> b[0] }
|
|
||||||
|
|
||||||
shell.say "Tasks:"
|
desc "version", "Show version"
|
||||||
shell.print_table(list, :ident => 2, :truncate => true)
|
def version
|
||||||
shell.say
|
require 'middleman-core/version'
|
||||||
end
|
say "Middleman #{Middleman::VERSION}"
|
||||||
end
|
|
||||||
|
|
||||||
# Intercept missing methods and search subtasks for them
|
|
||||||
# @param [Symbol] meth
|
|
||||||
def method_missing(meth, *args)
|
|
||||||
meth = meth.to_s
|
|
||||||
|
|
||||||
if self.class.map.has_key?(meth)
|
|
||||||
meth = self.class.map[meth]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
|
# Override the Thor help method to find help for subtasks
|
||||||
|
# @param [Symbol, String, nil] meth
|
||||||
if klass.nil?
|
# @param [Boolean] subcommand
|
||||||
tasks_dir = File.join(Dir.pwd, "tasks")
|
# @return [void]
|
||||||
|
def help(meth = nil, subcommand = false)
|
||||||
if File.exists?(tasks_dir)
|
if meth && !self.respond_to?(meth)
|
||||||
Dir[File.join(tasks_dir, "**/*_task.rb")].each { |f| require f }
|
|
||||||
klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
|
klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
|
||||||
|
klass.start(["-h", task].compact, :shell => self.shell)
|
||||||
|
else
|
||||||
|
list = []
|
||||||
|
Thor::Util.thor_classes_in(Middleman::Cli).each do |klass|
|
||||||
|
list += klass.printable_tasks(false)
|
||||||
|
end
|
||||||
|
list.sort!{ |a,b| a[0] <=> b[0] }
|
||||||
|
|
||||||
|
shell.say "Tasks:"
|
||||||
|
shell.print_table(list, :ident => 2, :truncate => true)
|
||||||
|
shell.say
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if klass.nil?
|
# Intercept missing methods and search subtasks for them
|
||||||
super
|
# @param [Symbol] meth
|
||||||
else
|
def method_missing(meth, *args)
|
||||||
args.unshift(task) if task
|
meth = meth.to_s
|
||||||
klass.start(args, :shell => self.shell)
|
|
||||||
|
if self.class.map.has_key?(meth)
|
||||||
|
meth = self.class.map[meth]
|
||||||
|
end
|
||||||
|
|
||||||
|
klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
|
||||||
|
|
||||||
|
if klass.nil?
|
||||||
|
tasks_dir = File.join(Dir.pwd, "tasks")
|
||||||
|
|
||||||
|
if File.exists?(tasks_dir)
|
||||||
|
Dir[File.join(tasks_dir, "**/*_task.rb")].each { |f| require f }
|
||||||
|
klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if klass.nil?
|
||||||
|
super
|
||||||
|
else
|
||||||
|
args.unshift(task) if task
|
||||||
|
klass.start(args, :shell => self.shell)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,9 +1,3 @@
|
||||||
# Use Rack::Test for inspecting a running server for output
|
|
||||||
require "rack"
|
|
||||||
require "rack/test"
|
|
||||||
|
|
||||||
require 'find'
|
|
||||||
|
|
||||||
# CLI Module
|
# CLI Module
|
||||||
module Middleman::Cli
|
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?"
|
raise Thor::Error, "Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
|
||||||
end
|
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_instance(options["verbose"] || false)
|
||||||
|
|
||||||
self.class.shared_rack
|
self.class.shared_rack
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
require "middleman-core/preview_server"
|
|
||||||
|
|
||||||
# CLI Module
|
# CLI Module
|
||||||
module Middleman::Cli
|
module Middleman::Cli
|
||||||
|
|
||||||
|
@ -34,6 +32,9 @@ module Middleman::Cli
|
||||||
|
|
||||||
# Start the server
|
# Start the server
|
||||||
def server
|
def server
|
||||||
|
require "middleman-core"
|
||||||
|
require "middleman-core/preview_server"
|
||||||
|
|
||||||
if !ENV["MM_ROOT"]
|
if !ENV["MM_ROOT"]
|
||||||
puts "== Could not find a Middleman project config.rb"
|
puts "== Could not find a Middleman project config.rb"
|
||||||
puts "== Treating directory as a static site to be served"
|
puts "== Treating directory as a static site to be served"
|
||||||
|
@ -50,7 +51,7 @@ module Middleman::Cli
|
||||||
}
|
}
|
||||||
|
|
||||||
puts "== The Middleman is loading"
|
puts "== The Middleman is loading"
|
||||||
Middleman::PreviewServer.start(params)
|
::Middleman::PreviewServer.start(params)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,13 @@ module Middleman
|
||||||
if defined?(Bundler)
|
if defined?(Bundler)
|
||||||
Bundler.require
|
Bundler.require
|
||||||
else
|
else
|
||||||
|
require "rubygems"
|
||||||
|
|
||||||
|
begin
|
||||||
|
require "middleman-more"
|
||||||
|
rescue LoadError
|
||||||
|
end
|
||||||
|
|
||||||
extensions = rubygems_latest_specs.select do |spec|
|
extensions = rubygems_latest_specs.select do |spec|
|
||||||
spec_has_file?(spec, EXTENSION_FILE)
|
spec_has_file?(spec, EXTENSION_FILE)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue