aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/auditor.rb
blob: ec17f3cad63e97b84a217ec62d50c6b62ca916db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
57
58
59
60
61
62
63
module Hbc
  class Auditor
    def self.audit(cask, audit_download: false, check_token_conflicts: false)
      new(cask, audit_download, check_token_conflicts).audit
    end

    attr_reader :cask

    def initialize(cask, audit_download, check_token_conflicts)
      @cask = cask
      @audit_download = audit_download
      @check_token_conflicts = check_token_conflicts
    end

    def audit_download?
      @audit_download
    end

    def check_token_conflicts?
      @check_token_conflicts
    end

    def audit
      if !ARGV.value("language") && language_blocks
        audit_all_languages
      else
        audit_cask_instance(cask)
      end
    end

    private

    def audit_all_languages
      saved_languages = MacOS.instance_variable_get(:@languages)
      begin
        language_blocks.keys.all?(&method(:audit_languages))
      ensure
        MacOS.instance_variable_set(:@languages, saved_languages)
      end
    end

    def audit_languages(languages)
      ohai "Auditing language: #{languages.map { |lang| "'#{lang}'" }.join(", ")}"
      MacOS.instance_variable_set(:@languages, languages)
      audit_cask_instance(CaskLoader.load_from_file(cask.sourcefile_path))
    ensure
      CLI::Cleanup.run(cask.token) if audit_download?
    end

    def audit_cask_instance(cask)
      download = audit_download? && Download.new(cask)
      audit = Audit.new(cask, download:              download,
                              check_token_conflicts: check_token_conflicts?)
      audit.run!
      puts audit.summary
      audit.success?
    end

    def language_blocks
      cask.instance_variable_get(:@dsl).instance_variable_get(:@language_blocks)
    end
  end
end