Sha256: a184b01ecd0ef13389cc8a0b3f9e7728971cda71cce1fa636106016579d64a99

Contents?: true

Size: 1.57 KB

Versions: 31

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module LicenseFinder
  class ProjectFinder
    def initialize(main_project_path, strict_matching = false)
      @package_managers = LicenseFinder::Scanner::PACKAGE_MANAGERS
      @strict_matching = strict_matching
      @main_project_path = main_project_path
    end

    def find_projects
      project_paths = []
      all_paths = find_all_paths
      until all_paths.empty?
        project_paths << collect_project_path(all_paths)
        all_paths.shift
      end
      project_paths.compact
    end

    def collect_project_path(all_paths)
      potential_project_path = all_paths.first
      is_active_project = active_project?(potential_project_path)
      return unless is_active_project

      potential_project_path.to_s
    end

    private

    def find_all_paths
      Dir.glob("#{@main_project_path}/**/").map { |path| full_path(path) }
    end

    def remove_nested(pathname, paths)
      return if project_root?(pathname)

      paths.reject! { |path| nested_path?(path, pathname) }
    end

    def project_root?(pathname)
      full_path(@main_project_path).to_s == pathname.to_s
    end

    def active_project?(project_path)
      active_project = @package_managers.map do |pm_class|
        pm = pm_class.new(project_path: project_path, strict_matching: @strict_matching)
        pm.active?
      end

      active_project.include?(true)
    end

    def full_path(rel_path)
      Pathname.new(rel_path).expand_path
    end

    def nested_path?(path, pathname)
      path.to_s.start_with?(pathname.to_s) && path.to_s != pathname.to_s
    end
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
license_finder-7.1.0 lib/license_finder/project_finder.rb
license_finder-7.0.1 lib/license_finder/project_finder.rb
license_finder-7.0.0 lib/license_finder/project_finder.rb
license_finder-6.15.0 lib/license_finder/project_finder.rb
gitlab-license_finder-6.14.2.1 lib/license_finder/project_finder.rb
license_finder-6.14.2 lib/license_finder/project_finder.rb
license_finder-6.14.1 lib/license_finder/project_finder.rb
license_finder-6.13.0 lib/license_finder/project_finder.rb
license_finder-6.12.2 lib/license_finder/project_finder.rb
license_finder-6.12.1 lib/license_finder/project_finder.rb
license_finder-6.12.0 lib/license_finder/project_finder.rb
license_finder-6.11.0 lib/license_finder/project_finder.rb
license_finder-6.10.1 lib/license_finder/project_finder.rb
license_finder-6.10.0 lib/license_finder/project_finder.rb
license_finder-6.9.0 lib/license_finder/project_finder.rb
license_finder-6.8.2 lib/license_finder/project_finder.rb
license_finder-6.8.1 lib/license_finder/project_finder.rb
license_finder-6.8.0 lib/license_finder/project_finder.rb
license_finder-6.7.0 lib/license_finder/project_finder.rb
license_finder-6.6.2 lib/license_finder/project_finder.rb