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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
171
172
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
  | 
module Hbc
  module CaskLoader
    class FromContentLoader
      attr_reader :content
      def self.can_load?(ref)
        return false unless ref.respond_to?(:to_str)
        content = ref.to_str
        token  = /(?:"[^"]*"|'[^']*')/
        curly  = /\(\s*#{token}\s*\)\s*\{.*\}/
        do_end = /\s+#{token}\s+do(?:\s*;\s*|\s+).*end/
        regex  = /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m
        content.match?(regex)
      end
      def initialize(content)
        @content = content
      end
      def load
        instance_eval(content.force_encoding("UTF-8"), __FILE__, __LINE__)
      end
      private
      def cask(header_token, **options, &block)
        Cask.new(header_token, **options, &block)
      end
    end
    class FromPathLoader < FromContentLoader
      def self.can_load?(ref)
        path = Pathname(ref)
        path.extname == ".rb" && path.expand_path.exist?
      end
      attr_reader :token, :path
      def initialize(path)
        path = Pathname(path).expand_path
        @token = path.basename(".rb").to_s
        @path = path
      end
      def load
        raise CaskUnavailableError.new(token, "'#{path}' does not exist.")  unless path.exist?
        raise CaskUnavailableError.new(token, "'#{path}' is not readable.") unless path.readable?
        raise CaskUnavailableError.new(token, "'#{path}' is not a file.")   unless path.file?
        @content = IO.read(path)
        super
      end
      private
      def cask(header_token, **options, &block)
        if token != header_token
          raise CaskTokenMismatchError.new(token, header_token)
        end
        super(header_token, **options, sourcefile_path: path, &block)
      end
    end
    class FromURILoader < FromPathLoader
      def self.can_load?(ref)
        uri_regex = ::URI::DEFAULT_PARSER.make_regexp
        ref.to_s.match?(Regexp.new('\A' + uri_regex.source + '\Z', uri_regex.options))
      end
      attr_reader :url
      def initialize(url)
        @url = URI(url)
        super Hbc.cache/File.basename(@url.path)
      end
      def load
        path.dirname.mkpath
        begin
          ohai "Downloading #{url}."
          curl_download url, to: path
        rescue ErrorDuringExecution
          raise CaskUnavailableError.new(token, "Failed to download #{Formatter.url(url)}.")
        end
        super
      end
    end
    class FromTapPathLoader < FromPathLoader
      def self.can_load?(ref)
        ref.to_s.match?(HOMEBREW_TAP_PATH_REGEX) && super
      end
      attr_reader :tap
      def initialize(tap_path)
        @tap = Tap.from_path(tap_path)
        super tap_path
      end
      private
      def cask(*args, &block)
        super(*args, tap: tap, &block)
      end
    end
    class FromTapLoader < FromTapPathLoader
      def self.can_load?(ref)
        ref.to_s.match?(HOMEBREW_TAP_CASK_REGEX)
      end
      def initialize(tapped_name)
        user, repo, token = tapped_name.split("/", 3)
        super Tap.fetch(user, repo).cask_dir/"#{token}.rb"
      end
      def load
        tap.install unless tap.installed?
        super
      end
    end
    class FromInstanceLoader
      attr_reader :cask
      def self.can_load?(ref)
        ref.is_a?(Cask)
      end
      def initialize(cask)
        @cask = cask
      end
      def load
        cask
      end
    end
    class NullLoader < FromPathLoader
      def self.can_load?(*)
        true
      end
      def initialize(ref)
        token = File.basename(ref, ".rb")
        super CaskLoader.default_path(token)
      end
      def load
        raise CaskUnavailableError.new(token, "No Cask with this name exists.")
      end
    end
    def self.path(ref)
      self.for(ref).path
    end
    def self.load(ref)
      self.for(ref).load
    end
    def self.for(ref)
      [
        FromInstanceLoader,
        FromContentLoader,
        FromURILoader,
        FromTapLoader,
        FromTapPathLoader,
        FromPathLoader,
      ].each do |loader_class|
        return loader_class.new(ref) if loader_class.can_load?(ref)
      end
      if FromTapPathLoader.can_load?(default_path(ref))
        return FromTapPathLoader.new(default_path(ref))
      end
      case (possible_tap_casks = tap_paths(ref)).count
      when 1
        return FromTapPathLoader.new(possible_tap_casks.first)
      when 2..Float::INFINITY
        loaders = possible_tap_casks.map(&FromTapPathLoader.method(:new))
        raise CaskError, <<~EOS
          Cask #{ref} exists in multiple taps:
          #{loaders.map { |loader| "  #{loader.tap}/#{loader.token}" }.join("\n")}
        EOS
      end
      possible_installed_cask = Cask.new(ref)
      if possible_installed_cask.installed?
        return FromPathLoader.new(possible_installed_cask.installed_caskfile)
      end
      NullLoader.new(ref)
    end
    def self.default_path(token)
      Hbc.default_tap.cask_dir/"#{token.to_s.downcase}.rb"
    end
    def self.tap_paths(token)
      Tap.map { |t| t.cask_dir/"#{token.to_s.downcase}.rb" }
         .select(&:exist?)
    end
  end
end
  |