diff --git a/bin/middleman b/bin/middleman index 39b302b4..eeb87b00 100755 --- a/bin/middleman +++ b/bin/middleman @@ -1,8 +1,60 @@ #!/usr/bin/env ruby -require "rubygems" libdir = File.join(File.dirname(File.dirname(__FILE__)), "lib") $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) -require 'middleman' -Middleman::CLI.start \ No newline at end of file +require 'pathname' +require 'rubygems' + +module Middleman + module ProjectLocator + def self.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?" + return + end + + if in_middleman_project? + did_locate_middleman_project(cwd) + 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 self.in_middleman_project? + File.exists?('config.rb') + end + + def self.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 self.did_locate_middleman_project(path) + # Set up gems listed in the Gemfile. + ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', path) + + require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) + + start_cli! + end + + def self.start_cli! + require 'middleman' + Middleman::CLI.start + end + end +end + +if ARGV.length < 1 || %w(server build migrate).include?(ARGV.first) + Middleman::ProjectLocator.locate_middleman_root! +else + Middleman::ProjectLocator.start_cli! +end \ No newline at end of file diff --git a/lib/middleman/cli.rb b/lib/middleman/cli.rb index 363e2918..c2840276 100644 --- a/lib/middleman/cli.rb +++ b/lib/middleman/cli.rb @@ -34,7 +34,7 @@ module Middleman method_option "livereload", :default => false, :type => :boolean, :desc => "Whether to enable Livereload or not" method_option "livereload-port", :default => "35729", :desc => "The port Livereload will listen on" def server - config_check && v1_check + v1_check if options["livereload"] livereload_options = {:port => options["livereload-port"]} end @@ -48,13 +48,12 @@ module Middleman desc "build", "Builds the static site for deployment" method_option "relative", :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls' def build - config_check && v1_check + v1_check thor_group = Middleman::Builder.new([], options).invoke_all end desc "migrate", "Migrates an older Middleman project to the 2.0 structure" def migrate - config_check return if File.exists?("source") `mv public source` `cp -R views/* source/` @@ -68,17 +67,10 @@ module Middleman end private - - def config_check - if !File.exists?("config.rb") - $stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?" - exit 1 - end - end def v1_check if File.exists?("views") || File.exists?("public") - $stderr.puts "== Error: The views and public folders are have been combined. Create a new 'source' folder, add the contents of views and public to it and then remove the empty views and public folders." + $stderr.puts "== Error: The views and public folders are have been combined. Use the 'middleman migrate' command to merge your folders automatically." exit 1 end end