From d7ebacd646b025c7c7a991a0b7b887579c16ce9d Mon Sep 17 00:00:00 2001 From: Mike Rousos Date: Sun, 21 Feb 2021 20:11:03 -0500 Subject: [PATCH 1/3] Allow try-convert to run on projects with unrecognized imports --- .../MSBuildConversionWorkspace.cs | 56 ++++--------------- src/MSBuild.Conversion.Facts/MSBuildFacts.cs | 7 ++- 2 files changed, 15 insertions(+), 48 deletions(-) diff --git a/src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs b/src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs index 9b5211688..f1ac5b9af 100644 --- a/src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs +++ b/src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs @@ -276,55 +276,21 @@ private ProjectStyle GetProjectStyle(IProjectRootElement projectRootElement) return ProjectStyle.Custom; } - var cleansedImports = imports.Select(import => Path.GetFileName(import.Project)); - var allImportsConvertibleToSdk = - cleansedImports.All(import => - MSBuildFacts.PropsConvertibleToSDK.Contains(import, StringComparer.OrdinalIgnoreCase) || - MSBuildFacts.TargetsConvertibleToSDK.Contains(import, StringComparer.OrdinalIgnoreCase)); - - if (allImportsConvertibleToSdk) + if (MSBuildHelpers.IsNETFrameworkMSTestProject(projectRootElement)) { - if (MSBuildHelpers.IsNETFrameworkMSTestProject(projectRootElement)) - { - return ProjectStyle.MSTest; - } - else if (MSBuildHelpers.IsWPF(projectRootElement) || MSBuildHelpers.IsWinForms(projectRootElement) || MSBuildHelpers.IsDesktop(projectRootElement)) - { - return ProjectStyle.WindowsDesktop; - } - else if (MSBuildHelpers.IsWeb(projectRootElement)) - { - return ProjectStyle.Web; - } - else - { - return ProjectStyle.Default; - } + return ProjectStyle.MSTest; + } + else if (MSBuildHelpers.IsWPF(projectRootElement) || MSBuildHelpers.IsWinForms(projectRootElement) || MSBuildHelpers.IsDesktop(projectRootElement)) + { + return ProjectStyle.WindowsDesktop; + } + else if (MSBuildHelpers.IsWeb(projectRootElement)) + { + return ProjectStyle.Web; } else { - Console.WriteLine("This project has custom imports that are not accepted by try-convert."); - Console.WriteLine("Unexpected custom imports were found:"); - - var customImports = - cleansedImports.Where(import => - !(MSBuildFacts.PropsConvertibleToSDK.Contains(import, StringComparer.OrdinalIgnoreCase) || - MSBuildFacts.TargetsConvertibleToSDK.Contains(import, StringComparer.OrdinalIgnoreCase))); - - foreach (var import in customImports) - { - Console.WriteLine($"\t{import}"); - } - - Console.WriteLine("The following imports are considered valid for conversion:"); - - foreach (var import in MSBuildFacts.TargetsConvertibleToSDK.Union(MSBuildFacts.PropsConvertibleToSDK)) - { - Console.WriteLine($"\t{import}"); - } - - // It's something else, no idea what though - return ProjectStyle.Custom; + return ProjectStyle.Default; } } diff --git a/src/MSBuild.Conversion.Facts/MSBuildFacts.cs b/src/MSBuild.Conversion.Facts/MSBuildFacts.cs index c823a978e..7aba4ab30 100644 --- a/src/MSBuild.Conversion.Facts/MSBuildFacts.cs +++ b/src/MSBuild.Conversion.Facts/MSBuildFacts.cs @@ -10,15 +10,16 @@ namespace MSBuild.Conversion.Facts public static class MSBuildFacts { /// - /// Props files which are known to be imported in standard projects created from templates that can be converted to use the SDK + /// Props files which are known to be imported in standard projects created from templates that can be omitted from SDK projects. /// public static ImmutableArray PropsConvertibleToSDK => ImmutableArray.Create( "Microsoft.Common.props", - "MSTest.TestAdapter.props" + "MSTest.TestAdapter.props", + "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" ); /// - /// Targets files which are known to be imported in standard projects created from templates that can be converted to use the SDK. + /// Targets files which are known to be imported in standard projects created from templates that can be omitted from SDK projects. /// public static ImmutableArray TargetsConvertibleToSDK => ImmutableArray.Create( "Microsoft.CSharp.targets", From f74eb51272d7fbec0b292013640cfe3ec1ea3680 Mon Sep 17 00:00:00 2001 From: Mike Rousos Date: Sun, 21 Feb 2021 20:42:08 -0500 Subject: [PATCH 2/3] Only remove recognized imports --- .../ProjectRootElementExtensionsForConversion.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs b/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs index e80d9b32c..162909e5d 100644 --- a/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs +++ b/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs @@ -16,7 +16,16 @@ public static IProjectRootElement ChangeImportsAndAddSdkAttribute(this IProjectR { foreach (var import in projectRootElement.Imports) { - projectRootElement.RemoveChild(import); + var fileName = Path.GetFileName(import.Project); + if (MSBuildFacts.PropsConvertibleToSDK.Contains(fileName, StringComparer.OrdinalIgnoreCase) || + MSBuildFacts.TargetsConvertibleToSDK.Contains(fileName, StringComparer.OrdinalIgnoreCase)) + { + projectRootElement.RemoveChild(import); + } + else + { + Console.WriteLine($"This project has an unrecognized custom import which may need reviewed after conversion: {fileName}"); + } } if (baselineProject.ProjectStyle is ProjectStyle.WindowsDesktop && baselineProject.TargetTFM is MSBuildFacts.NetCoreApp31) From 7d6c6d345c7da740a92673cded78e146931743ef Mon Sep 17 00:00:00 2001 From: Mike Rousos Date: Sun, 21 Feb 2021 22:44:43 -0500 Subject: [PATCH 3/3] Update arrays of imports to remove/keep --- src/MSBuild.Conversion.Facts/MSBuildFacts.cs | 15 ++++++++++++--- .../ProjectRootElementExtensionsForConversion.cs | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/MSBuild.Conversion.Facts/MSBuildFacts.cs b/src/MSBuild.Conversion.Facts/MSBuildFacts.cs index 7aba4ab30..e57c298b4 100644 --- a/src/MSBuild.Conversion.Facts/MSBuildFacts.cs +++ b/src/MSBuild.Conversion.Facts/MSBuildFacts.cs @@ -12,16 +12,17 @@ public static class MSBuildFacts /// /// Props files which are known to be imported in standard projects created from templates that can be omitted from SDK projects. /// - public static ImmutableArray PropsConvertibleToSDK => ImmutableArray.Create( + public static ImmutableArray PropsToRemove => ImmutableArray.Create( "Microsoft.Common.props", "MSTest.TestAdapter.props", - "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" + "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props", + "Microsoft.Net.Compilers.props" // https://stackoverflow.com/a/60623906 ); /// /// Targets files which are known to be imported in standard projects created from templates that can be omitted from SDK projects. /// - public static ImmutableArray TargetsConvertibleToSDK => ImmutableArray.Create( + public static ImmutableArray TargetsToRemove => ImmutableArray.Create( "Microsoft.CSharp.targets", "Microsoft.VisualBasic.targets", "Microsoft.Portable.CSharp.targets", @@ -32,6 +33,14 @@ public static class MSBuildFacts "Microsoft.WebApplication.targets" ); + /// + /// Props and targets files which are recognized and can be left unchanged during conversion. + /// + public static ImmutableArray ImportsToKeep => ImmutableArray.Create( + "Microsoft.TypeScript.Default.props", + "Microsoft.TypeScript.targets" + ); + /// /// Mapping of PCL profiles to netstandard versions. /// diff --git a/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs b/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs index 162909e5d..771b5133e 100644 --- a/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs +++ b/src/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs @@ -17,12 +17,12 @@ public static IProjectRootElement ChangeImportsAndAddSdkAttribute(this IProjectR foreach (var import in projectRootElement.Imports) { var fileName = Path.GetFileName(import.Project); - if (MSBuildFacts.PropsConvertibleToSDK.Contains(fileName, StringComparer.OrdinalIgnoreCase) || - MSBuildFacts.TargetsConvertibleToSDK.Contains(fileName, StringComparer.OrdinalIgnoreCase)) + if (MSBuildFacts.PropsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase) || + MSBuildFacts.TargetsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase)) { projectRootElement.RemoveChild(import); } - else + else if (!MSBuildFacts.ImportsToKeep.Contains(fileName, StringComparer.OrdinalIgnoreCase)) { Console.WriteLine($"This project has an unrecognized custom import which may need reviewed after conversion: {fileName}"); }