class Bundler::Injector

def remove_nested_blocks(gemfile, block_name)

Parameters:
  • block_name (String) -- Name of block name to look for.
  • gemfile (Array) -- Array of gemfile contents.
def remove_nested_blocks(gemfile, block_name)
  nested_blocks = 0
  # count number of nested blocks
  gemfile.each_with_index {|line, index| nested_blocks += 1 if !gemfile[index + 1].nil? && gemfile[index + 1].include?(block_name) && line.include?(block_name) }
  while nested_blocks >= 0
    nested_blocks -= 1
    gemfile.each_with_index do |line, index|
      next unless !line.nil? && line.strip.start_with?(block_name)
      if /^\s*end\s*$/.match?(gemfile[index + 1])
        gemfile[index] = nil
        gemfile[index + 1] = nil
      end
    end
    gemfile.compact!
  end
end