2020年8月30日日曜日

CLI アプリケーションな gem を作った

CLI アプリケーションな gem を作った

テストでは test-unitsimplecov を使用。

手順を記録していなかったので、思い出しながらまとめ直し。

ひな形作成

$ bundle gem newgem
Creating gem 'newgem'...
Do you want to generate tests with your gem?
Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none): minitest
Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at https://choosealicense.com/licenses/mit. y/(n): n
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see https://bit.ly/coc-enforcement. y/(n): n
      create  newgem/Gemfile
      create  newgem/lib/newgem.rb
      create  newgem/lib/newgem/version.rb
      create  newgem/newgem.gemspec
      create  newgem/Rakefile
      create  newgem/README.md
      create  newgem/bin/console
      create  newgem/bin/setup
      create  newgem/.gitignore
      create  newgem/.travis.yml
      create  newgem/test/test_helper.rb
      create  newgem/test/newgem_test.rb
Initializing git repo in /work/newgem
Gem 'newgem' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html

基本情報更新

  1. newgem/newgem.gemspec を更新
    • spec.author
    • spec.email
    • spec.summary
    • spec.description
    • spec.homepage
    • spec.metadata["source_code_uri"]
    • spec.metadata["changelog_uri"]
  2. spec.metadata["allowed_push_host" = "TODO: Set to 'http://mygemserver.com'" を削除
    • rubygems.org に push できるようにするため

test-unit and simplecov を使うための設定

  1. newgem/Gemfile から minitest の行を削除
  2. newgem/newgem.gemspec に依存関係定義を追加
    • spec.add_development_dependency 'test-unit'
    • spec.add_development_dependency 'simplecov'
  3. newgem/test/test_helper.rb の更新
    • よくわからないけどこんな感じになった

      $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
      
      require 'simplecov'
      require "test/unit"
      
      SimpleCov.start do
        enable_coverage :branch
        add_filter 'test'
      end
      
      require "newgem"
  4. newgem/test/newgem_test.rb の更新
    • 更新というか、テストに合わせて新規作成する感じ。ひな形は以下のような感じ。

      require "test_helper"
      
      require_relative '../lib/test/target/file.rb'
      
      class TestTargetClass < Test::Unit::TestCase
        sub_test_case "method_name" do
          test "test perspective" do
      
            expected = "dummy"
            actual = ...(snip)
      
            assert_equal(expected, actual)
          end
        end
      end

開発手順

依存パッケージ取得

bundle install

テスト

$ bundle exec rake test

ビルド

$ bundle exec rake build

リリース

$ gem signin
$ bundle exec rake release

多分こんな感じ…。 以上。

参考資料

0 件のコメント:

コメントを投稿