LoginSignup
6
3

More than 5 years have passed since last update.

rubotyのgemを作るための小ネタ ( local でGEMを作るときにも! )

Last updated at Posted at 2016-07-20

TL;DR

rubotyのgem作成にあたって、覚えておいた方がいいのは下記2点

  • RubotyGeneratorでひな形
  • 呼び出し元のRubotyでは、bundle configでgemのpath書き換えると捗る

RubotyGeneratorを使う

ほとんど、作者の方のブログに書かれていますが、
この記事だけで完結するように、簡単にしたものをこちらに記述させて頂きます。

gem installと設定

gem install ruboty-generator
ruboty-generator init
vim RubotyGenerator

RubotyGeneratorの中身

user_name @github_user_name

gem_class_name "Sample" # rubotyのtask classの名前
gem_name "sample" # gemの名前がruboty-#{gem_name} となる

# rubotyにて利用する環境変数を指定する
env do |e|
  e.name "ENV1"
  e.description "ENV1 desc"
end

# rubotyのコマンドを指定する
command do |c|
  c.name "name"
  c.pattern "pattern\\z"
  c.description "description"
end

gemの雛形作成

% ruboty-generator generate -a
% tree                                                                       
.
├── CODE_OF_CONDUCT.md
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│   ├── console
│   └── setup
├── lib
│   └── ruboty
│       ├── handlers
│       │   └── sample.rb
│       ├── sample
│       │   ├── actions
│       │   │   └── name.rb
│       │   └── version.rb
│       └── sample.rb
├── ruboty-sample.gemspec
└── spec
    ├── ruboty
    │   └── sample_spec.rb
    └── spec_helper.rb
lib/ruboty/handlers/sample.rb
module Ruboty
  module Handlers
    class Sample < Base
      on /pattern\z/, name: 'name', description: 'description'
      env :ENV1, "ENV1 desc"

      def name(message)
        Ruboty::Sample::Actions::Name.new(message).call
      end

    end
  end
end

Gemの開発

ベースとなるrubotyの設定をしておく

ここを参照.

ベースとなるRubotyのGemfile設定

方法は2つあるので、お好きな方をお試し下さい。
参考URL: http://bundler.io/git.html#local

1.Gemfileの設定を書き換える

一番簡単ではあるものの、呼び出し元を修正する必要があった場合に、
Commitのたびに元に戻すなど面倒。

gem 'ruboty-capistrano', path: '/Users/project/ruboty-capistrano'

2. bundler configでgemのpathを上書きする

呼び出し元を一緒に更新するなら一番おすすめ。
修正完了後、元に戻すことを忘れずに。

  • pathの上書き方法
% bundle config local.ruboty-capistrano /Users/shuhei_morioka/project/morioka/ruboty-capistrano
% bundle show ruboty-capistrano                                              
/Users/shuhei_morioka/project/morioka/ruboty-capistrano
bundle config disable_local_branch_check true #localでの変更をチェックする
  • 上書きした path を削除する方法
% bundle config --delete local.ruboty-capistrano
% bundle show ruboty-capistrano                                              
/Users/shuhei_morioka/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/ruboty-capistrano-2b4b08d944a7

あとは handler なり action なりにコードを記述すればOK!

最後に

この方法で作成した rubotyのgemがこちらになります!

ruboty利用者の認可認証を行う
https://github.com/selmertsx/ruboty-authorization

rubotyを使った deploy ができる
https://github.com/selmertsx/ruboty-capistrano

6
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
6
3