Skip to content

Commit

Permalink
allow casks to support arbitrary blocks
Browse files Browse the repository at this point in the history
this allows us to experiment with behavior that we may not want to
promote to an official feature just yet.

i'm thinking about stuff like #544, and other things i can't foresee

we'll have to be careful to not let this get out of hand, but i think
it could be helpful for cask authors to be able to try and problem solve
locally.
  • Loading branch information
phinze committed Dec 6, 2013
1 parent f75c47e commit 79aa1b7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/cask/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module Cask::Artifact; end
require 'cask/artifact/base'

require 'cask/artifact/app'
require 'cask/artifact/block'
require 'cask/artifact/font'
require 'cask/artifact/nested_container'
require 'cask/artifact/pkg'
require 'cask/artifact/prefpane'
require 'cask/artifact/qlplugin'
require 'cask/artifact/font'

module Cask::Artifact
#
Expand All @@ -22,6 +23,7 @@ def self.artifacts
Cask::Artifact::Prefpane,
Cask::Artifact::Qlplugin,
Cask::Artifact::Font,
Cask::Artifact::Block,
]
end

Expand Down
15 changes: 15 additions & 0 deletions lib/cask/artifact/block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Cask::Artifact::Block < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:after_install].any? ||
cask.artifacts[:after_uninstall].any?
end

def install
@cask.artifacts[:after_install].each { |block| block.call(@cask) }
end

def uninstall
@cask.artifacts[:after_uninstall].each { |block| block.call(@cask) }
end
end

11 changes: 11 additions & 0 deletions lib/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def artifacts
end
end

ARTIFACT_BLOCK_TYPES = [
:after_install,
:after_uninstall
]

ARTIFACT_BLOCK_TYPES.each do |type|
define_method(type) do |&block|
artifacts[type] << block
end
end

attr_reader :sums

def md5(md5=nil)
Expand Down
45 changes: 45 additions & 0 deletions test/cask/artifact/block_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

describe Cask::Artifact::Block do
describe 'install' do
it 'calls the specified block after installing, passing the cask' do
called = false
yielded_arg = nil

CaskWithAfterInstall = Class.new(Cask)
CaskWithAfterInstall.class_eval do
after_install do |c|
called = true
yielded_arg = c
end
end

cask = CaskWithAfterInstall.new
Cask::Artifact::Block.new(cask).install

called.must_equal true
yielded_arg.must_equal cask
end
end

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

CaskWithAfterUninstall = Class.new(Cask)
CaskWithAfterUninstall.class_eval do
after_uninstall do |c|
called = true
yielded_arg = c
end
end

cask = CaskWithAfterUninstall.new
Cask::Artifact::Block.new(cask).uninstall

called.must_equal true
yielded_arg.must_equal cask
end
end
end

0 comments on commit 79aa1b7

Please sign in to comment.