Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace Microsoft.ComponentDetection.Detectors.Uv;

using System.Runtime.Serialization;

[DataContract]
internal class UvDependency
{
public required string Name { get; init; }
[DataMember(Name = "name")]
public string Name { get; set; } = string.Empty;

[DataMember(Name = "specifier")]
public string? Specifier { get; set; }
Comment thread
ryanbrandenburg marked this conversation as resolved.
}
137 changes: 16 additions & 121 deletions src/Microsoft.ComponentDetection.Detectors/uv/UvLock.cs
Original file line number Diff line number Diff line change
@@ -1,146 +1,41 @@
namespace Microsoft.ComponentDetection.Detectors.Uv;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using Tomlyn;
using Tomlyn.Model;

[DataContract]
internal class UvLock
{
// a list of packages with their dependencies
// A list of packages with their dependencies.
[DataMember(Name = "package")]
public List<UvPackage> Packages { get; set; } = [];
Comment thread
ryanbrandenburg marked this conversation as resolved.

// static method to parse the TOML stream into a UvLock model
public static UvLock Parse(Stream tomlStream)
{
using var reader = new StreamReader(tomlStream);
var tomlContent = reader.ReadToEnd();
var model = Toml.ToModel(tomlContent);
return new UvLock
var options = new TomlModelOptions
{
Packages = ParsePackagesFromModel(model),
IgnoreMissingProperties = true,
};
}

internal static List<UvPackage> ParsePackagesFromModel(object? model)
{
if (model is not TomlTable table)
{
throw new InvalidOperationException("TOML root is not a table");
}

if (!table.TryGetValue("package", out var packagesObj) || packagesObj is not TomlTableArray packages)
{
return [];
}

var result = new List<UvPackage>();
foreach (var pkg in packages)
{
var parsed = ParsePackage(pkg);
if (parsed is not null)
{
result.Add(parsed);
}
}

return result;
}

internal static UvPackage? ParsePackage(object? pkg)
{
if (pkg is not TomlTable pkgTable)
{
return null;
}

if (pkgTable.TryGetValue("name", out var nameObj) && nameObj is string name &&
pkgTable.TryGetValue("version", out var versionObj) && versionObj is string version)
{
var uvPackage = new UvPackage
{
Name = name,
Version = version,
Dependencies = [],
MetadataRequiresDist = [],
MetadataRequiresDev = [],
};

if (pkgTable.TryGetValue("dependencies", out var depsObj) && depsObj is TomlArray depsArray)
{
uvPackage.Dependencies = ParseDependenciesArray(depsArray);
}

if (pkgTable.TryGetValue("metadata", out var metadataObj) && metadataObj is TomlTable metadataTable)
{
ParseMetadata(metadataTable, uvPackage);
}

// Parse source
if (pkgTable.TryGetValue("source", out var sourceObj) && sourceObj is TomlTable sourceTable)
{
var source = new UvSource
{
Registry = sourceTable.TryGetValue("registry", out var regObj) && regObj is string reg ? reg : null,
Virtual = sourceTable.TryGetValue("virtual", out var virtObj) && virtObj is string virt ? virt : null,
Git = sourceTable.TryGetValue("git", out var gitObj) && gitObj is string git ? git : null,
};
uvPackage.Source = source;
}

return uvPackage;
}

return null;
var parsed = Toml.ToModel<UvLock>(tomlContent, options: options);
parsed.Normalize();
return parsed;
}

internal static List<UvDependency> ParseDependenciesArray(TomlArray? depsArray)
private void Normalize()
{
var deps = new List<UvDependency>();
if (depsArray is null)
{
return deps;
}

foreach (var dep in depsArray)
{
if (dep is TomlTable depTable &&
depTable.TryGetValue("name", out var depNameObj) && depNameObj is string depName)
{
var depSpec = depTable.TryGetValue("specifier", out var specObj) && specObj is string s ? s : null;
deps.Add(new UvDependency
{
Name = depName,
Specifier = depSpec,
});
}
}

return deps;
}

internal static void ParseMetadata(TomlTable? metadataTable, UvPackage uvPackage)
{
if (metadataTable is null)
{
return;
}

if (metadataTable.TryGetValue("requires-dist", out var requiresDistObj) && requiresDistObj is TomlArray requiresDistArr)
{
uvPackage.MetadataRequiresDist = ParseDependenciesArray(requiresDistArr);
}
this.Packages = this.Packages
.Where(p => !string.IsNullOrWhiteSpace(p.Name) && !string.IsNullOrWhiteSpace(p.Version))
.ToList();

if (metadataTable.TryGetValue("requires-dev", out var requiresDevObj) && requiresDevObj is TomlTable requiresDevTable)
foreach (var package in this.Packages)
{
foreach (var kvp in requiresDevTable)
{
if (kvp.Value is TomlArray groupArr)
{
uvPackage.MetadataRequiresDev.AddRange(ParseDependenciesArray(groupArr));
}
}
package.Normalize();
}
}
}
32 changes: 32 additions & 0 deletions src/Microsoft.ComponentDetection.Detectors/uv/UvMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Microsoft.ComponentDetection.Detectors.Uv;

using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

[DataContract]
internal class UvMetadata
{
[DataMember(Name = "requires-dist")]
public List<UvDependency> RequiresDist { get; set; } = [];

[DataMember(Name = "requires-dev")]
public Dictionary<string, List<UvDependency>> RequiresDev { get; set; } = [];
Comment thread
ryanbrandenburg marked this conversation as resolved.

public void Normalize()
{
this.RequiresDist = this.RequiresDist
.Where(d => !string.IsNullOrWhiteSpace(d.Name))
.ToList();

var normalizedDev = new Dictionary<string, List<UvDependency>>(this.RequiresDev.Count, this.RequiresDev.Comparer);
foreach (var (group, dependencies) in this.RequiresDev)
{
normalizedDev[group] = dependencies
.Where(d => !string.IsNullOrWhiteSpace(d.Name))
.ToList();
}

this.RequiresDev = normalizedDev;
}
}
37 changes: 29 additions & 8 deletions src/Microsoft.ComponentDetection.Detectors/uv/UvPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@ namespace Microsoft.ComponentDetection.Detectors.Uv;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Microsoft.ComponentDetection.Contracts.TypedComponent;

[DataContract]
internal class UvPackage
{
public required string Name { get; init; }
[DataMember(Name = "name")]
public string Name { get; set; } = string.Empty;

public required string Version { get; init; }
[DataMember(Name = "version")]
public string Version { get; set; } = string.Empty;
Comment thread
ryanbrandenburg marked this conversation as resolved.

[DataMember(Name = "dependencies")]
public List<UvDependency> Dependencies { get; set; } = [];

// Metadata dependencies (requires-dist)
public List<UvDependency> MetadataRequiresDist { get; set; } = [];
[DataMember(Name = "metadata")]
Comment thread
ryanbrandenburg marked this conversation as resolved.
Outdated
public UvMetadata? Metadata { get; set; }

// Metadata dev dependencies (requires-dev)
public List<UvDependency> MetadataRequiresDev { get; set; } = [];

// Source property for uv.lock
[DataMember(Name = "source")]
public UvSource? Source { get; set; }

[IgnoreDataMember]
public List<UvDependency> MetadataRequiresDist => this.Metadata?.RequiresDist ?? [];

[IgnoreDataMember]
public List<UvDependency> MetadataRequiresDev => this.Metadata?.RequiresDev?.Values
.Where(group => group != null)
.SelectMany(group => group!)
.ToList() ?? [];

public TypedComponent ToTypedComponent()
{
if (this.Source?.Git != null)
Expand All @@ -32,6 +44,15 @@ public TypedComponent ToTypedComponent()
return new PipComponent(this.Name, this.Version);
}

public void Normalize()
{
this.Dependencies = this.Dependencies
.Where(d => !string.IsNullOrWhiteSpace(d.Name))
.ToList();

this.Metadata?.Normalize();
}

private static (Uri RepositoryUrl, string CommitHash) ParseGitUrl(string gitUrl)
{
var uri = new Uri(gitUrl);
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.ComponentDetection.Detectors/uv/UvSource.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
namespace Microsoft.ComponentDetection.Detectors.Uv;

using System.Runtime.Serialization;

[DataContract]
internal class UvSource
{
[DataMember(Name = "registry")]
public string? Registry { get; set; }

[DataMember(Name = "virtual")]
public string? Virtual { get; set; }

[DataMember(Name = "git")]
public string? Git { get; set; }
Comment thread
ryanbrandenburg marked this conversation as resolved.
}
Loading
Loading