LoginSignup
1
1

More than 5 years have passed since last update.

rubocop設定を共有するためのgemの作り方

Last updated at Posted at 2018-04-22

よく 開発チームで横断的にrubocopを共有してルールを統一化した! というのを見かけて
自分でもやってみたいなと思い、普段gemを作ったことがない人でも作れるように
メモがてら手順を起こしました。

作り方

gemの雛形作成

rubocopを管理するgemを作る。
今回は moshi2police 1という名前のGEMを想定してサンプルを作る

bundle gem moshi2police --test=rspec

※テストはrspecを指定したがminitestがいい場合は書き換える

gemspecの書き換え

以下2点を行う

  • gemの詳細の書き換え
  • rubocopの追記
gem名.gemspec
# 前略

Gem::Specification.new do |spec|
  # ここらへんの情報はローカルgemなのでご自由に
  spec.name          = "moshi2police"
  spec.version       = Moshi2police::VERSION
  spec.authors       = ["Sample Taro"]
  spec.email         = ["sample@sample.com"]

  spec.summary       = "rubocop conifg file to team"
  spec.description   = "rubocop conifg file to team"
  spec.homepage      = "https://sample.com"
  spec.license       = "MIT"

  spec.files         = `git ls-files -z`.split("\x0").reject do |f|
    f.match(%r{^(test|spec|features)/})
  end
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  # rubocopのバージョンはここでコントロールする
  spec.add_dependency 'rubocop', '~> 0.53.0'
  ()
end

rubocop.ymlの配置

configディレクトリを作成して、共有したい rubocop.yml を配置

作る場合の例
$ mkdir config
$ cd config 
$ vi rubocop.yml

使い方

Gemの追加

gemファイルに作ったgemファイルのgit情報を記述する。
httpsで取得できるURL記述すれば良い

/.Gemfile
group :development, :test do
   gem 'moshi2police' , :git => 'https://github.com/{該当するジェムのパス}'
end

なお、privateリポジトリを指定すると
CI環境等でbundle installの度に
IDとパスワードの入力要求を求められてしまいます。
そのため、環境変数でgithub関係の情報を記述しておくと良いです。

$ export BUNDLE_GITHUB__COM={githubのユーザ名}:{パーソナルアクセストークン}

rubocopへの追加

利用したいプログラムのrubocop.ymlに
以下のように記述する

/.rubocop.yml
# inherit_gem で gem名:gem内の配置場所 で記述
inherit_gem:
  moshi2police: "config/rubocop.yml"

参考にしたもの


  1. githubにサンプルもアップしてある。もしもしポリスメン。 

1
1
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
1
1