Sha256: 5fe595c97344bf07fdbb6f649bb2bca9e0452f98f9dea76bbea04ffd3ff6c263

Contents?: true

Size: 1.32 KB

Versions: 15

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 = path
        @dir = File.dirname(path)
        @document = Nokogiri::XML(IO.read(path)).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

15 entries across 15 versions & 1 rubygems

Version Path
spandx-0.13.3 lib/spandx/dotnet/project_file.rb
spandx-0.13.2 lib/spandx/dotnet/project_file.rb
spandx-0.13.1 lib/spandx/dotnet/project_file.rb
spandx-0.13.0 lib/spandx/dotnet/project_file.rb
spandx-0.12.3 lib/spandx/dotnet/project_file.rb
spandx-0.12.2 lib/spandx/dotnet/project_file.rb
spandx-0.12.1 lib/spandx/dotnet/project_file.rb
spandx-0.12.0 lib/spandx/dotnet/project_file.rb
spandx-0.11.0 lib/spandx/dotnet/project_file.rb
spandx-0.10.1 lib/spandx/dotnet/project_file.rb
spandx-0.10.0 lib/spandx/dotnet/project_file.rb
spandx-0.9.0 lib/spandx/dotnet/project_file.rb
spandx-0.8.0 lib/spandx/dotnet/project_file.rb
spandx-0.7.0 lib/spandx/dotnet/project_file.rb
spandx-0.6.0 lib/spandx/dotnet/project_file.rb