Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle package reference version ranges in .csproj files #814

Merged
merged 2 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 37 additions & 23 deletions src/OmniSharp.MSBuild/MSBuildProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,24 +495,6 @@ private void UpdateReferences(Project project, IList<string> references)
}
}

private List<PackageDependency> CreatePackageDependencies(IEnumerable<PackageReference> packageReferences)
{
var list = new List<PackageDependency>();

foreach (var packageReference in packageReferences)
{
var dependency = new PackageDependency
{
Name = packageReference.Identity.Id,
Version = packageReference.Identity.Version?.ToNormalizedString()
};

list.Add(dependency);
}

return list;
}

private void CheckForUnresolvedDependences(ProjectFileInfo projectFileInfo, ProjectFileInfo previousProjectFileInfo = null, bool allowAutoRestore = false)
{
List<PackageDependency> unresolvedDependencies;
Expand All @@ -534,22 +516,22 @@ private void CheckForUnresolvedDependences(ProjectFileInfo projectFileInfo, Proj
// Did the project file change? Diff the package references and see if there are unresolved dependencies.
if (previousProjectFileInfo != null)
{
var packageReferencesToRemove = new HashSet<PackageReference>(previousProjectFileInfo.PackageReferences);
var remainingPackageReferences = new HashSet<PackageReference>(previousProjectFileInfo.PackageReferences);
var packageReferencesToAdd = new HashSet<PackageReference>();

foreach (var packageReference in projectFileInfo.PackageReferences)
{
if (packageReferencesToRemove.Contains(packageReference))
if (remainingPackageReferences.Contains(packageReference))
{
packageReferencesToRemove.Remove(packageReference);
remainingPackageReferences.Remove(packageReference);
}
else
{
packageReferencesToAdd.Add(packageReference);
}
}

unresolvedPackageReferences = packageReferencesToAdd.Concat(packageReferencesToRemove);
unresolvedPackageReferences = packageReferencesToAdd.Concat(remainingPackageReferences);
}
else
{
Expand All @@ -561,7 +543,7 @@ private void CheckForUnresolvedDependences(ProjectFileInfo projectFileInfo, Proj
var lockFile = lockFileFormat.Read(projectFileInfo.ProjectAssetsFile);

unresolvedPackageReferences = projectFileInfo.PackageReferences
.Where(pr => lockFile.GetLibrary(pr.Identity.Id, pr.Identity.Version) == null);
.Where(pr => !ContainsPackageReference(lockFile, pr));
}

unresolvedDependencies = CreatePackageDependencies(unresolvedPackageReferences);
Expand All @@ -583,6 +565,38 @@ private void CheckForUnresolvedDependences(ProjectFileInfo projectFileInfo, Proj
}
}

private List<PackageDependency> CreatePackageDependencies(IEnumerable<PackageReference> packageReferences)
{
var list = new List<PackageDependency>();

foreach (var packageReference in packageReferences)
{
var dependency = new PackageDependency
{
Name = packageReference.Dependency.Id,
Version = packageReference.Dependency.VersionRange.ToNormalizedString()
};

list.Add(dependency);
}

return list;
}

private static bool ContainsPackageReference(LockFile lockFile, PackageReference reference)
{
foreach (var library in lockFile.Libraries)
{
if (string.Equals(library.Name, reference.Dependency.Id) &&
reference.Dependency.VersionRange.Satisfies(library.Version))
{
return true;
}
}

return false;
}

private void FireUnresolvedDependenciesEvent(ProjectFileInfo projectFileInfo, List<PackageDependency> unresolvedDependencies)
{
_eventEmitter.Emit(EventTypes.UnresolvedDependencies,
Expand Down
13 changes: 6 additions & 7 deletions src/OmniSharp.MSBuild/ProjectFile/PackageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ namespace OmniSharp.MSBuild.ProjectFile
{
public class PackageReference : IEquatable<PackageReference>
{
public PackageIdentity Identity { get; }
public PackageDependency Dependency { get; }
public bool IsImplicitlyDefined { get; }

public PackageReference(PackageIdentity identity, bool isImplicitlyDefined)
public PackageReference(PackageDependency dependency, bool isImplicitlyDefined)
{
this.Identity = identity;
this.Dependency = dependency;
this.IsImplicitlyDefined = isImplicitlyDefined;
}

public override string ToString()
{
var version = Identity.HasVersion ? ", " + Identity.Version.ToNormalizedString() : string.Empty;
var implicitSuffix = IsImplicitlyDefined ? " (implicit)" : string.Empty;

return Identity.Id + version + implicitSuffix;
return Dependency.Id + ", " + Dependency.VersionRange + implicitSuffix;
}

public bool Equals(PackageReference other)
{
if (!Identity.Equals(other.Identity))
if (!Dependency.Equals(other.Dependency))
{
return false;
}
Expand All @@ -34,7 +33,7 @@ public bool Equals(PackageReference other)

public override int GetHashCode()
{
return this.Identity.GetHashCode() + (IsImplicitlyDefined ? 1 : 0);
return this.Dependency.GetHashCode() + (IsImplicitlyDefined ? 1 : 0);
}
}
}
9 changes: 4 additions & 5 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Logging;
using NuGet.Packaging.Core;
using OmniSharp.Models;
using OmniSharp.Options;
using OmniSharp.Utilities;

Expand Down Expand Up @@ -114,7 +113,7 @@ public static ProjectFileInfo Create(
string solutionDirectory,
ILogger logger,
MSBuildOptions options = null,
ICollection<MSBuildDiagnosticsMessage> diagnostics = null,
ICollection<Models.MSBuildDiagnosticsMessage> diagnostics = null,
bool isUnityProject = false)
{
if (!File.Exists(projectFilePath))
Expand Down Expand Up @@ -250,11 +249,11 @@ private static IList<PackageReference> GetPackageReferences(ICollection<ProjectI
foreach (var item in items)
{
var name = item.EvaluatedInclude;
var version = PropertyConverter.ToNuGetVersion(item.GetMetadataValue(MetadataNames.Version));
var identity = new PackageIdentity(name, version);
var versionRange = PropertyConverter.ToVersionRange(item.GetMetadataValue(MetadataNames.Version));
var dependency = new PackageDependency(name, versionRange);
var isImplicitlyDefined = PropertyConverter.ToBoolean(item.GetMetadataValue(MetadataNames.IsImplicitlyDefined), false);

list.Add(new PackageReference(identity, isImplicitlyDefined));
list.Add(new PackageReference(dependency, isImplicitlyDefined));
}

return list;
Expand Down
5 changes: 2 additions & 3 deletions src/OmniSharp.MSBuild/ProjectFile/PropertyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ public static OutputKind ToOutputKind(string propertyValue)
}
}

public static NuGetVersion ToNuGetVersion(string propertyValue)
public static VersionRange ToVersionRange(string propertyValue)
{
NuGetVersion version;
if (NuGetVersion.TryParse(propertyValue.Trim(), out version))
if (VersionRange.TryParse(propertyValue.Trim(), out var version))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I'm weird but doesn't it strike you as a bit inconsistent that in the same namespace they'd have two different naming styles - NuGetVersion and VersionRange (used for, well, NuGetVersions)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bunch of them: NuGetVersion, SemanticVersion, VersionRange, FloatRange. They're all in NuGet.Versioning. NuGetVersion is a specific version, used for package identity. A range wouldn't be suitable for that.

{
return version;
}
Expand Down