Skip to content

Commit

Permalink
Merge pull request #2742 from rolandwalker/before_install
Browse files Browse the repository at this point in the history
add `before_install` and `before_uninstall`
  • Loading branch information
rolandwalker committed Feb 13, 2014
2 parents d6aa094 + 50864dc commit 82b9d30
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/cask/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Cask::Artifact; end
require 'cask/artifact/app'
require 'cask/artifact/binary'
require 'cask/artifact/after_block'
require 'cask/artifact/before_block'
require 'cask/artifact/colorpicker'
require 'cask/artifact/font'
require 'cask/artifact/nested_container'
Expand All @@ -27,6 +28,7 @@ module Cask::Artifact
#
def self.artifacts
[
Cask::Artifact::BeforeBlock,
Cask::Artifact::NestedContainer,
Cask::Artifact::App,
Cask::Artifact::Colorpicker,
Expand Down
14 changes: 14 additions & 0 deletions lib/cask/artifact/before_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Cask::Artifact::BeforeBlock < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:before_install].any? ||
cask.artifacts[:before_uninstall].any?
end

def install
@cask.artifacts[:before_install].each { |block| @cask.instance_eval &block }
end

def uninstall
@cask.artifacts[:before_uninstall].each { |block| @cask.instance_eval &block }
end
end
4 changes: 3 additions & 1 deletion lib/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def caveats(*string, &block)

ARTIFACT_BLOCK_TYPES = [
:after_install,
:after_uninstall
:after_uninstall,
:before_install,
:before_uninstall,
]

ARTIFACT_BLOCK_TYPES.each do |type|
Expand Down
45 changes: 45 additions & 0 deletions test/cask/artifact/before_block_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

describe Cask::Artifact::BeforeBlock do
describe 'install' do
it 'calls the specified block before installing' do
called = false
yielded_arg = nil

CaskWithBeforeInstall = Class.new(Cask)
CaskWithBeforeInstall.class_eval do
before_install do |c|
called = true
yielded_arg = c
end
end

cask = CaskWithBeforeInstall.new
Cask::Artifact::BeforeBlock.new(cask).install

called.must_equal true
yielded_arg.must_equal cask
end
end

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

CaskWithBeforeUninstall = Class.new(Cask)
CaskWithBeforeUninstall.class_eval do
before_uninstall do |c|
called = true
yielded_arg = c
end
end

cask = CaskWithBeforeUninstall.new
Cask::Artifact::BeforeBlock.new(cask).uninstall

called.must_equal true
yielded_arg.must_equal cask
end
end
end

0 comments on commit 82b9d30

Please sign in to comment.