module PhusionPassenger::LoaderSharedHelpers

def running_bundler(options)

def running_bundler(options)
	yield
rescue Exception => e
	if (defined?(Bundler::GemNotFound) && e.is_a?(Bundler::GemNotFound)) ||
	   (defined?(Bundler::GitError) && e.is_a?(Bundler::GitError))
		PhusionPassenger.require_passenger_lib 'platform_info/ruby'
		comment =
			"<p>It looks like Bundler could not find a gem. Maybe you didn't install all the " +
			"gems that this application needs. To install your gems, please run:</p>\n\n" +
			"  <pre class=\"commands\">bundle install</pre>\n\n"
		ruby = options["ruby"]
		if ruby =~ %r(^/usr/local/rvm/)
			comment <<
				"<p>If that didn't work, then maybe the problem is that your gems are installed " +
				"to <code>#{h home_dir}/.rvm/gems</code>, while at the same time you set " +
				"<code>PassengerRuby</code> (Apache) or <code>passenger_ruby</code> (Nginx) to " +
				"<code>#{h ruby}</code>. Because of the latter, RVM does not load gems from the " +
				"home directory.</p>\n\n" +
				"<p>To make RVM load gems from the home directory, you need to set " +
				"<code>PassengerRuby</code>/<code>passenger_ruby</code> to an RVM wrapper script " +
				"inside the home directory:</p>\n\n" +
				"<ol>\n" +
				"  <li>Login as #{h whoami}.</li>\n"
			if PlatformInfo.rvm_installation_mode == :multi
				comment <<
					"  <li>Enable RVM mixed mode by running:\n" +
					"      <pre class=\"commands\">rvm user gemsets</pre></li>\n"
			end
			comment <<
				"  <li>Run this to find out what to set <code>PassengerRuby</code>/<code>passenger_ruby</code> to:\n" +
				"      <pre class=\"commands\">#{PlatformInfo.ruby_command} \\\n" +
				"#{PhusionPassenger.bin_dir}/passenger-config --detect-ruby</pre></li>\n" +
				"</ol>\n\n" +
				"<p>If that didn't help either, then maybe your application is being run under a " +
				"different environment than it's supposed to. Please check the following:</p>\n\n"
		else
			comment <<
				"<p>If that didn't work, then the problem is probably caused by your " +
				"application being run under a different environment than it's supposed to. " +
				"Please check the following:</p>\n\n"
		end
		comment << "<ol>\n"
		comment <<
			"  <li>Is this app supposed to be run as the <code>#{h whoami}</code> user?</li>\n" +
			"  <li>Is this app being run on the correct Ruby interpreter? Below you will\n" +
			"      see which Ruby interpreter Phusion Passenger attempted to use.</li>\n"
		if PlatformInfo.in_rvm?
			comment <<
				"  <li>Please check whether the correct RVM gemset is being used.</li>\n" +
				"  <li>Sometimes, RVM gemsets may be broken.\n" +
				"      <a href=\"https://github.com/phusion/passenger/wiki/Resetting-RVM-gemsets\">Try resetting them.</a></li>\n"
		end
		comment << "</ol>\n"
		prepend_exception_html_comment(e, comment)
	end
	raise e
end