複数のgemに関する情報を表示する

  • ○○がしたい!!!!
  • どんなライブラリがあるか調べよう!
  • gem search ○○ --remote する
  • 名前だけたくさん出てくる…
  • 調べるのめんどくさい…………

というわけで適当にやっつけで書いてみました。

require "yaml"
require "rubygems/specification"

search_word = ARGV[0]

output = ` gem search #{search_word} --remote `
gem_names = output.split("\n").map{|line|
  /^(.+) \(.+\)$/ =~ line
  $1
}

gem_names.each{|gem_name|
  yaml = ` gem specification #{gem_name} --remote` 
  info = YAML.load(yaml)
  
  puts "** [%s:title=%s (%s)]\n\n" % [info.homepage, info.name, info.version]

  puts "- date: %s" % info.date
  puts "- licenses: %s" % [ info.licenses.empty? ? "(no data)" : info.licenses.join(", ") ]
  puts "- dependencies: %s" % info.dependencies.map{|dep|
    dep.name
  }.join(", ")
  #  puts "requirements: %s" % info.requirements.join(", ")
  puts "- summary: %s" % info.summary

  puts "description: "
  puts ">>"
  puts info.description
  puts "<<\n\n"

  sleep 1
}

gem search に与えるキーワードを与えて実行

ruby list_gem_info.rb {キーワード}

すると、次のように出力されます。以下は「diff」で検索してみた場合。
はてな記法です。

** [http://github.com/agrussellknives/activerecord-diff:title=activerecord-diff (0.0.2)]

- date: 2011-10-13 14:00:00 +0900
- licenses: (no data)
- dependencies: activerecord, activesupport, bundler, rake, sqlite3-ruby
- summary: Gemify Simple diff for ActiveRecord objects.
description: 
>>
Simple diff for ActiveRecord 
<<

** [http://moonwolf.com/:title=algorithm-diff (0.1)]

- date: 2004-10-25 13:00:00 +09:00
- licenses: (no data)
- dependencies: 
- summary: Computes the differences between two arrays of elements
description: 
>>

<<

** [http://github.com/radamant/analdiffist:title=analdiffist (0.4.0)]

- date: 2011-07-06 16:00:00 +0900
- licenses: (no data)
- dependencies: reek, flog, rspec, bundler, jeweler, autotest
- summary: A professional twice over: an analyst and a diffist.
description: 
>>
A tool for comparing the complexity and code smells between two git revisions
<<

(以下略)

※ちなみに gem search のオプションで --details というのもあります(でも動いてくれませんでした…)。
※summary だけでいいなら http://rubygems.org/ で検索するのも良いかも。