2012-07-17 02:24:13 +02:00
|
|
|
# Core Pathname library used for traversal
|
2013-12-28 01:26:31 +01:00
|
|
|
require 'pathname'
|
2012-07-17 02:24:13 +02:00
|
|
|
|
|
|
|
module Middleman
|
|
|
|
class << self
|
|
|
|
def setup_load_paths
|
|
|
|
@_is_setup ||= begin
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-07-17 02:24:13 +02:00
|
|
|
# Only look for config.rb if MM_ROOT isn't set
|
2014-03-04 08:08:32 +01:00
|
|
|
if !ENV['MM_ROOT'] && (found_path = findup('config.rb'))
|
2013-12-28 01:26:31 +01:00
|
|
|
ENV['MM_ROOT'] = found_path
|
2012-07-17 02:24:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# If we've found the root, try to setup Bundler
|
2014-07-20 23:25:47 +02:00
|
|
|
setup_bundler if ENV['MM_ROOT']
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-07-17 02:24:13 +02:00
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-03-04 08:08:32 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
# Set BUNDLE_GEMFILE and run Bundler setup. Raises an exception if there is no Gemfile
|
|
|
|
def setup_bundler
|
2016-02-03 18:57:30 +01:00
|
|
|
if found_gemfile_root = findup('Gemfile', ENV['MM_ROOT'])
|
|
|
|
ENV['BUNDLE_GEMFILE'] ||= File.join(found_gemfile_root, 'Gemfile')
|
|
|
|
end
|
2014-03-04 08:08:32 +01:00
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
unless File.exist?(ENV['BUNDLE_GEMFILE'])
|
2014-03-04 08:08:32 +01:00
|
|
|
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../../../Gemfile', __FILE__)
|
|
|
|
end
|
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
if File.exist?(ENV['BUNDLE_GEMFILE'])
|
2014-03-04 08:08:32 +01:00
|
|
|
require 'bundler/setup'
|
|
|
|
Bundler.require
|
|
|
|
else
|
|
|
|
raise "Couldn't find your Gemfile. Middleman projects require a Gemfile for specifying dependencies."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Recursive method to find a file in parent directories
|
2014-11-09 20:30:11 +01:00
|
|
|
def findup(filename, cwd=Dir.pwd)
|
|
|
|
cwd = Pathname(cwd)
|
2014-03-04 08:08:32 +01:00
|
|
|
return cwd.to_s if (cwd + filename).exist?
|
2012-07-17 02:24:13 +02:00
|
|
|
return false if cwd.root?
|
2014-03-05 07:43:10 +01:00
|
|
|
findup(filename, cwd.parent)
|
2012-07-17 02:24:13 +02:00
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|