simplify middleman root detection, add MM_ROOT env

This commit is contained in:
Thomas Reynolds 2011-12-24 11:30:41 -08:00
parent 75fba6aa54
commit c3f501913d
7 changed files with 47 additions and 62 deletions

View file

@ -18,6 +18,7 @@
* config.rb and extensions can add command-line commands
* Nested layouts using `wrap_layout` helper
* Support for placekitten.com
* Added MM_ROOT environmental variable
2.0.14
====

View file

@ -1,64 +1,30 @@
#!/usr/bin/env ruby
libdir = File.join(File.dirname(File.dirname(__FILE__)), "lib")
require "rubygems"
require "pathname"
libdir = File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), "lib"))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
require 'pathname'
require 'rubygems'
ARGV << "server" if ARGV.length < 1
module Middleman
module ProjectLocator
class << self
def locate_middleman_root!
cwd = Dir.pwd
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)
def locate_middleman_root!(cwd = Pathname.new(Dir.pwd))
return cwd.to_s if File.exists?(File.join(cwd, 'config.rb'))
return false if cwd.root?
locate_middleman_root!(cwd.parent)
end
if in_middleman_project?
did_locate_middleman_project(cwd)
return
if !ENV['MM_ROOT'] && found_path = locate_middleman_root!
ENV['MM_ROOT'] = found_path
end
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
end
rescue SystemCallError
# could not chdir, no problem just return
end
def in_middleman_project?
File.exists?('config.rb')
end
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)
if ENV['MM_ROOT']
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', path)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', ENV['MM_ROOT'])
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
start_cli!
end
def start_cli!
require 'middleman'
Dir.chdir(ENV['MM_ROOT'] || Dir.pwd) do
Middleman::Cli::Base.start
end
end
end
end
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

View file

@ -1,7 +1,7 @@
require "rbconfig"
# Setup our load paths
libdir = File.dirname(__FILE__)
libdir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
# Top-level Middleman object
@ -230,7 +230,7 @@ module Middleman
'webrick' # Maybe Kirk?
else
require "thin"
::Thin::Logging.silent = !options[:is_logging]
::Thin::Logging.silent = !options[:logging]
'thin'
end

View file

@ -147,7 +147,7 @@ class Middleman::Base
# Root project directory (overwritten in middleman build/server)
# @return [String]
set :root, Dir.pwd
set :root, ENV['MM_ROOT'] || Dir.pwd
# Name of the source directory
# @return [String]
@ -324,6 +324,8 @@ class Middleman::Base
@req = Rack::Request.new(env)
@res = Rack::Response.new
puts "== Request: #{env["PATH_INFO"]}" if logging?
if env["PATH_INFO"] == "/__middleman__" && env["REQUEST_METHOD"] == "POST"
if req.params.has_key?("change")
file_did_change(req.params["change"])
@ -402,6 +404,7 @@ class Middleman::Base
end
# End the request
puts "== Finishing Request: #{self.current_path}" if logging?
halt res.finish
end

View file

@ -25,6 +25,11 @@ module Middleman::Cli
:default => nil,
:desc => 'Build a subset of the project'
def build
if !ENV["MM_ROOT"]
$stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
exit(1)
end
if options.has_key?("relative") && options["relative"]
self.class.shared_instance.activate :relative_assets
end

View file

@ -19,16 +19,21 @@ module Middleman::Cli
:aliases => "-p",
:default => "4567",
:desc => "The port Middleman will listen on"
method_option "debug",
method_option "verbose",
:type => :boolean,
:default => false,
:desc => 'Print debug messages'
def server
if !ENV["MM_ROOT"]
$stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
exit(1)
end
params = {
:port => options["port"],
:host => options["host"],
:environment => options["environment"],
:debug => options["debug"]
:debug => options["verbose"]
}
puts "== The Middleman is loading"

View file

@ -57,6 +57,8 @@ module Middleman::Sitemap
end
def render(opts={}, locs={}, &block)
puts "== Render Start: #{source_file}" if app.logging?
md = metadata.dup
opts = options.deep_merge(md[:options]).deep_merge(opts)
locs = locals.deep_merge(md[:locals]).deep_merge(locs)
@ -71,7 +73,10 @@ module Middleman::Sitemap
end
app.instance_eval(&block) if block_given?
app.render_template(source_file, locs, opts)
result = app.render_template(source_file, locs, opts)
puts "== Render End: #{source_file}" if app.logging?
result
end
end
end