From c35dbd9563d0bb9b3c715db67e6d56f48dab85cd Mon Sep 17 00:00:00 2001 From: Justin Goshi Date: Thu, 9 Mar 2017 12:17:18 -1000 Subject: [PATCH] Migration of xproj with name different from folder leaves ref to old project in solution (#5958) * WIP trying to remove unused xproj files * Use relative path * Add a test * Fix a few test failures * Fix path issue * Address PR Comments --- .../FolderHasDifferentName.sln | 22 +++++++ .../FolderHasDifferentName.xproj | 18 ++++++ .../Program.cs | 16 +++++ .../project.json | 26 ++++++++ .../commands/dotnet-migrate/MigrateCommand.cs | 62 ++++++++++--------- .../GivenThatIWantToMigrateSolutions.cs | 19 ++++++ 6 files changed, 135 insertions(+), 28 deletions(-) create mode 100644 TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.sln create mode 100644 TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.xproj create mode 100644 TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/Program.cs create mode 100644 TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/project.json diff --git a/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.sln b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.sln new file mode 100644 index 000000000..5fe171b9d --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FolderHasDifferentName", "FolderHasDifferentName.xproj", "{0138CB8F-4AA9-4029-A21E-C07C30F425BA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.xproj b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.xproj new file mode 100644 index 000000000..d18702195 --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/FolderHasDifferentName.xproj @@ -0,0 +1,18 @@ + + + + 14.0.23107 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 0138cb8f-4aa9-4029-a21e-c07c30f425ba + TestAppWithContents + ..\..\..\artifacts\obj\$(MSBuildProjectName) + ..\..\..\artifacts\ + + + 2.0 + + + diff --git a/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/Program.cs b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/Program.cs new file mode 100644 index 000000000..f6b060a91 --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/Program.cs @@ -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; + +namespace TestApp +{ + public class Program + { + public static int Main(string[] args) + { + Console.WriteLine("Hello World"); + return 0; + } + } +} diff --git a/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/project.json b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/project.json new file mode 100644 index 000000000..166d41c2b --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/PJAppWithXprojNameDifferentThanDirName/project.json @@ -0,0 +1,26 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "emitEntryPoint": true, + "preserveCompilationContext": true + }, + "dependencies": { + "Microsoft.NETCore.App": "1.0.1" + }, + "frameworks": { + "netcoreapp1.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/src/dotnet/commands/dotnet-migrate/MigrateCommand.cs b/src/dotnet/commands/dotnet-migrate/MigrateCommand.cs index 131a9c3c2..e49d7932a 100644 --- a/src/dotnet/commands/dotnet-migrate/MigrateCommand.cs +++ b/src/dotnet/commands/dotnet-migrate/MigrateCommand.cs @@ -131,6 +131,7 @@ namespace Microsoft.DotNet.Tools.Migrate } var csprojFilesToAdd = new HashSet(); + var xprojFilesToRemove = new HashSet(); var slnPathWithTrailingSlash = PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory); foreach (var report in migrationReport.ProjectMigrationReports) @@ -140,35 +141,35 @@ namespace Microsoft.DotNet.Tools.Migrate slnPathWithTrailingSlash, reportPathWithTrailingSlash); - var xprojPath = Path.Combine(relativeReportPath, report.ProjectName + ".xproj"); - var xprojProjectsReferencedBySolution = slnFile.Projects.Where(p => p.FilePath == xprojPath); - var migratedProjectName = report.ProjectName + ".csproj"; - if (xprojProjectsReferencedBySolution.Count() == 1) - { - var slnProject = xprojProjectsReferencedBySolution.Single(); - slnProject.FilePath = Path.Combine( - Path.GetDirectoryName(slnProject.FilePath), - migratedProjectName); - slnProject.TypeGuid = ProjectTypeGuids.CSharpProjectTypeGuid; - } - else - { - var csprojPath = Path.Combine(relativeReportPath, migratedProjectName); - var solutionContainsCsprojPriorToMigration = slnFile.Projects - .Where(p => p.FilePath == csprojPath) - .Any(); + var csprojPath = Path.Combine(relativeReportPath, migratedProjectName); + var solutionContainsCsprojPriorToMigration = slnFile.Projects + .Where(p => p.FilePath == csprojPath) + .Any(); - if (!solutionContainsCsprojPriorToMigration) - { - csprojFilesToAdd.Add(Path.Combine(report.ProjectDirectory, migratedProjectName)); - } + if (!solutionContainsCsprojPriorToMigration) + { + csprojFilesToAdd.Add(Path.Combine(report.ProjectDirectory, migratedProjectName)); } foreach (var preExisting in report.PreExistingCsprojDependencies) { csprojFilesToAdd.Add(Path.Combine(report.ProjectDirectory, preExisting)); } + + var projectDirectory = new DirectoryInfo(report.ProjectDirectory); + foreach (var xprojFileName in projectDirectory.EnumerateFiles("*.xproj")) + { + var xprojPath = Path.Combine(relativeReportPath, xprojFileName.Name); + var solutionContainsXprojFileToRemove = slnFile.Projects + .Where(p => p.FilePath == xprojPath) + .Any(); + + if (solutionContainsXprojFileToRemove) + { + xprojFilesToRemove.Add(Path.Combine(report.ProjectDirectory, xprojFileName.Name)); + } + } } Version version; @@ -185,7 +186,12 @@ namespace Microsoft.DotNet.Tools.Migrate foreach (var csprojFile in csprojFilesToAdd) { - AddProject(slnFile.FullPath, csprojFile); + RunDotnetSlnCommand(slnFile.FullPath, csprojFile, "add"); + } + + foreach (var xprojFile in xprojFilesToRemove) + { + RunDotnetSlnCommand(slnFile.FullPath, xprojFile, "remove"); } } @@ -209,19 +215,19 @@ namespace Microsoft.DotNet.Tools.Migrate slnFile.RemoveEmptySolutionFolders(); } - private void AddProject(string slnPath, string csprojPath) + private void RunDotnetSlnCommand(string slnPath, string projPath, string commandName) { - List args = new List() + var args = new List() { "sln", slnPath, - "add", - csprojPath, + commandName, + projPath, }; var dotnetPath = Path.Combine(AppContext.BaseDirectory, "dotnet.dll"); - var addCommand = new ForwardingApp(dotnetPath, args); - addCommand.Execute(); + var command = new ForwardingApp(dotnetPath, args); + command.Execute(); } private void MoveProjectJsonArtifactsToBackup(MigrationReport migrationReport) diff --git a/test/dotnet-migrate.Tests/GivenThatIWantToMigrateSolutions.cs b/test/dotnet-migrate.Tests/GivenThatIWantToMigrateSolutions.cs index 30387fecc..b3c679a86 100644 --- a/test/dotnet-migrate.Tests/GivenThatIWantToMigrateSolutions.cs +++ b/test/dotnet-migrate.Tests/GivenThatIWantToMigrateSolutions.cs @@ -235,6 +235,25 @@ namespace Microsoft.DotNet.Migration.Tests .Should().Pass(); } + [Fact] + public void WhenXprojNameIsDifferentThanDirNameItGetsRemovedFromSln() + { + var projectDirectory = TestAssets + .Get("NonRestoredTestProjects", "PJAppWithXprojNameDifferentThanDirName") + .CreateInstance() + .WithSourceFiles() + .Root; + + new DotnetCommand() + .WithWorkingDirectory(projectDirectory) + .Execute($"migrate") + .Should().Pass(); + + var slnFile = SlnFile.Read(Path.Combine(projectDirectory.FullName, "FolderHasDifferentName.sln")); + slnFile.Projects.Count.Should().Be(1); + slnFile.Projects[0].FilePath.Should().Be("PJAppWithXprojNameDifferentThanDirName.csproj"); + } + private void MigrateAndBuild(string groupName, string projectName, [CallerMemberName] string callingMethod = "", string identifier = "") { var projectDirectory = TestAssets