brew/Library/Homebrew/cask/lib/hbc/artifact/abstract_flight_block.rb

49 lines
1.2 KiB
Ruby
Raw Normal View History

require "hbc/artifact/abstract_artifact"
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
module Hbc
module Artifact
class AbstractFlightBlock < AbstractArtifact
def self.dsl_key
super.to_s.sub(/_block$/, "").to_sym
2016-09-24 13:52:43 +02:00
end
def self.uninstall_dsl_key
dsl_key.to_s.prepend("uninstall_").to_sym
2016-09-24 13:52:43 +02:00
end
def self.for_cask(cask)
[dsl_key, uninstall_dsl_key].flat_map do |key|
[*cask.artifacts[key]].map { |block| new(cask, key => block) }
end
2016-09-24 13:52:43 +02:00
end
attr_reader :directives
def initialize(cask, **directives)
super(cask)
@directives = directives
2016-09-24 13:52:43 +02:00
end
def install_phase(**)
abstract_phase(self.class.dsl_key)
2016-09-24 13:52:43 +02:00
end
def uninstall_phase(**)
abstract_phase(self.class.uninstall_dsl_key)
2016-09-24 13:52:43 +02:00
end
private
def class_for_dsl_key(dsl_key)
namespace = self.class.name.to_s.sub(/::.*::.*$/, "")
self.class.const_get("#{namespace}::DSL::#{dsl_key.to_s.split("_").collect(&:capitalize).join}")
end
2016-09-24 13:52:43 +02:00
def abstract_phase(dsl_key)
return if (block = directives[dsl_key]).nil?
class_for_dsl_key(dsl_key).new(cask).instance_eval(&block)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
end
end