Skip to content

Commit

Permalink
fix aboutcode-org#54 by setting the type property of dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Eckert <[email protected]>
  • Loading branch information
georg-eckert-zeiss committed Sep 3, 2024
1 parent 9cb431e commit 581f73d
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 24 deletions.
32 changes: 26 additions & 6 deletions src/nuget-inspector/LockFileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,21 @@ public DependencyResolution Process()
{
var tree_builder = new PackageTree();
var resolution = new DependencyResolution();
var project_references = ProjectLockFile.Libraries
.Where( l => l.Type.Equals(ComponentType.Project) )
.Select( l => l.Name)
.ToList();

foreach (var target in ProjectLockFile.Targets)
{
foreach (var library in target.Libraries)
{
var type = library.Type;
var name = library.Name;
var version = library.Version.ToNormalizedString();
var package = new BasePackage(name: name, version: version);
var package = new BasePackage(name: name, type: type, version: version);
var dependencies = new List<BasePackage>();

foreach (var dependency in library.Dependencies)
{
var dep_name = dependency.Id;
Expand All @@ -90,7 +96,10 @@ public DependencyResolution Process()
}
else
{
var depId = new BasePackage(name: dep_name, version: best_version.ToNormalizedString());
var dep_type = project_references.Contains( dep_name)
? ComponentType.Project
: ComponentType.NuGet;
var depId = new BasePackage(name: dep_name, dep_type, version: best_version.ToNormalizedString());
dependencies.Add(item: depId);
}
}
Expand All @@ -111,7 +120,10 @@ public DependencyResolution Process()
foreach (var dep in ProjectLockFile.PackageSpec.Dependencies)
{
var version = tree_builder.GetResolvedVersion(name: dep.Name, range: dep.LibraryRange.VersionRange);
resolution.Dependencies.Add(item: new BasePackage(name: dep.Name, version: version));
var dep_type1 = project_references.Contains( dep.Name )
? ComponentType.Project
: ComponentType.NuGet;
resolution.Dependencies.Add(item: new BasePackage(name: dep.Name, dep_type1, version: version));
}
}
else
Expand All @@ -128,8 +140,11 @@ public DependencyResolution Process()
{
foreach (var dep in framework.Dependencies)
{
var dep_type1 = project_references.Contains( dep.Name )
? ComponentType.Project
: ComponentType.NuGet;
var version = tree_builder.GetResolvedVersion(name: dep.Name, range: dep.LibraryRange.VersionRange);
resolution.Dependencies.Add(item: new BasePackage(name: dep.Name, version: version));
resolution.Dependencies.Add(item: new BasePackage(name: dep.Name, dep_type1, version: version));
}
}
}
Expand All @@ -152,8 +167,13 @@ public DependencyResolution Process()
version = library_version.ToNormalizedString();
}

resolution.Dependencies.Add(
item: new BasePackage(name: project_dependency.GetName()!, version: version));
var name = project_dependency.GetName()!;


var dep_type1 = project_references.Contains( name )
? ComponentType.Project
: ComponentType.NuGet;
resolution.Dependencies.Add(item: new BasePackage(name: name, dep_type1, version: version));
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/nuget-inspector/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ public class Dependency
public string? name;
public NuGetFramework? framework;
public VersionRange? version_range;
public string type;
public bool is_direct;

//True only for legacy packages.config-based projects only when set there
public bool is_development_dependency = false;

public Dependency(
string? name,
string type,
VersionRange? version_range,
NuGetFramework? framework = null,
bool is_direct = false,
bool is_development_dependency = false)
{
this.framework = framework;
this.name = name;
this.type = type;
this.version_range = version_range;
this.is_direct = is_direct;
this.is_development_dependency = is_development_dependency;
Expand All @@ -40,6 +43,7 @@ public BasePackage CreateEmptyBasePackage()
{
return new BasePackage(
name: name!,
type: type,
version: version_range?.MinVersion.ToNormalizedString(),
framework: framework?.ToString()
);
Expand Down Expand Up @@ -144,6 +148,12 @@ public VersionPair(string rawVersion, NuGetVersion version)
}
}

public static class ComponentType
{
public const string NuGet = "nuget";
public const string Project = "project";
}

/// <summary>
/// Package data object using purl as identifying attributes as
/// specified here https://github.com/package-url/purl-spec
Expand Down Expand Up @@ -196,9 +206,10 @@ public class BasePackage : IEquatable<BasePackage>, IComparable<BasePackage>

public BasePackage(){}

public BasePackage(string name, string? version, string? framework = "", string? datafile_path = "")
public BasePackage(string name, string type, string? version, string? framework = "", string? datafile_path = "")
{
this.name = name;
this.type = type;
this.version = version;
if (!string.IsNullOrWhiteSpace(framework))
this.version = version;
Expand All @@ -210,7 +221,7 @@ public BasePackage(string name, string? version, string? framework = "", string?

public static BasePackage FromPackage(BasePackage package, List<BasePackage> dependencies)
{
return new(name: package.name, version: package.version)
return new(name: package.name, type: package.type, version: package.version)
{
extra_data = package.extra_data,
dependencies = dependencies
Expand All @@ -226,6 +237,7 @@ public BasePackage Clone(bool with_deps=false)

return new BasePackage(
name: name,
type: type,
version:version,
datafile_path: datafile_path
)
Expand Down Expand Up @@ -318,7 +330,8 @@ public void Update(NugetApi nugetApi, bool with_details = false)

try
{
UpdateWithRemoteMetadata(nugetApi, with_details: with_details);
if( !type.Equals( ComponentType.Project ) )
UpdateWithRemoteMetadata(nugetApi, with_details: with_details);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -638,7 +651,7 @@ public bool IsEnhanced(){
public static PackageDownload FromSpdi(SourcePackageDependencyInfo spdi)
{
PackageDownload download = new(){ download_url = spdi.DownloadUri.ToString() };
/// Note that this hash is unlikely there per https://github.com/NuGet/NuGetGallery/issues/9433
// Note that this hash is unlikely there per https://github.com/NuGet/NuGetGallery/issues/9433
if (!string.IsNullOrEmpty(spdi.PackageHash))
{
download.hash = spdi.PackageHash;
Expand Down
7 changes: 5 additions & 2 deletions src/nuget-inspector/NugetResolverHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public void ResolveOne(Dependency dependency)
}

if (dependency.name != null)
package_tree.AddOrUpdatePackage(id: new BasePackage(name: dependency.name, version: version));
package_tree.AddOrUpdatePackage(id: new BasePackage(name: dependency.name, type: dependency.type, version: version));
return;
}

var base_package = new BasePackage(
name: dependency.name!,
type: dependency.type,
version: psmr.Identity.Version.ToNormalizedString());

IEnumerable<NuGet.Packaging.Core.PackageDependency> packages = nugetApi.GetPackageDependenciesForPackage(
Expand All @@ -74,7 +75,7 @@ public void ResolveOne(Dependency dependency)
var resolved_version = package_tree.GetResolvedVersion(name: pkg.Id, range: pkg.VersionRange);
if (resolved_version != null)
{
var base_pkg = new BasePackage(name: pkg.Id, version: resolved_version);
var base_pkg = new BasePackage(name: pkg.Id, type: ComponentType.NuGet, version: resolved_version);
dependencies.Add(item: base_pkg);
if (Config.TRACE)
Console.WriteLine($" dependencies.Add name: {pkg.Id}, version: {resolved_version}");
Expand All @@ -93,6 +94,7 @@ public void ResolveOne(Dependency dependency)

var dependent_package = new BasePackage(
name: psrm.Identity.Id,
type: ComponentType.NuGet,
version: psrm.Identity.Version.ToNormalizedString());

dependencies.Add(item: dependent_package);
Expand All @@ -101,6 +103,7 @@ public void ResolveOne(Dependency dependency)
{
Dependency pd = new(
name: pkg.Id,
type: ComponentType.NuGet,
version_range: pkg.VersionRange,
framework: dependency.framework);

Expand Down
16 changes: 13 additions & 3 deletions src/nuget-inspector/PackagesConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public PackagesConfigHelper(NugetApi nugetApi)
foreach (var depPair in pkg.Dependencies)
{
if (depPair.Key == id)
result.Add(item: depPair.Value);
result.Add(item: depPair.Value);
}
}

Expand All @@ -39,8 +39,10 @@ public List<BasePackage> ProcessAll(List<Dependency> dependencies)
{
foreach (var dependency in dependencies)
{
Console.WriteLine( $"ProcessAll() Adding {dependency.type} {dependency.name} to builder" );
Add(
id: dependency.name!,
type: dependency.type,
name: dependency.name,
range: dependency.version_range,
framework: dependency.framework);
Expand All @@ -60,37 +62,43 @@ public List<BasePackage> ProcessAll(List<Dependency> dependencies)
{
deps.Add(item: new BasePackage(
name: ResolutionDatas[key: dep].Name!,
type: ResolutionDatas[key: dep].Type!,
version: ResolutionDatas[key: dep].CurrentVersion?.ToNormalizedString()));
}
}

builder.AddOrUpdatePackage(
base_package: new BasePackage(name: data.Name!,
base_package: new BasePackage(
name: data.Name!,
type: data.Type!,
version: data.CurrentVersion?.ToNormalizedString()),
dependencies: deps!);
}

return builder.GetPackageList();
}

public void Add(string id, string? name, VersionRange? range, NuGetFramework? framework)
public void Add(string id, string type, string? name, VersionRange? range, NuGetFramework? framework)
{
id = id.ToLower();
Resolve(
id: id,
type: type,
name: name,
project_target_framework: framework,
overrideRange: range);
}

private void Resolve(
string id,
string type,
string? name,
NuGetFramework? project_target_framework = null,
VersionRange? overrideRange = null)
{
id = id.ToLower();
ResolutionData data = new();
data.Type = type;
if (ResolutionDatas.ContainsKey(key: id))
{
data = ResolutionDatas[key: id];
Expand Down Expand Up @@ -138,6 +146,7 @@ private void Resolve(
data.Dependencies.Add(key: dependency.Id.ToLower(), value: dependency.VersionRange);
Resolve(
id: dependency.Id.ToLower(),
type: ComponentType.NuGet,
name: dependency.Id,
project_target_framework: project_target_framework);
}
Expand All @@ -150,5 +159,6 @@ private class ResolutionData
public readonly Dictionary<string, VersionRange?> Dependencies = new();
public VersionRange? ExternalVersionRange;
public string? Name;
public string? Type;
}
}
1 change: 1 addition & 0 deletions src/nuget-inspector/PackagesConfigProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private List<Dependency> GetDependencies()

Dependency dep = new(
name: name,
type: ComponentType.NuGet,
version_range: range,
framework: package_framework,
is_direct: true,
Expand Down
9 changes: 5 additions & 4 deletions src/nuget-inspector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void Main(string[] args)
}

/// <summary>
/// Return True if there is an warning in the results.
/// Return True if there is a warning in the results.
/// </summary>
public static bool Has_warnings(OutputFormatJson output)
{
Expand All @@ -52,7 +52,7 @@ public static bool Has_warnings(OutputFormatJson output)
{
if (dep.warnings.Any())
has_dep_level = true;
break;
break;
}
return has_dep_level;
}
Expand All @@ -73,7 +73,7 @@ public static bool Has_errors(OutputFormatJson output)
{
if (dep.errors.Any())
has_dep_level = true;
break;
break;
}
return has_dep_level;
}
Expand Down Expand Up @@ -111,6 +111,7 @@ private static ExecutionResult ExecuteInspector(Options options)

Stopwatch deps_timer = Stopwatch.StartNew();
ScanResult scan_result = scanner.RunScan();

deps_timer.Stop();

Stopwatch meta_timer = Stopwatch.StartNew();
Expand Down Expand Up @@ -163,7 +164,7 @@ private static ExecutionResult ExecuteInspector(Options options)
if (with_warnings)
PrintWarnings(scan_result, project_package);

return ExecutionResult.Succeeded();
return ExecutionResult.Succeeded();
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions src/nuget-inspector/ProjectFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public List<Dependency> GetDependenciesFromReferences(List<PackageReference> ref
var rpid = reference.PackageIdentity;
var dep = new Dependency(
name: rpid.Id,
type: ComponentType.NuGet,
version_range: reference.AllowedVersions ?? new VersionRange(rpid.Version),
framework: ProjectFramework,
is_direct: true);
Expand Down Expand Up @@ -408,6 +409,7 @@ public DependencyResolution ResolveUseGather()
}
BasePackage dep = new(
name: resolved_dep.Id,
type: ComponentType.NuGet,
version: resolved_dep.Version.ToString(),
framework: ProjectFramework!.GetShortFolderName());

Expand Down Expand Up @@ -456,6 +458,7 @@ public DependencyResolution ResolveUsingLib()
}
BasePackage dep = new(
name: resolved_dep.Id,
type: ComponentType.NuGet,
version: resolved_dep.Version.ToString(),
framework: ProjectFramework!.GetShortFolderName());

Expand Down
1 change: 1 addition & 0 deletions src/nuget-inspector/ProjectJsonProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public DependencyResolution Resolve()
{
var bpwd = new BasePackage(
name: package.Name,
type: ComponentType.NuGet,
version: package.LibraryRange.VersionRange.OriginalString
);
resolution.Dependencies.Add(item: bpwd);
Expand Down
Loading

0 comments on commit 581f73d

Please sign in to comment.