Permalink
Browse files

Generate Gemfile for plugin runner dynamically

Add a template and method for generating the Gemfile needed for
stand alone staging script

Test plan:
- New unit test passes
- Existing unit tests pass

Change-Id: Iab3a188e6b79cbf6843fb536d68b62b3e9beb9c4
  • Loading branch information...
1 parent 0d85e28 commit 2211850defdb43f7cdc88461eb995e1d8a83790a mpage committed Oct 25, 2011
@@ -0,0 +1,12 @@
+source :rubygems
+
+gem 'vcap_logging', '>= 0.1.1'
+gem 'vcap_cloud_controller_ipc'
+
+<% for plugin_gem in plugin_gems %>
+<% if plugin_gem["version"] %>
+gem '<%= plugin_gem["name"] %>', '= <%= plugin_gem["version"] %>'
+<% else %>
+gem '<%= plugin_gem["name"] %>'
+<% end %>
+<% end %>
@@ -1,3 +1,4 @@
+require 'erb'
require 'fileutils'
require 'vcap/cloud_controller/ipc'
@@ -16,6 +17,8 @@ module Stager
# Responsible for orchestrating the execution of all staging plugins selected
# by the user.
class VCAP::Stager::PluginRunner
+ GEMFILE_TEMPLATE_PATH = File.join(VCAP::Stager::ASSET_DIR, 'plugin_runner_gemfile.erb')
+
class << self
attr_reader :registered_plugins
@@ -28,6 +31,24 @@ def register_plugins(*plugins)
def reset_registered_plugins
@registered_plugins = []
end
+
+ # Generates a Gemfile that should be used in conjuction with running the
+ # standalone script using 'bundle exec'. We assume that every plugin has
+ # already been installed locally on the system (therefore its individual
+ # dependencies have been satisfied) and rely on Bundler to resolve possible
+ # conflicts in plugin dependencies.
+ #
+ # @param dest_path String Where to place the generated Gemfile
+ # @param plugin_gems Array Plugins to be loaded
+ # [{'name' => GEM_NAME, 'version' => GEM_VERSION}]
+ def generate_standalone_gemfile(dest_path, plugins)
+ plugin_gems = plugins.map {|p| p['gem'] }
+ template = File.read(GEMFILE_TEMPLATE_PATH)
+ renderer = ERB.new(template)
+ gemfile_contents = renderer.result(binding())
+ File.open(dest_path, 'w+') {|f| f.write(gemfile_contents) }
+ dest_path
+ end
end
# @param source_dir String Directory containing application source
@@ -279,5 +279,4 @@ def run_logged(command, expected_exitstatus=0, timeout=nil)
ret
end
-
end
@@ -4,6 +4,20 @@
require 'tmpdir'
describe VCAP::Stager::PluginRunner do
+ describe '.generate_standalone_gemfile' do
+ it 'should write out a Gemfile containing all plugins to be run' do
+ tmpdir = Dir.mktmpdir
+ gemfile_path = File.join(tmpdir, 'Gemfile')
+ plugins = [{'gem' => {'name' => 'test1'}},
+ {'gem' => {'name' => 'test2', 'version' => '0.0.1'}}]
+ VCAP::Stager::PluginRunner.generate_standalone_gemfile(gemfile_path, plugins)
+ File.exist?(gemfile_path).should be_true
+ gemfile_contents = File.read(gemfile_path)
+ gemfile_contents.match(/^gem 'test1'$/).should be_true
+ gemfile_contents.match(/^gem 'test2', '= 0.0.1'$/).should be_true
+ end
+ end
+
describe '#run_plugins' do
before :each do
@src_dir = Dir.mktmpdir

0 comments on commit 2211850

Please sign in to comment.