add trimming of published output based on sharedfx

Any dependencies which **exactly** match the version requested in the
graph originating at the `type: platform` dependency (if any) are
trimmed from the publish output
This commit is contained in:
Andrew Stanton-Nurse 2016-03-25 15:40:16 -07:00
parent 8df6f5405d
commit 62b7740c95
5 changed files with 101 additions and 1 deletions

View file

@ -0,0 +1,12 @@
using System;
namespace PortableApp
{
public static class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}

View file

@ -0,0 +1,21 @@
{
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netstandard1.5": {
"imports": [
"dnxcore50",
"portable-net45+win8"
],
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-23925"
},
"System.Linq": "4.0.0"
}
}
}
}

View file

@ -14,6 +14,8 @@ using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.DotNet.Files;
using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.ProjectModel.Utilities;
using Microsoft.DotNet.ProjectModel.Graph;
using NuGet.Versioning;
namespace Microsoft.DotNet.Tools.Publish
{
@ -153,7 +155,14 @@ namespace Microsoft.DotNet.Tools.Publish
var exporter = context.CreateExporter(configuration);
var isPortable = string.IsNullOrEmpty(context.RuntimeIdentifier);
foreach (var export in exporter.GetAllExports())
// Collect all exports and organize them
var exports = exporter.GetAllExports()
.Where(e => e.Library.Identity.Type.Equals(LibraryType.Package))
.ToDictionary(e => e.Library.Identity.Name);
var collectExclusionList = isPortable ? GetExclusionList(context, exports) : new HashSet<string>();
foreach (var export in exporter.GetAllExports().Where(e => !collectExclusionList.Contains(e.Library.Identity.Name)))
{
Reporter.Verbose.WriteLine($"Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
@ -198,6 +207,40 @@ namespace Microsoft.DotNet.Tools.Publish
return true;
}
private HashSet<string> GetExclusionList(ProjectContext context, Dictionary<string, LibraryExport> exports)
{
var exclusionList = new HashSet<string>();
var redistPackages = context.RootProject.Dependencies
.Where(r => r.Type.Equals(LibraryDependencyType.Platform))
.ToList();
if (redistPackages.Count == 0)
{
return exclusionList;
}
else if (redistPackages.Count > 1)
{
throw new InvalidOperationException("Multiple packages with type: \"platform\" were specified!");
}
var redistExport = exports[redistPackages[0].Name];
exclusionList.Add(redistExport.Library.Identity.Name);
CollectDependencies(exports, redistExport.Library.Dependencies, exclusionList);
return exclusionList;
}
private void CollectDependencies(Dictionary<string, LibraryExport> exports, IEnumerable<LibraryRange> dependencies, HashSet<string> exclusionList)
{
foreach (var dependency in dependencies)
{
var export = exports[dependency.Name];
if(export.Library.Identity.Version.Equals(dependency.VersionRange.MinVersion))
{
exclusionList.Add(export.Library.Identity.Name);
CollectDependencies(exports, export.Library.Dependencies, exclusionList);
}
}
}
private static void PublishRefs(LibraryExport export, string outputPath)
{
var refsPath = Path.Combine(outputPath, "refs");

View file

@ -36,6 +36,9 @@ namespace Microsoft.DotNet.Tools.Publish.Tests
"PortableAppWithNative.deps.json"
});
// Prior to `type:platform` trimming, this would have been published.
publishDir.Should().NotHaveFile("System.Linq.dll");
var runtimesOutput = publishDir.Sub("runtimes");
runtimesOutput.Should().Exist();
@ -50,5 +53,26 @@ namespace Microsoft.DotNet.Tools.Publish.Tests
nativeDir.Should().HaveFile(output.Item2);
}
}
[Fact]
public void PortableAppWithIntentionalDowngradePublishesDowngradedManagedCode()
{
var testInstance = TestAssetsManager.CreateTestInstance("PortableTests")
.WithLockFiles();
var publishCommand = new PublishCommand(Path.Combine(testInstance.TestRoot, "PortableAppWithIntentionalManagedDowngrade"));
var publishResult = publishCommand.Execute();
publishResult.Should().Pass();
var publishDir = publishCommand.GetOutputDirectory(portable: true);
publishDir.Should().HaveFiles(new[]
{
"PortableAppWithIntentionalManagedDowngrade.dll",
"PortableAppWithIntentionalManagedDowngrade.deps",
"PortableAppWithIntentionalManagedDowngrade.deps.json",
"System.Linq.dll"
});
}
}
}