Remove fragment file's involvement in design time scenarios

This commit is contained in:
Troy Dai 2016-03-31 11:15:50 -07:00
parent 429eb93cd9
commit e176c1d3b9
7 changed files with 44 additions and 30 deletions

View file

@ -141,7 +141,7 @@ namespace Microsoft.DotNet.Cli.Utils
try try
{ {
lockFile = LockFileReader.Read(lockFilePath); lockFile = LockFileReader.Read(lockFilePath, designTime: false);
} }
catch (FileFormatException ex) catch (FileFormatException ex)
{ {

View file

@ -22,14 +22,13 @@ namespace Microsoft.DotNet.ProjectModel.Graph
_msbuildTargetLibraries = msbuildProjectLibraries.ToDictionary(GetProjectLibraryKey, l => GetTargetsForLibrary(_lockFile, l)); _msbuildTargetLibraries = msbuildProjectLibraries.ToDictionary(GetProjectLibraryKey, l => GetTargetsForLibrary(_lockFile, l));
} }
public void PatchIfNecessary() public void Patch()
{ {
var exportFilePath = GetExportFilePath(_lockFile.LockFilePath); var exportFilePath = GetExportFilePath(_lockFile.LockFilePath);
if (File.Exists(exportFilePath) && _msbuildTargetLibraries.Any()) if (File.Exists(exportFilePath) && _msbuildTargetLibraries.Any())
{ {
var exportFile = LockFileReader.ReadExportFile(exportFilePath); var exportFile = LockFileReader.ReadExportFile(exportFilePath);
PatchLockWithExport(exportFile); PatchLockWithExport(exportFile);
} }
else else
{ {

View file

@ -14,14 +14,14 @@ using NuGet.Versioning;
namespace Microsoft.DotNet.ProjectModel.Graph namespace Microsoft.DotNet.ProjectModel.Graph
{ {
public static class LockFileReader public static class LockFileReader
{ {
public static LockFile Read(string lockFilePath, bool patchWithExportFile = true) public static LockFile Read(string lockFilePath, bool designTime)
{ {
using (var stream = ResilientFileStreamOpener.OpenFile(lockFilePath)) using (var stream = ResilientFileStreamOpener.OpenFile(lockFilePath))
{ {
try try
{ {
return Read(lockFilePath, stream, patchWithExportFile); return Read(lockFilePath, stream, designTime);
} }
catch (FileFormatException ex) catch (FileFormatException ex)
{ {
@ -34,7 +34,7 @@ namespace Microsoft.DotNet.ProjectModel.Graph
} }
} }
public static LockFile Read(string lockFilePath, Stream stream, bool patchWithExportFile = true) public static LockFile Read(string lockFilePath, Stream stream, bool designTime)
{ {
try try
{ {
@ -47,16 +47,11 @@ namespace Microsoft.DotNet.ProjectModel.Graph
} }
var lockFile = ReadLockFile(lockFilePath, jobject); var lockFile = ReadLockFile(lockFilePath, jobject);
var patcher = new LockFilePatcher(lockFile); if (!designTime)
if (patchWithExportFile)
{ {
patcher.PatchIfNecessary(); var patcher = new LockFilePatcher(lockFile);
} patcher.Patch();
else
{
patcher.ThrowIfAnyMsbuildLibrariesPresent();
} }
return lockFile; return lockFile;

View file

@ -32,6 +32,8 @@ namespace Microsoft.DotNet.ProjectModel
private string PackagesDirectory { get; set; } private string PackagesDirectory { get; set; }
private string ReferenceAssembliesPath { get; set; } private string ReferenceAssembliesPath { get; set; }
private bool IsDesignTime { get; set; }
private Func<string, Project> ProjectResolver { get; set; } private Func<string, Project> ProjectResolver { get; set; }
@ -116,6 +118,12 @@ namespace Microsoft.DotNet.ProjectModel
Settings = settings; Settings = settings;
return this; return this;
} }
public ProjectContextBuilder AsDesignTime()
{
IsDesignTime = true;
return this;
}
public IEnumerable<ProjectContext> BuildAllTargets() public IEnumerable<ProjectContext> BuildAllTargets()
{ {
@ -389,7 +397,12 @@ namespace Microsoft.DotNet.ProjectModel
} }
} }
private void ScanLibraries(LockFileTarget target, LockFileLookup lockFileLookup, Dictionary<LibraryKey, LibraryDescription> libraries, MSBuildDependencyProvider msbuildResolver, PackageDependencyProvider packageResolver, ProjectDependencyProvider projectResolver) private void ScanLibraries(LockFileTarget target,
LockFileLookup lockFileLookup,
Dictionary<LibraryKey, LibraryDescription> libraries,
MSBuildDependencyProvider msbuildResolver,
PackageDependencyProvider packageResolver,
ProjectDependencyProvider projectResolver)
{ {
foreach (var library in target.Libraries) foreach (var library in target.Libraries)
{ {
@ -404,7 +417,7 @@ namespace Microsoft.DotNet.ProjectModel
{ {
if (MSBuildDependencyProvider.IsMSBuildProjectLibrary(projectLibrary)) if (MSBuildDependencyProvider.IsMSBuildProjectLibrary(projectLibrary))
{ {
description = msbuildResolver.GetDescription(TargetFramework, projectLibrary, library); description = msbuildResolver.GetDescription(TargetFramework, projectLibrary, library, IsDesignTime);
type = LibraryType.MSBuildProject; type = LibraryType.MSBuildProject;
} }
else else
@ -483,7 +496,7 @@ namespace Microsoft.DotNet.ProjectModel
{ {
var projectLockJsonPath = Path.Combine(projectDir, LockFile.FileName); var projectLockJsonPath = Path.Combine(projectDir, LockFile.FileName);
return File.Exists(projectLockJsonPath) ? return File.Exists(projectLockJsonPath) ?
LockFileReader.Read(Path.Combine(projectDir, LockFile.FileName)) : LockFileReader.Read(Path.Combine(projectDir, LockFile.FileName), designTime: false) :
null; null;
} }

View file

@ -21,11 +21,17 @@ namespace Microsoft.DotNet.ProjectModel.Resolution
_projectResolver = projectResolver; _projectResolver = projectResolver;
} }
public MSBuildProjectDescription GetDescription(NuGetFramework targetFramework, LockFileProjectLibrary projectLibrary, LockFileTargetLibrary targetLibrary) public MSBuildProjectDescription GetDescription(NuGetFramework targetFramework,
LockFileProjectLibrary projectLibrary,
LockFileTargetLibrary targetLibrary,
bool isDesignTime)
{ {
// During design time fragment file could be missing. When fragment file is missing none of the
// assets can be found but it is acceptable during design time.
var compatible = targetLibrary.FrameworkAssemblies.Any() || var compatible = targetLibrary.FrameworkAssemblies.Any() ||
targetLibrary.CompileTimeAssemblies.Any() || targetLibrary.CompileTimeAssemblies.Any() ||
targetLibrary.RuntimeAssemblies.Any(); targetLibrary.RuntimeAssemblies.Any() ||
isDesignTime;
var dependencies = new List<LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count); var dependencies = new List<LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count);
PopulateDependencies(dependencies, targetLibrary, targetFramework); PopulateDependencies(dependencies, targetLibrary, targetFramework);

View file

@ -211,7 +211,7 @@ namespace Microsoft.DotNet.ProjectModel
{ {
try try
{ {
currentEntry.Model = LockFileReader.Read(currentEntry.FilePath, fs); currentEntry.Model = LockFileReader.Read(currentEntry.FilePath, fs, designTime: true);
currentEntry.UpdateLastWriteTimeUtc(); currentEntry.UpdateLastWriteTimeUtc();
} }
catch (FileFormatException ex) catch (FileFormatException ex)
@ -257,7 +257,8 @@ namespace Microsoft.DotNet.ProjectModel
.WithProjectResolver(path => GetProject(path).Model) .WithProjectResolver(path => GetProject(path).Model)
.WithLockFileResolver(path => GetLockFile(path)) .WithLockFileResolver(path => GetLockFile(path))
.WithProject(project) .WithProject(project)
.WithTargetFramework(framework.FrameworkName); .WithTargetFramework(framework.FrameworkName)
.AsDesignTime();
currentEntry.ProjectContexts.Add(builder.Build()); currentEntry.ProjectContexts.Add(builder.Build());
} }

View file

@ -21,7 +21,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
public void TestExportFileIsParsed() public void TestExportFileIsParsed()
{ {
var lockFilePath = GetLockFilePath("valid"); var lockFilePath = GetLockFilePath("valid");
var lockFile = LockFileReader.Read(lockFilePath); var lockFile = LockFileReader.Read(lockFilePath, designTime: false);
var exportFile = lockFile.ExportFile; var exportFile = lockFile.ExportFile;
@ -41,7 +41,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
public void TestLockFileIsPatchedWithExportData() public void TestLockFileIsPatchedWithExportData()
{ {
var lockFilePath = GetLockFilePath("valid"); var lockFilePath = GetLockFilePath("valid");
var lockFile = LockFileReader.Read(lockFilePath); var lockFile = LockFileReader.Read(lockFilePath, designTime: false);
// check lock file structure is similar to export structure // check lock file structure is similar to export structure
foreach (var target in lockFile.Targets) foreach (var target in lockFile.Targets)
@ -59,7 +59,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
public void TestFragmentExistsButNoHolesInLockFile() public void TestFragmentExistsButNoHolesInLockFile()
{ {
var lockFilePath = GetLockFilePath("valid_staleFragment"); var lockFilePath = GetLockFilePath("valid_staleFragment");
var lockFile = LockFileReader.Read(lockFilePath); var lockFile = LockFileReader.Read(lockFilePath, designTime: false);
var exportFile = lockFile.ExportFile; var exportFile = lockFile.ExportFile;
@ -75,7 +75,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
{ {
var lockFilePath = GetLockFilePath("invalid_nofragment"); var lockFilePath = GetLockFilePath("invalid_nofragment");
Assert.Throws<FileFormatException>(() => LockFileReader.Read(lockFilePath)); Assert.Throws<FileFormatException>(() => LockFileReader.Read(lockFilePath, designTime: false));
} }
[Fact] [Fact]
@ -83,7 +83,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
{ {
var lockFilePath = GetLockFilePath("invalid_missing-exports"); var lockFilePath = GetLockFilePath("invalid_missing-exports");
Assert.Throws<FileFormatException>(() => LockFileReader.Read(lockFilePath)); Assert.Throws<FileFormatException>(() => LockFileReader.Read(lockFilePath, designTime: false));
} }
[Fact] [Fact]
@ -91,7 +91,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
{ {
var lockFilePath = GetLockFilePath("invalid_missmatching-versions"); var lockFilePath = GetLockFilePath("invalid_missmatching-versions");
Assert.Throws<FileFormatException>(() => LockFileReader.Read(lockFilePath)); Assert.Throws<FileFormatException>(() => LockFileReader.Read(lockFilePath, designTime: false));
} }
private static int LibraryNumberFromName(Microsoft.DotNet.ProjectModel.Graph.LockFileTargetLibrary library) private static int LibraryNumberFromName(Microsoft.DotNet.ProjectModel.Graph.LockFileTargetLibrary library)