summaryrefslogtreecommitdiff
path: root/lib/bundler/injector.rb
diff options
context:
space:
mode:
authorAndre Arko <andre@arko.net>2012-08-26 00:43:27 -0700
committerAndre Arko <andre@arko.net>2012-08-26 00:43:27 -0700
commit7a61d7bb7e1d597749fe680cb7673bf2df140317 (patch)
treeea2a4f0bfcf0eded36ea34cbe11fcf6aa32e3fbf /lib/bundler/injector.rb
parentb7f579429e3f2c8428795ea6f5f732fa5b6fbd40 (diff)
downloadbundler-7a61d7bb7e1d597749fe680cb7673bf2df140317.tar.gz
injector class to handle gem injection
Diffstat (limited to 'lib/bundler/injector.rb')
-rw-r--r--lib/bundler/injector.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb
new file mode 100644
index 0000000000..5c1603fa9c
--- /dev/null
+++ b/lib/bundler/injector.rb
@@ -0,0 +1,55 @@
+module Bundler
+ class Injector
+ def self.inject(new_deps)
+ injector = new(new_deps)
+ injector.inject(Bundler.default_gemfile, Bundler.default_lockfile)
+ end
+
+ def initialize(new_deps)
+ @new_deps = new_deps
+ end
+
+ def inject(gemfile_path, lockfile_path)
+ # evaluate the Gemfile we have now
+ builder = Dsl.new
+ builder.eval_gemfile(gemfile_path)
+
+ # don't inject any gems that are already in the Gemfile
+ @new_deps -= builder.dependencies
+
+ # add new deps to the end of the in-memory Gemfile
+ builder.eval_gemfile("injected gems", new_gem_lines) if @new_deps.any?
+
+ # resolve to see if the new deps broke anything
+ definition = builder.to_definition(lockfile_path, {})
+ definition.resolve_remotely!
+
+ # since nothing broke, we can add those gems to the gemfile
+ append_to(gemfile_path) if @new_deps.any?
+
+ # since we resolved successfully, write out the lockfile
+ definition.lock(Bundler.default_lockfile)
+
+ # return an array of the deps that we added
+ return @new_deps
+ end
+
+ private
+
+ def new_gem_lines
+ @new_deps.map do |d|
+ %|gem '#{d.name}', '#{d.requirement}'|
+ end.join("\n")
+ end
+
+ def append_to(gemfile_path)
+ gemfile_path.open("a") do |f|
+ f.puts
+ f.puts "# Added at #{Time.now} by #{`whoami`.chomp}:"
+ f.puts new_gem_lines
+ end
+ end
+
+
+ end
+end \ No newline at end of file