Sha256: 25c5efafc6cd17f31dd9245421a1bab743ad5f99eb98151c622fb08f0929ebb6

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module AnnotateGem
  class Gemfile

    GEM_LINE_REGEX = /\A\s*gem[\s(]+["'](?<name>[^'"]*)["']/.freeze

    attr_accessor :gemfile_path, :gem_lines, :source, :options

    def initialize(gemfile_path, options = {})
      @gemfile_path = gemfile_path
      @source = []
      @gem_lines = []
      @options = options
    end

    def parse
      self.source = File.readlines(gemfile_path)
      source.each_with_index do |line, i|
        if match = GEM_LINE_REGEX.match(line)
          prev_line = source[i - 1] if i > 0
          prev_line_comment = prev_line if is_line_a_comment?(prev_line)
          self.gem_lines << GemLine.new(
            name: match[:name],
            original_line: line,
            location: i,
            prev_line_comment: prev_line_comment,
            options: options
          )
        end
      end
    end

    def write_comments
      gem_lines.reverse.each do |gem_line|
        source.insert(gem_line.location, gem_line.comment) if gem_line.should_insert?
      end
      File.write(gemfile_path, source.join)
    end

    private

    def is_line_a_comment?(line)
      line && line.strip.start_with?("#")
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
annotate_gem-0.0.10 lib/annotate_gem/gemfile.rb
annotate_gem-0.0.8 lib/annotate_gem/gemfile.rb