aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/source.rb
blob: af298108a79014da94ab55a96f3b8915a04cde90 (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
module Hbc::Source; end

require "hbc/source/gone"
require "hbc/source/path_slash_required"
require "hbc/source/path_slash_optional"
require "hbc/source/tapped_qualified"
require "hbc/source/untapped_qualified"
require "hbc/source/tapped"
require "hbc/source/uri"

module Hbc::Source
  def self.sources
    [
      Hbc::Source::URI,
      Hbc::Source::PathSlashRequired,
      Hbc::Source::TappedQualified,
      Hbc::Source::UntappedQualified,
      Hbc::Source::Tapped,
      Hbc::Source::PathSlashOptional,
      Hbc::Source::Gone,
    ]
  end

  def self.for_query(query)
    odebug "Translating '#{query}' into a valid Cask source"
    raise Hbc::CaskUnavailableError, query if query.to_s =~ %r{^\s*$}
    source = sources.find { |s|
      odebug "Testing source class #{s}"
      s.me?(query)
    }
    raise Hbc::CaskUnavailableError, query unless source
    odebug "Success! Using source class #{source}"
    resolved_cask_source = source.new(query)
    odebug "Resolved Cask URI or file source to '#{resolved_cask_source}'"
    resolved_cask_source
  end
end