Merge pull request #3477 from reitermarkus/caveats

Refactor caveats and add `kext` caveat.
This commit is contained in:
Markus Reiter 2017-11-28 02:32:57 +01:00 committed by GitHub
commit 0d26baa938
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 90 additions and 79 deletions

View File

@ -11,7 +11,6 @@ require "hbc/caskroom"
require "hbc/checkable" require "hbc/checkable"
require "hbc/cli" require "hbc/cli"
require "hbc/cask_dependencies" require "hbc/cask_dependencies"
require "hbc/caveats"
require "hbc/container" require "hbc/container"
require "hbc/download" require "hbc/download"
require "hbc/download_strategy" require "hbc/download_strategy"

View File

@ -30,6 +30,13 @@ module Hbc
super(cask) super(cask)
directives[:signal] = [*directives[:signal]].flatten.each_slice(2).to_a directives[:signal] = [*directives[:signal]].flatten.each_slice(2).to_a
@directives = directives @directives = directives
return if MacOS.version < :high_sierra
return unless directives.key?(:kext)
cask.caveats do
kext
end
end end
def to_h def to_h

View File

@ -24,7 +24,7 @@ module Hbc
end end
DSL::DSL_METHODS.each do |method_name| DSL::DSL_METHODS.each do |method_name|
define_method(method_name) { @dsl.send(method_name) } define_method(method_name) { |&block| @dsl.send(method_name, &block) }
end end
def timestamped_versions def timestamped_versions

View File

@ -1,14 +0,0 @@
module Hbc
class Caveats
def initialize(block)
@block = block
end
def eval_and_print(cask)
dsl = DSL::Caveats.new(cask)
retval = dsl.instance_eval(&@block)
return if retval.nil?
puts retval.to_s.sub(/[\r\n \t]*\Z/, "\n\n")
end
end
end

View File

@ -74,7 +74,7 @@ module Hbc
value = value[artifact_name] if artifact_name value = value[artifact_name] if artifact_name
end end
if value.nil? || (value.respond_to?(:to_a) && value.to_a.empty?) if value.nil? || (value.respond_to?(:empty?) && value.empty?)
stanza_name = artifact_name ? artifact_name : stanza stanza_name = artifact_name ? artifact_name : stanza
raise CaskError, "no such stanza '#{stanza_name}' on Cask '#{cask}'" raise CaskError, "no such stanza '#{stanza_name}' on Cask '#{cask}'"
end end

View File

@ -232,12 +232,16 @@ module Hbc
@staged_path = caskroom_path.join(cask_version.to_s) @staged_path = caskroom_path.join(cask_version.to_s)
end end
def caveats(*string, &block) def caveats(*strings, &block)
@caveats ||= [] @caveats ||= DSL::Caveats.new(cask)
if block_given? if block_given?
@caveats << Hbc::Caveats.new(block) @caveats.eval_caveats(&block)
elsif string.any? elsif strings.any?
@caveats << string.map { |s| s.to_s.sub(/[\r\n \t]*\Z/, "\n\n") } strings.each do |string|
@caveats.eval_caveats { string }
end
else
return @caveats.to_s
end end
@caveats @caveats
end end

View File

@ -8,96 +8,130 @@
module Hbc module Hbc
class DSL class DSL
class Caveats < Base class Caveats < Base
def path_environment_variable(path) def initialize(*args)
puts <<~EOS super(*args)
@built_in_caveats = {}
@custom_caveats = []
end
def self.caveat(name, &block)
define_method(name) do |*args|
key = [name, *args]
@built_in_caveats[key] = instance_exec(*args, &block)
:built_in_caveat
end
end
private_class_method :caveat
def to_s
(@custom_caveats + @built_in_caveats.values).join("\n")
end
# Override `puts` to collect caveats.
def puts(*args)
@custom_caveats += args
:built_in_caveat
end
def eval_caveats(&block)
result = instance_eval(&block)
return unless result
return if result == :built_in_caveat
@custom_caveats << result.to_s.sub(/\s*\Z/, "\n")
end
caveat :kext do
<<~EOS
To install and/or use #{@cask} you may need to enable their kernel extension in
System Preferences Security & Privacy General
For more information refer to vendor documentation or the Apple Technical Note:
#{Formatter.url("https://developer.apple.com/library/content/technotes/tn2459/_index.html")}
EOS
end
caveat :path_environment_variable do |path|
<<~EOS
To use #{@cask}, you may need to add the #{path} directory To use #{@cask}, you may need to add the #{path} directory
to your PATH environment variable, eg (for bash shell): to your PATH environment variable, eg (for bash shell):
export PATH=#{path}:"$PATH" export PATH=#{path}:"$PATH"
EOS EOS
end end
def zsh_path_helper(path) caveat :zsh_path_helper do |path|
puts <<~EOS <<~EOS
To use #{@cask}, zsh users may need to add the following line to their To use #{@cask}, zsh users may need to add the following line to their
~/.zprofile. (Among other effects, #{path} will be added to the ~/.zprofile. (Among other effects, #{path} will be added to the
PATH environment variable): PATH environment variable):
eval `/usr/libexec/path_helper -s` eval `/usr/libexec/path_helper -s`
EOS EOS
end end
def files_in_usr_local caveat :files_in_usr_local do
localpath = "/usr/local" next unless HOMEBREW_PREFIX.to_s.downcase.start_with?("/usr/local")
return unless HOMEBREW_PREFIX.to_s.downcase.start_with?(localpath) <<~EOS
puts <<~EOS Cask #{@cask} installs files under /usr/local. The presence of such
Cask #{@cask} installs files under "#{localpath}". The presence of such
files can cause warnings when running "brew doctor", which is considered files can cause warnings when running "brew doctor", which is considered
to be a bug in Homebrew-Cask. to be a bug in Homebrew-Cask.
EOS EOS
end end
def depends_on_java(java_version = "any") caveat :depends_on_java do |java_version = :any|
if java_version == "any" if java_version == :any
puts <<~EOS <<~EOS
#{@cask} requires Java. You can install the latest version with #{@cask} requires Java. You can install the latest version with
brew cask install java brew cask install java
EOS EOS
elsif java_version.include?("9") || java_version.include?("+") elsif java_version.include?("9") || java_version.include?("+")
puts <<~EOS <<~EOS
#{@cask} requires Java #{java_version}. You can install the latest version with #{@cask} requires Java #{java_version}. You can install the latest version with
brew cask install java brew cask install java
EOS EOS
else else
puts <<~EOS <<~EOS
#{@cask} requires Java #{java_version}. You can install it with #{@cask} requires Java #{java_version}. You can install it with
brew cask install caskroom/versions/java#{java_version} brew cask install caskroom/versions/java#{java_version}
EOS EOS
end end
end end
def logout caveat :logout do
puts <<~EOS <<~EOS
You must log out and log back in for the installation of #{@cask} You must log out and log back in for the installation of #{@cask} to take effect.
to take effect.
EOS EOS
end end
def reboot caveat :reboot do
puts <<~EOS <<~EOS
You must reboot for the installation of #{@cask} to take effect. You must reboot for the installation of #{@cask} to take effect.
EOS EOS
end end
def discontinued caveat :discontinued do
puts <<~EOS <<~EOS
#{@cask} has been officially discontinued upstream. #{@cask} has been officially discontinued upstream.
It may stop working correctly (or at all) in recent versions of macOS. It may stop working correctly (or at all) in recent versions of macOS.
EOS EOS
end end
def free_license(web_page) caveat :free_license do |web_page|
puts <<~EOS <<~EOS
The vendor offers a free license for #{@cask} at The vendor offers a free license for #{@cask} at
#{web_page} #{web_page}
EOS EOS
end end
def malware(radar_number) caveat :malware do |radar_number|
puts <<~EOS <<~EOS
#{@cask} has been reported to bundle malware. Like with any app, use at your own risk. #{@cask} has been reported to bundle malware. Like with any app, use at your own risk.
A report has been made to Apple about this app. Their certificate will hopefully be revoked. A report has been made to Apple about this app. Their certificate will hopefully be revoked.
@ -108,7 +142,6 @@ module Hbc
#{Formatter.url("https://bugreport.apple.com/")} #{Formatter.url("https://bugreport.apple.com/")}
If this report is a mistake, please let us know by opening an issue at If this report is a mistake, please let us know by opening an issue at
#{Formatter.url("https://github.com/caskroom/homebrew-cask/issues/new")} #{Formatter.url("https://github.com/caskroom/homebrew-cask/issues/new")}
EOS EOS
end end
end end

View File

@ -35,30 +35,12 @@ module Hbc
def self.print_caveats(cask) def self.print_caveats(cask)
odebug "Printing caveats" odebug "Printing caveats"
return if cask.caveats.empty?
output = capture_output do caveats = cask.caveats
cask.caveats.each do |caveat| return if caveats.empty?
if caveat.respond_to?(:eval_and_print)
caveat.eval_and_print(cask)
else
puts caveat
end
end
end
return if output.empty?
ohai "Caveats" ohai "Caveats"
puts output puts caveats + "\n"
end
def self.capture_output(&block)
old_stdout = $stdout
$stdout = Buffer.new($stdout.tty?)
block.call
output = $stdout.string
$stdout = old_stdout
output
end end
def fetch def fetch