forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
programmatically modify alfred scope
refs Homebrew#18
- Loading branch information
Showing
5 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
class Cask::CLI::Alfred | ||
def self.run(*args) | ||
subcommand = args.first | ||
|
||
if args.length > 1 && args.last.is_a?(Class) | ||
@system_command = args.last | ||
else | ||
@system_command = Cask::SystemCommand | ||
end | ||
|
||
case subcommand | ||
when "link" then link | ||
when "unlink" then unlink | ||
when "status" then status | ||
else | ||
alfred_help | ||
end | ||
end | ||
|
||
def self.alfred_help | ||
ohai 'brew cask alfred', <<-ALFREDHELP.undent | ||
manages integration with Alfred; allows applications installed with | ||
homebrew cask to be launched by Alfred by adding the Caskroom to Alfreds | ||
search paths | ||
subcommands: | ||
status - reports whether Alfred is linked | ||
link - adds Caskroom to alfred search paths | ||
link - removes Cakroom from Alfred search paths | ||
ALFREDHELP | ||
end | ||
|
||
def self.link | ||
if !alfred_installed? | ||
opoo "Could not find any Alfred scopes, Alfred is probably not installed." | ||
elsif linked? | ||
opoo "Alfred is already linked to homebrew-cask." | ||
else | ||
save_alfred_scopes(alfred_scopes << Cask.caskroom) | ||
ohai "Successfully linked Alfred to homebrew-cask." | ||
end | ||
end | ||
|
||
def self.unlink | ||
if !alfred_installed? | ||
opoo "Could not find any Alfred scopes, Alfred is probably not installed." | ||
elsif !linked? | ||
opoo "Alfred is already unlinked from homebrew-cask." | ||
else | ||
save_alfred_scopes(alfred_scopes.reject { |x| x == Cask.caskroom.to_s }) | ||
ohai "Successfully unlinked Alfred from homebrew-cask." | ||
end | ||
end | ||
|
||
def self.status | ||
if !alfred_installed? | ||
opoo "Could not find any Alfred scopes, Alfred is probably not installed." | ||
elsif linked? | ||
ohai "Alfred is happily linked to homebrew-cask!" | ||
else | ||
ohai "Alfred is not linked to homebrew-cask." | ||
end | ||
end | ||
|
||
def self.help | ||
"used to modify Alfred's scope to include the Caskroom" | ||
end | ||
|
||
def self.save_alfred_scopes(scopes) | ||
scopes_arg = "(#{scopes.join(",")})" | ||
@system_command.run("defaults write com.alfredapp.Alfred scope.paths '#{scopes_arg}'") | ||
end | ||
|
||
def self.alfred_installed? | ||
!alfred_scopes.empty? | ||
end | ||
|
||
def self.linked? | ||
alfred_scopes.include?(Cask.caskroom.to_s) | ||
end | ||
|
||
# output looks like this: | ||
# ( | ||
# "/some/path", | ||
# "/other/path" | ||
# ) | ||
# | ||
# and we would like %w[/some/path /other/path] | ||
SCOPE_REGEXP = /^\s*"(.*)",?$/ | ||
|
||
def self.alfred_scopes | ||
alfred_preference("scope.paths").map do |line| | ||
matchdata = line.match(SCOPE_REGEXP) | ||
matchdata ? matchdata.captures.first : nil | ||
end.compact | ||
end | ||
|
||
def self.alfred_preference(key) | ||
@system_command.run("defaults read com.alfredapp.Alfred #{key}") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Cask::SystemCommand | ||
def self.run(command) | ||
%x(#{command}).split("\n") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
require 'test_helper' | ||
|
||
def _fake_alfred_scope_paths(response) | ||
Cask::FakeSystemCommand.fake_response_for('defaults read com.alfredapp.Alfred scope.paths', response) | ||
end | ||
|
||
describe Cask::CLI::Alfred do | ||
describe "status" do | ||
it "properly reports when alfred is not installed" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
2013-04-28 12:55:52.417 defaults[69808:707] | ||
The domain/default pair of (com.alfredapp.Alfred, scope.paths) does not exist | ||
SCOPE_RESPONSE | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('status', Cask::FakeSystemCommand) | ||
}, "Warning: Could not find any Alfred scopes, Alfred is probably not installed.") | ||
end | ||
|
||
it "properly reports when alfred is installed but unlinked" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
( | ||
"/Applications", | ||
"/Library/PreferencePanes", | ||
"/System/Library/PreferencePanes" | ||
) | ||
SCOPE_RESPONSE | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('status', Cask::FakeSystemCommand) | ||
}, "==> Alfred is not linked to homebrew-cask.") | ||
end | ||
end | ||
|
||
describe "link" do | ||
it "properly reports when alfred is not installed" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
2013-04-28 12:55:52.417 defaults[69808:707] | ||
The domain/default pair of (com.alfredapp.Alfred, scope.paths) does not exist | ||
SCOPE_RESPONSE | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('link', Cask::FakeSystemCommand) | ||
}, "Warning: Could not find any Alfred scopes, Alfred is probably not installed.") | ||
end | ||
|
||
it "warns when alfred is already linked" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
( | ||
"/Applications", | ||
"/Library/PreferencePanes", | ||
"#{Cask.caskroom}", | ||
"/System/Library/PreferencePanes" | ||
) | ||
SCOPE_RESPONSE | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('link', Cask::FakeSystemCommand) | ||
}, "Warning: Alfred is already linked to homebrew-cask.") | ||
end | ||
|
||
it "links when it needs to" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
( | ||
"/Applications", | ||
"/Library/PreferencePanes", | ||
"/System/Library/PreferencePanes" | ||
) | ||
SCOPE_RESPONSE | ||
|
||
Cask::FakeSystemCommand.fake_response_for( | ||
"defaults write com.alfredapp.Alfred scope.paths '(/Applications,/Library/PreferencePanes,/System/Library/PreferencePanes,#{Cask.caskroom})'" | ||
) | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('link', Cask::FakeSystemCommand) | ||
}, "==> Successfully linked Alfred to homebrew-cask.") | ||
end | ||
end | ||
|
||
describe "unlink" do | ||
it "properly reports when alfred is not installed" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
2013-04-28 12:55:52.417 defaults[69808:707] | ||
The domain/default pair of (com.alfredapp.Alfred, scope.paths) does not exist | ||
SCOPE_RESPONSE | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('unlink', Cask::FakeSystemCommand) | ||
}, "Warning: Could not find any Alfred scopes, Alfred is probably not installed.") | ||
end | ||
|
||
it "warns when alfred is already unlinked" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
( | ||
"/Applications", | ||
"/Library/PreferencePanes", | ||
"/System/Library/PreferencePanes" | ||
) | ||
SCOPE_RESPONSE | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('unlink', Cask::FakeSystemCommand) | ||
}, "Warning: Alfred is already unlinked from homebrew-cask.") | ||
end | ||
|
||
it "unlinks when it needs to" do | ||
_fake_alfred_scope_paths <<-SCOPE_RESPONSE.undent | ||
( | ||
"/Applications", | ||
"/Library/PreferencePanes", | ||
"#{Cask.caskroom}", | ||
"/System/Library/PreferencePanes" | ||
) | ||
SCOPE_RESPONSE | ||
|
||
Cask::FakeSystemCommand.fake_response_for( | ||
"defaults write com.alfredapp.Alfred scope.paths '(/Applications,/Library/PreferencePanes,/System/Library/PreferencePanes)'" | ||
) | ||
|
||
TestHelper.must_output(self, lambda { | ||
Cask::CLI::Alfred.run('unlink', Cask::FakeSystemCommand) | ||
}, "==> Successfully unlinked Alfred from homebrew-cask.") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Cask::FakeSystemCommand | ||
def self.fake_response_for(command, response='') | ||
@responses[command] = response | ||
end | ||
|
||
def self.init | ||
@responses = {} | ||
end | ||
|
||
def self.clear | ||
@responses = {} | ||
end | ||
|
||
def self.run(command) | ||
@responses ||= {} | ||
unless @responses.key?(command) | ||
fail("no response faked for #{command.inspect}") | ||
end | ||
@responses[command].split("\n") | ||
end | ||
end | ||
|
||
module FakeSystemCommandHooks | ||
def before_setup | ||
Cask::FakeSystemCommand.init | ||
super | ||
end | ||
|
||
def after_teardown | ||
super | ||
Cask::FakeSystemCommand.clear | ||
end | ||
end | ||
|
||
class MiniTest::Spec | ||
include FakeSystemCommandHooks | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters