Class: AnnotateGemfile::Annotator

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_gemfile/annotator.rb

Constant Summary collapse

COMMENT =
/^#.*/
BEGIN_COMMENT =
/^=begin/
END_COMMENT =
/^=end/
GEM_DECLARATION =
/^gem\s+/
GEM_NAME =
/gem\s+[('"]([^'")]+)/
BLANK_LINE =
/^\s*$/

Instance Method Summary collapse

Constructor Details

#initializeAnnotator

Returns a new instance of Annotator.



16
17
18
19
# File 'lib/annotate_gemfile/annotator.rb', line 16

def initialize
  @block_comment = false
  @progress = ProgressBar.create
end

Instance Method Details

#annotate(filename) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/annotate_gemfile/annotator.rb', line 21

def annotate(filename)
  lines = File.readlines(filename)
  gem_count = lines.count { |line| line.strip =~ GEM_DECLARATION }

  @progress = ProgressBar.create(total: gem_count,
                                 format:  '%a %bᗧ%i %p%% %t %c/%C',
                                 progress_mark:  ' ',
                                 remainder_mark: '')

  annotated = annotate_lines(lines)
  File.write("#{filename}.annotated", annotated.join("\n"))
end

#annotate_lines(lines) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/annotate_gemfile/annotator.rb', line 34

def annotate_lines(lines)
  output = lines.each_with_object([]) do |line, output|
    annotated = Array(send(type_of(line), line))
    output.concat annotated
  end.flatten
  output
end

#blank(line) ⇒ Object



50
51
52
# File 'lib/annotate_gemfile/annotator.rb', line 50

def blank(line)
  line.gsub("\n", '')
end

#comment(line) ⇒ Object



54
55
56
# File 'lib/annotate_gemfile/annotator.rb', line 54

def comment(line)
  line.gsub("\n", '')
end

#describe_gem(line) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/annotate_gemfile/annotator.rb', line 62

def describe_gem(line)
  indent = line[/\s*/].to_s
  comment = indent + '# '
  line_width = 80 - comment.length

  gem_name = line[GEM_NAME, 1]
  return unless description = gem_info_for(gem_name)
  description = word_wrap(description, line_width).split("\n")
  description.map {|d| comment + d.to_s }
end

#gem(line) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/annotate_gemfile/annotator.rb', line 42

def gem(line)
  [
    describe_gem(line),
    line,
    '',
  ].compact.flatten.tap { @progress.increment }
end

#gem_info_for(gem_name) ⇒ Object



73
74
75
76
77
78
# File 'lib/annotate_gemfile/annotator.rb', line 73

def gem_info_for(gem_name)
  return unless info = Gems.info(gem_name)
  comment = info['info']
  comment += " (#{info['source_code_uri']})" if info['source_code_uri']
  comment
end

#squish(text) ⇒ Object



105
106
107
108
109
# File 'lib/annotate_gemfile/annotator.rb', line 105

def squish(text)
  text.gsub(/\A[[:space:]]+/, '')
      .gsub(/[[:space:]]+\z/, '')
      .gsub(/[[:space:]]+/, ' ')
end

#type_of(line) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/annotate_gemfile/annotator.rb', line 86

def type_of(line)
  stripped = line.strip
  return :comment if @block_comment && stripped !=~ /^=end/

  case stripped
  when COMMENT
    :comment
  when BEGIN_COMMENT || END_COMMENT
    @block_comment = !@block_comment
    :comment
  when GEM_DECLARATION
    :gem
  when BLANK_LINE
    :blank
  else
    :unknown
  end
end

#unknown(line) ⇒ Object



58
59
60
# File 'lib/annotate_gemfile/annotator.rb', line 58

def unknown(line)
  line.gsub("\n", '')
end

#word_wrap(text, line_width) ⇒ Object



80
81
82
83
84
# File 'lib/annotate_gemfile/annotator.rb', line 80

def word_wrap(text, line_width)
  squish(text).split("\n").map do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end.join
end