Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DSL and Artifact classes. #3267

Merged
merged 4 commits into from Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 0 additions & 39 deletions Library/Homebrew/cask/lib/hbc/artifact.rb
Expand Up @@ -25,44 +25,5 @@

module Hbc
module Artifact
# NOTE: Order is important here!
#
# The `uninstall` stanza should be run first, as it may
# depend on other artifacts still being installed.
#
# We want to extract nested containers before we
# handle any other artifacts.
#
CLASSES = [
PreflightBlock,
Uninstall,
NestedContainer,
Installer,
App,
Suite,
Artifact, # generic 'artifact' stanza
Colorpicker,
Pkg,
Prefpane,
Qlplugin,
Dictionary,
Font,
Service,
StageOnly,
Binary,
InputMethod,
InternetPlugin,
AudioUnitPlugin,
VstPlugin,
Vst3Plugin,
ScreenSaver,
PostflightBlock,
Zap,
].freeze

def self.for_cask(cask)
odebug "Determining which artifacts are present in Cask #{cask}"
CLASSES.flat_map { |klass| klass.for_cask(cask) }
end
end
end
40 changes: 38 additions & 2 deletions Library/Homebrew/cask/lib/hbc/artifact/abstract_artifact.rb
@@ -1,6 +1,7 @@
module Hbc
module Artifact
class AbstractArtifact
include Comparable
extend Predicable

def self.english_name
Expand All @@ -19,8 +20,43 @@ def self.dirmethod
@dirmethod ||= "#{dsl_key}dir".to_sym
end

def self.for_cask(cask)
cask.artifacts[dsl_key].to_a
def <=>(other)
return unless other.class < AbstractArtifact
return 0 if self.class == other.class

@@sort_order ||= [ # rubocop:disable Style/ClassVars
PreflightBlock,
# The `uninstall` stanza should be run first, as it may
# depend on other artifacts still being installed.
Uninstall,
# We want to extract nested containers before we
# handle any other artifacts.
NestedContainer,
Installer,
[
App,
Suite,
Artifact,
Colorpicker,
Prefpane,
Qlplugin,
Dictionary,
Font,
Service,
InputMethod,
InternetPlugin,
AudioUnitPlugin,
VstPlugin,
Vst3Plugin,
ScreenSaver,
],
Binary,
Pkg,
PostflightBlock,
Zap,
].each_with_index.flat_map { |classes, i| [*classes].map { |c| [c, i] } }.to_h

(@@sort_order[self.class] <=> @@sort_order[other.class]).to_i
end

# TODO: this sort of logic would make more sense in dsl.rb, or a
Expand Down
Expand Up @@ -11,12 +11,6 @@ def self.uninstall_dsl_key
dsl_key.to_s.prepend("uninstall_").to_sym
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
end

attr_reader :directives

def initialize(cask, **directives)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/lib/hbc/audit.rb
Expand Up @@ -218,7 +218,7 @@ def bad_osdn_url?
end

def check_generic_artifacts
cask.artifacts[:artifact].each do |artifact|
cask.artifacts.select { |a| a.is_a?(Hbc::Artifact::Artifact) }.each do |artifact|
unless artifact.target.absolute?
add_error "target must be absolute path for #{artifact.class.english_name} #{artifact.source}"
end
Expand Down
10 changes: 5 additions & 5 deletions Library/Homebrew/cask/lib/hbc/cli/info.rb
Expand Up @@ -77,11 +77,11 @@ def self.repo_info(cask)

def self.artifact_info(cask)
ohai "Artifacts"
DSL::ORDINARY_ARTIFACT_CLASSES.flat_map { |klass| klass.for_cask(cask) }
.select { |artifact| artifact.respond_to?(:install_phase) }
.each do |artifact|
puts artifact.to_s
end
cask.artifacts.each do |artifact|
next unless artifact.respond_to?(:install_phase)
next unless DSL::ORDINARY_ARTIFACT_CLASSES.include?(artifact.class)
puts artifact.to_s
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/lib/hbc/cli/list.rb
Expand Up @@ -30,7 +30,7 @@ def list
end

def self.list_artifacts(cask)
Artifact.for_cask(cask).group_by(&:class).each do |klass, artifacts|
cask.artifacts.group_by(&:class).each do |klass, artifacts|
next unless klass.respond_to?(:english_description)
ohai klass.english_description, artifacts.map(&:summarize_installed)
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/lib/hbc/container/base.rb
Expand Up @@ -20,7 +20,7 @@ def extract_nested_inside(dir)

unless children.count == 1 &&
!nested_container.directory? &&
@cask.artifacts[:nested_container].empty? &&
@cask.artifacts.none? { |a| a.is_a?(Artifact::NestedContainer) } &&
extract_nested_container(nested_container)

children.each do |src|
Expand Down
21 changes: 10 additions & 11 deletions Library/Homebrew/cask/lib/hbc/dsl.rb
Expand Up @@ -43,7 +43,7 @@ class DSL
Artifact::Zap,
].freeze

ACTIVATABLE_ARTIFACT_TYPES = (ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key) - [:stage_only]).freeze
ACTIVATABLE_ARTIFACT_CLASSES = ORDINARY_ARTIFACT_CLASSES - [Artifact::StageOnly]

ARTIFACT_BLOCK_CLASSES = [
Artifact::PreflightBlock,
Expand Down Expand Up @@ -71,11 +71,12 @@ class DSL
:version,
:appdir,
*ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key),
*ACTIVATABLE_ARTIFACT_TYPES,
*ACTIVATABLE_ARTIFACT_CLASSES.map(&:dsl_key),
*ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] },
].freeze

attr_reader :token, :cask
attr_reader :cask, :token

def initialize(cask)
@cask = cask
@token = cask.token
Expand Down Expand Up @@ -175,7 +176,7 @@ def container(*args)
DSL::Container.new(*args).tap do |container|
# TODO: remove this backward-compatibility section after removing nested_container
if container&.nested
artifacts[:nested_container] << Artifact::NestedContainer.new(cask, container.nested)
artifacts.add(Artifact::NestedContainer.new(cask, container.nested))
end
end
end
Expand Down Expand Up @@ -218,7 +219,7 @@ def conflicts_with(*args)
end

def artifacts
@artifacts ||= Hash.new { |hash, key| hash[key] = Set.new }
@artifacts ||= SortedSet.new
end

def caskroom_path
Expand Down Expand Up @@ -250,15 +251,13 @@ def auto_updates(auto_updates = nil)
end

ORDINARY_ARTIFACT_CLASSES.each do |klass|
type = klass.dsl_key

define_method(type) do |*args|
define_method(klass.dsl_key) do |*args|
begin
if [*artifacts.keys, type].include?(:stage_only) && (artifacts.keys & ACTIVATABLE_ARTIFACT_TYPES).any?
if [*artifacts.map(&:class), klass].include?(Artifact::StageOnly) && (artifacts.map(&:class) & ACTIVATABLE_ARTIFACT_CLASSES).any?
raise CaskInvalidError.new(cask, "'stage_only' must be the only activatable artifact.")
end

artifacts[type].add(klass.from_args(cask, *args))
artifacts.add(klass.from_args(cask, *args))
rescue CaskInvalidError
raise
rescue StandardError => e
Expand All @@ -270,7 +269,7 @@ def auto_updates(auto_updates = nil)
ARTIFACT_BLOCK_CLASSES.each do |klass|
[klass.dsl_key, klass.uninstall_dsl_key].each do |dsl_key|
define_method(dsl_key) do |&block|
artifacts[dsl_key] << block
artifacts.add(klass.new(cask, dsl_key => block))
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions Library/Homebrew/cask/lib/hbc/dsl/appcast.rb
Expand Up @@ -3,17 +3,17 @@
module Hbc
class DSL
class Appcast
attr_reader :parameters, :checkpoint
attr_reader :uri, :checkpoint, :parameters

def initialize(uri, parameters = {})
@parameters = parameters
@uri = URI(uri)
@checkpoint = @parameters[:checkpoint]
def initialize(uri, **parameters)
@uri = URI(uri)
@parameters = parameters
@checkpoint = parameters[:checkpoint]
end

def calculate_checkpoint
curl_executable, *args = curl_args(
"--compressed", "--location", "--fail", @uri,
"--compressed", "--location", "--fail", uri,
user_agent: :fake
)
result = SystemCommand.run(curl_executable, args: args, print_stderr: false)
Expand All @@ -30,11 +30,11 @@ def calculate_checkpoint
end

def to_yaml
[@uri, @parameters].to_yaml
[uri, parameters].to_yaml
end

def to_s
@uri.to_s
uri.to_s
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cask/lib/hbc/dsl/base.rb
Expand Up @@ -10,14 +10,14 @@ def initialize(cask, command = SystemCommand)

def_delegators :@cask, :token, :version, :caskroom_path, :staged_path, :appdir, :language

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

def method_missing(method, *)
if method
underscored_class = self.class.name.gsub(/([[:lower:]])([[:upper:]][[:lower:]])/, '\1_\2').downcase
section = underscored_class.downcase.split("::").last
section = underscored_class.split("::").last
Utils.method_missing_message(method, @cask.to_s, section)
nil
else
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cask/lib/hbc/installer.rb
Expand Up @@ -177,7 +177,7 @@ def install_artifacts
already_installed_artifacts = []

odebug "Installing artifacts"
artifacts = Artifact.for_cask(@cask)
artifacts = @cask.artifacts
odebug "#{artifacts.length} artifact/s defined", artifacts

artifacts.each do |artifact|
Expand Down Expand Up @@ -374,7 +374,7 @@ def uninstall

def uninstall_artifacts
odebug "Un-installing artifacts"
artifacts = Artifact.for_cask(@cask)
artifacts = @cask.artifacts

odebug "#{artifacts.length} artifact/s defined", artifacts

Expand All @@ -388,7 +388,7 @@ def uninstall_artifacts
def zap
ohai %Q(Implied "brew cask uninstall #{@cask}")
uninstall_artifacts
if (zap_stanzas = Artifact::Zap.for_cask(@cask)).empty?
if (zap_stanzas = @cask.artifacts.select { |a| a.is_a?(Artifact::Zap) }).empty?
opoo "No zap stanza present for Cask '#{@cask}'"
else
ohai "Dispatching zap stanza"
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/lib/hbc/staged.rb
Expand Up @@ -4,7 +4,7 @@ def info_plist_file(index = 0)
index = 0 if index == :first
index = 1 if index == :second
index = -1 if index == :last
@cask.artifacts[:app].to_a.at(index).target.join("Contents", "Info.plist")
@cask.artifacts.select { |a| a.is_a?(Artifact::App) }.at(index).target.join("Contents", "Info.plist")
end

def plist_exec(cmd)
Expand Down
6 changes: 5 additions & 1 deletion Library/Homebrew/test/cask/artifact/alt_target_spec.rb
Expand Up @@ -3,7 +3,11 @@
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-alt-target.rb") }

let(:install_phase) {
-> { described_class.for_cask(cask).each { |artifact| artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false) } }
lambda do
cask.artifacts.select { |a| a.is_a?(described_class) }.each do |artifact|
artifact.install_phase(command: Hbc::NeverSudoSystemCommand, force: false)
end
end
}

let(:source_path) { cask.staged_path.join("Caffeine.app") }
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/cask/artifact/app_spec.rb
Expand Up @@ -2,7 +2,7 @@
let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-caffeine.rb") }
let(:command) { Hbc::SystemCommand }
let(:force) { false }
let(:app) { described_class.for_cask(cask).first }
let(:app) { cask.artifacts.find { |a| a.is_a?(described_class) } }

let(:source_path) { cask.staged_path.join("Caffeine.app") }
let(:target_path) { Hbc.appdir.join("Caffeine.app") }
Expand Down