brew/Library/Homebrew/extend/os/mac/requirements/java_requirement.rb
JCount 224864b149 java_requirement: support prompting users to install legacy Java casks
This enhances the Java requirement to support prompting the user
to install the correct legacy Java version via Cask for formulae
that don't yet work with the latest version of Java. Previously,
even if the formula had a strict requirement that a specific,
older version of Java be used, the messaging would tell the user to
`brew cask install java` (i.e. to install the latest version of Java),
which wouldn't actually satisfy the requirement.
2017-10-26 09:57:03 -04:00

55 lines
1.5 KiB
Ruby

class JavaRequirement < Requirement
env do
env_java_common
env_oracle_jdk || env_apple
end
# A strict Java 8 requirement (1.8) should prompt the user to install
# the legacy java8 cask because the current version, Java 9, is not
# completely backwards compatible, and contains breaking changes such as
# strong encapsulation of JDK-internal APIs and a modified version scheme
# (9.0 not 1.9).
def cask
if @version.nil? || @version.to_s.end_with?("+") ||
@version.to_f >= JAVA_CASK_MAP.keys.max.to_f
JAVA_CASK_MAP.fetch(JAVA_CASK_MAP.keys.max)
else
JAVA_CASK_MAP.fetch("1.8")
end
end
private
JAVA_CASK_MAP = {
"1.8" => "caskroom/versions/java8",
"9.0" => "java",
}.freeze
def possible_javas
javas = []
javas << Pathname.new(ENV["JAVA_HOME"])/"bin/java" if ENV["JAVA_HOME"]
javas << java_home_cmd
which_java = which("java")
# /usr/bin/java is a stub on macOS
javas << which_java if which_java.to_s != "/usr/bin/java"
javas
end
def java_home_cmd
return nil unless File.executable?("/usr/libexec/java_home")
args = %w[--failfast]
args << "--version" << @version.to_s if @version
java_home = Utils.popen_read("/usr/libexec/java_home", *args).chomp
return nil unless $CHILD_STATUS.success?
Pathname.new(java_home)/"bin/java"
end
def env_apple
ENV.append_to_cflags "-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/"
end
def oracle_java_os
:darwin
end
end