diff --git a/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestApp/Program.cs b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestApp/Program.cs new file mode 100644 index 000000000..ac3163a58 --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestApp/Program.cs @@ -0,0 +1,17 @@ +// 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 int Main(string[] args) + { + Console.WriteLine(TestLibrary.Helper.GetMessage()); + return 100; + } + } +} diff --git a/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestApp/project.json b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestApp/project.json new file mode 100644 index 000000000..419f935a3 --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestApp/project.json @@ -0,0 +1,33 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "emitEntryPoint": true, + "preserveCompilationContext": true + }, + "dependencies": { + "Microsoft.NETCore.App": "1.0.1" + }, + "frameworks": { + "netcoreapp1.0": { + "dependencies": { + "TestLibrary": { + "target": "project", + "version": "1.0.0-*" + } + } + } + }, + "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": {} + } +} diff --git a/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/.noautobuild b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/.noautobuild new file mode 100644 index 000000000..e69de29bb diff --git a/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/Helper.cs b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/Helper.cs new file mode 100644 index 000000000..8c643796b --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/Helper.cs @@ -0,0 +1,24 @@ +// 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 Helper + { + /// + /// Gets the message from the helper. This comment is here to help test XML documentation file generation, please do not remove it. + /// + /// A message + public static string GetMessage() + { + return "This string came from the test library!"; + } + + public static void SayHi() + { + Console.WriteLine("Hello there!"); + } + } +} diff --git a/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/project.json b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/project.json new file mode 100644 index 000000000..48bc772d8 --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/TestLibrary/project.json @@ -0,0 +1,18 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "nowarn": [ + "CS1591" + ], + "xmlDoc": true, + "additionalArguments": [ + "-highentropyva+" + ] + }, + "dependencies": { + "NETStandard.Library": "1.6.0" + }, + "frameworks": { + "netstandard1.5": {} + } +} diff --git a/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/global.json b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/global.json new file mode 100644 index 000000000..3a4684c26 --- /dev/null +++ b/TestAssets/TestProjects/TestAppWithLibraryUnderTFM/global.json @@ -0,0 +1,3 @@ +{ + "projects": [ "."] +} \ No newline at end of file diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateProjectDependenciesRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateProjectDependenciesRule.cs index d2e346ede..bda537b2c 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateProjectDependenciesRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateProjectDependenciesRule.cs @@ -79,21 +79,46 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules HashSet migratedXProjDependencyNames, ProjectRootElement outputMSBuildProject) { + if(projectContexts.Any()) + { + MigrateProjectJsonProjectDependency( + projectContexts.First().ProjectFile, + null, + migratedXProjDependencyNames, + outputMSBuildProject); + } + foreach (var projectContext in projectContexts) { - var projectDependencies = _projectDependencyFinder.ResolveProjectDependencies(projectContext, migratedXProjDependencyNames); - - var projectDependencyTransformResults = projectDependencies.Select(p => ProjectDependencyTransform.Transform(p)); - - if (projectDependencyTransformResults.Any()) - { - AddProjectDependenciesToNewItemGroup( - outputMSBuildProject.AddItemGroup(), - projectDependencyTransformResults, - projectContext.TargetFramework); - } + MigrateProjectJsonProjectDependency( + projectContext.ProjectFile, + projectContext.TargetFramework, + migratedXProjDependencyNames, + outputMSBuildProject); } + } + + private void MigrateProjectJsonProjectDependency( + Project project, + NuGetFramework framework, + HashSet migratedXProjDependencyNames, + ProjectRootElement outputMSBuildProject) + { + var projectDependencies = _projectDependencyFinder.ResolveProjectDependenciesForFramework( + project, + framework, + migratedXProjDependencyNames); + + var projectDependencyTransformResults = + projectDependencies.Select(p => ProjectDependencyTransform.Transform(p)); + if (projectDependencyTransformResults.Any()) + { + AddProjectDependenciesToNewItemGroup( + outputMSBuildProject.AddItemGroup(), + projectDependencyTransformResults, + framework); + } } private void AddProjectDependenciesToNewItemGroup( diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProjectDependencies.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProjectDependencies.cs index 59bfab6b7..c077727fb 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProjectDependencies.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProjectDependencies.cs @@ -34,7 +34,32 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests var projectReferences = mockProj.Items.Where(item => item.ItemType.Equals("ProjectReference", StringComparison.Ordinal)); projectReferences.Count().Should().Be(1); - projectReferences.First().Include.Should().Be(Path.Combine("..", "TestLibrary", "TestLibrary.csproj")); + var projectReference = projectReferences.First(); + projectReference.Include.Should().Be(Path.Combine("..", "TestLibrary", "TestLibrary.csproj")); + projectReference.Parent.Condition.Should().BeEmpty(); + } + + [Fact] + public void TFM_specific_Project_dependencies_are_migrated_to_ProjectReference_under_condition_ItemGroup() + { + var solutionDirectory = + TestAssetsManager.CreateTestInstance("TestAppWithLibraryUnderTFM", callingMethod: "p").Path; + + var appDirectory = Path.Combine(solutionDirectory, "TestApp"); + + var projectContext = ProjectContext.Create(appDirectory, FrameworkConstants.CommonFrameworks.NetCoreApp10); + 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 projectReferences = mockProj.Items.Where(item => item.ItemType.Equals("ProjectReference", StringComparison.Ordinal)); + projectReferences.Count().Should().Be(1); + + var projectReference = projectReferences.First(); + projectReference.Include.Should().Be(Path.Combine("..", "TestLibrary", "TestLibrary.csproj")); + projectReference.Parent.Condition.Should().Be(" '$(TargetFramework)' == 'netcoreapp1.0' "); } [Fact]