Sha256: cead7f020a91644b8ba0c1f2ec1ae67a912f8028eddf0913e1aaef142925ec1c

Contents?: true

Size: 1.44 KB

Versions: 16

Compression:

Stored size: 1.44 KB

Contents

module LicenseFinder
  class ProjectFinder
    def initialize(main_project_path)
      @package_managers = LicenseFinder::PackageManager.package_managers
      @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
      if active_project?(potential_project_path)
        remove_nested(potential_project_path, all_paths)
        return potential_project_path.to_s
      end
    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|
        pm.new(project_path: project_path).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

16 entries across 16 versions & 1 rubygems

Version Path
license_finder-3.0.4 lib/license_finder/project_finder.rb
license_finder-3.0.2 lib/license_finder/project_finder.rb
license_finder-3.0.1 lib/license_finder/project_finder.rb
license_finder-3.0.0 lib/license_finder/project_finder.rb
license_finder-2.1.2 lib/license_finder/project_finder.rb
license_finder-2.1.1 lib/license_finder/project_finder.rb
license_finder-2.1.0 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc9 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc8 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc7 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc6 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc5 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc4 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc3 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc2 lib/license_finder/project_finder.rb
license_finder-2.1.0.rc1 lib/license_finder/project_finder.rb