Skip to content

Commit

Permalink
Only mark dotnet dependencies to be dev dependency instead of forcibl…
Browse files Browse the repository at this point in the history
…y remove them.
  • Loading branch information
Tibor Götz committed Feb 7, 2024
1 parent aa4f23d commit d4a9cc1
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 48 deletions.
4 changes: 2 additions & 2 deletions CycloneDX.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task CallingCycloneDX_CreatesOutputDirectory()
});
var mockSolutionFileService = new Mock<ISolutionFileService>();
mockSolutionFileService
.Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>()))
.Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new HashSet<DotnetDependency>());

Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null);
Expand All @@ -72,7 +72,7 @@ public async Task CallingCycloneDX_WithOutputFilename_CreatesOutputFilename()
});
var mockSolutionFileService = new Mock<ISolutionFileService>();
mockSolutionFileService
.Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>()))
.Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new HashSet<DotnetDependency>());

Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null);
Expand Down
6 changes: 3 additions & 3 deletions CycloneDX.Tests/ProjectAssetsFileServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public void GetDotnetDependencys_PackageAsTopLevelAndTransitive(string framework
});

var projectAssetsFileService = new ProjectAssetsFileService(mockFileSystem, () => mockAssetReader.Object);
var packages = projectAssetsFileService.GetDotnetDependencys(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), XFS.Path(@"c:\SolutionPath\Project1\obj\project.assets.json"), false, false);
var packages = projectAssetsFileService.GetDotnetDependencys(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), XFS.Path(@"c:\SolutionPath\Project1\obj\project.assets.json"), false);
var sortedPackages = new List<DotnetDependency>(packages);

sortedPackages.Sort();
Expand Down Expand Up @@ -396,7 +396,7 @@ public void GetDotnetDependencys_MissingResolvedPackageVersion(string framework,
});

var projectAssetsFileService = new ProjectAssetsFileService(mockFileSystem, () => mockAssetReader.Object);
var packages = projectAssetsFileService.GetDotnetDependencys(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), XFS.Path(@"c:\SolutionPath\Project1\obj\project.assets.json"), false, false);
var packages = projectAssetsFileService.GetDotnetDependencys(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), XFS.Path(@"c:\SolutionPath\Project1\obj\project.assets.json"), false);
var sortedPackages = new List<DotnetDependency>(packages);

sortedPackages.Sort();
Expand Down Expand Up @@ -530,7 +530,7 @@ public void GetDotnetDependencys_MissingDependencies(string framework, int frame
});

var projectAssetsFileService = new ProjectAssetsFileService(mockFileSystem, () => mockAssetReader.Object);
var packages = projectAssetsFileService.GetDotnetDependencys(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), XFS.Path(@"c:\SolutionPath\Project1\obj\project.assets.json"), false, false);
var packages = projectAssetsFileService.GetDotnetDependencys(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), XFS.Path(@"c:\SolutionPath\Project1\obj\project.assets.json"), false);
var sortedPackages = new List<DotnetDependency>(packages);

sortedPackages.Sort();
Expand Down
20 changes: 10 additions & 10 deletions CycloneDX.Tests/ProjectFileServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task GetProjectDotnetDependencys_WithProjectAssetsFile_ReturnsDotne
var mockPackageFileService = new Mock<IPackagesFileService>();
var mockProjectAssetsFileService = new Mock<IProjectAssetsFileService>();
mockProjectAssetsFileService
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Returns(new HashSet<DotnetDependency>
{
new DotnetDependency { Name = "Package", Version = "1.2.3" },
Expand All @@ -93,7 +93,7 @@ public async Task GetProjectDotnetDependencys_WithProjectAssetsFile_ReturnsDotne
mockPackageFileService.Object,
mockProjectAssetsFileService.Object);

var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, false, "", "").ConfigureAwait(true);
var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, "", "").ConfigureAwait(true);

Assert.Collection(packages,
item => {
Expand All @@ -117,7 +117,7 @@ public async Task GetProjectDotnetDependencys_WithProjectAssetsFileWithoutRestor
var mockPackageFileService = new Mock<IPackagesFileService>();
var mockProjectAssetsFileService = new Mock<IProjectAssetsFileService>();
mockProjectAssetsFileService
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Returns(new HashSet<DotnetDependency>
{
new DotnetDependency { Name = "Package", Version = "1.2.3" },
Expand All @@ -129,7 +129,7 @@ public async Task GetProjectDotnetDependencys_WithProjectAssetsFileWithoutRestor
mockProjectAssetsFileService.Object);
projectFileService.DisablePackageRestore = true;

var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, false, "", "").ConfigureAwait(true);
var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, "", "").ConfigureAwait(true);

Assert.Collection(packages,
item => {
Expand All @@ -153,7 +153,7 @@ public async Task GetProjectDotnetDependencys_WithProjectAssetsFile_ReturnsMulti
var mockPackageFileService = new Mock<IPackagesFileService>();
var mockProjectAssetsFileService = new Mock<IProjectAssetsFileService>();
mockProjectAssetsFileService
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Returns(new HashSet<DotnetDependency>
{
new DotnetDependency { Name = "Package1", Version = "1.2.3" },
Expand All @@ -166,7 +166,7 @@ public async Task GetProjectDotnetDependencys_WithProjectAssetsFile_ReturnsMulti
mockPackageFileService.Object,
mockProjectAssetsFileService.Object);

var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, false, "", "").ConfigureAwait(true);
var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, "", "").ConfigureAwait(true);
var sortedPackages = new List<DotnetDependency>(packages);
sortedPackages.Sort();

Expand Down Expand Up @@ -199,15 +199,15 @@ public async Task GetProjectDotnetDependencys_WithPackagesConfig_ReturnsDotnetDe
);
var mockProjectAssetsFileService = new Mock<IProjectAssetsFileService>();
mockProjectAssetsFileService
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Returns(new HashSet<DotnetDependency>());
var projectFileService = new ProjectFileService(
mockFileSystem,
mockDotnetUtilsService.Object,
mockPackageFileService.Object,
mockProjectAssetsFileService.Object);

var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, false, "", "").ConfigureAwait(true);
var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, "", "").ConfigureAwait(true);

Assert.Collection(packages,
item => {
Expand Down Expand Up @@ -241,15 +241,15 @@ public async Task GetProjectDotnetDependencys_WithPackagesConfig_ReturnsMultiple
);
var mockProjectAssetsFileService = new Mock<IProjectAssetsFileService>();
mockProjectAssetsFileService
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Setup(s => s.GetDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Returns(new HashSet<DotnetDependency>());
var projectFileService = new ProjectFileService(
mockFileSystem,
mockDotnetUtilsService.Object,
mockPackageFileService.Object,
mockProjectAssetsFileService.Object);

var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, false, "", "").ConfigureAwait(true);
var packages = await projectFileService.GetProjectDotnetDependencysAsync(XFS.Path(@"c:\Project\Project.csproj"), "", false, "", "").ConfigureAwait(true);
var sortedPackages = new List<DotnetDependency>(packages);
sortedPackages.Sort();

Expand Down
2 changes: 1 addition & 1 deletion CycloneDX.Tests/ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task Validation(string fileFormat, bool disableGitHubLicenses)

var mockProjectFileService = new Mock<IProjectFileService>();
mockProjectFileService.Setup(mock =>
mock.GetProjectDotnetDependencysAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>())
mock.GetProjectDotnetDependencysAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>())
).ReturnsAsync(packages);

Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, projectFileService: mockProjectFileService.Object, solutionFileService: null, null);
Expand Down
2 changes: 1 addition & 1 deletion CycloneDX/Interfaces/IProjectAssetsFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ namespace CycloneDX.Interfaces
{
public interface IProjectAssetsFileService
{
HashSet<DotnetDependency> GetDotnetDependencys(string projectFilePath, string projectAssetsFilePath, bool IsTestProject, bool excludeDevDependencies);
HashSet<DotnetDependency> GetDotnetDependencys(string projectFilePath, string projectAssetsFilePath, bool IsTestProject);
}
}
4 changes: 2 additions & 2 deletions CycloneDX/Interfaces/IProjectFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ namespace CycloneDX.Interfaces
public interface IProjectFileService
{
bool DisablePackageRestore { get; set; }
Task<HashSet<DotnetDependency>> GetProjectDotnetDependencysAsync(string projectFilePath, string baseIntermediateOutputPath, bool excludeTestProjects, bool excludeDevDependencies, string framework, string runtime);
Task<HashSet<DotnetDependency>> GetProjectDotnetDependencysAsync(string projectFilePath, string baseIntermediateOutputPath, bool excludeTestProjects, string framework, string runtime);
Task<HashSet<string>> GetProjectReferencesAsync(string projectFilePath);
Task<HashSet<DotnetDependency>> RecursivelyGetProjectDotnetDependencysAsync(string projectFilePath, string baseIntermediateOutputPath, bool excludeTestProjects, bool excludeDevDependencies, string framework, string runtime);
Task<HashSet<DotnetDependency>> RecursivelyGetProjectDotnetDependencysAsync(string projectFilePath, string baseIntermediateOutputPath, bool excludeTestProjects, string framework, string runtime);
Task<HashSet<DotnetDependency>> RecursivelyGetProjectReferencesAsync(string projectFilePath);
Component GetComponent(DotnetDependency dotnetDependency);
bool IsTestProject(string projectFilePath);
Expand Down
2 changes: 1 addition & 1 deletion CycloneDX/Interfaces/ISolutionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ namespace CycloneDX.Interfaces
public interface ISolutionFileService
{
Task<HashSet<string>> GetSolutionProjectReferencesAsync(string solutionFilePath);
Task<HashSet<DotnetDependency>> GetSolutionDotnetDependencys(string solutionFilePath, string baseIntermediateOutputPath, bool excludeTestProjects, bool excludeDevDependencies, string framework, string runtime);
Task<HashSet<DotnetDependency>> GetSolutionDotnetDependencys(string solutionFilePath, string baseIntermediateOutputPath, bool excludeTestProjects, string framework, string runtime);
}
}
6 changes: 3 additions & 3 deletions CycloneDX/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ public async Task<int> HandleCommandAsync(RunOptions options)
{
if (SolutionOrProjectFile.ToLowerInvariant().EndsWith(".sln", StringComparison.OrdinalIgnoreCase))
{
packages = await solutionFileService.GetSolutionDotnetDependencys(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, excludeDev, framework, runtime).ConfigureAwait(false);
packages = await solutionFileService.GetSolutionDotnetDependencys(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
}
else if (Utils.IsSupportedProjectType(SolutionOrProjectFile) && scanProjectReferences)
{
packages = await projectFileService.RecursivelyGetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, excludeDev, framework, runtime).ConfigureAwait(false);
packages = await projectFileService.RecursivelyGetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
}
else if (Utils.IsSupportedProjectType(SolutionOrProjectFile))
{
packages = await projectFileService.GetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, excludeDev, framework, runtime).ConfigureAwait(false);
packages = await projectFileService.GetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
}
else if (this.fileSystem.Path.GetFileName(SolutionOrProjectFile).ToLowerInvariant().Equals("packages.config", StringComparison.OrdinalIgnoreCase))
Expand Down
39 changes: 21 additions & 18 deletions CycloneDX/Services/ProjectAssetsFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using CycloneDX.Models;
using System.Linq;
using CycloneDX.Interfaces;
using NuGet.Versioning;
using CycloneDX.Models;
using NuGet.LibraryModel;
using NuGet.ProjectModel;
using System.IO;
using NuGet.Versioning;

namespace CycloneDX.Services
{
Expand All @@ -39,7 +39,7 @@ public ProjectAssetsFileService(IFileSystem fileSystem, Func<IAssetFileReader> a
_assetFileReaderFactory = assetFileReaderFactory;
}

public HashSet<DotnetDependency> GetDotnetDependencys(string projectFilePath, string projectAssetsFilePath, bool isTestProject, bool excludeDevDependencies)
public HashSet<DotnetDependency> GetDotnetDependencys(string projectFilePath, string projectAssetsFilePath, bool isTestProject)
{
var packages = new HashSet<DotnetDependency>();

Expand Down Expand Up @@ -100,21 +100,21 @@ public HashSet<DotnetDependency> GetDotnetDependencys(string projectFilePath, st
var allPackages = runtimePackages.Select(p => p.Name);
var packagesNotInAllPackages = allDependencies.Except(allPackages);

if (excludeDevDependencies)
var realRuntimePackages = new HashSet<DotnetDependency>();
foreach (DotnetDependency dd in runtimePackages)
{
var realRuntimePackages = new HashSet<DotnetDependency>();
foreach (DotnetDependency dd in runtimePackages)
if (!dd.IsDirectReference)
{
if (!dd.IsDirectReference)
{
continue;
}

ResolvedRuntimeDependencies(dd, runtimePackages, realRuntimePackages);
continue;
}
runtimePackages = realRuntimePackages;
}


ResolvedRuntimeDependencies(dd, runtimePackages, realRuntimePackages);
}
foreach (DotnetDependency dd in runtimePackages.Except(realRuntimePackages))
{
dd.IsDevDependency = true;
}

// Check if there is an "unresolved" dependency on NetStandard
if (packagesNotInAllPackages.Any(p => p == "NETStandard.Library"))
{
Expand Down Expand Up @@ -159,8 +159,11 @@ private static void ResolvedRuntimeDependencies(DotnetDependency dotnetDependenc

foreach (KeyValuePair<string,string> child in dotnetDependency.Dependencies)
{
DotnetDependency childDependency = allpackages.First(p => string.Equals(p.Name, child.Key, StringComparison.OrdinalIgnoreCase));
ResolvedRuntimeDependencies(childDependency, allpackages, collectedDependencies);
DotnetDependency childDependency = allpackages.FirstOrDefault(p => string.Equals(p.Name, child.Key, StringComparison.OrdinalIgnoreCase));
if (childDependency != null)
{
ResolvedRuntimeDependencies(childDependency, allpackages, collectedDependencies);
}
}
}

Expand Down
Loading

0 comments on commit d4a9cc1

Please sign in to comment.