Skip to content

Commit

Permalink
programmatically modify alfred scope
Browse files Browse the repository at this point in the history
  • Loading branch information
phinze committed Apr 28, 2013
1 parent 157f584 commit d4cecf3
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 0 deletions.
101 changes: 101 additions & 0 deletions lib/cask/cli/alfred.rb
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
5 changes: 5 additions & 0 deletions lib/cask/system_command.rb
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
126 changes: 126 additions & 0 deletions test/cli/alfred_test.rb
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
37 changes: 37 additions & 0 deletions test/support/fake_system_command.rb
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
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def self.valid_alias?(candidate)

require 'support/fake_fetcher'
require 'support/fake_appdir'
require 'support/fake_system_command'

# pretend like we installed the cask tap
project_root = Pathname.new(File.expand_path("#{File.dirname(__FILE__)}/../"))
Expand Down

0 comments on commit d4cecf3

Please sign in to comment.