Sha256: be6fb9d60a7a5e0acd3e3ad18c1e4111919a0e605a5f077c5d4c9a1318bf0afb

Contents?: true

Size: 1.98 KB

Versions: 10

Compression:

Stored size: 1.98 KB

Contents

module VMC
  class Detector
    def initialize(client, path)
      @client = client
      @path = path
    end

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

    def find_top(entries)
      found = false

      entries.each do |e|
        is_toplevel =
          e.ftype == :directory && e.name.index("/") + 1 == e.name.size

        if is_toplevel && e.name !~ /^(\.|__MACOSX)/
          if found
            return false
          else
            found = e.name
          end
        end
      end

      found
    end

    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]
              elsif @path =~ /\.(zip|jar|war)/
                lines = CFoundry::Zip.entry_lines(@path)
                top = find_top(lines)

                lines.collect(&:name).select do |path|
                  File.fnmatch(file, path) ||
                    top && File.fnmatch(top + file, path)
                end
              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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
vmc-0.4.0.beta.13 vmc-ng/lib/vmc/detect.rb
vmc-0.4.0.beta.12 vmc-ng/lib/vmc/detect.rb
vmc-0.4.0.beta.11 vmc-ng/lib/vmc/detect.rb
vmc-0.4.0.beta.10 vmc-ng/lib/vmc/detect.rb
vmc-0.4.0.beta.9 lib/vmc/detect.rb
vmc-0.4.0.beta.8 vmc-ng/lib/vmc/detect.rb
vmc-0.4.0.beta.7 vmc-ng/lib/vmc/detect.rb
vmc-0.4.0.beta.6 lib/vmc/detect.rb
vmc-0.4.0.beta.5 vmc-ng/lib/vmc/detect.rb
vmc-0.4.0.beta.3 vmc-ng/lib/vmc/detect.rb