Merge pull request #1502 from dotnet/pakrym/dc-deps
Add support for reading DepedencyContext from deps file
This commit is contained in:
commit
acd581c376
18 changed files with 438 additions and 20 deletions
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0.24720" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.24720</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>6d84ef36-a5d5-4eaf-b38b-ced635473785</ProjectGuid>
|
||||
<RootNamespace>DependencyContextValidator</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
|
@ -0,0 +1,65 @@
|
|||
// 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.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyModel
|
||||
{
|
||||
public static class DependencyContextValidator
|
||||
{
|
||||
private static void Error(string message)
|
||||
{
|
||||
throw new InvalidOperationException(message);
|
||||
}
|
||||
|
||||
private static void CheckMetadata(Library library)
|
||||
{
|
||||
if (string.Equals(library.LibraryType, "package", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(library.PackageName) ||
|
||||
string.IsNullOrWhiteSpace(library.Hash) ||
|
||||
string.IsNullOrWhiteSpace(library.Version))
|
||||
{
|
||||
Error($"Empty metadata for {library.GetType().ToString()} {library.PackageName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Validate(bool full)
|
||||
{
|
||||
var context = DependencyContext.Default;
|
||||
if (full)
|
||||
{
|
||||
if (!context.CompileLibraries.Any())
|
||||
{
|
||||
Error("Compilation libraries empty");
|
||||
}
|
||||
foreach (var compilationLibrary in context.CompileLibraries)
|
||||
{
|
||||
CheckMetadata(compilationLibrary);
|
||||
var resolvedPaths = compilationLibrary.ResolveReferencePaths();
|
||||
foreach (var resolvedPath in resolvedPaths)
|
||||
{
|
||||
if (!File.Exists(resolvedPath))
|
||||
{
|
||||
Error($"Compilataion library resolved to non existent path {resolvedPath}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var runtimeLibrary in context.RuntimeLibraries)
|
||||
{
|
||||
CheckMetadata(runtimeLibrary);
|
||||
foreach (var runtimeAssembly in runtimeLibrary.Assemblies)
|
||||
{
|
||||
var assembly = Assembly.Load(runtimeAssembly.Name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"version": "1.0.0-*",
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.0.0-rc2-23811",
|
||||
"Microsoft.Extensions.DependencyModel": {
|
||||
"target": "project",
|
||||
"version": "1.0.0-*"
|
||||
}
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"dnxcore50": { }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
// 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.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace TestApp
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Microsoft.Extensions.DependencyModel.DependencyContextValidator.Validate(true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0-*",
|
||||
"compilationOptions": {
|
||||
"emitEntryPoint": true,
|
||||
"preserveCompilationContext": true
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.0.0-rc2-23811",
|
||||
"DependencyContextValidator": "1.0.0-*"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"dnxcore50": { }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
// 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.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace TestApp
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Microsoft.Extensions.DependencyModel.DependencyContextValidator.Validate(false);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"version": "1.0.0-*",
|
||||
"compilationOptions": {
|
||||
"emitEntryPoint": true,
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.0.0-rc2-23811",
|
||||
"DependencyContextValidator": "1.0.0-*"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"dnxcore50": { }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"projects": [ ".", "../../../src" ]
|
||||
}
|
|
@ -8,10 +8,8 @@ namespace TestApp
|
|||
{
|
||||
public class Program
|
||||
{
|
||||
public static int Main(string[] args)
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(TestLibrary.Helper.GetMessage());
|
||||
return 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
{
|
||||
"projects": [ "."]
|
||||
"projects": [ "." ]
|
||||
}
|
|
@ -11,6 +11,7 @@ namespace Microsoft.Extensions.DependencyModel
|
|||
public class DependencyContext
|
||||
{
|
||||
private const string DepsResourceSufix = ".deps.json";
|
||||
private const string DepsFileExtension = ".deps";
|
||||
|
||||
private static readonly Lazy<DependencyContext> _defaultContext = new Lazy<DependencyContext>(LoadDefault);
|
||||
|
||||
|
@ -43,22 +44,29 @@ namespace Microsoft.Extensions.DependencyModel
|
|||
|
||||
public static DependencyContext Load(Assembly assembly)
|
||||
{
|
||||
var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + DepsResourceSufix);
|
||||
|
||||
if (stream == null)
|
||||
if (assembly == null)
|
||||
{
|
||||
return null;
|
||||
throw new ArgumentNullException(nameof(assembly));
|
||||
}
|
||||
|
||||
using (stream)
|
||||
using (var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + DepsResourceSufix))
|
||||
{
|
||||
return Load(stream);
|
||||
if (stream != null)
|
||||
{
|
||||
return new DependencyContextJsonReader().Read(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static DependencyContext Load(Stream stream)
|
||||
{
|
||||
return new DependencyContextReader().Read(stream);
|
||||
var depsFile = Path.ChangeExtension(assembly.Location, DepsFileExtension);
|
||||
if (File.Exists(depsFile))
|
||||
{
|
||||
using (var stream = File.OpenRead(depsFile))
|
||||
{
|
||||
return new DependencyContextCsvReader().Read(stream);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
// 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.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyModel
|
||||
{
|
||||
public class DependencyContextCsvReader
|
||||
{
|
||||
public DependencyContext Read(Stream stream)
|
||||
{
|
||||
var lines = new List<DepsFileLine>();
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = new DepsFileLine();
|
||||
line.LibraryType = ReadValue(reader);
|
||||
line.PackageName = ReadValue(reader);
|
||||
line.PackageVersion = ReadValue(reader);
|
||||
line.PackageHash = ReadValue(reader);
|
||||
line.AssetType = ReadValue(reader);
|
||||
line.AssetName = ReadValue(reader);
|
||||
line.AssetPath = ReadValue(reader);
|
||||
|
||||
if (line.AssetType == "runtime" &&
|
||||
!line.AssetPath.EndsWith(".ni.dll"))
|
||||
{
|
||||
lines.Add(line);
|
||||
}
|
||||
SkipWhitespace(reader);
|
||||
}
|
||||
}
|
||||
|
||||
var runtimeLibraries = new List<RuntimeLibrary>();
|
||||
var packageGroups = lines.GroupBy(PackageIdentity);
|
||||
foreach (var packageGroup in packageGroups)
|
||||
{
|
||||
var identity = packageGroup.Key;
|
||||
runtimeLibraries.Add(new RuntimeLibrary(
|
||||
libraryType: identity.Item1,
|
||||
packageName: identity.Item2,
|
||||
version: identity.Item3,
|
||||
hash: identity.Item4,
|
||||
assemblies: packageGroup.Select(l => l.AssetPath).ToArray(),
|
||||
dependencies: new Dependency[] { },
|
||||
serviceable: false
|
||||
));
|
||||
}
|
||||
|
||||
return new DependencyContext(
|
||||
target: string.Empty,
|
||||
runtime: string.Empty,
|
||||
compilationOptions: CompilationOptions.Default,
|
||||
compileLibraries: new CompilationLibrary[] {},
|
||||
runtimeLibraries: runtimeLibraries.ToArray());
|
||||
}
|
||||
|
||||
private Tuple<string, string, string, string> PackageIdentity(DepsFileLine line)
|
||||
{
|
||||
return Tuple.Create(line.LibraryType, line.PackageName, line.PackageVersion, line.PackageHash);
|
||||
}
|
||||
|
||||
private void SkipWhitespace(StreamReader reader)
|
||||
{
|
||||
// skip all whitespace
|
||||
while (!reader.EndOfStream && char.IsWhiteSpace((char)reader.Peek()))
|
||||
{
|
||||
reader.Read();
|
||||
}
|
||||
}
|
||||
|
||||
private string ReadValue(StreamReader reader)
|
||||
{
|
||||
SkipWhitespace(reader);
|
||||
|
||||
var c = ReadSucceed(reader.Read());
|
||||
if (c != '"')
|
||||
{
|
||||
throw new FormatException("Deps file value should start with '\"'");
|
||||
}
|
||||
|
||||
var value = new StringBuilder();
|
||||
while (ReadSucceed(reader.Peek()) != '"')
|
||||
{
|
||||
c = ReadSucceed(reader.Read());
|
||||
if (c == '\\')
|
||||
{
|
||||
value.Append(ReadSucceed(reader.Read()));
|
||||
}
|
||||
else
|
||||
{
|
||||
value.Append(c);
|
||||
}
|
||||
}
|
||||
// Read last "
|
||||
ReadSucceed(reader.Read());
|
||||
// Read comment
|
||||
if (reader.Peek() == ',')
|
||||
{
|
||||
reader.Read();
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
private char ReadSucceed(int c)
|
||||
{
|
||||
if (c == -1)
|
||||
{
|
||||
throw new FormatException("Unexpected end of file");
|
||||
}
|
||||
return (char) c;
|
||||
}
|
||||
|
||||
private struct DepsFileLine
|
||||
{
|
||||
public string LibraryType;
|
||||
public string PackageName;
|
||||
public string PackageVersion;
|
||||
public string PackageHash;
|
||||
public string AssetType;
|
||||
public string AssetName;
|
||||
public string AssetPath;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ using Newtonsoft.Json.Linq;
|
|||
|
||||
namespace Microsoft.Extensions.DependencyModel
|
||||
{
|
||||
public class DependencyContextReader
|
||||
public class DependencyContextJsonReader
|
||||
{
|
||||
public DependencyContext Read(Stream stream)
|
||||
{
|
|
@ -8,18 +8,20 @@ namespace Microsoft.Extensions.DependencyModel
|
|||
{
|
||||
public class RuntimeAssembly
|
||||
{
|
||||
private readonly string _assemblyName;
|
||||
|
||||
public RuntimeAssembly(string path)
|
||||
: this(new AssemblyName(System.IO.Path.GetFileNameWithoutExtension(path)), path)
|
||||
: this(System.IO.Path.GetFileNameWithoutExtension(path), path)
|
||||
{
|
||||
}
|
||||
|
||||
public RuntimeAssembly(AssemblyName name, string path)
|
||||
public RuntimeAssembly(string assemblyName, string path)
|
||||
{
|
||||
Name = name;
|
||||
_assemblyName = assemblyName;
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public AssemblyName Name { get; }
|
||||
public AssemblyName Name => new AssemblyName(_assemblyName);
|
||||
|
||||
public string Path { get; }
|
||||
}
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
// 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.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyModel.Tests
|
||||
{
|
||||
public class DependencyContextCsvReaderTests
|
||||
{
|
||||
private DependencyContext Read(string text)
|
||||
{
|
||||
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
|
||||
{
|
||||
return new DependencyContextCsvReader().Read(stream);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GroupsAssetsCorrectlyIntoLibraries()
|
||||
{
|
||||
var context = Read(@"
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.Runtime.dll""
|
||||
");
|
||||
context.RuntimeLibraries.Should().HaveCount(1);
|
||||
var library = context.RuntimeLibraries.Single();
|
||||
library.LibraryType.Should().Be("Package");
|
||||
library.PackageName.Should().Be("runtime.any.System.AppContext");
|
||||
library.Version.Should().Be("4.1.0-rc2-23811");
|
||||
library.Hash.Should().Be("sha512-1");
|
||||
library.Assemblies.Should().HaveCount(2).And
|
||||
.Contain(a => a.Path == "lib\\dnxcore50\\System.AppContext.dll").And
|
||||
.Contain(a => a.Path == "lib\\dnxcore50\\System.Runtime.dll");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IgnoresAllButRuntimeAssets()
|
||||
{
|
||||
var context = Read(@"
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""native"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext2.so""
|
||||
");
|
||||
context.RuntimeLibraries.Should().HaveCount(1);
|
||||
var library = context.RuntimeLibraries.Single();
|
||||
library.Assemblies.Should().HaveCount(1).And
|
||||
.Contain(a => a.Path == "lib\\dnxcore50\\System.AppContext.dll");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IgnoresNiDllAssemblies()
|
||||
{
|
||||
var context = Read(@"
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.ni.dll""
|
||||
");
|
||||
context.RuntimeLibraries.Should().HaveCount(1);
|
||||
var library = context.RuntimeLibraries.Single();
|
||||
library.Assemblies.Should().HaveCount(1).And
|
||||
.Contain(a => a.Path == "lib\\dnxcore50\\System.AppContext.dll");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UsesTypeNameVersionAndHashToGroup()
|
||||
{
|
||||
var context = Read(@"
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23812"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-2"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
""Package"",""runtime.any.System.AppContext2"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
""Project"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll""
|
||||
");
|
||||
context.RuntimeLibraries.Should().HaveCount(5);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("text")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("\"")]
|
||||
[InlineData(@""",""")]
|
||||
[InlineData(@"\\")]
|
||||
public void ThrowsFormatException(string intput)
|
||||
{
|
||||
Assert.Throws<FormatException>(() => Read(intput));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.TestFramework;
|
||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
@ -146,6 +147,26 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
|
|||
result.StdOut.Should().Contain("MyNamespace.Util");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmbeddedDependencyContextIsValidOnBuild()
|
||||
{
|
||||
var testProjectPath = Path.Combine(RepoRoot, "TestAssets", "TestProjects", "DependencyContextValidator", "TestApp");
|
||||
var testProject = Path.Combine(testProjectPath, "project.json");
|
||||
|
||||
var runCommand = new RunCommand(testProject);
|
||||
runCommand.Execute().Should().Pass();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DepsDependencyContextIsValidOnBuild()
|
||||
{
|
||||
var testProjectPath = Path.Combine(RepoRoot, "TestAssets", "TestProjects", "DependencyContextValidator", "TestAppDeps");
|
||||
var testProject = Path.Combine(testProjectPath, "project.json");
|
||||
|
||||
var runCommand = new RunCommand(testProject);
|
||||
runCommand.Execute().Should().Pass();
|
||||
}
|
||||
|
||||
private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir)
|
||||
{
|
||||
// copy all the files to temp dir
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
},
|
||||
|
||||
"content": [
|
||||
"../../TestAssets/TestProjects/DependencyContextValidator/**/*",
|
||||
"../../TestAssets/TestProjects/TestLibraryWithAnalyzer/*",
|
||||
"../../TestAssets/TestProjects/TestAppWithLibrary/TestLibrary/*",
|
||||
"../../TestAssets/TestProjects/TestProjectWithCultureSpecificResource/*",
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.TestFramework;
|
||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
@ -163,7 +164,6 @@ namespace Microsoft.DotNet.Tools.Publish.Tests
|
|||
refsDirectory.Should().NotHaveFile("TestLibrary.dll");
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void CompilationFailedTest()
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue