lib/bundler/injector.rb in bundler-2.1.4 vs lib/bundler/injector.rb in bundler-2.2.0.rc.1

- old
+ new

@@ -178,13 +178,24 @@ # @param [Array] gems Array of names of gems to be removed. # @param [Pathname] gemfile_path The Gemfile from which to remove dependencies. def remove_gems_from_gemfile(gems, gemfile_path) patterns = /gem\s+(['"])#{Regexp.union(gems)}\1|gem\s*\((['"])#{Regexp.union(gems)}\2\)/ - # remove lines which match the regex - new_gemfile = IO.readlines(gemfile_path).reject {|line| line.match(patterns) } + new_gemfile = [] + multiline_removal = false + IO.readlines(gemfile_path).each do |line| + if line.match(patterns) + multiline_removal = line.rstrip.end_with?(",") + # skip lines which match the regex + next + end - # remove lone \n and append them with other strings + # skip followup lines until line does not end with ',' + new_gemfile << line unless multiline_removal + multiline_removal = line.rstrip.end_with?(",") if multiline_removal + end + + # remove line \n and append them with other strings new_gemfile.each_with_index do |_line, index| if new_gemfile[index + 1] == "\n" new_gemfile[index] += new_gemfile[index + 1] new_gemfile.delete_at(index + 1) end