Sha256: 89fc878a481240cf2db8a5440b9215cbe3ec7930aae200cb5daf61a9f108d3eb

Contents?: true

Size: 1.32 KB

Versions: 12

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Spandx
  module Dotnet
    class ProjectFile
      attr_reader :catalogue, :document, :nuget

      def initialize(path)
        @path = Pathname(path)
        @dir = @path.dirname
        @document = Nokogiri::XML(@path.read).tap(&:remove_namespaces!)
      end

      def package_references
        project_references.flat_map(&:package_references) +
          references('GlobalPackageReference') +
          references('PackageReference')
      end

      private

      def project_references
        document.search('//ProjectReference').map do |node|
          relative_project_path = node.attribute('Include').value.strip.tr('\\', '/')
          absolute_project_path = File.expand_path(File.join(@dir, relative_project_path))
          self.class.new(absolute_project_path)
        end
      end

      def references(key)
        document.search("//#{key}").map do |node|
          PackageReference.new(
            name: name_from(node),
            version: attribute_for('Version', node)
          )
        end
      end

      def name_from(node)
        attribute_for('Include', node) ||
          attribute_for('Update', node)
      end

      def attribute_for(key, node)
        node.attribute(key)&.value&.strip ||
          node.at_xpath("./#{key}")&.content&.strip
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spandx-0.18.3 lib/spandx/dotnet/project_file.rb
spandx-0.18.2 lib/spandx/dotnet/project_file.rb
spandx-0.18.1 lib/spandx/dotnet/project_file.rb
spandx-0.18.0 lib/spandx/dotnet/project_file.rb
spandx-0.17.0 lib/spandx/dotnet/project_file.rb
spandx-0.16.1 lib/spandx/dotnet/project_file.rb
spandx-0.16.0 lib/spandx/dotnet/project_file.rb
spandx-0.15.1 lib/spandx/dotnet/project_file.rb
spandx-0.15.0 lib/spandx/dotnet/project_file.rb
spandx-0.14.0 lib/spandx/dotnet/project_file.rb
spandx-0.13.5 lib/spandx/dotnet/project_file.rb
spandx-0.13.4 lib/spandx/dotnet/project_file.rb