Class: CF::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/cf/detect.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, path) ⇒ Detector

Returns a new instance of Detector.



3
4
5
6
# File 'lib/cf/detect.rb', line 3

def initialize(client, path)
  @client = client
  @path = path
end

Instance Method Details

#all_frameworksObject



8
9
10
11
# File 'lib/cf/detect.rb', line 8

def all_frameworks
  info = @client.info
  info["frameworks"] || {}
end

#frameworksObject



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
# File 'lib/cf/detect.rb', line 13

def frameworks
  info = @client.info

  matches = {}
  all_frameworks.each do |name, meta|
    matched = false

    # e.g. standalone has no detection
    next if meta["detection"].nil?

    meta["detection"].first.each do |file, match|
      files =
        if File.file? @path
          if File.fnmatch(file, @path)
            [@path]
          else
            []
          end
        else
          Dir.glob("#@path/#{file}")
        end

      unless files.empty?
        if match == true
          matched = true
        elsif match == false
          matched = false
          break
        else
          files.each do |f|
            contents = File.open(f, &:read)
            if contents =~ Regexp.new(match)
              matched = true
            end
          end
        end
      end
    end

    if matched
      matches[name] = meta
    end
  end

  if matches.size == 1
    default = matches.keys.first
  end

  [matches, default]
end