Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/407'
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGELOG.md
  • Loading branch information
netzpirat committed Mar 27, 2013
2 parents a6d7392 + 71cbe93 commit 8f1f00b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


- [#400][] Drop dependency on `terminal-table` and use `formatador` instead. ([@netzpirat][]) - [#400][] Drop dependency on `terminal-table` and use `formatador` instead. ([@netzpirat][])
- Change the current work dir to the `watchdir`. ([@netzpirat][]) - Change the current work dir to the `watchdir`. ([@netzpirat][])
- Add file notifier to write notifications to a configured file. ([@amiel][])


### Bug fixes ### Bug fixes


Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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). 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 Add Guard plugins
----------------- -----------------


Expand Down
4 changes: 3 additions & 1 deletion lib/guard/notifier.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Notifier
require 'guard/notifiers/terminal_notifier' require 'guard/notifiers/terminal_notifier'
require 'guard/notifiers/terminal_title' require 'guard/notifiers/terminal_title'
require 'guard/notifiers/tmux' require 'guard/notifiers/tmux'
require 'guard/notifiers/file_notifier'


extend self extend self


Expand All @@ -66,7 +67,8 @@ module Notifier
], ],
[[:emacs, ::Guard::Notifier::Emacs]], [[:emacs, ::Guard::Notifier::Emacs]],
[[:tmux, ::Guard::Notifier::Tmux]], [[:tmux, ::Guard::Notifier::Tmux]],
[[:terminal_title, ::Guard::Notifier::TerminalTitle]] [[:terminal_title, ::Guard::Notifier::TerminalTitle]],
[[:file, ::Guard::Notifier::FileNotifier]]
] ]


# Get the available notifications. # Get the available notifications.
Expand Down
55 changes: 55 additions & 0 deletions lib/guard/notifiers/file_notifier.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/guard/notifier_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
Guard::Notifier.should_receive(:add_notification).with(:emacs, { }, true).and_return false 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(: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(:tmux, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:file, { }, true).and_return false
Guard::Notifier.turn_on Guard::Notifier.turn_on
end end


Expand All @@ -60,6 +61,7 @@
Guard::Notifier.should_receive(:add_notification).with(:emacs, { }, true) 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(:terminal_title, { }, true)
Guard::Notifier.should_receive(:add_notification).with(:tmux, { }, true) Guard::Notifier.should_receive(:add_notification).with(:tmux, { }, true)
Guard::Notifier.should_receive(:add_notification).with(:file, { }, true)
Guard::Notifier.turn_on Guard::Notifier.turn_on
end end


Expand Down
33 changes: 33 additions & 0 deletions spec/guard/notifiers/file_notifier_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -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.