2015-11-16 11:21:57 -08:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2015-11-27 16:19:54 -08:00
|
|
|
using Microsoft.DotNet.ProjectModel.Utilities;
|
2015-10-13 14:31:29 -07:00
|
|
|
using NuGet.Versioning;
|
|
|
|
|
2015-11-27 16:19:54 -08:00
|
|
|
namespace Microsoft.DotNet.ProjectModel.Graph
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
public class LockFile
|
|
|
|
{
|
|
|
|
public static readonly int CurrentVersion = 2;
|
|
|
|
public static readonly string FileName = "project.lock.json";
|
|
|
|
|
2016-01-14 11:52:03 -08:00
|
|
|
public string LockFilePath { get; }
|
|
|
|
|
2015-10-13 14:31:29 -07:00
|
|
|
public int Version { get; set; }
|
|
|
|
public IList<ProjectFileDependencyGroup> ProjectFileDependencyGroups { get; set; } = new List<ProjectFileDependencyGroup>();
|
|
|
|
public IList<LockFilePackageLibrary> PackageLibraries { get; set; } = new List<LockFilePackageLibrary>();
|
|
|
|
public IList<LockFileProjectLibrary> ProjectLibraries { get; set; } = new List<LockFileProjectLibrary>();
|
|
|
|
public IList<LockFileTarget> Targets { get; set; } = new List<LockFileTarget>();
|
|
|
|
|
2016-01-14 11:52:03 -08:00
|
|
|
public LockFile(string lockFilePath)
|
|
|
|
{
|
|
|
|
LockFilePath = lockFilePath;
|
|
|
|
}
|
|
|
|
|
2015-10-13 14:31:29 -07:00
|
|
|
public bool IsValidForProject(Project project)
|
|
|
|
{
|
|
|
|
string message;
|
|
|
|
return IsValidForProject(project, out message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsValidForProject(Project project, out string message)
|
|
|
|
{
|
|
|
|
if (Version != CurrentVersion)
|
|
|
|
{
|
|
|
|
message = $"The expected lock file version does not match the actual version";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = $"Dependencies in {Project.FileName} were modified";
|
|
|
|
|
|
|
|
var actualTargetFrameworks = project.GetTargetFrameworks();
|
|
|
|
|
|
|
|
// The lock file should contain dependencies for each framework plus dependencies shared by all frameworks
|
|
|
|
if (ProjectFileDependencyGroups.Count != actualTargetFrameworks.Count() + 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var group in ProjectFileDependencyGroups)
|
|
|
|
{
|
|
|
|
IOrderedEnumerable<string> actualDependencies;
|
|
|
|
var expectedDependencies = group.Dependencies.OrderBy(x => x);
|
|
|
|
|
|
|
|
// If the framework name is empty, the associated dependencies are shared by all frameworks
|
|
|
|
if (group.FrameworkName == null)
|
|
|
|
{
|
2016-01-22 10:22:53 -08:00
|
|
|
actualDependencies = project.Dependencies
|
|
|
|
.Select(RenderDependency)
|
|
|
|
.OrderBy(x => x, StringComparer.OrdinalIgnoreCase);
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var framework = actualTargetFrameworks
|
|
|
|
.FirstOrDefault(f => Equals(f.FrameworkName, group.FrameworkName));
|
|
|
|
if (framework == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:22:53 -08:00
|
|
|
actualDependencies = framework.Dependencies
|
|
|
|
.Select(RenderDependency)
|
|
|
|
.OrderBy(x => x, StringComparer.OrdinalIgnoreCase);
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!actualDependencies.SequenceEqual(expectedDependencies))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
message = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:22:53 -08:00
|
|
|
private string RenderDependency(LibraryRange arg) => $"{arg.Name} {VersionUtility.RenderVersion(arg.VersionRange)}";
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
}
|