Sha256: da7bf9fa74b2f8fca862b6a414faa961dba286e12e869b2598ef7d33d491c968

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module OrganizeGemfile
  class BundleParser
    def initialize(gemfile_path, gemfile_lock_path)
      @gemfile_path = gemfile_path
      @gemfile_lock_path = gemfile_lock_path
      @dsl = Bundler::Definition.build(@gemfile_path, @gemfile_lock_path, {})
      @requires = @dsl.requires
      @sources = sources
    end

    def groups
      ret = {}

      before_default = @sources.select { |source| source[:appear_at_top] == true }
      if before_default.any?
        ret["before_default"] = GemfileGroup.new(:before_default, before_default)
      end

      @sources.reject { |source| source[:appear_at_top] == true }
        .group_by { |source| source[:groups] }
        .map do |group, sources|
        ret[group.map(&:to_s).join("_")] = GemfileGroup.new(group, sources)
      end

      ret
    end

    def ruby_version
      @dsl.ruby_version&.versions&.first
    end

    private

    def sources
      specs = Gem.loaded_specs

      @dsl.dependencies.map do |dep|
        spec = specs.key?(dep.name) ? specs[dep.name] : nil
        spec = spec&.to_spec

        version = dep.requirement.to_s
        if version.nil? || version == "" || version == ">= 0"
          if spec&.version
            version = "~> #{spec.version}"
          end
        end

        {
          name: dep.name,
          summary: spec&.summary,
          homepage: spec&.homepage,
          groups: dep.groups,
          git: dep.git,
          branch: dep.branch,
          platforms: dep.platforms,
          gemfile: dep.gemfile,
          version: version,
          source: dep.source,
          requires: @requires[dep.name],
          appear_at_top: dep.name.include?("dotenv/rails-now")
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
organize_gemfile-0.1.2 lib/organize_gemfile/bundle_parser.rb