brew/Library/Homebrew/cask/artifact/abstract_artifact.rb

134 lines
3.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-09-06 08:29:14 +02:00
module Cask
2016-09-24 13:52:43 +02:00
module Artifact
class AbstractArtifact
2017-10-04 17:08:35 +02:00
include Comparable
extend Predicable
def self.english_name
@english_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1 \2')
2016-09-24 13:52:43 +02:00
end
def self.english_article
@english_article ||= (english_name =~ /^[aeiou]/i) ? "an" : "a"
2016-09-24 13:52:43 +02:00
end
def self.dsl_key
@dsl_key ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase.to_sym
2016-09-24 13:52:43 +02:00
end
def self.dirmethod
@dirmethod ||= "#{dsl_key}dir".to_sym
2016-09-24 13:52:43 +02:00
end
def staged_path_join_executable(path)
path = Pathname(path)
absolute_path = if path.absolute?
path
else
cask.staged_path.join(path)
end
FileUtils.chmod "+x", absolute_path if absolute_path.exist? && !absolute_path.executable?
if absolute_path.exist?
absolute_path
else
path
end
end
2017-10-04 17:08:35 +02:00
def <=>(other)
2017-10-04 17:46:29 +02:00
return unless other.class < AbstractArtifact
return 0 if self.class == other.class
2017-10-04 17:08:35 +02:00
@@sort_order ||= [ # rubocop:disable Style/ClassVars
PreflightBlock,
2017-10-04 17:46:29 +02:00
# The `uninstall` stanza should be run first, as it may
# depend on other artifacts still being installed.
2017-10-04 17:08:35 +02:00
Uninstall,
Installer,
2017-10-04 17:46:29 +02:00
[
App,
Suite,
Artifact,
Colorpicker,
Prefpane,
Qlplugin,
Dictionary,
Font,
Service,
InputMethod,
InternetPlugin,
AudioUnitPlugin,
VstPlugin,
Vst3Plugin,
ScreenSaver,
],
2017-10-07 23:10:59 +10:00
# `pkg` should be run before `binary`, so
# targets are created prior to linking.
2017-10-04 17:46:29 +02:00
Pkg,
Binary,
2019-10-24 09:43:59 +03:00
Manpage,
2017-10-04 17:08:35 +02:00
PostflightBlock,
Zap,
2017-10-04 17:46:29 +02:00
].each_with_index.flat_map { |classes, i| [*classes].map { |c| [c, i] } }.to_h
2017-10-04 17:08:35 +02:00
2017-10-04 17:46:29 +02:00
(@@sort_order[self.class] <=> @@sort_order[other.class]).to_i
2016-09-24 13:52:43 +02:00
end
# TODO: this sort of logic would make more sense in dsl.rb, or a
# constructor called from dsl.rb, so long as that isn't slow.
def self.read_script_arguments(arguments, stanza, default_arguments = {}, override_arguments = {}, key = nil)
# TODO: when stanza names are harmonized with class names,
# stanza may not be needed as an explicit argument
2017-03-10 09:33:48 +01:00
description = key ? "#{stanza} #{key.inspect}" : stanza.to_s
2016-09-24 13:52:43 +02:00
# backward-compatible string value
arguments = { executable: arguments } if arguments.is_a?(String)
# key sanity
permitted_keys = [:args, :input, :executable, :must_succeed, :sudo, :print_stdout, :print_stderr]
2016-09-24 13:52:43 +02:00
unknown_keys = arguments.keys - permitted_keys
unless unknown_keys.empty?
opoo "Unknown arguments to #{description} -- " \
"#{unknown_keys.inspect} (ignored). Running " \
2019-04-05 12:24:10 -04:00
"`brew update; brew cleanup` will likely fix it."
2016-09-24 13:52:43 +02:00
end
2017-05-29 18:24:52 +01:00
arguments.select! { |k| permitted_keys.include?(k) }
2016-09-24 13:52:43 +02:00
# key warnings
override_keys = override_arguments.keys
ignored_keys = arguments.keys & override_keys
unless ignored_keys.empty?
onoe "Some arguments to #{description} will be ignored -- :#{unknown_keys.inspect} (overridden)."
end
# extract executable
executable = arguments.key?(:executable) ? arguments.delete(:executable) : nil
arguments = default_arguments.merge arguments
arguments.merge! override_arguments
[executable, arguments]
end
attr_reader :cask
2016-09-24 13:52:43 +02:00
def initialize(cask)
2016-09-24 13:52:43 +02:00
@cask = cask
end
def config
cask.config
end
def to_s
"#{summarize} (#{self.class.english_name})"
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
end
end