Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools/tool is deprecated in cdx standard. Using tools/components instead. #804

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CycloneDX.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Threading.Tasks;
using CycloneDX.Interfaces;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void CheckMetaDataTemplate()
{
var bom = new Bom();
string resourcePath = Path.Join(AppContext.BaseDirectory, "Resources", "metadata");
bom = Runner.ReadMetaDataFromFile(bom, Path.Join(resourcePath, "cycloneDX-metadata-template.xml"));
bom = Runner.ReadMetaDataFromFile(bom, new FileSystem(), Path.Join(resourcePath, "cycloneDX-metadata-template.xml"));
Assert.NotNull(bom.Metadata);
Assert.Matches("CycloneDX", bom.Metadata.Component.Name);
Assert.NotEmpty(bom.Metadata.Tools.Tools);
Expand Down
43 changes: 27 additions & 16 deletions CycloneDX/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,14 @@ public async Task<int> HandleCommandAsync(RunOptions options)

if (!string.IsNullOrEmpty(importMetadataPath))
{
if (!File.Exists(importMetadataPath))
if (!fileSystem.File.Exists(importMetadataPath))
{
Console.Error.WriteLine($"Metadata template '{importMetadataPath}' does not exist.");
return (int)ExitCode.InvalidOptions;
}
else
{
bom = ReadMetaDataFromFile(bom, importMetadataPath);
bom = ReadMetaDataFromFile(bom, fileSystem, importMetadataPath);
}
}
SetMetadataComponentIfNecessary(bom, topLevelComponent);
Expand Down Expand Up @@ -442,11 +442,11 @@ private static void SetMetadataComponentIfNecessary(Bom bom, Component topLevelC
}
}

internal static Bom ReadMetaDataFromFile(Bom bom, string templatePath)
internal static Bom ReadMetaDataFromFile(Bom bom, IFileSystem fileSystem, string templatePath)
{
try
{
return Xml.Serializer.Deserialize(File.ReadAllText(templatePath));
return Xml.Serializer.Deserialize(fileSystem.File.ReadAllText(templatePath));
}
catch (IOException ex)
{
Expand All @@ -462,26 +462,37 @@ internal static void AddMetadataTool(Bom bom)

bom.Metadata ??= new Metadata();
bom.Metadata.Tools ??= new ToolChoices();
#pragma warning disable CS0618 // Type or member is obsolete
bom.Metadata.Tools.Tools ??= new List<Tool>();
#pragma warning restore CS0618 // Type or member is obsolete

var index = bom.Metadata.Tools.Tools.FindIndex(p => p.Name == toolname);
if (bom.Metadata.Tools.Components == null)
{
if(bom.Metadata.Tools.Services != null)
{
Console.WriteLine("Cannot add CycloneDX as test component because there is already a service-collection (components and services are exclusive)");
return;
}
bom.Metadata.Tools.Components = new List<Component>();
}

var index = bom.Metadata.Tools.Components.FindIndex(p => p.Name == toolname);
if (index == -1)
{
#pragma warning disable CS0618 // Type or member is obsolete
bom.Metadata.Tools.Tools.Add(new Tool
bom.Metadata.Tools.Components.Add(
new Component
{
Type = Component.Classification.Application,
Name = toolname,
Vendor = "CycloneDX",
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString()
}
);
#pragma warning restore CS0618 // Type or member is obsolete
Author = "CycloneDX",
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(),
ExternalReferences = new List<ExternalReference> {
new ExternalReference {
Type = ExternalReference.ExternalReferenceType.Website,
Url = "https://github.com/CycloneDX/cyclonedx-dotnet"
}}});

}
else
{
bom.Metadata.Tools.Tools[index].Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
bom.Metadata.Tools.Components[index].Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}

Expand Down