Skip to content

Commit

Permalink
feat: Added support for project file dependency group when parsing a …
Browse files Browse the repository at this point in the history
…lock file.
  • Loading branch information
taikuukaits committed Aug 25, 2021
1 parent 4cb2679 commit 99e0034
Showing 1 changed file with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ private NuGet.Versioning.NuGetVersion BestVersion(string name, NuGet.Versioning.
}
}

private NuGet.Versioning.NuGetVersion BestLibraryVersion(string name, NuGet.Versioning.VersionRange range, IList<NuGet.ProjectModel.LockFileLibrary> libraries)
{
var versions = libraries.Where(lib => lib.Name == name).Select(lib => lib.Version);
var bestMatch = range.FindBestMatch(versions);
if (bestMatch == null)
{
if (versions.Count() == 1)
{
return versions.First();
}
else
{
Console.WriteLine($"WARNING: Unable to find a version to satisfy range {range.PrettyPrint()} for the dependency " + name);
if (range.HasUpperBound && !range.HasLowerBound)
{
Console.WriteLine($"Instead will return the maximum range demanded: " + range.MaxVersion.ToFullString());
return range.MaxVersion;
}
else
{
Console.WriteLine($"Instead will return the minimum range demanded: " + range.MinVersion.ToFullString());
return range.MinVersion;
}

}
}
else
{
return bestMatch;
}
}

public DependencyResult Process()
{
Expand Down Expand Up @@ -99,6 +130,22 @@ public DependencyResult Process()
}
}

foreach (var projectFileDependencyGroup in LockFile.ProjectFileDependencyGroups)
{
foreach (var projectFileDependency in projectFileDependencyGroup.Dependencies)
{
var projectDependencyParsed = ParseProjectFileDependencyGroup(projectFileDependency);
var libraryVersion = BestLibraryVersion(projectDependencyParsed.GetName(), projectDependencyParsed.GetVersionRange(), LockFile.Libraries);
String version = null;
if (libraryVersion != null)
{
version = libraryVersion.ToNormalizedString();
}
result.Dependencies.Add(new Model.PackageId(projectDependencyParsed.GetName(), version));
}
}


if (result.Dependencies.Count == 0)
{
Console.WriteLine("Found no dependencies for lock file: " + LockFile.Path);
Expand All @@ -108,5 +155,83 @@ public DependencyResult Process()
return result;
}



public ProjectFileDependency ParseProjectFileDependencyGroup(String projectFileDependency)
{
//Reverse engineered from: https://github.com/NuGet/NuGet.Client/blob/538727480d93b7d8474329f90ccb9ff3b3543714/src/NuGet.Core/NuGet.LibraryModel/LibraryRange.cs#L68
//With some hints from https://github.com/dotnet/NuGet.BuildTasks/pull/23/files
int firstSpace = projectFileDependency.IndexOf(' ');
if (firstSpace <= -1)
{
return new ProjectFileDependency(projectFileDependency, NuGet.Versioning.VersionRange.All);
}
else
{
var name = projectFileDependency.Substring(0, firstSpace);
var versionRaw = projectFileDependency.Substring(firstSpace).Trim();
NuGet.Versioning.VersionRange nugetVersion;
if (versionRaw.StartsWith(">= "))
{
nugetVersion = MinVersionOrFloat(versionRaw.Substring(3).Trim(), true /* Include min version. */);
}
else if (versionRaw.StartsWith("> "))
{
nugetVersion = MinVersionOrFloat(versionRaw.Substring(2).Trim(), false /* Do not include min version. */);
}
else if (versionRaw.StartsWith("<= "))
{
var maxVersion = NuGet.Versioning.NuGetVersion.Parse(versionRaw.Substring(3).Trim());
nugetVersion = new NuGet.Versioning.VersionRange(null, false, maxVersion, true /* Include Max */);
}
else if (versionRaw.StartsWith("< "))
{
var maxVersion = NuGet.Versioning.NuGetVersion.Parse(versionRaw.Substring(2).Trim());
nugetVersion = new NuGet.Versioning.VersionRange(null, false, maxVersion, false /* Do NOT Include Max */);
}
else
{
nugetVersion = NuGet.Versioning.VersionRange.All;
}
return new ProjectFileDependency(name, nugetVersion);
}
}

private NuGet.Versioning.VersionRange MinVersionOrFloat(String versionValueRaw, bool includeMin)
{
//could be Floating or MinVersion
if (NuGet.Versioning.NuGetVersion.TryParse(versionValueRaw, out NuGet.Versioning.NuGetVersion minVersion))
{
return new NuGet.Versioning.VersionRange(minVersion, includeMin);
}
else
{
return NuGet.Versioning.VersionRange.Parse(versionValueRaw, true);
}
}

public class ProjectFileDependency
{
private readonly String name;
private readonly NuGet.Versioning.VersionRange versionRange;

public ProjectFileDependency(string name, NuGet.Versioning.VersionRange versionRange)
{
this.name = name;
this.versionRange = versionRange;
}

public String GetName()
{
return name;
}

public NuGet.Versioning.VersionRange GetVersionRange()
{
return versionRange;
}
}

}
}

}

0 comments on commit 99e0034

Please sign in to comment.