From 61f2fc354ef8e8bf808238805acb33c5edc0bbe3 Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Mon, 9 Dec 2019 15:17:47 -0800 Subject: [PATCH] use a switch statement to ensure we don't miss any supported cases fixes #156 --- src/MSBuild.Abstractions/MSBuildWorkspace.cs | 114 +++++++++++-------- 1 file changed, 68 insertions(+), 46 deletions(-) diff --git a/src/MSBuild.Abstractions/MSBuildWorkspace.cs b/src/MSBuild.Abstractions/MSBuildWorkspace.cs index 50680fa3c..1166b7ac7 100644 --- a/src/MSBuild.Abstractions/MSBuildWorkspace.cs +++ b/src/MSBuild.Abstractions/MSBuildWorkspace.cs @@ -208,7 +208,7 @@ private ProjectStyle GetProjectStyle(IProjectRootElement projectRootElement) // It's something else, no idea what though return ProjectStyle.Custom; } - } + } } } @@ -229,79 +229,101 @@ private bool IsSupportedProjectType(MSBuildProjectRootElement root) // Lots of wild old project types have project type guids that the old project system uses to light things up! if (MSBuildHelpers.HasProjectTypeGuidsNode(root)) { - // Check if it's an acceptable desktop type and reject those that aren't - if (MSBuildHelpers.IsDesktop(root)) + var projectType = GetProjectSupportType(root); + switch (projectType) { - if (!root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.AllProjectTypeGuidsAreDesktopProjectTypeGuids))) - { + case ProjectSupportType.LegacyWeb: + Console.WriteLine($"'{root.FullPath}' is a legacy web project, which is unsupported by this tool."); + return false; + case ProjectSupportType.CodedUITest: + Console.WriteLine($"'{root.FullPath}' is a coded UI test. Coded UI tests are deprecated and not convertable to .NET Core."); + return false; + case ProjectSupportType.UnknownTestProject: + Console.WriteLine($"'{root.FullPath}' has invalid Project Type Guids for test projects and is not supported."); + return false; + case ProjectSupportType.UnsupportedTestType: + Console.WriteLine($"'{root.FullPath}' is an unsupported MSTest test type. Only Unit Tests are supported."); + return false; + case ProjectSupportType.Desktop: + case ProjectSupportType.MSTest: + // Let them know about System.Web while we're here + if (MSBuildHelpers.IsProjectReferencingSystemWeb(root)) + { + Console.WriteLine($"'{root.FullPath}' references System.Web, which is unsupported on .NET Core. You may have significant work remaining after conversion."); + } + return true; + case ProjectSupportType.Unsupported: + default: var allSupportedProjectTypeGuids = DesktopFacts.KnownSupportedDesktopProjectTypeGuids.Select(ptg => ptg.ToString()); var allReadProjectTypeGuids = MSBuildHelpers.GetAllProjectTypeGuids(root); - Console.WriteLine($"{root.FullPath} is an unsupported project type. Not all project type guids are supported."); - PrintGuidMessage(allSupportedProjectTypeGuids, allReadProjectTypeGuids); - return false; - } } - // Reject if it's a web project - else if (root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.IsLegacyWebProjectTypeGuidsProperty))) + } + + // assume its supported + return true; + + static void PrintGuidMessage(IEnumerable allSupportedProjectTypeGuids, IEnumerable allReadProjectTypeGuids) + { + Console.WriteLine("All supported project type guids:"); + foreach (var guid in allSupportedProjectTypeGuids) + { + Console.WriteLine($"\t{guid}"); + } + + Console.WriteLine("All given project type guids:"); + foreach (var guid in allReadProjectTypeGuids) + { + Console.WriteLine($"\t{guid}"); + } + } + + static ProjectSupportType GetProjectSupportType(MSBuildProjectRootElement root) + { + if (root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.IsLegacyWebProjectTypeGuidsProperty))) { - Console.WriteLine($"'{root.FullPath}' is a legacy web project, which is unsupported by this tool."); - return false; + return ProjectSupportType.LegacyWeb; } - // Check if it's a valid MSTest project type. else if (MSBuildHelpers.IsNETFrameworkMSTestProject(root)) { if (!root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.AllProjectTypeGuidsAreLegacyTestProjectTypeGuids))) { - Console.WriteLine($"'{root.FullPath}' has invalid Project Type Guids for test projects and is not supported."); - return false; + return ProjectSupportType.UnknownTestProject; } if (root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.IsCodedUITest))) { - Console.WriteLine($"'{root.FullPath}' is a coded UI test. Coded UI tests are deprecated and not convertable to .NET Core."); - return false; + return ProjectSupportType.CodedUITest; } if (!root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.IsUnitTestType))) { - Console.WriteLine($"'{root.FullPath}' is an unsupported MSTest test type. Only Unit Tests are supported."); - return false; + return ProjectSupportType.UnsupportedTestType; } + + return ProjectSupportType.MSTest; } - // If it still has project type guids, bail if it's anything other than a language GUID because it probably wouldn't work on .NET Core - else if (!root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.AllProjectTypeGuidsAreLanguageProjectTypeGuids))) + if (MSBuildHelpers.IsDesktop(root) && + !root.PropertyGroups.Any(pg => pg.Properties.Any(ProjectPropertyHelpers.AllProjectTypeGuidsAreDesktopProjectTypeGuids))) { - Console.WriteLine($"{root.FullPath} is an unsupported project type."); - return false; + return ProjectSupportType.Unsupported; } - } - // Let them know about System.Web while we're here - if (MSBuildHelpers.IsProjectReferencingSystemWeb(root)) - { - Console.WriteLine($"'{root.FullPath}' references System.Web, which is unsupported on .NET Core. You may have significant work remaining after conversion."); + return ProjectSupportType.Desktop; } + } - // It's supported - return true; - - static void PrintGuidMessage(IEnumerable allSupportedProjectTypeGuids, IEnumerable allReadProjectTypeGuids) - { - Console.WriteLine("All supported project type guids:"); - foreach (var guid in allSupportedProjectTypeGuids) - { - Console.WriteLine($"\t{guid}"); - } - - Console.WriteLine("All given project type guids:"); - foreach (var guid in allReadProjectTypeGuids) - { - Console.WriteLine($"\t{guid}"); - } - } + private enum ProjectSupportType + { + Desktop, + LegacyWeb, + MSTest, + Unsupported, + CodedUITest, + UnsupportedTestType, + UnknownTestProject, } } }