Permalink
Browse files

Merge remote-tracking branch 'origin/pr/407'

Conflicts:
	CHANGELOG.md
  • Loading branch information...
2 parents a6d7392 + 71cbe93 commit 8f1f00b8eee716bea17e79461e33a2287e1bd5c0 @netzpirat netzpirat committed Mar 27, 2013
View
@@ -4,6 +4,7 @@
- [#400][] Drop dependency on `terminal-table` and use `formatador` instead. ([@netzpirat][])
- Change the current work dir to the `watchdir`. ([@netzpirat][])
+- Add file notifier to write notifications to a configured file. ([@amiel][])
### Bug fixes
View
@@ -265,6 +265,36 @@ You can use nice powerline chars here if you have that configured.
You can get the message history by using `Ctrl+b ~` (where `Ctrl+b` is your key to activate TMux).
+### File
+
+* You can also have Guard write notifications to a file. Each notification will
+ overwrite the file. This allows other commands to be run based on the status
+ of other guard commands.
+
+Example:
+
+```ruby
+# Guardfile
+notification :file, path: '.guard_result'
+
+guard :shell do
+ watch '.guard_result' do
+ if File.read('.guard_result').lines.first.strip == 'failed'
+ # ...
+ end
+ end
+end
+```
+
+Configuration:
+
+```ruby
+# Guardfile
+notification :file,
+ :path => '.guard_result', # Required, no default
+ :format => 'result: %s\ntitle: %s\nmessage: %s\n' # Default: '%s\n%s\n%s\n'
+```
+
Add Guard plugins
-----------------
@@ -49,6 +49,7 @@ module Notifier
require 'guard/notifiers/terminal_notifier'
require 'guard/notifiers/terminal_title'
require 'guard/notifiers/tmux'
+ require 'guard/notifiers/file_notifier'
extend self
@@ -66,7 +67,8 @@ module Notifier
],
[[:emacs, ::Guard::Notifier::Emacs]],
[[:tmux, ::Guard::Notifier::Tmux]],
- [[:terminal_title, ::Guard::Notifier::TerminalTitle]]
+ [[:terminal_title, ::Guard::Notifier::TerminalTitle]],
+ [[:file, ::Guard::Notifier::FileNotifier]]
]
# Get the available notifications.
@@ -0,0 +1,55 @@
+module Guard
+ module Notifier
+
+ # Writes guard notification results to a file
+ #
+ # @example Add the `:file` notifier to your `Guardfile`
+ # notification :file, path: 'tmp/guard_result'
+ #
+ module FileNotifier
+ extend self
+
+ # Default options for FileNotifier
+ DEFAULTS = {
+ :format => "%s\n%s\n%s\n"
+ }
+
+ # Test if the file notification option is available?
+ #
+ # REVIEW: This could test if there is a path provided in options, but
+ # we don't get options in available?.
+ #
+ # @param [Boolean] silent true if no error messages should be shown
+ # @return [Boolean] the availability status
+ #
+ def available?(silent = false)
+ true
+ end
+
+ # Write the notification to a file. By default it writes type, tytle, and
+ # message separated by newlines.
+ #
+ # @param [String] type the notification type. Either 'success', 'pending', 'failed' or 'notify'
+ # @param [String] title the notification title
+ # @param [String] message the notification message body
+ # @param [String] image the path to the notification image
+ # @param [Hash] options additional notification library options
+ # @option options [String] format printf style format for file contents
+ # @option options [String] path the path of where to write the file
+ #
+ def notify(type, title, message, image, options = { })
+ if options[:path]
+ format = options.fetch(:format, DEFAULTS[:format])
+
+ write(options[:path], format % [type, title, message])
+ end
+ end
+
+ private
+ def write(path, contents)
+ File.write(path, contents)
+ end
+ end
+
+ end
+end
@@ -46,6 +46,7 @@
Guard::Notifier.should_receive(:add_notification).with(:emacs, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:terminal_title, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:tmux, { }, true).and_return false
+ Guard::Notifier.should_receive(:add_notification).with(:file, { }, true).and_return false
Guard::Notifier.turn_on
end
@@ -60,6 +61,7 @@
Guard::Notifier.should_receive(:add_notification).with(:emacs, { }, true)
Guard::Notifier.should_receive(:add_notification).with(:terminal_title, { }, true)
Guard::Notifier.should_receive(:add_notification).with(:tmux, { }, true)
+ Guard::Notifier.should_receive(:add_notification).with(:file, { }, true)
Guard::Notifier.turn_on
end
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+
+describe Guard::Notifier::FileNotifier do
+
+ describe '.available?' do
+ it 'is always true' do
+ subject.should be_available
+ end
+ end
+
+ describe '.notify' do
+ it 'writes to a file on success' do
+ subject.should_receive(:write).with('tmp/guard_result', "success\nany title\nany message\n")
+
+ subject.notify('success', 'any title', 'any message', 'any image', { :path => 'tmp/guard_result' })
+ end
+
+ it 'also writes to a file on failure' do
+ subject.should_receive(:write).with('tmp/guard_result', "failed\nany title\nany message\n")
+
+ subject.notify('failed', 'any title', 'any message', 'any image', { :path => 'tmp/guard_result' })
+ end
+
+ # We don't have a way to return false in .available? when no path is
+ # specified. So, we just don't do anything in .notify if there's no path.
+ it 'does not write to a file if no path is specified' do
+ subject.should_not_receive(:write)
+
+ subject.notify('success', 'any title', 'any message', 'any image', { })
+ end
+ end
+end

0 comments on commit 8f1f00b

Please sign in to comment.