LoginSignup
3
3

More than 5 years have passed since last update.

Gemfile にある Gem の説明を自動でコメントとして追記してくれる Gem

Last updated at Posted at 2016-05-04

AnnotateGem

Gemfile に書かれている Gem の概要と Web サイトの URL を自動でコメントしてくれる Gem。チーム開発していて、全ての Gem が把握できていない場合とか便利そう。

使ってみる

AnnotateGem を入れて、

gem install annotate_gem

例えば以下のような Gemfile を用意してみる。

Gemfile
source 'https://rubygems.org'

gem 'rails'
gem 'jquery-rails'
gem 'haml-rails'
gem 'mysql2'
gem 'aws-sdk'

AnnotateGem を実行!

$ annotate-gem
Fetching gem metadata.......

すぐ終わった。
Gemfile を確認すると

Gemfile
source 'https://rubygems.org'

# Full-stack web application framework. (http://www.rubyonrails.org)
gem 'rails'
# Use jQuery with Rails 3 (http://rubygems.org/gems/jquery-rails)
gem 'jquery-rails'
# let your Gemfile do the configuring (http://github.com/indirect/haml-rails)
gem 'haml-rails'
# A simple, fast Mysql library for Ruby, binding to libmysql (http://github.com/brianmario/mysql2)
gem 'mysql2'
# AWS SDK for Ruby (http://github.com/aws/aws-sdk-ruby)
gem 'aws-sdk'

おー!コメントが追記されている!

中を見てみる

spec_finder.rb
module AnnotateGem
  module SpecFinder
    extend self
    ...
    def find_matching_specs_for(gem_lines)
      gem_lines.each do |line|
        matching_specs = Gem::Dependency.new(line.name).matching_specs
        # TODO: need to find latest
        line.spec = matching_specs.first if matching_specs.any?
      end
    end
gem_line.rb
module AnnotateGem
  class GemLine
  ...
    def description
      "#{spec.summary}" if spec
    end

    def website
      "#{spec.homepage.to_s}" if spec
    end

    def description_and_website
      output = "#{description}"
      output << " (#{website})" unless website.nil? || website.empty?
      output
    end

中を見てみると find_matching_specs_for メソッドで Gem::Specification を取得して、 Gem の summary と homepage を取得&出力しているだけですね。

なるほどー。こういう使い方があったか。
ちょっとのアイディアで便利なライブラリが作れるもんですね。

あと

% tree annotate_gem-0.0.10/
annotate_gem-0.0.10/
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── annotate_gem.gemspec
├── bin
│   ├── annotate-gem
│   └── annotate_gem
...

こういう風に Gem の bin 下に実行可能なファイルを置いておくと、コマンドラインから実行出来るようになるんですね。知らなかった!

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3