middleman/middleman-core/bin/middleman
2012-06-01 19:05:32 -07:00

56 lines
1.6 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# 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))
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
# 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
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
end
# Automatically discover extensions in RubyGems
require "middleman-core/extensions"
::Middleman.load_extensions_in_path
require "middleman-core/cli"
# Default command is server
ARGV.unshift("server") if ARGV.length < 1 || ARGV.first.include?("-")
# Start the CLI
if ENV["MM_ROOT"]
# Change directory to the root
Dir.chdir(ENV["MM_ROOT"]) do
Middleman::Cli::Base.start
end
else
Middleman::Cli::Base.start
end