diff options
author | Andre Arko <andre@arko.net> | 2012-08-26 00:43:27 -0700 |
---|---|---|
committer | Andre Arko <andre@arko.net> | 2012-08-26 00:43:27 -0700 |
commit | 7a61d7bb7e1d597749fe680cb7673bf2df140317 (patch) | |
tree | ea2a4f0bfcf0eded36ea34cbe11fcf6aa32e3fbf /lib | |
parent | b7f579429e3f2c8428795ea6f5f732fa5b6fbd40 (diff) | |
download | bundler-7a61d7bb7e1d597749fe680cb7673bf2df140317.tar.gz |
injector class to handle gem injection
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bundler.rb | 1 | ||||
-rw-r--r-- | lib/bundler/definition.rb | 3 | ||||
-rw-r--r-- | lib/bundler/injector.rb | 55 |
3 files changed, 58 insertions, 1 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb index 2de1683c16..0f5e469375 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -24,6 +24,7 @@ module Bundler autoload :Graph, 'bundler/graph' autoload :Index, 'bundler/index' autoload :Installer, 'bundler/installer' + autoload :Injector, 'bundler/injector' autoload :LazySpecification, 'bundler/lazy_specification' autoload :LockfileParser, 'bundler/lockfile_parser' autoload :MatchPlatform, 'bundler/match_platform' diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index ccd7f7fe47..e0b3e5cedd 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -5,7 +5,8 @@ module Bundler class Definition include GemHelpers - attr_reader :dependencies, :platforms, :sources, :ruby_version + attr_reader :dependencies, :platforms, :sources, :ruby_version, + :locked_deps def self.build(gemfile, lockfile, unlock) unlock ||= {} 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 |