Skip to content

Commit

Permalink
Merge pull request #5723 from federicobond/postflight-dsl
Browse files Browse the repository at this point in the history
Add DSL for after_install and similar blocks
  • Loading branch information
federicobond committed Aug 12, 2014
2 parents a6effdc + 3cb833c commit ef71905
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Cask; end
require 'cask/caveats'
require 'cask/conflicts_with'
require 'cask/container'
require 'cask/decorator'
require 'cask/depends_on'
require 'cask/download'
require 'cask/download_strategy'
Expand Down
8 changes: 6 additions & 2 deletions lib/cask/artifact/after_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ def self.me?(cask)
end

def install_phase
@cask.artifacts[:after_install].each { |block| @cask.instance_eval &block }
@cask.artifacts[:after_install].each do |block|
Cask::Decorator.new(Cask::DSL::AfterInstall, @cask).instance_eval &block
end
end

def uninstall_phase
@cask.artifacts[:after_uninstall].each { |block| @cask.instance_eval &block }
@cask.artifacts[:after_uninstall].each do |block|
Cask::Decorator.new(Cask::DSL::AfterUninstall, @cask).instance_eval &block
end
end
end
8 changes: 6 additions & 2 deletions lib/cask/artifact/before_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ def self.me?(cask)
end

def install_phase
@cask.artifacts[:before_install].each { |block| @cask.instance_eval &block }
@cask.artifacts[:before_install].each do |block|
Cask::Decorator.new(Cask::DSL::BeforeInstall, @cask).instance_eval &block
end
end

def uninstall_phase
@cask.artifacts[:before_uninstall].each { |block| @cask.instance_eval &block }
@cask.artifacts[:before_uninstall].each do |block|
Cask::Decorator.new(Cask::DSL::BeforeUninstall, @cask).instance_eval &block
end
end
end
20 changes: 20 additions & 0 deletions lib/cask/decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Cask::Decorator
def initialize(module_, cask, command = Cask::SystemCommand)
self.extend(module_)

@cask = cask
@command = command
end

def system_command(executable, options = {})
@command.run!(executable, options)
end

def method_missing(m, *args, &block)
if @cask.respond_to?(m)
@cask.send(m, *args, &block)
else
super
end
end
end
8 changes: 8 additions & 0 deletions lib/cask/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require 'checksum'
require 'set'

module Cask::DSL; end

require 'cask/dsl/installed'
require 'cask/dsl/after_install'
require 'cask/dsl/after_uninstall'
require 'cask/dsl/before_install'
require 'cask/dsl/before_uninstall'

module Cask::DSL
def self.included(base)
base.extend(ClassMethods)
Expand Down
18 changes: 18 additions & 0 deletions lib/cask/dsl/after_install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Cask::DSL::AfterInstall
include Cask::DSL::Installed

def suppress_move_to_applications
system_command("/usr/bin/defaults", :args => ["write", bundle_identifier, "moveToApplicationsFolderAlertSuppress", "-bool", "true"])
end

def enable_accessibility_access
if MacOS.version < :mavericks
system_command("touch", :args => ["/private/var/db/.AccessibilityAPIEnabled"])
else
system_command("sqlite3", :args => [
"/Library/Application\ Support/com.apple.TCC/TCC.db",
"INSERT INTO access VALUES('kTCCServiceAccessibility','#{bundle_identifier}',0,1,1,NULL);"
], :sudo => true)
end
end
end
1 change: 1 addition & 0 deletions lib/cask/dsl/after_uninstall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Cask::DSL::AfterUninstall; end
1 change: 1 addition & 0 deletions lib/cask/dsl/before_install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Cask::DSL::BeforeInstall; end
12 changes: 12 additions & 0 deletions lib/cask/dsl/before_uninstall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Cask::DSL::BeforeUninstall
include Cask::DSL::Installed

def remove_accessibility_access
if MacOS.version >= :mavericks
system_command("sqlite3", :args => [
"/Library/Application\ Support/com.apple.TCC/TCC.db",
"DELETE FROM access WHERE client='#{bundle_identifier}';"
], :sudo => true)
end
end
end
17 changes: 17 additions & 0 deletions lib/cask/dsl/installed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Cask::DSL::Installed
def info_plist
"#{destination_path}/#{artifacts[:link].first.first}/Contents/Info.plist"
end

def plist_exec(cmd)
system_command("/usr/libexec/PlistBuddy", :args => ["-c", cmd, info_plist])
end

def plist_set(key, value)
plist_exec("Set #{key} #{value}")
end

def bundle_identifier
plist_exec("Print CFBundleIdentifier")
end
end
8 changes: 4 additions & 4 deletions test/cask/artifact/after_block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Cask::Artifact::AfterBlock do
describe 'install_phase' do
it 'calls the specified block after installing, passing the cask' do
it 'calls the specified block after installing, passing a cask decorator' do
called = false
yielded_arg = nil

Expand All @@ -18,12 +18,12 @@
Cask::Artifact::AfterBlock.new(cask).install_phase

called.must_equal true
yielded_arg.must_equal cask
yielded_arg.must_be_kind_of Cask::Decorator
end
end

describe 'uninstall_phase' do
it 'calls the specified block after uninstalling, passing the cask' do
it 'calls the specified block after uninstalling, passing a cask decorator' do
called = false
yielded_arg = nil

Expand All @@ -39,7 +39,7 @@
Cask::Artifact::AfterBlock.new(cask).uninstall_phase

called.must_equal true
yielded_arg.must_equal cask
yielded_arg.must_be_kind_of Cask::Decorator
end
end
end
8 changes: 4 additions & 4 deletions test/cask/artifact/before_block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Cask::Artifact::BeforeBlock do
describe 'install_phase' do
it 'calls the specified block before installing' do
it 'calls the specified block before installing, passing a cask decorator' do
called = false
yielded_arg = nil

Expand All @@ -18,12 +18,12 @@
Cask::Artifact::BeforeBlock.new(cask).install_phase

called.must_equal true
yielded_arg.must_equal cask
yielded_arg.must_be_kind_of Cask::Decorator
end
end

describe 'uninstall_phase' do
it 'calls the specified block before uninstalling, passing the cask' do
it 'calls the specified block before uninstalling, passing a cask decorator' do
called = false
yielded_arg = nil

Expand All @@ -39,7 +39,7 @@
Cask::Artifact::BeforeBlock.new(cask).uninstall_phase

called.must_equal true
yielded_arg.must_equal cask
yielded_arg.must_be_kind_of Cask::Decorator
end
end
end
9 changes: 9 additions & 0 deletions test/cask/decorator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

describe Cask::Decorator do
it "forwards methods to cask" do
cask = Cask.load('basic-cask')
decorator = Cask::Decorator.new(Cask::DSL::BeforeInstall, cask)
decorator.title.must_equal 'basic-cask'
end
end
70 changes: 70 additions & 0 deletions test/cask/dsl/after_install_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require "test_helper"

describe Cask::DSL::AfterInstall do
before do
cask = Cask.load('basic-cask')
@decorator = Cask::Decorator.new(Cask::DSL::AfterInstall, cask, Cask::FakeSystemCommand)
end

it "can run system commands with list-form arguments" do
Cask::FakeSystemCommand.expects_command(
['echo', 'homebrew-cask', 'rocks!']
)
@decorator.system_command("echo", :args => ["homebrew-cask", "rocks!"])
end

it "can get the Info.plist file for the primary app" do
@decorator.info_plist.must_include "basic-cask/1.2.3/TestCask.app/Contents/Info.plist"
end

it "can execute commands on the Info.plist file" do
Cask::FakeSystemCommand.expects_command(
['/usr/libexec/PlistBuddy', '-c', 'Print CFBundleIdentifier', @decorator.info_plist]
)
@decorator.plist_exec('Print CFBundleIdentifier')
end

it "can retrieve the bundle identifier for the primary app" do
Cask::FakeSystemCommand.stubs_command(
['/usr/libexec/PlistBuddy', '-c', 'Print CFBundleIdentifier', @decorator.info_plist],
"com.example.BasicCask"
)
@decorator.bundle_identifier.must_equal "com.example.BasicCask"
end

it "can set a key in the Info.plist file" do
Cask::FakeSystemCommand.expects_command(
['/usr/libexec/PlistBuddy', '-c', 'Set :JVMOptions:JVMVersion 1.6+', @decorator.info_plist]
)
@decorator.plist_set(':JVMOptions:JVMVersion', '1.6+')
end

it "can suppress move to applications folder alert " do
@decorator.stubs(:bundle_identifier => 'com.example.BasicCask')

Cask::FakeSystemCommand.expects_command(
['/usr/bin/defaults', 'write', 'com.example.BasicCask', 'moveToApplicationsFolderAlertSuppress', '-bool', 'true']
)
@decorator.suppress_move_to_applications
end

it "can enable accessibility access" do
MacOS.stubs(:version => OS::Mac::Version.new('10.9'))

@decorator.stubs(:bundle_identifier => 'com.example.BasicCask')

Cask::FakeSystemCommand.expects_command(
["/usr/bin/sudo", "-E", "--", "sqlite3", "/Library/Application Support/com.apple.TCC/TCC.db", "INSERT INTO access VALUES('kTCCServiceAccessibility','com.example.BasicCask',0,1,1,NULL);"]
)
@decorator.enable_accessibility_access
end

it "can enable accessibility access in OS X versions prior to Mavericks" do
MacOS.stubs(:version => OS::Mac::Version.new('10.8'))

Cask::FakeSystemCommand.expects_command(
['touch', '/private/var/db/.AccessibilityAPIEnabled']
)
@decorator.enable_accessibility_access
end
end
19 changes: 19 additions & 0 deletions test/cask/dsl/before_uninstall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "test_helper"

describe Cask::DSL::BeforeUninstall do
before do
cask = Cask.load('basic-cask')
@decorator = Cask::Decorator.new(Cask::DSL::BeforeUninstall, cask, Cask::FakeSystemCommand)
end

it "can remove accessibility access" do
MacOS.stubs(:version => OS::Mac::Version.new('10.9'))

@decorator.stubs(:bundle_identifier => 'com.example.BasicCask')

Cask::FakeSystemCommand.expects_command(
["/usr/bin/sudo", "-E", "--", "sqlite3", "/Library/Application Support/com.apple.TCC/TCC.db", "DELETE FROM access WHERE client='com.example.BasicCask';"]
)
@decorator.remove_accessibility_access
end
end

0 comments on commit ef71905

Please sign in to comment.