Sha256: 4c7d4d9b3fbe8f07fd9f9aca0327fa4b09f260bb6e4702342668d8e315148ad6

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module OrganizeGemfile
  class GemfileGroup < Struct.new(:name, :specs)
    def initialize(name, specs = [])
      super
    end

    def to_gemfile_lines
      lines = []
      indent_spaces = ""
      max_length = 0

      is_before_default = name == :before_default
      is_default = name == [:default]

      if !is_before_default && !is_default
        indent_spaces = "  "
        lines << "group #{name.map { |n| ":#{n}" }.join(", ")} do"
      end

      spec_lines = specs.sort_by { |s| s[:name] }.map do |spec|
        parts = []
        parts << "gem \"#{spec[:name]}\""

        if spec[:version] && spec[:version] != ">= 0"
          version_parts = []
          spec[:version].split(",").each do |version|
            version_parts << "\"#{version.strip}\""
          end
          parts << version_parts.join(", ")
        end

        require_name = spec[:requires]&.first
        if require_name != spec[:name] && !require_name.nil? && require_name != ""
          parts << "require: \"#{require_name}\""
        end
        parts << "require: false" if spec[:requires] == []
        parts << "path: \"#{spec[:source].path}\"" if spec[:source]
        parts << "platforms: %i[ #{spec[:platforms].join(" ")} ]" if spec[:platforms].any?
        line = parts.join(", ")

        if spec[:summary]
          max_length = [max_length, line.length].max
        end

        {line: line, spec: spec}
      end

      spec_lines.each do |l|
        line = l[:line]
        spec = l[:spec]

        if spec[:summary]
          line = line.ljust(max_length)
          line += " # #{spec[:summary]}"
          if spec[:homepage]
            line += " [#{spec[:homepage]}]"
          end
        end
        lines << indent_spaces + line
      end

      if !is_before_default && !is_default
        lines << "end"
      end

      lines
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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