Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #378 from mjrousos/mjrousos/CustomImports
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp authored Feb 22, 2021
2 parents 93294d4 + 887149e commit 2a955fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 51 deletions.
56 changes: 11 additions & 45 deletions src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,55 +281,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;
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/MSBuild.Conversion.Facts/MSBuildFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ namespace MSBuild.Conversion.Facts
public static class MSBuildFacts
{
/// <summary>
/// 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.
/// </summary>
public static ImmutableArray<string> PropsConvertibleToSDK => ImmutableArray.Create(
public static ImmutableArray<string> PropsToRemove => ImmutableArray.Create(
"Microsoft.Common.props",
"MSTest.TestAdapter.props"
"MSTest.TestAdapter.props",
"Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props",
"Microsoft.Net.Compilers.props" // https://stackoverflow.com/a/60623906
);

/// <summary>
/// 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.
/// </summary>
public static ImmutableArray<string> TargetsConvertibleToSDK => ImmutableArray.Create(
public static ImmutableArray<string> TargetsToRemove => ImmutableArray.Create(
"Microsoft.CSharp.targets",
"Microsoft.VisualBasic.targets",
"Microsoft.Portable.CSharp.targets",
Expand All @@ -31,6 +33,14 @@ public static class MSBuildFacts
"Microsoft.WebApplication.targets"
);

/// <summary>
/// Props and targets files which are recognized and can be left unchanged during conversion.
/// </summary>
public static ImmutableArray<string> ImportsToKeep => ImmutableArray.Create(
"Microsoft.TypeScript.Default.props",
"Microsoft.TypeScript.targets"
);

/// <summary>
/// Mapping of PCL profiles to netstandard versions.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.PropsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase) ||
MSBuildFacts.TargetsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase))
{
projectRootElement.RemoveChild(import);
}
else if (!MSBuildFacts.ImportsToKeep.Contains(fileName, StringComparer.OrdinalIgnoreCase))
{
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)
Expand Down

0 comments on commit 2a955fb

Please sign in to comment.