diff options
author | Samuel Giddins <segiddins@segiddins.me> | 2017-07-01 14:24:54 +0200 |
---|---|---|
committer | Samuel Giddins <segiddins@segiddins.me> | 2017-07-01 14:24:54 +0200 |
commit | 46570f77f38fae8466398b29d2bb8b1362d4100c (patch) | |
tree | 03573c1b96246b944ab69637d8702a8e7ed51421 | |
parent | 5e8243ba4845bd87238eb9dfb8ca765683f9229f (diff) | |
download | bundler-46570f77f38fae8466398b29d2bb8b1362d4100c.tar.gz |
Extract lockfile generation into a new classseg-extract-lockfile-generation
-rw-r--r-- | lib/bundler/definition.rb | 47 | ||||
-rw-r--r-- | lib/bundler/lockfile_generator.rb | 94 |
2 files changed, 96 insertions, 45 deletions
diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index 68bc40844e..9a0ee5cf17 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -362,51 +362,8 @@ module Bundler end def to_lock - out = String.new - - sources.lock_sources.each do |source| - # Add the source header - out << source.to_lock - # Find all specs for this source - resolve. - select {|s| source.can_lock?(s) }. - # This needs to be sorted by full name so that - # gems with the same name, but different platform - # are ordered consistently - sort_by(&:full_name). - each do |spec| - next if spec.name == "bundler" - out << spec.to_lock - end - out << "\n" - end - - out << "PLATFORMS\n" - - platforms.map(&:to_s).sort.each do |p| - out << " #{p}\n" - end - - out << "\n" - out << "DEPENDENCIES\n" - - handled = [] - dependencies.sort_by(&:to_s).each do |dep| - next if handled.include?(dep.name) - out << dep.to_lock - handled << dep.name - end - - if locked_ruby_version - out << "\nRUBY VERSION\n" - out << " #{locked_ruby_version}\n" - end - - # Record the version of Bundler that was used to create the lockfile - out << "\nBUNDLED WITH\n" - out << " #{locked_bundler_version}\n" - - out + require "bundler/lockfile_generator" + LockfileGenerator.generate(self) end def ensure_equivalent_gemfile_and_lockfile(explicit_flag = false) diff --git a/lib/bundler/lockfile_generator.rb b/lib/bundler/lockfile_generator.rb new file mode 100644 index 0000000000..2a29b20f79 --- /dev/null +++ b/lib/bundler/lockfile_generator.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true +module Bundler + class LockfileGenerator + attr_reader :definition + attr_reader :out + + # @private + def initialize(definition) + @definition = definition + @out = String.new + end + + def self.generate(definition) + new(definition).generate! + end + + def generate! + add_sources + add_platforms + add_dependencies + add_locked_ruby_version + add_bundled_with + + out + end + + private + + def add_sources + definition.send(:sources).lock_sources.each_with_index do |source, idx| + out << "\n" unless idx.zero? + + # Add the source header + out << source.to_lock + + # Find all specs for this source + specs = definition.resolve.select {|s| source.can_lock?(s) } + add_specs(specs) + end + end + + def add_specs(specs) + # This needs to be sorted by full name so that + # gems with the same name, but different platform + # are ordered consistently + specs.sort_by(&:full_name).each do |spec| + next if spec.name == "bundler".freeze + out << spec.to_lock + end + end + + def add_platforms + add_section("PLATFORMS", definition.platforms) + end + + def add_dependencies + out << "\nDEPENDENCIES\n" + + handled = [] + definition.dependencies.sort_by(&:to_s).each do |dep| + next if handled.include?(dep.name) + out << dep.to_lock + handled << dep.name + end + end + + def add_locked_ruby_version + return unless locked_ruby_version = definition.locked_ruby_version + add_section("RUBY VERSION", locked_ruby_version.to_s) + end + + def add_bundled_with + add_section("BUNDLED WITH", definition.locked_bundler_version.to_s) + end + + def add_section(name, value) + out << "\n#{name}\n" + case value + when Array + value.map(&:to_s).sort.each do |val| + out << " #{val}\n" + end + when Hash + value.to_a.sort_by {|k, _| k.to_s }.each do |key, val| + out << " #{key}: #{val}\n" + end + when String + out << " #{value}\n" + else + raise ArgumentError, "#{value.inspect} can't be serialized in a lockfile" + end + end + end +end |