Class: Homebrew::UnversionedCaskChecker Private

Inherits:
Object
  • Object
show all
Includes:
SystemCommand::Mixin
Defined in:
unversioned_cask_checker.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Check unversioned casks for updates by extracting their contents and guessing the version from contained files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SystemCommand::Mixin

#system_command, #system_command!

Constructor Details

#initialize(cask) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:



19
20
21
# File 'unversioned_cask_checker.rb', line 19

def initialize(cask)
  @cask = cask
end

Instance Attribute Details

#caskCask::Cask (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



16
17
18
# File 'unversioned_cask_checker.rb', line 16

def cask
  @cask
end

Instance Method Details

#all_versionsHash{String => BundleVersion}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'unversioned_cask_checker.rb', line 103

def all_versions
  versions = {}

  parse_info_plist = proc do |info_plist_path|
    plist = system_command!("plutil", args: ["-convert", "xml1", "-o", "-", info_plist_path]).plist

    id = plist["CFBundleIdentifier"]
    version = BundleVersion.from_info_plist_content(plist)

    versions[id] = version if id && version
  end

  Dir.mktmpdir("cask-checker", HOMEBREW_TEMP) do |dir|
    dir = Pathname(dir)

    installer.extract_primary_container(to: dir)

    info_plist_paths = [
      *apps,
      *keyboard_layouts,
      *mdimporters,
      *colorpickers,
      *dictionaries,
      *qlplugins,
      *installers,
      *screen_savers,
    ].flat_map do |artifact|
      sources = if artifact.is_a?(Cask::Artifact::Installer)
        # Installers are sometimes contained within an `.app`, so try both.
        installer_path = artifact.path
        installer_path.ascend
                      .select { |path| path == installer_path || path.extname == ".app" }
                      .sort
      else
        [artifact.source.basename]
      end

      sources.flat_map do |source|
        top_level_info_plists(Pathname.glob(dir/"**"/source/"Contents"/"Info.plist")).sort
      end
    end

    info_plist_paths.each(&parse_info_plist)

    pkg_paths = pkgs.flat_map { |pkg| Pathname.glob(dir/"**"/pkg.path.basename).sort }
    pkg_paths = Pathname.glob(dir/"**"/"*.pkg").sort if pkg_paths.empty?

    pkg_paths.each do |pkg_path|
      Dir.mktmpdir("cask-checker", HOMEBREW_TEMP) do |extract_dir|
        extract_dir = Pathname(extract_dir)
        FileUtils.rmdir extract_dir

        system_command! "pkgutil", args: ["--expand-full", pkg_path, extract_dir]

        top_level_info_plist_paths = top_level_info_plists(Pathname.glob(extract_dir/"**/Contents/Info.plist"))

        top_level_info_plist_paths.each(&parse_info_plist)
      ensure
        Cask::Utils.gain_permissions_remove(extract_dir)
        Pathname(extract_dir).mkpath
      end
    end

    nil
  end

  versions
end

#appsArray<Cask::Artifact::App>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



29
30
31
# File 'unversioned_cask_checker.rb', line 29

def apps
  @apps ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::App) }
end

#colorpickersArray<Cask::Artifact::Colorpicker>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



54
55
56
# File 'unversioned_cask_checker.rb', line 54

def colorpickers
  @colorpickers ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::Colorpicker) }
end

#dictionariesArray<Cask::Artifact::Dictionary>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



44
45
46
# File 'unversioned_cask_checker.rb', line 44

def dictionaries
  @dictionaries ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::Dictionary) }
end

#guess_cask_versionString?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'unversioned_cask_checker.rb', line 173

def guess_cask_version
  if apps.empty? && pkgs.empty? && qlplugins.empty?
    opoo "Cask #{cask} does not contain any apps, qlplugins or PKG installers."
    return
  end

  Dir.mktmpdir("cask-checker", HOMEBREW_TEMP) do |dir|
    dir = Pathname(dir)

    installer.then do |i|
      i.extract_primary_container(to: dir)
    rescue ErrorDuringExecution => e
      onoe e
      return nil
    end

    info_plist_paths = apps.flat_map do |app|
      top_level_info_plists(Pathname.glob(dir/"**"/app.source.basename/"Contents"/"Info.plist")).sort
    end

    info_plist_paths.each do |info_plist_path|
      if (version = BundleVersion.from_info_plist(info_plist_path))
        return version.nice_version
      end
    end

    pkg_paths = pkgs.flat_map do |pkg|
      Pathname.glob(dir/"**"/pkg.path.basename).sort
    end

    pkg_paths.each do |pkg_path|
      packages =
        system_command!("installer", args: ["-plist", "-pkginfo", "-pkg", pkg_path])
        .plist
        .map { |package| package.fetch("Package") }

      Dir.mktmpdir("cask-checker", HOMEBREW_TEMP) do |extract_dir|
        extract_dir = Pathname(extract_dir)
        FileUtils.rmdir extract_dir

        begin
          system_command! "pkgutil", args: ["--expand-full", pkg_path, extract_dir]
        rescue ErrorDuringExecution => e
          onoe "Failed to extract #{pkg_path.basename}: #{e}"
          next
        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.filter_map { |i| BundleVersion.from_info_plist(i)&.nice_version }
                                    .uniq
        return unique_info_plist_versions.first if unique_info_plist_versions.count == 1

        package_info_path = extract_dir/"PackageInfo"
        if package_info_path.exist?
          if (version = BundleVersion.from_package_info(package_info_path))
            return version.nice_version
          end
        elsif packages.count == 1
          onoe "#{pkg_path.basename} does not contain a `PackageInfo` file."
        end

        distribution_path = extract_dir/"Distribution"
        if distribution_path.exist?
          require "rexml/document"

          xml = REXML::Document.new(distribution_path.read)

          product = xml.get_elements("//installer-gui-script//product").first
          product_version = product["version"] if product
          return product_version if product_version.present?
        end

        opoo "#{pkg_path.basename} contains multiple packages: #{packages}" if packages.count != 1

        $stderr.puts Pathname.glob(extract_dir/"**/*")
                             .map { |path|
                               regex = %r{\A(.*?\.(app|qlgenerator|saver|plugin|kext|bundle|osax))/.*\Z}
                               path.to_s.sub(regex, '\1')
                             }.uniq
      ensure
        Cask::Utils.gain_permissions_remove(extract_dir)
        Pathname(extract_dir).mkpath
      end
    end

    nil
  end
end

#installerCask::Installer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



24
25
26
# File 'unversioned_cask_checker.rb', line 24

def installer
  @installer ||= Cask::Installer.new(cask, verify_download_integrity: false)
end

#installersArray<Cask::Artifact::Installer>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



64
65
66
# File 'unversioned_cask_checker.rb', line 64

def installers
  @installers ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::Installer) }
end

#keyboard_layoutsArray<Cask::Artifact::KeyboardLayout>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



34
35
36
# File 'unversioned_cask_checker.rb', line 34

def keyboard_layouts
  @keyboard_layouts ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::KeyboardLayout) }
end

#mdimportersArray<Cask::Artifact::Mdimporter>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



59
60
61
# File 'unversioned_cask_checker.rb', line 59

def mdimporters
  @mdimporters ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::Mdimporter) }
end

#pkgsArray<Cask::Artifact::Pkg>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



69
70
71
# File 'unversioned_cask_checker.rb', line 69

def pkgs
  @pkgs ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::Pkg) }
end

#qlpluginsArray<Cask::Artifact::Qlplugin>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



39
40
41
# File 'unversioned_cask_checker.rb', line 39

def qlplugins
  @qlplugins ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::Qlplugin) }
end

#screen_saversArray<Cask::Artifact::ScreenSaver>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



49
50
51
# File 'unversioned_cask_checker.rb', line 49

def screen_savers
  @screen_savers ||= @cask.artifacts.select { |a| a.is_a?(Cask::Artifact::ScreenSaver) }
end

#single_app_cask?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


74
75
76
# File 'unversioned_cask_checker.rb', line 74

def single_app_cask?
  apps.count == 1
end

#single_pkg_cask?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


84
85
86
# File 'unversioned_cask_checker.rb', line 84

def single_pkg_cask?
  pkgs.count == 1
end

#single_qlplugin_cask?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


79
80
81
# File 'unversioned_cask_checker.rb', line 79

def single_qlplugin_cask?
  qlplugins.count == 1
end

#top_level_info_plists(paths) ⇒ Array<Pathname>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Filter paths to Info.plist files so that ones belonging to e.g. nested .apps are ignored.

Parameters:

Returns:



91
92
93
94
95
96
97
98
99
100
# File 'unversioned_cask_checker.rb', line 91

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