Gem 分享下较新的把 Guard 加入 Gemfile 的正确做法

howiehu · June 05, 2013 · Last by howiehu replied at June 05, 2013 · 3991 hits

最近写 Rails 程序练手的时候,由于在家用 Mac OS X,单位电脑上用 Ubuntu,所以在对 Guard 配置的过程中尤其注重了保证体验一致的办法,其中最主要的就是如何调用系统通知中心这一方面。

先前很多教程或者网上资料中(比如 RailsTutorial 和 RailsCast 等)对 Guard 的 Gemfile 写法都存在过期或冗余,所以自己查找资料更新了相关办法。

详细内容请看:http://blog.huhao.name/blog/2013/06/05/use-guard-in-rails-project/

group :development do
  # If you need to use rspec, please replace this by 'guard-repec'.
  gem 'guard'
  # Use for notify on Mac OS X, you must install growl and growlnotify before.
  gem 'growl'
end

Guard 一定要配置 Notification 嘛?我个人觉得在 Console 里面直接查看 STDOUT 信息足够了。

如果团队中有人用 Mac,有人用 Linux,这个就的确比较麻烦,所以 Guard 官方应该是把具体用哪个 Notification 方式交给开发者自己去决定了吧。我觉得比较麻烦的是 Guardfile 的写法,所以通常都是直接让 guard 自己生成这个文件。

#1 楼 @lgn21st 各有所好,我这里也就是告诉大家,没必要像教程上那样写那么多东西,另外,我这个就是为了解决 Mac 和 Linux 的不一致问题,保证体验是一样的,Guard 自身如不特别设置,是默认搜索可用的一切能够显示结果的地方来显示的。

平台不是可能用 platform 吗? 我以前也用 Notification,后来不用了,确实没必要

确实方便很多。以前要用都是用 development_linux development_mac 这个 group,然后根据自己的系统去 without。

比较烦人的其实是 emacs 和 tmux 的 notifier,这货检测到就使用,颜色配置需要通过 add_notification 时修改,但用了 add_notification 就不会自动检测可用的 notifier 了,后来找到个办法就是用 ~/.guard.rb 去覆盖默认的配置。

require 'guard/notifier'

notifiers_options = {
  :emacs => {
    :fontcolor => "#8fb28f",
    :default => "#2b2b2b",
    :success => "#013009",
    :failed => "#310602",
    :pending => "#534626"
  },
  :tmux => {
    :success                => 'colour64',
    :failed                 => 'colour124',
    :pending                => 'colour136',
    :default                => 'colour239',
    :display_message        => false,
  }
}

::Guard::Notifier::NOTIFIERS.each do |group|
  group.map { |n| n.first }.find { |notifier|
    ::Guard::Notifier.add_notification(notifier,
                                       notifiers_options[notifier] || {},
                                       true)
  }
end
Unknow user #6 June 05, 2013

#4 楼 @ZombieCoder 用通知中心的好处就是可以在 Sublime Text 2 编辑的过程中不需要切换窗口就能看到测试结果。

You need to Sign in before reply, if you don't have an account, please Sign up first.