Sha256: f157358631393d54690779ee4bbfee9658e5403f5f4dff3b7e7d8270574f82d3

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 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|
        next unless gem_line.should_insert?
        if options[:inline]
          source[gem_line.location] = gem_line.inline_comment
        else
          source.insert(gem_line.location, gem_line.comment)
        end
      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

1 entries across 1 versions & 1 rubygems

Version Path
annotate_gem-0.0.11 lib/annotate_gem/gemfile.rb