class Bundler::Injector

def remove_gems_from_gemfile(gems, gemfile_path)

Parameters:
  • gemfile_path (Pathname) -- The Gemfile from which to remove dependencies.
  • gems (Array) -- Array of names of gems to be removed.
def remove_gems_from_gemfile(gems, gemfile_path)
  patterns = /gem\s+(['"])#{Regexp.union(gems)}\1|gem\s*\((['"])#{Regexp.union(gems)}\2\)/
  new_gemfile = []
  multiline_removal = false
  File.readlines(gemfile_path).each do |line|
    match_data = line.match(patterns)
    if match_data && is_not_within_comment?(line, match_data)
      multiline_removal = line.rstrip.end_with?(",")
      # skip lines which match the regex
      next
    end
    # 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
  end
  %w[group source env install_if].each {|block| remove_nested_blocks(new_gemfile, block) }
  new_gemfile.join.chomp
end