Class: Hbc::Pkg

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/cask/lib/hbc/pkg.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_id, command = SystemCommand) ⇒ Pkg

Returns a new instance of Pkg



11
12
13
14
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 11

def initialize(package_id, command = SystemCommand)
  @package_id = package_id
  @command = command
end

Instance Attribute Details

#package_idObject (readonly)

Returns the value of attribute package_id



9
10
11
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 9

def package_id
  @package_id
end

Class Method Details

.all_matching(regexp, command) ⇒ Object



3
4
5
6
7
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 3

def self.all_matching(regexp, command)
  command.run("/usr/sbin/pkgutil", args: ["--pkgs=#{regexp}"]).stdout.split("\n").map do |package_id|
    new(package_id.chomp, command)
  end
end

Instance Method Details

#forgetObject



46
47
48
49
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 46

def forget
  odebug "Unregistering pkg receipt (aka forgetting)"
  @command.run!("/usr/sbin/pkgutil", args: ["--forget", package_id], sudo: true)
end

#infoObject



75
76
77
78
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 75

def info
  @info ||= @command.run!("/usr/sbin/pkgutil", args: ["--pkg-info-plist", package_id])
                    .plist
end

#pkgutil_bom_allObject



63
64
65
66
67
68
69
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 63

def pkgutil_bom_all
  @pkgutil_bom_all ||= @command.run!("/usr/sbin/pkgutil", args: ["--files", package_id])
                               .stdout
                               .split("\n")
                               .map { |path| root.join(path) }
                               .reject(&MacOS.public_method(:undeletable?))
end

#pkgutil_bom_dirsObject



59
60
61
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 59

def pkgutil_bom_dirs
  @pkgutil_bom_dirs ||= pkgutil_bom_all.select(&:directory?) - pkgutil_bom_specials
end

#pkgutil_bom_filesObject



51
52
53
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 51

def pkgutil_bom_files
  @pkgutil_bom_files ||= pkgutil_bom_all.select(&:file?) - pkgutil_bom_specials
end

#pkgutil_bom_specialsObject



55
56
57
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 55

def pkgutil_bom_specials
  @pkgutil_bom_specials ||= pkgutil_bom_all.select(&method(:special?))
end

#rootObject



71
72
73
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 71

def root
  @root ||= Pathname.new(info.fetch("volume")).join(info.fetch("install-location"))
end

#uninstallObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'Library/Homebrew/cask/lib/hbc/pkg.rb', line 16

def uninstall
  unless pkgutil_bom_files.empty?
    odebug "Deleting pkg files"
    @command.run!("/usr/bin/xargs", args: ["-0", "--", "/bin/rm", "--"], input: pkgutil_bom_files.join("\0"), sudo: true)
  end

  unless pkgutil_bom_specials.empty?
    odebug "Deleting pkg symlinks and special files"
    @command.run!("/usr/bin/xargs", args: ["-0", "--", "/bin/rm", "--"], input: pkgutil_bom_specials.join("\0"), sudo: true)
  end

  unless pkgutil_bom_dirs.empty?
    odebug "Deleting pkg directories"
    deepest_path_first(pkgutil_bom_dirs).each do |dir|
      with_full_permissions(dir) do
        clean_broken_symlinks(dir)
        clean_ds_store(dir)
        rmdir(dir)
      end
    end
  end

  if root.directory? && !MacOS.undeletable?(root)
    clean_ds_store(root)
    rmdir(root)
  end

  forget
end