Class: AnnotateGemfile::Annotator
- Inherits:
-
Object
- Object
- AnnotateGemfile::Annotator
- 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
- #annotate(filename) ⇒ Object
- #annotate_lines(lines) ⇒ Object
- #blank(line) ⇒ Object
- #comment(line) ⇒ Object
- #describe_gem(line) ⇒ Object
- #gem(line) ⇒ Object
- #gem_info_for(gem_name) ⇒ Object
-
#initialize ⇒ Annotator
constructor
A new instance of Annotator.
- #squish(text) ⇒ Object
- #type_of(line) ⇒ Object
- #unknown(line) ⇒ Object
- #word_wrap(text, line_width) ⇒ Object
Constructor Details
#initialize ⇒ Annotator
Returns a new instance of Annotator.
14 15 16 17 |
# File 'lib/annotate_gemfile/annotator.rb', line 14 def initialize @block_comment = false @progress = ProgressBar.create end |
Instance Method Details
#annotate(filename) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/annotate_gemfile/annotator.rb', line 19 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
32 33 34 35 36 37 38 |
# File 'lib/annotate_gemfile/annotator.rb', line 32 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
48 49 50 |
# File 'lib/annotate_gemfile/annotator.rb', line 48 def blank(line) line.gsub("\n", '') end |
#comment(line) ⇒ Object
52 53 54 |
# File 'lib/annotate_gemfile/annotator.rb', line 52 def comment(line) line.gsub("\n", '') end |
#describe_gem(line) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/annotate_gemfile/annotator.rb', line 60 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
40 41 42 43 44 45 46 |
# File 'lib/annotate_gemfile/annotator.rb', line 40 def gem(line) [ describe_gem(line), line, '', ].compact.flatten.tap { @progress.increment } end |
#gem_info_for(gem_name) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/annotate_gemfile/annotator.rb', line 71 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
103 104 105 106 107 |
# File 'lib/annotate_gemfile/annotator.rb', line 103 def squish(text) text.gsub(/\A[[:space:]]+/, '') .gsub(/[[:space:]]+\z/, '') .gsub(/[[:space:]]+/, ' ') end |
#type_of(line) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/annotate_gemfile/annotator.rb', line 84 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
56 57 58 |
# File 'lib/annotate_gemfile/annotator.rb', line 56 def unknown(line) line.gsub("\n", '') end |
#word_wrap(text, line_width) ⇒ Object
78 79 80 81 82 |
# File 'lib/annotate_gemfile/annotator.rb', line 78 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 |