middleman/middleman-core/bin/middleman

71 lines
1.8 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env ruby
2011-12-29 07:52:51 +01:00
# Add our lib/ directory to the path
libdir = File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), "lib"))
$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))
2012-06-02 04:05:32 +02:00
return cwd.to_s if (cwd + 'config.rb').exist?
return false if cwd.root?
locate_root(cwd.parent)
end
2011-12-29 07:52:51 +01:00
# Only look for config.rb if MM_ROOT isn't set
if !ENV["MM_ROOT"] && found_path = locate_root
2011-12-29 00:29:19 +01:00
ENV["MM_ROOT"] = found_path
end
is_bundler_setup = false
2011-12-29 07:52:51 +01:00
# If we've found the root, try to setup Bundler
2011-12-29 00:29:19 +01:00
if ENV["MM_ROOT"]
2011-12-29 07:52:51 +01:00
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"
2012-06-05 20:48:34 +02:00
# Change flag to a module
ARGV.unshift("help") if ARGV.delete("--help")
2011-12-29 07:52:51 +01:00
# Default command is server
2012-06-05 20:48:34 +02:00
if ARGV[0] != "help" && (ARGV.length < 1 || ARGV.first.include?("-"))
ARGV.unshift("server")
end
2011-12-29 07:52:51 +01:00
# 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