Class: Hackmac::KextUpgrader

Inherits:
Object
  • Object
show all
Includes:
FileUtils, AssetTools
Defined in:
lib/hackmac/kext_upgrader.rb

Instance Method Summary collapse

Constructor Details

#initialize(path:, config:, force: false) ⇒ KextUpgrader

Returns a new instance of KextUpgrader.



10
11
12
# File 'lib/hackmac/kext_upgrader.rb', line 10

def initialize(path:, config:, force: false)
  @path, @config, @force = path, config, force
end

Instance Method Details

#performObject



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
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hackmac/kext_upgrader.rb', line 16

def perform
  isolate do |dir|
    kext = Kext.new(path: @path, config: @config)
    case
    when kext.remote_version.nil?
      puts "No source defined for #{kext}"
    when (kext.remote_version > kext.version rescue false) || @force
      name, data = kext.remote_kext.download_asset
      if name
        File.secure_write(name, data)
        decompress(name)
        kext_pathes = []
        Find.find(dir) do |path|
          if File.directory?(path)
            case File.basename(path)
            when /\A\./
              Find.prune
            when "#{kext.name}.kext"
              kext_pathes.unshift path
            when
              *@config.kext.sources[kext.name]&.plugins?&.map { |p| p + '.kext' }
            then
              kext_pathes << path
            end
          end
        end
        kext_pathes.each do |kext_path|
          old_path = (Pathname.new(@path).dirname + File.basename(kext_path)).to_s
          if ask("Really upgrade #{old_path.inspect} to version #{kext.remote_version}? (y/n) ")
            rm_rf old_path
            cp_r kext_path, old_path
          end
        end
      else
        fail "#{kext.remote_kext.name} could not be downloaded"
      end
    else
      puts "#{kext} is already the latest version"
    end
  end
end