1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/middleman-core/bin/middleman
moe cf897e1e9e Use plain Dir.chdir instead of enclosing middleman in a Dir.chdir-block.
This allows extensions, templates and monkey-patches to chdir
when needed, without triggering this ruby warning:

  warning: conflicting chdir during another chdir block
2012-07-14 14:46:46 +02:00

70 lines
1.8 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
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"
# 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
# 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