Sha256: e501519539fb765542ab94a0c1058be118d3001de94c1274e998342640d29cff

Contents?: true

Size: 854 Bytes

Versions: 7

Compression:

Stored size: 854 Bytes

Contents

require 'hrw/scanner'

module Hrw
  #
  # To detect which platform used by current project
  #
  module Detector
    #
    # Errors
    #
    class MultipleChoiceError < StandardError; end

    #
    # Constants
    #
    DETECTABLE_FILES = {
      'Gemfile.lock': Scanner::Gemfile,
      'Pipfile.lock': Scanner::Pipfile
    }.freeze

    # Detect package manager in root dir
    #
    # @param [String] root root dir for project
    # @return [Hrw::Scanner] scanner
    def self.detect(root = Dir.pwd)
      files = Dir['*', base: root].map { |file| File.basename(file).to_sym }
      pkg_files = DETECTABLE_FILES.keys & files

      if pkg_files.empty?
        nil
      elsif pkg_files.size > 1
        raise MultipleChoiceError
      else
        file = pkg_files.first
        DETECTABLE_FILES[file].new(root, file.to_s)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hrw-0.3.2 lib/hrw/detector.rb
hrw-0.3.1 lib/hrw/detector.rb
hrw-0.3.0 lib/hrw/detector.rb
hrw-0.2.3 lib/hrw/detector.rb
hrw-0.2.2 lib/hrw/detector.rb
hrw-0.2.1 lib/hrw/detector.rb
hrw-0.2.0 lib/hrw/detector.rb