Emacsでgtagsと真剣に向き合い楽しくRails開発する2017

これまで

めちゃくちゃgrepしてた。もうやめたい。

やりたいこと。

  • タグジャンプがしたい
  • できればhelm/anythingで
  • タグファイルは自動更新してほしい

実行

gtagsのインストール

brew install global --with-exuberant-ctags --with-pygments

gtags.elはなんかパス通ってた

なければ
www.gnu.org
から落として展開してelみつけてパスの通ったディレクトリに置いて。githubとかで開発されてない。

anythin-gtagsのインストール

M-x auto-install-from-url [RET] https://www.emacswiki.org/emacs/download/anything-gtags.el

設定

(require 'gtags)
(require 'anything-gtags)
(add-hook 'ruby-mode-hook
          '(lambda()
             (gtags-mode 1)))

とりまruby-modeにフック

anythingの定義とかキーバインドとか

F3は定義ジャンプの定番(Eclipse教徒、信者ではない)

(defun anything-gtags-from-here ()
    (interactive)
    (anything
     :sources '(anything-c-source-imenu
                anything-c-source-gtags-select
                )
     :input (thing-at-point 'symbol)))
(global-set-key [f3] 'anything-gtags-from-here)

自動更新

;; update GTAGS
(defun update-gtags (&optional prefix)
  (interactive "P")
  (let ((rootdir (gtags-get-rootpath))
        (args (if prefix "-v" "-iv")))
    (when rootdir
      (let* ((default-directory rootdir)
             (buffer (get-buffer-create "*update GTAGS*")))
        (save-excursion
          (set-buffer buffer)
          (erase-buffer)
          (let ((result (shell-command "/usr/local/bin/gtags --gtagslabel=pygments")))
            (if (= 0 result)
                (message "GTAGS successfully updated.")
              (message "update GTAGS error with exit status %d" result))))))))

参考URLと違って生でshell-command叩いてる。特に深い意味はない。

プロジェクト初期作業

gtagsをプロジェクトルートで一回実行しておく

$ cd /path/to/project/
$ /usr/local/bin/gtags --gtagslabel=pygments

実際はaliasを設定しておけばよく、こんなコマンド覚える必要はない。

確認

メソッドがあるかどうかを確認するには global <メソッド名>

$ global new
app/controllers/users_controller.rb

*1

結果

ファイルを保存したらgtagsが走り、データベースが再生成される。メソッド呼び出しでF3押せばanythingで検索、ジャンプできる。M-*でジャンプ先から戻れる。
すごーい!
たのしー

*1:newメソッドはRailsプロジェクトなら多分あるよね。。。

Rails Tutorial 6章

minitest-reportersと(結局)guardを入れました。
じゃぁということでterminal-notifier-guardも入れて通知領域にguardの結果が表示されます。

guardよいですね。Emacsのモードラインが赤くなったり緑になったり大変気持ち良い

仕事ではJava書いてますけどやっぱrubyは良いです。

Gemfile
group :development, :test do
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3','1.3.11'
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug','9.0.0', platform: :mri
  gem 'rails-controller-testing'
  gem 'minitest-reporters',       '1.1.9'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
  gem 'terminal-notifier-guard', '~> 1.6.1'
end
Guardfile
# Guardのマッチング規則を定義
guard :minitest, spring: "bin/rails test", all_on_start: false do
  watch(%r{^test/(.*)/?(.*)_test\.rb$})
  watch('test/test_helper.rb') { 'test' }
  watch('config/routes.rb')    { integration_tests }
  watch(%r{^app/models/(.*?)\.rb$}) do |matches|
    "test/models/#{matches[1]}_test.rb"
  end
  watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
    resource_tests(matches[1])
  end
  watch(%r{^app/views/([^/]*?)/.*\.html\.erb$}) do |matches|
    ["test/controllers/#{matches[1]}_controller_test.rb"] +
    integration_tests(matches[1])
  end
  watch(%r{^app/helpers/(.*?)_helper\.rb$}) do |matches|
    integration_tests(matches[1])
  end
  watch('app/views/layouts/application.html.erb') do
    'test/integration/site_layout_test.rb'
  end
  watch('app/helpers/sessions_helper.rb') do
    integration_tests << 'test/helpers/sessions_helper_test.rb'
  end
  watch('app/controllers/sessions_controller.rb') do
    ['test/controllers/sessions_controller_test.rb',
     'test/integration/users_login_test.rb']
  end
  watch('app/controllers/account_activations_controller.rb') do
    'test/integration/users_signup_test.rb'
  end
  watch(%r{app/views/users/*}) do
    resource_tests('users') +
    ['test/integration/microposts_interface_test.rb']
  end
end

途中で一度Springを全部殺すというのをやった。

kill `ps ax|grep spring|grep -v grep|cut -d" " -f 1`
kill `ps ax|grep spring|grep -v grep|cut -d" " -f 2`

Rails Tutorial 5章

scss初めてちゃんと触ったかも。まぁまぁ良いじゃん。単品で使う気にはならないけれど。コンパイルがめんどくさそう。Railsみたいに仕組みが上手く動いていたらとても良いですね。

結合テストで怒られたので言われたとおりにgem入れる

Error:
SiteLayoutTest#test_layout_links:
NoMethodError: assert_template has been extracted to a gem. To continue using it,
        add `gem 'rails-controller-testing'` to your Gemfile.
    test/integration/site_layout_test.rb:9:in `block in <class:SiteLayoutTest>'

testがわからんからサイトよんだ

昔はRspecだった気がするのだけれど。
railsguides.jp

むかしはguardかけてGrowlに出すとかに血道を上げていたけど、いまはもう、`rails test`って普通に叩いて満足だもんな。

Rails Tutorial 2章~3章

croud9とか使ってないんじゃ。
rvmで2.4入れてるんじゃ。
rubyの界隈では最新に追いついておくのが定石。
最新でウガーっとうなるのがよくあるパターン。
コミュニティが活発なのでなんとかなる。
この場合、
github.com
これ食らう。

そこでGemfileをtutorialからちといじる

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '5.0.1'
# Use Puma as the app server
gem 'puma', '3.4.0'
# Use SCSS for stylesheets
gem 'sass-rails', '5.0.6'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '3.0.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '4.2.1'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails','4.1.1'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '5.0.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '2.4.1'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3','1.3.11'
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug','9.0.0', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '3.3.1'
  gem 'listen', '3.0.8'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring','1.7.2'
  gem 'spring-watcher-listen', '2.0.0'
end

group :production do
  gem 'pg','0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

5.0.1で治らんというひともいるけど我が環境では治った。

成果物
Home | Ruby on Rails Tutorial Sample App

cloud9ちょっと昼休みに使ったけどコレすごい良いね。
Emacsいらんやんとおもったり。

Emacsには
qiita.com
qiita.com
を入れて現代風。
flymakeは古いんじゃー。まだ設定ファイルに残っているけど。整理しなきゃだね。