fortissimo1997's diary

備忘録的な使い方をする予定

Gemを作ってみた (5) 公開

前回はCoverallsでカバレッジを取得するところまで終わったので、 今回はついにRubyGemsに公開!

公開

まずはバージョンを上げる。

$ rake version:bump:patch
Current version: 0.1.0
Updated version: 0.1.1

今回はversion:bump:patchを使ったが、version:bump:minor,version:bump:majorも今後は使う機会がありそう(・∀・)
(色々試していて、いつの間にか0.1.1に(T_T))

ここでようやくリリース

$ rake release
Committing simplecov-lcov.gemspec
Pushing master to origin
Tagging v0.1.1
Pushing v0.1.1 to origin
Generated: simplecov-lcov.gemspec
simplecov-lcov.gemspec is valid.
WARNING:  description and summary are identical
  Successfully built RubyGem
  Name: simplecov-lcov
  Version: 0.1.1
  File: simplecov-lcov-0.1.1.gem
Executing "gem push ./pkg/simplecov-lcov-0.1.1.gem":
gem push ./pkg/simplecov-lcov-0.1.1.gem
Enter your  credentials.
Don't have an account yet? Create one at https:///sign_up
   Email:   

どうやらRubyGemsへの登録が必要らしい^^;

登録は以下の画面から

f:id:fortissimo1997:20140627133759p:plain

すぐに終わったので続きへ

   Email:  your@address.com
Password:  password
Signed in.
Pushing gem to https://rubygems.org...
Successfully registered gem: simplecov-lcov (0.1.1)

公開されたgemがこちら!(2014/06/27 現在)

f:id:fortissimo1997:20140627134503p:plain

公開された後はtweetされるらしい(・∀・)

あっさりと公開できてしまった(゚∀゚)
現在、CodeClimateGemnasiumが入った0.2.1を公開中です!

ということで、次回からは公開後に入れたサービス群について

Gemを作ってみた (4) Coveralls

前回TravisCIの導入が完了したので、 今回はCoverallsを導入する。

導入

サインインしてリポジトリをアクティブにするところまでは前回と変わらず。

f:id:fortissimo1997:20140618152010p:plain

あとはドキュメントにしたがって、

  • Gemfileにcoverallsを追加
  • spec_helper.rbに以下のコードを追加
require 'coveralls'

Coveralls.wear!

カバーオールを着るんですね(・∀・)

結果

f:id:fortissimo1997:20140618153530p:plain

変なファイルのカバレッジまでとってる( ゚д゚)

再実装

CoverallsのフォーマッタをSimpleCov::Formatter::MultiFormatterに渡すように変更

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
  SimpleCov::Formatter::HTMLFormatter,
  Coveralls::SimpleCov::Formatter
]

コミットしてTravisCIの結果を待つ

...

Coveralls無反応...

もう一度spec_helper.rbを確認

ENV['COVERAGE'] && SimpleCov.start do
  add_filter '/.rvm/'
end

jewelerが自動出力した部分に罠が(T_T)
環境変数COVERAGEの設定が必要なのね、、、

.travis.yml環境変数の設定を入れて再実行

f:id:fortissimo1997:20140618153514p:plain

ようやく100%に(・∀・)
これで公開できそう!

ということで、次回はRubygemsに公開

Gemを作ってみた (3) TravisCI

前回までで大まかな実装は完了しているため、 今回はTravisCIでテストを自動実行できるように設定を行ってみる

導入

サインインしてAccountsから対象のリポジトリをアクティブにするだけ

とっても簡単(・∀・)

f:id:fortissimo1997:20140616151802p:plain

.travis.yml

TravisCIの設定ファイルは.travis.ymlに置くというところまでわかっていたので、 とりあえずドキュメントを読んでみることにした。

今回は、SimpleCov用のフォーマッタなので、SimpleCovがサポートしていない1.8系はテストしなくて良さそう。

ということで、.travis.ymlは以下の通りとした。

language: ruby
rvm:
  - 1.9.3
  - 2.0.0
  - 2.1.1

.travis.ymlを追加してコミット・プッシュ

f:id:fortissimo1997:20140616151824p:plain

あっさり通っちゃいました(゚д゚)!

複数の設定した3つのバージョンで同時にテストが実行されるため、ほとんど待たずに終わってしまった。

次回はカバレッジ測定のためのCoveralls導入

Gemを作ってみた (2) Rakefile

gemspecファイルを作りたい

jewelerではrakeタスクでgemspecを自動生成してくれるらしい。 そこで一旦タスクリストを確認してみる

$ rake -T

# (略)

rake gemspec             # Generate and validate gemspec
rake gemspec:debug       # Display the gemspec for debugging purposes, as jeweler knows it (not from the filesystem)
rake gemspec:generate    # Regenerate the gemspec on the filesystem
rake gemspec:release     # Regenerate and validate gemspec, and then commits and pushes to git
rake gemspec:validate    # Validates the gemspec on the filesystem
rake git:release         # Tag and push release to git
rake install             # Build and install gem using `gem install`
rake rdoc                # Build RDoc HTML files
rake release             # Release gem
rake rerdoc              # Rebuild RDoc HTML files
rake simplecov           # Code coverage detail
rake spec                # Run RSpec code examples
rake version             # Displays the current version
rake version:bump:major  # Bump the major version by 1
rake version:bump:minor  # Bump the a minor version by 1
rake version:bump:patch  # Bump the patch version by 1
rake version:write       # Writes out an explicit version

gemspec生成だけでなく、versionの管理等もやってくれるようだ(゚∀゚) とりあえずgemspecだけでも作ってみよう

$ rake gemspec
Expected VERSION or VERSION.yml to exist. Use 'rake version:write' to create an initial one.

、、、どうやらVERSIONが必要らしい(・_・;) そのためのタスクもあったので実行

$ rake version:write
Updated version: 0.0.0
$ cat VERSION
0.0.0

とりあえずバージョン0ができた(^_^;) これで待望のgemspecができる!

$ rake gemspec
Generated: simplecov-lcov.gemspec
simplecov-lcov.gemspec is valid.

gemspecの中身を確認

とりあえず、できたものの中身を見てみることすする

$ cat simplecov-lcov.gemspec
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
  s.name = "simplecov-lcov"
  s.version = "0.0.0"

  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
  s.authors = ["XXX XXX"]
  s.date = "2014-XX-XX"
  s.description = "TODO: longer description of your gem"
  s.email = "XXX.XXX@xxx.co.jp"
# 中略
end

最初のコメントに書いてあるとおり、 gemspecは直接いじってはいけないらしい。

それよりも気になるのがauthorsemail! 最初に指定したGitHubのUserのものではない( ゚д゚)

改めてRakefileも確認してみる

$ cat Rakefile
# 中略
require 'jeweler'
Jeweler::Tasks.new do |gem|
  # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
  gem.name = "simplecov-lcov"
  gem.homepage = "http://github.com/fortissimo1997/simplecov-lcov"
  gem.license = "MIT"
  gem.summary = %Q{TODO: one-line summary of your gem}
  gem.description = %Q{TODO: longer description of your gem}
  gem.email = "XXX.XXX@xxx.co.jp"
  gem.authors = ["XXX XXX"]
  # dependencies defined in Gemfile
end

homepageは指定したものだが、emailauthor~/.gitconfigのものを取ってきているようだ、、、 ~/.gitconfigは社用のアカウントが設定してあったため、私用のものに書きなおさなければ、、、

書きなおしてgit commit --amendでコミットしなおして一見落着。 と思ったが、一応git logを確認

$ git log
commit xxx
Author: fortissimo1997 <mail-for-github@xxx.com>
Date:   someday

  Version bump to 0.0.0

commit yyy
Author: XXX XXX <XXX.XXX@xxx.co.jp>
Date:   someday

  Initial commit to simplecov-lcov.

最初のコミットもコミッタが違いますね、、、 ~/.gitconfigを書き換えてから作りなおしたほうが早いかも(;_;)

結局、LICENSE.txtREADME.rdocにもあることが判明したため、 そちらも書き換えることに、、、

思わぬところに時間を取られたが、これで公開する準備が整った(・∀・)

次回はTravisCIの導入

Gemを作ってみた (1) jeweler

改めて、、、

jeweler2は動かなかったので、改めてjewelerをインストール

$ gem install jeweler
$ jeweler
Usage: jeweler [options] reponame
e.g. jeweler the-perfect-gem
        --directory [DIRECTORY]      specify the directory to generate into (deprecated)

        --rspec                      generate rspec code examples
        --shoulda                    generate shoulda tests
        --testunit                   generate test/unit tests
        --bacon                      generate bacon specifications
        --testspec                   generate test/spec tests
        --minitest                   generate minitest tests
        --micronaut                  generate micronaut examples
        --riot                       generate riot tests
        --shindo                     generate shindo tests

        --[no-]bundler               use bundler for managing dependencies
        --cucumber                   generate cucumber stories in addition to the other tests

        --reek                       generate rake task for reek
        --roodi                      generate rake task for roodi

        --summary [SUMMARY]          specify the summary of the project
        --description [DESCRIPTION]  specify a description of the project

        --user-name [USER_NAME]      the user's name, ie that is credited in the LICENSE
        --user-email [USER_EMAIL]    the user's email, ie that is credited in the Gem specification

        --github-username [GITHUB_USERNAME]
                                     name of the user on GitHub to set the project up under
        --git-remote [GIT_REMOTE]    URI to set the git origin remote to
        --homepage [HOMEPAGE]        the homepage for your project (defaults to the GitHub repo)
        --create-repo                create the repository on GitHub

        --yard                       use yard for documentation
        --rdoc                       use rdoc for documentation
    -v, --version                    show version
    -h, --help                       display this help and exit

オプションはテストにRSpecを入れるだけのはずなので、jeweler --rspec simplecov-lcovでいけそう

$ jeweler --rspec simplecov-lcov
Please specify --github-username or set github.user in ~/.gitconfig (see http://github.com/blog/180-local-github-config for details). For example: git config --global github.user defunkt

どうやら--github-usernameは追加する必要がありそう、、、

$ jeweler --rspec --github-username fortissimo1997 simplecov-lcov
The directory simplecov-lcov already exists. Maybe move it out of the way before continuing?

先ほどGitHubリポジトリを作った時にgit cloneしていたのが仇に、、、 ディレクトリを消して再実行

$ rm -rf simplecov-lcov
$ jeweler --rspec --github-username fortissimo1997 simplecov-lcov
        create  .gitignore
        create  Rakefile
        create  Gemfile
        create  LICENSE.txt
        create  README.rdoc
        create  .document
        create  lib
        create  lib/simplecov-lcov.rb
        create  spec
        create  spec/spec_helper.rb
        create  spec/simplecov-ldov_spec.rb
        create  .rspec
Jeweler has prepared your gem in ./simplecov-lcov

ようやくリポジトリ作成成功! これで実装が始められる(・∀・)

この時は自動生成されたファイルをあまり確認せずに実装に入ってしまいました が、このことを後悔することになるとは思ってもいませんでした、、、

次回へ続く

Gemを作ってみた (0) 導入編

きっかけ

t-wadaさんの以下のtweet

使いたい!、、、でもRubyでLcov形式のカバレッジレポートを出力できたかな、、、

ないなら作ればいいじゃない! ということで作ってみることに

要件

既存のテストにあまり手を加えなくていいように、SimpleCovのカスタムフォーマッタとして作成することに

RSpecでテストを書いて、TravisあたりでCIできたら、と漠然と考えてました

着手、、、?

とりあえずgithubリポジトリを作成 自動でライセンスやREADMEを作ってもらう

gemの作成支援用にjeweler(?)というgemがあるらしいというのを聞いたのでインストールしてみる

$ gem install jweler
ERROR:  Could not find a valid gem 'jweler' (>= 0) in any repository
ERROR:  Possible alternatives: jeweler, jeweler2, aweber, peeler, weeler

怪我の功名か、jewelerには2もあるらしい 2の方がいいという保証はないが、見つけたからには使ってみよう

$ gem install jeweler2
$ jeweler --rspec --github-username fortissimo1997 simplecov-lcov
/home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/lib/jeweler/generator.rb:221:in `read': No such file or directory - /home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/lib/jeweler/templates/.gitignore (Errno::ENOENT)
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/lib/jeweler/generator.rb:221:in `render_template'
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/lib/jeweler/generator.rb:230:in `output_template_in_target'
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/lib/jeweler/generator.rb:185:in `create_files'
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/lib/jeweler/generator.rb:126:in `run'
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/lib/jeweler/generator/application.rb:29:in `run!'
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/jeweler2-2.0.9/bin/jeweler:5:in `<top (required)>'
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/bin/jeweler:23:in `load'
        from /home/fortissimo1997/.rbenv/versions/1.9.3-p448/bin/jeweler:23:in `<main>'

( ゚д゚) どうやらgemにファイルが足りないらしい、、、 おとなしくjewelerをインストールしてやり直すか、、、

次回へ続く