Class: Bundler::Sbom::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/sbom/generator.rb

Class Method Summary collapse

Class Method Details

.convert_to_xml(sbom) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/bundler/sbom/generator.rb', line 31

def self.convert_to_xml(sbom)
  if sbom["bomFormat"] == "CycloneDX"
    CycloneDX.to_xml(sbom)
  else
    SPDX.to_xml(sbom)
  end
end

.generate_sbom(format = "spdx") ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bundler/sbom/generator.rb', line 13

def self.generate_sbom(format = "spdx")
  lockfile_path = Bundler.default_lockfile
  if !lockfile_path || !lockfile_path.exist?
    Bundler.ui.error "No Gemfile.lock found. Run `bundle install` first."
    raise GemfileLockNotFoundError, "No Gemfile.lock found"
  end

  lockfile = Bundler::LockfileParser.new(lockfile_path.read)
  document_name = File.basename(Dir.pwd)

  case format.to_s.downcase
  when "cyclonedx"
    CycloneDX.generate(lockfile, document_name)
  else # default to spdx
    SPDX.generate(lockfile, document_name)
  end
end

.parse_xml(xml_content) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bundler/sbom/generator.rb', line 39

def self.parse_xml(xml_content)
  doc = REXML::Document.new(xml_content)
  root = doc.root

  # Determine if it's CycloneDX or SPDX
  if root.name == "bom" && root.namespace.include?("cyclonedx.org")
    CycloneDX.parse_xml(doc)
  else
    SPDX.parse_xml(doc)
  end
end