2011-07-27 00:06:24 -04:00
|
|
|
#!/usr/bin/env ruby
|
2011-07-27 02:19:43 -04:00
|
|
|
|
|
|
|
libdir = File.join(File.dirname(File.dirname(__FILE__)), "lib")
|
|
|
|
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
|
|
|
|
2011-08-03 20:10:16 -04:00
|
|
|
require 'pathname'
|
|
|
|
require 'rubygems'
|
|
|
|
|
|
|
|
module Middleman
|
|
|
|
module ProjectLocator
|
2011-11-21 00:21:19 -05:00
|
|
|
class << self
|
|
|
|
def locate_middleman_root!(args)
|
|
|
|
cwd = Dir.pwd
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
if !in_middleman_project? && !in_middleman_project_subdirectory?
|
|
|
|
$stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
|
|
|
|
return
|
|
|
|
end
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
if in_middleman_project?
|
|
|
|
did_locate_middleman_project(cwd, args)
|
|
|
|
return
|
|
|
|
end
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
Dir.chdir("..") do
|
|
|
|
# Recurse in a chdir block: if the search fails we want to be sure
|
|
|
|
# the application is generated in the original working directory.
|
|
|
|
locate_middleman_root!(args) unless cwd == Dir.pwd
|
|
|
|
end
|
|
|
|
rescue SystemCallError
|
|
|
|
# could not chdir, no problem just return
|
2011-08-03 20:10:16 -04:00
|
|
|
end
|
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
def in_middleman_project?
|
|
|
|
File.exists?('config.rb')
|
|
|
|
end
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
def in_middleman_project_subdirectory?(path = Pathname.new(Dir.pwd))
|
|
|
|
File.exists?(File.join(path, 'config.rb')) || !path.root? && in_middleman_project_subdirectory?(path.parent)
|
|
|
|
end
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
def did_locate_middleman_project(path, args)
|
|
|
|
# Set up gems listed in the Gemfile.
|
|
|
|
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', path)
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
start_cli!(args)
|
|
|
|
end
|
2011-08-03 20:10:16 -04:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
def start_cli!(args)
|
|
|
|
require 'middleman'
|
|
|
|
Middleman::CLI.start(args)
|
|
|
|
end
|
2011-08-03 20:10:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-04 13:45:48 -04:00
|
|
|
args = ARGV.dup
|
|
|
|
|
|
|
|
ARG_ALIASES = {
|
|
|
|
"s" => "server",
|
|
|
|
"b" => "build",
|
2011-11-21 00:21:19 -05:00
|
|
|
"i" => "init",
|
|
|
|
"w" => "watch"
|
2011-08-04 13:45:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ARG_ALIASES.has_key?(args[0])
|
|
|
|
args[0] = ARG_ALIASES[args[0]]
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.length < 1 || %w(server build migrate).include?(args.first)
|
|
|
|
Middleman::ProjectLocator.locate_middleman_root!(args)
|
2011-08-03 20:10:16 -04:00
|
|
|
else
|
2011-08-04 13:45:48 -04:00
|
|
|
Middleman::ProjectLocator.start_cli!(args)
|
2011-08-03 20:10:16 -04:00
|
|
|
end
|