Class: Bundle::Locker::Parser
- Inherits:
-
Object
- Object
- Bundle::Locker::Parser
- Defined in:
- lib/bundle-locker.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(gemfile_name) ⇒ Parser
constructor
A new instance of Parser.
Constructor Details
#initialize(gemfile_name) ⇒ Parser
Returns a new instance of Parser.
6 7 8 9 |
# File 'lib/bundle-locker.rb', line 6 def initialize(gemfile_name) @gemfile_name = gemfile_name @gemfile_lock = Bundler::LockfileParser.new(File.read("#{@gemfile_name}.lock")) end |
Instance Method Details
#call ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bundle-locker.rb', line 11 def call gem_dictionary = {} File.readlines(@gemfile_name).each do |line| if /\A(\s*gem\s+['"])([^'"]+)(['"])/ =~ line prefix = $1 gem_name = $2 suffix = $3 gem_dictionary[gem_name] = {prefix: prefix, original: line, suffix: suffix, gem_name: gem_name} end end @gemfile_lock.specs.each do |spec| if gem_dictionary[spec.name] gem_dictionary[spec.name][:locked_version] = spec.version.to_s gem_dictionary[spec.name][:source] = spec.source.to_s end end contents = File.read(@gemfile_name) File.open(@gemfile_name, 'w+') do |file| gem_dictionary.each do |key, dictionary| if dictionary[:locked_version] replacement = "#{dictionary[:prefix]}#{dictionary[:gem_name]}#{dictionary[:suffix]}, " if dictionary[:source] =~ /git/ replacement << ":git => '#{dictionary[:source].sub(/\s.*\Z/,'')}'\n" else replacement << "'#{dictionary[:locked_version]}'\n" end contents.gsub!(dictionary[:original], replacement) end end file.puts contents end end |