1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/bin/middleman

64 lines
1.8 KiB
Text
Raw Normal View History

#!/usr/bin/env ruby
libdir = File.join(File.dirname(File.dirname(__FILE__)), "lib")
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
require 'pathname'
require 'rubygems'
module Middleman
module ProjectLocator
2011-11-20 21:21:19 -08:00
class << self
def locate_middleman_root!
2011-11-20 21:21:19 -08:00
cwd = Dir.pwd
2011-11-20 21:21:19 -08: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?"
exit(1)
2011-11-20 21:21:19 -08:00
end
2011-11-20 21:21:19 -08:00
if in_middleman_project?
did_locate_middleman_project(cwd)
2011-11-20 21:21:19 -08:00
return
end
2011-11-20 21:21:19 -08: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! unless cwd == Dir.pwd
2011-11-20 21:21:19 -08:00
end
rescue SystemCallError
# could not chdir, no problem just return
end
2011-11-20 21:21:19 -08:00
def in_middleman_project?
File.exists?('config.rb')
end
2011-11-20 21:21:19 -08: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
def did_locate_middleman_project(path)
2011-11-20 21:21:19 -08:00
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', path)
2011-11-20 21:21:19 -08:00
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
start_cli!
2011-11-20 21:21:19 -08:00
end
def start_cli!
2011-11-20 21:21:19 -08:00
require 'middleman'
2011-12-21 12:13:28 -08:00
Middleman::Cli::Base.start
2011-11-20 21:21:19 -08:00
end
end
end
end
2011-12-22 00:42:14 -08:00
ARGV << "server" if ARGV.length < 1
if %w(server s build b).include?(ARGV[0])
Middleman::ProjectLocator.locate_middleman_root!
else
Middleman::ProjectLocator.start_cli!
end