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
This commit is contained in:
Justin Goshi 2017-03-09 12:17:18 -10:00 committed by GitHub
parent 3a58cac351
commit c35dbd9563
6 changed files with 135 additions and 28 deletions

View file

@ -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

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.23107" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.23107</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>0138cb8f-4aa9-4029-a21e-c07c30f425ba</ProjectGuid>
<RootNamespace>TestAppWithContents</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\..\artifacts\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -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;
}
}
}

View file

@ -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": {}
}
}

View file

@ -131,6 +131,7 @@ namespace Microsoft.DotNet.Tools.Migrate
}
var csprojFilesToAdd = new HashSet<string>();
var xprojFilesToRemove = new HashSet<string>();
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<string> args = new List<string>()
var args = new List<string>()
{
"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)

View file

@ -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