From 6b54ae0bcc5c63e7c989ac19d851f234f9172bea Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 14 Jun 2016 14:55:59 -0700 Subject: [PATCH] Trim platfrom libraries from deps fiels --- .../Executable.cs | 21 ++++++---- .../ProjectModelPlatformExtensions.cs | 5 +++ .../DependencyContextJsonReader.cs | 11 ++++- .../DependencyContextStrings.cs | 2 + .../DependencyContextWriter.cs | 4 ++ .../commands/dotnet-publish/PublishCommand.cs | 12 +++--- .../DependencyContextJsonReaderTest.cs | 42 +++++++++++++++++++ .../DependencyContextJsonWriterTests.cs | 30 +++++++++++++ 8 files changed, 113 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.DotNet.Compiler.Common/Executable.cs b/src/Microsoft.DotNet.Compiler.Common/Executable.cs index 29a602a70..52ebbe924 100644 --- a/src/Microsoft.DotNet.Compiler.Common/Executable.cs +++ b/src/Microsoft.DotNet.Compiler.Common/Executable.cs @@ -157,9 +157,12 @@ namespace Microsoft.DotNet.Cli.Compiler.Common private void WriteDepsFileAndCopyProjectDependencies(LibraryExporter exporter) { - // When called this way we don't need to filter exports, so we pass the same list to both. var exports = exporter.GetAllExports().ToList(); - WriteConfigurationFiles(exports, exports, includeDevConfig: true); + var exportsLookup = exports.ToDictionary(e => e.Library.Identity.Name); + var platformExclusionList = _context.GetPlatformExclusionList(exportsLookup); + var filteredExports = exports.FilterExports(platformExclusionList); + + WriteConfigurationFiles(exports, filteredExports, exports, includeDevConfig: true); var projectExports = exporter.GetAllProjectTypeDependencies(); CopyAssemblies(projectExports); @@ -169,9 +172,13 @@ namespace Microsoft.DotNet.Cli.Compiler.Common CopyAssets(packageExports); } - public void WriteConfigurationFiles(IEnumerable allExports, IEnumerable depsExports, bool includeDevConfig) + public void WriteConfigurationFiles( + IEnumerable allExports, + IEnumerable depsRuntimeExports, + IEnumerable depsCompilationExports, + bool includeDevConfig) { - WriteDeps(depsExports); + WriteDeps(depsRuntimeExports, depsCompilationExports); if (_context.ProjectFile.HasRuntimeOutput(_configuration)) { WriteRuntimeConfig(allExports); @@ -272,7 +279,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common runtimeOptions.Add("additionalProbingPaths", additionalProbingPaths); } - public void WriteDeps(IEnumerable exports) + public void WriteDeps(IEnumerable runtimeExports, IEnumerable compilationExports) { Directory.CreateDirectory(_runtimeOutputPath); @@ -280,8 +287,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common var dependencyContext = new DependencyContextBuilder().Build( compilerOptions: includeCompile ? _compilerOptions : null, - compilationExports: includeCompile ? exports : null, - runtimeExports: exports, + compilationExports: includeCompile ? compilationExports : null, + runtimeExports: runtimeExports, portable: _context.IsPortable, target: _context.TargetFramework, runtime: _context.RuntimeIdentifier ?? string.Empty); diff --git a/src/Microsoft.DotNet.ProjectModel/ProjectModelPlatformExtensions.cs b/src/Microsoft.DotNet.ProjectModel/ProjectModelPlatformExtensions.cs index 11ef13c52..5f6ea1b94 100644 --- a/src/Microsoft.DotNet.ProjectModel/ProjectModelPlatformExtensions.cs +++ b/src/Microsoft.DotNet.ProjectModel/ProjectModelPlatformExtensions.cs @@ -64,5 +64,10 @@ namespace Microsoft.DotNet.ProjectModel } } } + + public static IEnumerable FilterExports(this IEnumerable exports, HashSet exclusionList) + { + return exports.Where(e => !exclusionList.Contains(e.Library.Identity.Name)); + } } } diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index 730fa9f63..5d3ac12fa 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -178,7 +178,9 @@ namespace Microsoft.Extensions.DependencyModel { return Enumerable.Empty(); } - return librariesObject.Properties().Select(property => ReadLibrary(property, runtime, libraryStubs)); + return librariesObject.Properties() + .Select(property => ReadLibrary(property, runtime, libraryStubs)) + .Where(library => library != null); } private Library ReadLibrary(JProperty property, bool runtime, Dictionary libraryStubs) @@ -202,6 +204,13 @@ namespace Microsoft.Extensions.DependencyModel if (runtime) { + // Runtime section of this library was trimmed by type:platform + var isCompilationOnly = libraryObject.Value(DependencyContextStrings.CompilationOnlyPropertyName); + if (isCompilationOnly == true) + { + return null; + } + var runtimeTargetsObject = (JObject)libraryObject[DependencyContextStrings.RuntimeTargetsPropertyName]; var entries = ReadRuntimeTargetEntries(runtimeTargetsObject).ToArray(); diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs index 393a1f493..ab457bca1 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs @@ -74,5 +74,7 @@ namespace Microsoft.Extensions.DependencyModel internal const string ResourceAssembliesPropertyName = "resources"; internal const string LocalePropertyName = "locale"; + + internal const string CompilationOnlyPropertyName = "compileOnly"; } } \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index 77652c240..7f9f70fa6 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -261,6 +261,10 @@ namespace Microsoft.Extensions.DependencyModel } AddDependencies(libraryObject, dependencies); + if (compilationLibrary != null && runtimeLibrary == null) + { + libraryObject.Add(DependencyContextStrings.CompilationOnlyPropertyName, true); + } return libraryObject; } diff --git a/src/dotnet/commands/dotnet-publish/PublishCommand.cs b/src/dotnet/commands/dotnet-publish/PublishCommand.cs index 437381f27..eebf24f66 100644 --- a/src/dotnet/commands/dotnet-publish/PublishCommand.cs +++ b/src/dotnet/commands/dotnet-publish/PublishCommand.cs @@ -140,8 +140,9 @@ namespace Microsoft.DotNet.Tools.Publish var buildExclusionList = context.GetTypeBuildExclusionList(exportsLookup); var allExclusionList = new HashSet(platformExclusionList); allExclusionList.UnionWith(buildExclusionList); + var filteredExports = exports.FilterExports(allExclusionList); - foreach (var export in FilterExports(exports, allExclusionList)) + foreach (var export in filteredExports) { Reporter.Verbose.WriteLine($"publish: Publishing {export.Library.Identity.ToString().Green().Bold()} ..."); @@ -173,7 +174,10 @@ namespace Microsoft.DotNet.Tools.Publish { // Make executable in the new location var executable = new Executable(context, buildOutputPaths, outputPath, buildOutputPaths.IntermediateOutputDirectoryPath, exporter, configuration); - executable.WriteConfigurationFiles(exports, FilterExports(exports, buildExclusionList), includeDevConfig: false); + var runtimeExports = filteredExports; + var compilationExports = exports.FilterExports(buildExclusionList); + + executable.WriteConfigurationFiles(exports, runtimeExports, compilationExports, includeDevConfig: false); } var contentFiles = new ContentFiles(context); @@ -206,10 +210,6 @@ namespace Microsoft.DotNet.Tools.Publish return true; } - private static IEnumerable FilterExports(IEnumerable exports, HashSet exclusionList) - { - return exports.Where(e => !exclusionList.Contains(e.Library.Identity.Name)); - } /// /// Filters which export's RuntimeAssets should get copied to the output path. diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index c70de2a94..310042d82 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -185,6 +185,48 @@ namespace Microsoft.Extensions.DependencyModel.Tests package.Serviceable.Should().Be(false); } + [Fact] + public void DoesNotReadRuntimeLibraryFromCompilationOnlyEntries() + { + var context = Read( +@"{ + ""targets"": { + "".NETCoreApp,Version=v1.0"": { + ""MyApp/1.0.1"": { + ""dependencies"": { + ""AspNet.Mvc"": ""1.0.0"" + }, + ""compile"": { + ""MyApp.dll"": { } + } + }, + ""System.Banana/1.0.0"": { + ""dependencies"": { + ""System.Foo"": ""1.0.0"" + }, + ""compileOnly"": true, + ""compile"": { + ""ref/dotnet5.4/System.Banana.dll"": { } + } + } + } + }, + ""libraries"":{ + ""MyApp/1.0.1"": { + ""type"": ""project"" + }, + ""System.Banana/1.0.0"": { + ""type"": ""package"", + ""serviceable"": false, + ""sha512"": ""HASH-System.Banana"" + }, + } +}"); + context.CompileLibraries.Should().HaveCount(2); + context.RuntimeLibraries.Should().HaveCount(1); + context.RuntimeLibraries[0].Name.Should().Be("MyApp"); + } + [Fact] public void ReadsRuntimeLibrariesWithSubtargetsFromMainTargetForPortable() diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs index f3d4841e9..ca4427ca4 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -453,6 +453,36 @@ namespace Microsoft.Extensions.DependencyModel.Tests } + [Fact] + public void WriteCompilationOnlyAttributeIfOnlyCompilationLibraryProvided() + { + var result = Save(Create( + "Target", + "runtime", + true, + compileLibraries: new[] + { + new CompilationLibrary( + "package", + "PackageName", + "1.2.3", + "HASH", + new [] { "ref/Banana.dll" }, + new [] { + new Dependency("Fruits.Abstract.dll","2.0.0") + }, + true + ) + })); + + // targets + var targets = result.Should().HavePropertyAsObject("targets").Subject; + var target = targets.Should().HavePropertyAsObject("Target").Subject; + var library = target.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + library.Should().HavePropertyValue("compileOnly", true); + } + + [Fact] public void WritesCompilationOptions() {