Add top_level_info_plists helper method.

This commit is contained in:
Markus Reiter 2020-12-14 18:43:31 +01:00
parent d73fc480a7
commit d9f472340d

View File

@ -46,6 +46,20 @@ module Homebrew
pkgs.count == 1 pkgs.count == 1
end end
# Filter paths to `Info.plist` files so that ones belonging
# to e.g. nested `.app`s are ignored.
sig { params(paths: T::Array[Pathname]).returns(T::Array[Pathname]) }
def top_level_info_plists(paths)
# Go from `./Contents/Info.plist` to `./`.
top_level_paths = paths.map { |path| path.parent.parent }
paths.reject do |path|
top_level_paths.any? do |_other_top_level_path|
path.ascend.drop(3).any? { |parent_path| top_level_paths.include?(parent_path) }
end
end
end
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }
def guess_cask_version def guess_cask_version
if apps.empty? && pkgs.empty? if apps.empty? && pkgs.empty?
@ -64,15 +78,12 @@ module Homebrew
end end
info_plist_paths = apps.flat_map do |app| info_plist_paths = apps.flat_map do |app|
Pathname.glob(dir/"**"/app.source.basename/"Contents"/"Info.plist").reject do |info_plist_path| top_level_info_plists(Pathname.glob(dir/"**"/app.source.basename/"Contents"/"Info.plist")).sort
# Ignore nested apps.
info_plist_path.parent.parent.parent.ascend.any? { |p| p.extname == ".app" }
end.sort
end end
info_plist_paths.each do |info_plist_path| info_plist_paths.each do |info_plist_path|
if (version = BundleVersion.from_info_plist(info_plist_path)&.nice_version) if (version = BundleVersion.from_info_plist(info_plist_path))
return version return version.nice_version
end end
end end
@ -85,7 +96,6 @@ module Homebrew
system_command!("installer", args: ["-plist", "-pkginfo", "-pkg", pkg_path]) system_command!("installer", args: ["-plist", "-pkginfo", "-pkg", pkg_path])
.plist .plist
.map { |package| package.fetch("Package") } .map { |package| package.fetch("Package") }
.uniq
Dir.mktmpdir do |extract_dir| Dir.mktmpdir do |extract_dir|
extract_dir = Pathname(extract_dir) extract_dir = Pathname(extract_dir)
@ -98,16 +108,34 @@ module Homebrew
next next
end end
top_level_info_plist_paths = top_level_info_plists(Pathname.glob(extract_dir/"**/Contents/Info.plist"))
unique_info_plist_versions =
top_level_info_plist_paths.map { |i| BundleVersion.from_info_plist(i)&.nice_version }
.compact.uniq
return unique_info_plist_versions.first if unique_info_plist_versions.count == 1
package_info_path = extract_dir/"PackageInfo" package_info_path = extract_dir/"PackageInfo"
if package_info_path.exist? if package_info_path.exist?
if (version = BundleVersion.from_package_info(package_info_path)&.nice_version) if (version = BundleVersion.from_package_info(package_info_path))
return version return version.nice_version
end end
elsif packages.count == 1 elsif packages.count == 1
onoe "#{pkg_path.basename} does not contain a `PackageInfo` file." onoe "#{pkg_path.basename} does not contain a `PackageInfo` file."
end end
opoo "#{pkg_path.basename} contains multiple packages: (#{packages.join(", ")})" if packages.count != 1 distribution_path = extract_dir/"Distribution"
if distribution_path.exist?
Homebrew.install_bundler_gems!
require "nokogiri"
xml = Nokogiri::XML(distribution_path.read)
product_version = xml.xpath("//installer-gui-script//product").first&.attr("version")
return product_version if product_version
end
opoo "#{pkg_path.basename} contains multiple packages: #{packages}" if packages.count != 1
$stderr.puts Pathname.glob(extract_dir/"**/*") $stderr.puts Pathname.glob(extract_dir/"**/*")
.map { |path| .map { |path|