Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4646 from livarcocc/hoist_framework_assemblies
Browse files Browse the repository at this point in the history
Hoisting FrameworkAssemblies for desktop frameworks during migration.
  • Loading branch information
Livar authored Nov 8, 2016
2 parents 8a12994 + 92e7d8e commit 7c6770f
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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;
using TestLibrary;

namespace TestApp
{
public class Program
{
public static int Main(string[] args)
{
Console.WriteLine("This string came from ProjectA");
Console.WriteLine($"{ProjectD.GetMessage()}");
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"dependencies": {
"ProjectB": {
"target": "project",
"version": "1.0.0-*"
}
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": "1.0.1"
}
},
"net451": {}
},
"runtimes": {
"win7-x64": {},
"win7-x86": {},
"osx.10.10-x64": {},
"osx.10.11-x64": {},
"ubuntu.14.04-x64": {},
"ubuntu.16.04-x64": {},
"centos.7-x64": {},
"rhel.7.2-x64": {},
"debian.8-x64": {},
"fedora.23-x64": {},
"opensuse.13.2-x64": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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;

namespace TestLibrary
{
public static class ProjectB
{
public static string GetMessage()
{
return "This string came from ProjectB";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "1.0.0-*",
"buildOptions": {
"nowarn": [
"CS1591"
],
"xmlDoc": true,
"additionalArguments": [
"-highentropyva+"
]
},
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.5": {},
"net451": {
"frameworkAssemblies": {
"System.ComponentModel.DataAnnotations": ""
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.DotNet.Internal.ProjectModel;
using Microsoft.DotNet.Tools.Common;
using NuGet.Frameworks;
using NuGet.LibraryModel;

namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{
Expand Down Expand Up @@ -123,6 +124,56 @@ private void MigrateProjectJsonProjectDependency(
projectDependencyTransformResults,
framework);
}

HoistFrameworkAssembliesForProjectDependencies(projectDependencies, outputMSBuildProject);
}

private void HoistFrameworkAssembliesForProjectDependencies(
IEnumerable<ProjectDependency> projectDependencies,
ProjectRootElement outputMSBuildProject)
{
foreach (var projectDependency in projectDependencies)
{
HoistFrameworkAssembliesForDesktopFrameworks(projectDependency, outputMSBuildProject);
}
}

private void HoistFrameworkAssembliesForDesktopFrameworks(
ProjectDependency projectDependency,
ProjectRootElement outputMSBuildProject)
{
var targetFrameworks = ProjectReader
.GetProject(projectDependency.ProjectFilePath)
.GetTargetFrameworks().Where(p => !p.FrameworkName.IsPackageBased);

foreach (var targetFramework in targetFrameworks)
{
HoistFrameworkAssemblies(targetFramework, outputMSBuildProject);
}
}

private void HoistFrameworkAssemblies(
TargetFrameworkInformation targetFramework,
ProjectRootElement outputMSBuildProject)
{
var frameworkAssemblies = targetFramework.Dependencies.Where(d =>
d.LibraryRange.TypeConstraint == LibraryDependencyTarget.Reference);
if(frameworkAssemblies.Any())
{
var condition = targetFramework.FrameworkName.GetMSBuildCondition();
var itemGroup =
outputMSBuildProject.ItemGroups.FirstOrDefault(i => i.Condition == condition) ??
outputMSBuildProject.AddItemGroup();
itemGroup.Condition = condition;

foreach (var frameworkAssembly in frameworkAssemblies)
{
_transformApplicator.Execute(
FrameworkDependencyTransform.Transform(frameworkAssembly),
itemGroup,
mergeExisting: true);
}
}
}

private void AddProjectDependenciesToNewItemGroup(
Expand Down Expand Up @@ -167,5 +218,13 @@ private void AddProjectDependenciesToNewItemGroup(
path => path,
path => "",
path => true);

private AddItemTransform<ProjectLibraryDependency> FrameworkDependencyTransform =>
new AddItemTransform<ProjectLibraryDependency>(
"Reference",
dep => dep.Name,
dep => "",
dep => true)
.WithMetadata("FromP2P", "true");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,29 @@ public void It_promotes_P2P_references_up_in_the_dependency_chain()
projectReferences.Count().Should().Be(7);
}

[Fact]
public void It_promotes_FrameworkAssemblies_from_P2P_references_up_in_the_dependency_chain()
{
var solutionDirectory = TestAssets.Get(TestAssetKinds.DesktopTestProjects, "TestAppWithFrameworkAssemblies")
.CreateInstance()
.WithSourceFiles().Root;

var appDirectory = Path.Combine(solutionDirectory.FullName, "ProjectA");

var projectContext = ProjectContext.Create(appDirectory, FrameworkConstants.CommonFrameworks.Net451);
var mockProj = ProjectRootElement.Create();
var testSettings = new MigrationSettings(appDirectory, appDirectory, "1.0.0", mockProj, null);
var testInputs = new MigrationRuleInputs(new[] {projectContext}, mockProj, mockProj.AddItemGroup(),
mockProj.AddPropertyGroup());
new MigrateProjectDependenciesRule().Apply(testSettings, testInputs);

var frameworkAssemblyReferences = mockProj.Items.Where(
item => item.ItemType == "Reference" &&
item.Include == "System.ComponentModel.DataAnnotations" &&
item.Parent.Condition == " '$(TargetFramework)' == 'net451' ");
frameworkAssemblyReferences.Count().Should().Be(1);
}

[Fact]
public void All_promoted_P2P_references_are_marked_with_a_FromP2P_attribute()
{
Expand Down Expand Up @@ -299,13 +322,21 @@ public void It_migrates_unqualified_dependencies_as_ProjectReference_when_a_matc
}

private ProjectRootElement MigrateProject(string solution, string project)
{
return MigrateProject(solution, project, FrameworkConstants.CommonFrameworks.NetCoreApp10);
}

private ProjectRootElement MigrateProject(
string solution,
string project,
NuGetFramework targetFramework)
{
var solutionDirectory =
TestAssetsManager.CreateTestInstance(solution, callingMethod: "p").Path;

var appDirectory = Path.Combine(solutionDirectory, project);

var projectContext = ProjectContext.Create(appDirectory, FrameworkConstants.CommonFrameworks.NetCoreApp10);
var projectContext = ProjectContext.Create(appDirectory, targetFramework);
var mockProj = ProjectRootElement.Create();
var testSettings = new MigrationSettings(appDirectory, appDirectory, "1.0.0", mockProj, null);
var testInputs = new MigrationRuleInputs(new[] {projectContext}, mockProj, mockProj.AddItemGroup(),
Expand Down

0 comments on commit 7c6770f

Please sign in to comment.