From c7a85ed4064c2de4305fc7ed758c49645c897707 Mon Sep 17 00:00:00 2001 From: Chris DiMartino Date: Fri, 8 May 2015 12:15:45 -0400 Subject: [PATCH] Push formatting output through formatter class --- lib/gemtastic.rb | 104 +----------------- lib/gemtastic/annotation.rb | 28 +---- .../formatters/annotation_formatter.rb | 68 ++++++++++++ .../colorized_annotation_formatter.rb | 4 + 4 files changed, 78 insertions(+), 126 deletions(-) create mode 100644 lib/gemtastic/formatters/annotation_formatter.rb create mode 100644 lib/gemtastic/formatters/colorized_annotation_formatter.rb diff --git a/lib/gemtastic.rb b/lib/gemtastic.rb index c6c9206..fba2177 100644 --- a/lib/gemtastic.rb +++ b/lib/gemtastic.rb @@ -5,108 +5,10 @@ module Gemtastic require 'gemtastic/gemfile' require 'gemtastic/source' + require 'gemtastic/formatters/annotation_formatter.rb' + require 'gemtastic/formatters/colorized_annotation_formatter.rb' + def self.ate file Gemfile.new(File.read(file)) end end - -# class -# def initialize -# process("Gemfile.test") -# end - -# def process(gemfile) -# # Assign gemfile path relative to location of execution -# local_gemfile = File.dirname(__FILE__) + "/../Gemfile.test" - -# # Exit if file doesn't exist -# abort("Could not find the file, exiting") unless File.file?(local_gemfile) - -# # Create new empty array, we'll store the list of gems here -# gemlist ||= [] - -# # Load the gems in the gemfile, stash each gem and it's version -# gemfile = Gemnasium::Parser::Gemfile.new(File.open(local_gemfile).read) -# gemfile.dependencies.each_with_index.map {|x, i| gemlist << [x.name, x.requirement.as_list, x.groups]} - -# # Sort by groups and group by uniques -# gemlist = gemlist.sort_by {|gem| gem[2].to_s }.group_by {|gem| gem[2].uniq} - -# # TODO: Fetch gem info from API server - -# # iterate through server and fill in gem info -# temp_file = Tempfile.new(File.dirname(__FILE__) + "Gemfile.test.tmp") -# File.open(local_gemfile, 'r') do |f| -# f.each_line do |line| -# line = line.gsub("\t", ' ') -# temp_file.puts line.include?("gem") ? gem_comment(line) : line -# end -# end -# temp_file.close -# FileUtils.mv(temp_file.path, local_gemfile) - -# end - -# def gem_comment(line) -# comment = "\n" - -# # Add prerequisite tabs -# line[/\A */].size.times { comment += " " } - -# # Add some arbitrary comment -# comment += "# This is the best comment evar." -# comment += "\n" -# return comment+line -# end - -# def process_old(gemfile) -# # Assign gemfile path relative to location of execution -# local_gemfile = File.dirname(__FILE__) + "/../Gemfile.test" - -# # Exit if file doesn't exist -# abort("Could not find the file, exiting") unless File.file?(local_gemfile) - -# # Create new empty array, we'll store the list of gems here -# gemlist ||= [] - -# # Load the gems in the gemfile, stash each gem and it's version -# gemfile = Gemnasium::Parser::Gemfile.new(File.open(local_gemfile).read) -# gemfile.dependencies.each_with_index.map {|x, i| gemlist << [x.name, x.requirement.as_list, x.groups]} - -# # Sort by groups and group by uniques -# gemlist = gemlist.sort_by {|gem| gem[2].to_s }.group_by {|gem| gem[2].uniq} - -# # TODO: Fetch gem info from API server - -# # Find the source(s) -# source = "" -# File.open(local_gemfile, 'r') do |f| -# f.each_line do |line| -# if line.include?("source") -# source += line -# end -# end -# end - -# temp_file = Tempfile.new(File.dirname(__FILE__) + "Gemfile.test.tmp") -# temp_file.puts source - -# gemlist.each do |group, value| -# unless group.include?(:default) -# group_names = group.map{|x| ":"+x.to_s}.join(',') -# puts "group #{group_names} do\n" -# end -# end - -# # temp_file.close -# # FileUtils.mv(temp_file.path, local_gemfile) - -# # File.open(local_gemfile, "w+") do |file| -# # file.each_line do |line| -# # file.puts "#comment here\n" + line -# # end -# # end - -# end -# end -# end diff --git a/lib/gemtastic/annotation.rb b/lib/gemtastic/annotation.rb index 1cb258f..b13ac88 100644 --- a/lib/gemtastic/annotation.rb +++ b/lib/gemtastic/annotation.rb @@ -7,30 +7,14 @@ class Annotation API = 'https://rubygems.org/api/v1/gems/' - def initialize gem, indent=nil, annotations=nil + def initialize gem, indent=nil, formatter=Gemtastic::AnnotationFormatter @gem = gem @indent = indent - @annotations = annotations || { - 'homepage_uri' => 'Homepage', - 'source_code_uri' => 'Source', - 'documentation_uri' => 'Documentation' - } + @formatter = formatter.new(self) end def to_s - <<-EOL.chomp -#{indent}#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ -#{indent}#~# -#{indent}#~# #{get['name']}: -#{indent}#~# #{"-" * get['name'].length} -#{indent}#~# -#{indent}#~# #{get['info'].gsub(/\n/, "\n#{indent}#~# ")} -#{indent}#~# -#{annotate} -#{indent}#~# -#{indent}#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ -#{indent}#~# - EOL + @formatter.to_s end def get @@ -49,11 +33,5 @@ def gem_api_url def self.source_string? str /\A#~#/.match str end - - def annotate - annotations.each_pair.map { |uri, header| - "#{indent}#~# #{'%-14s' % (header<<':')} #{get[uri]}" - }.join("\n") - end end end diff --git a/lib/gemtastic/formatters/annotation_formatter.rb b/lib/gemtastic/formatters/annotation_formatter.rb new file mode 100644 index 0000000..6c15412 --- /dev/null +++ b/lib/gemtastic/formatters/annotation_formatter.rb @@ -0,0 +1,68 @@ +module Gemtastic + class AnnotationFormatter + private + attr_reader :annotation, :annotations + + public + def initialize annotation, annotations=nil + @annotation = annotation + @annotations = annotations || { + 'homepage_uri' => 'Homepage', + 'source_code_uri' => 'Source', + 'documentation_uri' => 'Documentation' + } + end + + def to_s + to_s_lines.map { |line| "#{indent}#{line}" }.join("\n") + end + + private + + def to_s_lines + [ + header, + name, + underline, + spacer, + description, + spacer, + annotate, + spacer, + header, + spacer + ].flatten + end + + def spacer + "#~#" + end + + def header + "#~" * 40 + end + + def description + "#{spacer} #{annotation.get['info'].gsub(/\n/, "\n#{indent}#{spacer} ")}" + end + + def name + "#{spacer} #{annotation.get['name']}" + end + + def underline + "#{spacer} " + ('-' * annotation.get['name'].length) + end + + def annotate + @annotations.each_pair.map { |uri, header| + "#{spacer} #{'%-14s' % (header<<':')} #{annotation.get[uri]}" + } + end + + def indent + @annotation.indent + end + + end +end diff --git a/lib/gemtastic/formatters/colorized_annotation_formatter.rb b/lib/gemtastic/formatters/colorized_annotation_formatter.rb new file mode 100644 index 0000000..0548c79 --- /dev/null +++ b/lib/gemtastic/formatters/colorized_annotation_formatter.rb @@ -0,0 +1,4 @@ +module Gemtastic + class ColorizedAnnotationFormatter < AnnotationFormatter + end +end