Skip to content

Commit c475446

Browse files
authored
Merge pull request #96 from CycloneDX/library-refactor
Refactor to use utils library
2 parents 3ffc31e + 2cfbe8e commit c475446

File tree

13 files changed

+84
-262
lines changed

13 files changed

+84
-262
lines changed

cyclonedx/Commands/Convert/AnalyzeCommand.cs renamed to cyclonedx/Commands/AnalyzeCommand.cs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using CycloneDX.Json;
1212
using CycloneDX.CLI.Commands;
1313
using CycloneDX.CLI.Models;
14+
using CycloneDX.Utils;
1415

1516
namespace CycloneDX.CLI
1617
{
@@ -34,43 +35,16 @@ public static async Task<int> Analyze(
3435
var inputBomFormat = InputFormatHelper(inputFile, inputFormat);
3536
if (inputBomFormat == BomFormat.Unsupported) return (int)ExitCode.ParameterValidationError;
3637

37-
var inputBomString = InputFileHelper(inputFile);
38+
var inputBomString = await InputFileHelper(inputFile);
3839
if (inputBomString == null) return (int)ExitCode.ParameterValidationError;
3940

40-
var inputBom = Utils.BomDeserializer(inputBomString, inputBomFormat);
41+
var inputBom = CLIUtils.BomDeserializer(inputBomString, inputBomFormat);
4142

4243
var result = new AnalyzeResult();
4344

4445
if (multipleComponentVersions)
4546
{
46-
result.MultipleComponentVersions = new List<List<Component>>();
47-
48-
var componentCache = new Dictionary<string, List<Component>>();
49-
foreach (var component in inputBom.Components)
50-
{
51-
var componentIdentifier = $"{component.Group}:{component.Name}";
52-
if (!componentCache.ContainsKey(componentIdentifier))
53-
{
54-
componentCache[componentIdentifier] = new List<Component>();
55-
}
56-
componentCache[componentIdentifier].Add(component);
57-
}
58-
59-
foreach (var componentEntry in componentCache)
60-
{
61-
if (componentEntry.Value.Count > 1)
62-
{
63-
var firstVersion = componentEntry.Value.First().Version;
64-
foreach (var component in componentEntry.Value)
65-
{
66-
if (component.Version != firstVersion)
67-
{
68-
result.MultipleComponentVersions.Add(componentEntry.Value);
69-
break;
70-
}
71-
}
72-
}
73-
}
47+
result.MultipleComponentVersions = CycloneDXUtils.MultipleComponentVersions(inputBom);
7448
}
7549

7650
if (outputFormat == StandardOutputFormat.json)
@@ -108,9 +82,9 @@ public static async Task<int> Analyze(
10882
{
10983
foreach (var componentEntry in result.MultipleComponentVersions)
11084
{
111-
Console.Write(componentEntry.First().Name);
85+
Console.Write(componentEntry.Key);
11286
Console.Write(" versions:");
113-
foreach (var component in componentEntry)
87+
foreach (var component in componentEntry.Value)
11488
{
11589
Console.Write(" ");
11690
Console.Write(component.Version);

cyclonedx/Commands/Convert/ConvertCommand.cs renamed to cyclonedx/Commands/ConvertCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static async Task<int> Convert(string inputFile, string outputFile, Input
3838
Console.Error.WriteLine("You must specify a value for --output-format when standard output is used");
3939
return (int)ExitCode.ParameterValidationError;
4040
}
41-
outputBomFormat = Utils.DetectFileFormat(outputFile);
41+
outputBomFormat = CLIUtils.DetectFileFormat(outputFile);
4242
if (outputBomFormat == BomFormat.Unsupported)
4343
{
4444
Console.Error.WriteLine("Unable to auto-detect output format from output filename");
@@ -50,11 +50,11 @@ public static async Task<int> Convert(string inputFile, string outputFile, Input
5050
outputBomFormat = (BomFormat)outputFormat;
5151
}
5252

53-
inputBomString = InputFileHelper(inputFile);
53+
inputBomString = await InputFileHelper(inputFile);
5454
if (inputBomString == null) return (int)ExitCode.ParameterValidationError;
5555

56-
inputBom = Utils.BomDeserializer(inputBomString, inputBomFormat);
57-
outputBomString = Utils.BomSerializer(inputBom, outputBomFormat);
56+
inputBom = CLIUtils.BomDeserializer(inputBomString, inputBomFormat);
57+
outputBomString = CLIUtils.BomSerializer(inputBom, outputBomFormat);
5858

5959
if (string.IsNullOrEmpty(outputFile))
6060
{
File renamed without changes.
Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using CycloneDX.Models.v1_2;
1111
using CycloneDX.Json;
12+
using CycloneDX.Utils;
1213
using CycloneDX.CLI.Commands;
1314
using CycloneDX.CLI.Models;
1415

@@ -37,71 +38,17 @@ public static async Task<int> Diff(
3738
var toBomFormat = InputFormatHelper(toFile, toFormat);
3839
if (fromBomFormat == BomFormat.Unsupported || toBomFormat == BomFormat.Unsupported) return (int)ExitCode.ParameterValidationError;
3940

40-
var fromBomString = File.ReadAllText(fromFile);
41-
var toBomString = File.ReadAllText(toFile);
41+
var fromBomString = await File.ReadAllTextAsync(fromFile);
42+
var toBomString = await File.ReadAllTextAsync(toFile);
4243

43-
var fromBom = Utils.BomDeserializer(fromBomString, fromBomFormat);
44-
var toBom = Utils.BomDeserializer(toBomString, toBomFormat);
44+
var fromBom = CLIUtils.BomDeserializer(fromBomString, fromBomFormat);
45+
var toBom = CLIUtils.BomDeserializer(toBomString, toBomFormat);
4546

4647
var result = new DiffResult();
4748

4849
if (componentVersions)
4950
{
50-
result.ComponentVersions = new Dictionary<string, DiffItem<Component>>();
51-
52-
// make a copy of components that are still to be processed
53-
var fromComponents = new List<Component>(fromBom.Components);
54-
var toComponents = new List<Component>(toBom.Components);
55-
56-
// unchanged component versions
57-
// loop over the toBom and fromBom Components list as we will be modifying the fromComponents list
58-
foreach (var fromComponent in fromBom.Components)
59-
{
60-
// if component version is in both SBOMs
61-
if (toBom.Components.Count(toComponent =>
62-
toComponent.Group == fromComponent.Group
63-
&& toComponent.Name == fromComponent.Name
64-
&& toComponent.Version == fromComponent.Version
65-
) > 0)
66-
{
67-
var componentIdentifier = $"{fromComponent.Group}:{fromComponent.Name}";
68-
69-
if (!result.ComponentVersions.ContainsKey(componentIdentifier))
70-
{
71-
result.ComponentVersions.Add(componentIdentifier, new DiffItem<Component>());
72-
}
73-
74-
result.ComponentVersions[componentIdentifier].Unchanged.Add(fromComponent);
75-
76-
fromComponents.RemoveAll(c => c.Group == fromComponent.Group && c.Name == fromComponent.Name && c.Version == fromComponent.Version);
77-
toComponents.RemoveAll(c => c.Group == fromComponent.Group && c.Name == fromComponent.Name && c.Version == fromComponent.Version);
78-
}
79-
}
80-
81-
// added component versions
82-
foreach (var component in new List<Component>(toComponents))
83-
{
84-
var componentIdentifier = $"{component.Group}:{component.Name}";
85-
if (!result.ComponentVersions.ContainsKey(componentIdentifier))
86-
{
87-
result.ComponentVersions.Add(componentIdentifier, new DiffItem<Component>());
88-
}
89-
90-
result.ComponentVersions[componentIdentifier].Added.Add(component);
91-
}
92-
93-
// removed components versions
94-
foreach (var component in new List<Component>(fromComponents))
95-
{
96-
var componentIdentifier = $"{component.Group}:{component.Name}";
97-
if (!result.ComponentVersions.ContainsKey(componentIdentifier))
98-
{
99-
result.ComponentVersions.Add(componentIdentifier, new DiffItem<Component>());
100-
}
101-
102-
result.ComponentVersions[componentIdentifier].Removed.Add(component);
103-
}
104-
51+
result.ComponentVersions = CycloneDXUtils.ComponentVersionDiff(fromBom, toBom);
10552
}
10653

10754
if (outputFormat == StandardOutputFormat.json)

cyclonedx/Commands/MergeCommand.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
using System.CommandLine.Invocation;
55
using System.IO;
66
using System.Threading.Tasks;
7+
using CycloneDX.Json;
78
using CycloneDX.Models.v1_2;
89
using CycloneDX.Xml;
9-
using CycloneDX.Json;
10+
using CycloneDX.Utils;
1011

1112
namespace CycloneDX.CLI
1213
{
@@ -38,11 +39,11 @@ public static async Task<int> Merge(MergeCommandOptions options)
3839
var outputFormat = options.OutputFormat;
3940
if (outputFormat == StandardInputOutputSbomFormat.autodetect)
4041
{
41-
if (options.OutputFile != null && options.OutputFile.EndsWith(".json"))
42+
if (options.OutputFile != null && options.OutputFile.EndsWith(".json", StringComparison.InvariantCulture))
4243
{
4344
outputFormat = StandardInputOutputSbomFormat.json;
4445
}
45-
else if (options.OutputFile != null && options.OutputFile.EndsWith(".xml"))
46+
else if (options.OutputFile != null && options.OutputFile.EndsWith(".xml", StringComparison.InvariantCulture))
4647
{
4748
outputFormat = StandardInputOutputSbomFormat.xml;
4849
}
@@ -54,22 +55,19 @@ public static async Task<int> Merge(MergeCommandOptions options)
5455

5556
}
5657

57-
var outputBom = new Bom
58-
{
59-
Components = new List<Component>()
60-
};
58+
var outputBom = new Bom();
6159

6260
foreach (var inputFilename in options.InputFiles)
6361
{
6462
if (!outputToConsole) Console.WriteLine($"Processing input file {inputFilename}");
6563
var inputFormat = options.InputFormat;
6664
if (inputFormat == StandardInputOutputSbomFormat.autodetect)
6765
{
68-
if (inputFilename.EndsWith(".json"))
66+
if (inputFilename.EndsWith(".json", StringComparison.InvariantCulture))
6967
{
7068
inputFormat = StandardInputOutputSbomFormat.json;
7169
}
72-
else if (inputFilename.EndsWith(".xml"))
70+
else if (inputFilename.EndsWith(".xml", StringComparison.InvariantCulture))
7371
{
7472
inputFormat = StandardInputOutputSbomFormat.xml;
7573
}
@@ -93,10 +91,9 @@ public static async Task<int> Merge(MergeCommandOptions options)
9391
inputBom = XmlBomDeserializer.Deserialize(bomContents);
9492
}
9593

96-
if (inputBom.Components != null) {
97-
if (!outputToConsole) Console.WriteLine($" Contains {inputBom.Components.Count} components");
98-
outputBom.Components.AddRange(inputBom.Components);
99-
}
94+
outputBom = CycloneDXUtils.Merge(outputBom, inputBom);
95+
if (inputBom.Components != null && !outputToConsole)
96+
Console.WriteLine($" Contains {inputBom.Components.Count} components");
10097
}
10198

10299
string outputBomString;

0 commit comments

Comments
 (0)