module Polisher::GemfileParser::ClassMethods

Public Instance Methods

parse(path, args = {}) click to toggle source

Parse the specified gemfile & return new Gemfile instance from metadata

@param [String] path to gemfile to parse @return [Polisher::Gemfile] gemfile instantiated from parsed metadata

# File lib/polisher/gemfile/parser.rb, line 17
def parse(path, args = {})
  require 'bundler'

  groups = args[:groups]

  definition = nil
  path, gemfile = File.split(path)
  Dir.chdir(path) do
    begin
      definition = Bundler::Definition.build(gemfile, nil, false)
    rescue Bundler::GemfileNotFound
      raise ArgumentError, "invalid gemfile: #{path}"
    end
  end

  metadata = {}
  metadata[:deps] = definition.dependencies.select do |d|
    groups.nil? || groups.empty? ||                  # groups not specified
    groups.any? { |g| d.groups.include?(g.intern) }  # dep in any group
  end

  metadata[:dev_deps] = [] # TODO
  metadata[:definition] = definition

  new metadata
end