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 #262 from jmarolf/feature/discover-supported-tfms
Browse files Browse the repository at this point in the history
Programatically determine highest tfm
  • Loading branch information
jmarolf authored Mar 24, 2020
2 parents 2641e96 + ba22657 commit 9ab68fa
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 20 deletions.
7 changes: 4 additions & 3 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
<PropertyGroup>
<MicrosoftBuildLocatorVersion>1.2.6</MicrosoftBuildLocatorVersion>
<MicrosoftBuildVersion>16.0.461</MicrosoftBuildVersion>
<MicrosoftWin32RegistryVersion>4.6.0</MicrosoftWin32RegistryVersion>
<MicrosoftWin32RegistryVersion>4.7.0</MicrosoftWin32RegistryVersion>
<NewtonsoftJsonVersion>12.0.2</NewtonsoftJsonVersion>
<NuGetVersioningVersion>5.5.0</NuGetVersioningVersion>
<SystemCollectionsImmutableVersion>1.5.0</SystemCollectionsImmutableVersion>
<SystemCommandLineExperimentalVersion>0.2.0-alpha.19154.3</SystemCommandLineExperimentalVersion>
<SystemConfigurationConfigurationManagerVersion>4.6.0</SystemConfigurationConfigurationManagerVersion>
<SystemSecurityPrincipalWindowsVersion>4.6.0</SystemSecurityPrincipalWindowsVersion>
<SystemConfigurationConfigurationManagerVersion>4.7.0</SystemConfigurationConfigurationManagerVersion>
<SystemSecurityPrincipalWindowsVersion>4.7.0</SystemSecurityPrincipalWindowsVersion>
</PropertyGroup>
<!--
Nuget package versions for tests
Expand Down
6 changes: 5 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"tools": {
"dotnet": "3.1.101",
"dotnet": "3.1.200",
"runtimes": {
"dotnet": [
"2.1.11"
]
}
},
"sdk": {
"version": "3.1.200",
"rollForward": "major"
},
"msbuild-sdks": {
"Microsoft.DotNet.Arcade.Sdk": "5.0.0-beta.20171.1",
"Microsoft.DotNet.Helix.Sdk": "5.0.0-beta.20171.1"
Expand Down
2 changes: 1 addition & 1 deletion src/MSBuild.Abstractions/MSBuild.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/MSBuild.Conversion.Facts/MSBuildFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ public static class MSBuildFacts
public const string DefaultSDKAttribute = "Microsoft.NET.Sdk";
public const string LowestFrameworkVersionWithSystemValueTuple = "net47";
public const string SharedProjectsImportLabel = "Shared";
public const string NetCoreAppTFM = "netcoreapp3.1";
public const string SystemValueTupleName = "System.ValueTuple";
public const string DefineConstantsName = "DefineConstants";
public const string OutputPathName = "OutputPath";
Expand Down
8 changes: 4 additions & 4 deletions src/MSBuild.Conversion.Project/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public Converter(UnconfiguredProject project, BaselineProject sdkBaselineProject
_differs = GetDiffers();
}

public void Convert(string outputPath)
public void Convert(string defaultTFM, string outputPath)
{
ConvertProjectFile();
ConvertProjectFile(defaultTFM);
var projectXml = _projectRootElement.Xml;

// remove all use of xmlns attributes
Expand Down Expand Up @@ -64,13 +64,13 @@ public void Convert(string outputPath)
projectXml.Save(writer);
}

internal IProjectRootElement ConvertProjectFile()
internal IProjectRootElement ConvertProjectFile(string defaultTFM)
{
return _projectRootElement
.ChangeImports(_sdkBaselineProject)
.RemoveDefaultedProperties(_sdkBaselineProject, _differs)
.RemoveUnnecessaryPropertiesNotInSDKByDefault(_sdkBaselineProject.ProjectStyle)
.AddTargetFrameworkProperty(_sdkBaselineProject, out var tfm)
.AddTargetFrameworkProperty(_sdkBaselineProject, defaultTFM, out var tfm)
.AddGenerateAssemblyInfoAsFalse()
.AddDesktopProperties(_sdkBaselineProject)
.AddCommonPropertiesToTopLevelPropertyGroup()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public static IProjectRootElement ModifyProjectElement(this IProjectRootElement
return projectRootElement;
}

public static IProjectRootElement AddTargetFrameworkProperty(this IProjectRootElement projectRootElement, BaselineProject baselineProject, out string targetFrameworkMoniker)
public static IProjectRootElement AddTargetFrameworkProperty(this IProjectRootElement projectRootElement, BaselineProject baselineProject, string defaultTFM, out string targetFrameworkMoniker)
{
static string StripDecimals(string tfm)
{
Expand All @@ -470,7 +470,7 @@ static string StripDecimals(string tfm)

if (baselineProject.ProjectStyle == ProjectStyle.WindowsDesktop || baselineProject.ProjectStyle == ProjectStyle.MSTest)
{
targetFrameworkElement.Value = MSBuildFacts.NetCoreAppTFM;
targetFrameworkElement.Value = defaultTFM;
}
else
{
Expand Down
12 changes: 12 additions & 0 deletions src/MSBuild.Conversion.SDK/MSBuild.Conversion.SDK.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="$(MicrosoftBuildLocatorVersion)" />
<PackageReference Include="NuGet.Versioning" Version="$(NuGetVersioningVersion)" />
</ItemGroup>

</Project>
86 changes: 86 additions & 0 deletions src/MSBuild.Conversion.SDK/TargetFrameworkHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;

using Microsoft.Build.Locator;

using NuGet.Versioning;

namespace MSBuild.Conversion.SDK
{
public static class TargetFrameworkHelper
{
/// <summary>
/// Determine the TFM to use based on what is installed on the users machine
/// </summary>
public static string FindHighestInstalledTargetFramework(bool usePreviewSDK)
{
// Finds SDK path
string sdkPath = null;
try
{
sdkPath = Path.GetFullPath(Path.Combine(MSBuildLocator.QueryVisualStudioInstances().Single().VisualStudioRootPath, "..", ".."));
}
catch (Exception)
{
Console.WriteLine("Unable to find the .NET SDK on this machine, manually pass '-tfm'");
throw;
}

try
{
// Find templates path
var templatesPath = Path.Combine(sdkPath, "templates");

// Find highest SDK path (should include previews?)
var largestVersion = NuGetVersion.Parse("0.0.0.0");
var templatePath = string.Empty;
foreach (var templateDirectory in Directory.EnumerateDirectories(templatesPath))
{
if (NuGetVersion.TryParse(Path.GetFileName(templateDirectory), out var templatesVersion) &&
templatesVersion > largestVersion)
{
if (usePreviewSDK)
{
largestVersion = templatesVersion;
templatePath = Path.GetFullPath(templateDirectory);
}
else if (!templatesVersion.IsPrerelease)
{
largestVersion = templatesVersion;
templatePath = Path.GetFullPath(templateDirectory);
}
}
}

// upzip the common project templates into memory
var templateNugetPackagePath = Directory.EnumerateFiles(templatePath, "microsoft.dotnet.common.projecttemplates.*.nupkg", SearchOption.TopDirectoryOnly).Single();
using var templateNugetPackageFile = File.OpenRead(templateNugetPackagePath);
using var templateNugetPackage = new ZipArchive(templateNugetPackageFile, ZipArchiveMode.Read);
var templatesJsonFile = templateNugetPackage.Entries
.Where(x => x.Name.Equals("template.json", StringComparison.OrdinalIgnoreCase) &&
x.FullName.Contains("ClassLibrary-CSharp", StringComparison.OrdinalIgnoreCase)).Single();
using var templatesJson = templatesJsonFile.Open();

// read the template.json file to see what the tfm is called
var doc = JsonDocument.ParseAsync(templatesJson).GetAwaiter().GetResult();

return doc.RootElement.GetProperty("baselines").GetProperty("app").GetProperty("defaultOverrides").GetProperty("Framework").GetString();
}
catch (Exception)
{
return "netcoreapp3.1";
}

}

/// <summary>
/// Regect obviously wrong TFM specifiers
/// </summary>
public static bool IsValidTargetFramework(string tfm)
=> !tfm.Contains("-") && !tfm.Contains(" ") && tfm.Contains("net") && Regex.Match(tfm, "[0-9]").Success;
}
}
22 changes: 20 additions & 2 deletions src/try-convert/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using MSBuild.Abstractions;
using MSBuild.Conversion.Project;
using MSBuild.Conversion.SDK;

namespace MSBuild.Conversion
{
Expand All @@ -25,21 +26,24 @@ private static async Task<int> Main(string[] args)
.AddOption(new Option(new[] { "-p", "--project" }, "The path to a project to convert", new Argument<string>()))
.AddOption(new Option(new[] { "-w", "--workspace" }, "The solution or project file to operate on. If a project is not specified, the command will search the current directory for one.", new Argument<string>()))
.AddOption(new Option(new[] { "-m", "--msbuild-path" }, "The path to an MSBuild.exe, if you prefer to use that", new Argument<string>()))
.AddOption(new Option(new[] { "-tfm", "--target-framework" }, "The name of the framework you would like to upgrade to", new Argument<string>()))
.AddOption(new Option(new[] { "--preview" }, "Use preview SDKs as part of conversion", new Argument<string>()))
.AddOption(new Option(new[] { "--diff-only" }, "Produces a diff of the project to convert; no conversion is done", new Argument<bool>()))
.AddOption(new Option(new[] { "--no-backup" }, "Converts projects and does not create a backup of the originals.", new Argument<bool>()))
.Build();

return await parser.InvokeAsync(args.Length > 0 ? args : new string[] { "-h" }).ConfigureAwait(false);
}

public static int Run(string project, string workspace, string msbuildPath, bool diffOnly, bool noBackup)
public static int Run(string project, string workspace, string msbuildPath, string tfm, bool allowPreviews, bool diffOnly, bool noBackup)
{
if (!string.IsNullOrWhiteSpace(project) && !string.IsNullOrWhiteSpace(workspace))
{
Console.WriteLine("Cannot specify both a project and a workspace.");
return -1;
}


try
{
msbuildPath = MSBuildHelpers.HookAssemblyResolveForMSBuild(msbuildPath);
Expand All @@ -49,6 +53,20 @@ public static int Run(string project, string workspace, string msbuildPath, bool
return -1;
}

if (tfm is null)
{
tfm = TargetFrameworkHelper.FindHighestInstalledTargetFramework(allowPreviews);
}
else
{
tfm = tfm.Trim();
if(!TargetFrameworkHelper.IsValidTargetFramework(tfm))
{
Console.WriteLine($"Invalid framework specified for --target-framework: '{tfm}'");
return -1;
}
}

var currentDirectory = Environment.CurrentDirectory;
var workspacePath = string.Empty;
MSBuildWorkspaceType workspaceType;
Expand Down Expand Up @@ -82,7 +100,7 @@ public static int Run(string project, string workspace, string msbuildPath, bool
else
{
var converter = new Converter(item.UnconfiguredProject, item.SdkBaselineProject, item.ProjectRootElement);
converter.Convert(item.ProjectRootElement.FullPath);
converter.Convert(tfm, item.ProjectRootElement.FullPath);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/try-convert/try-convert.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ServerGarbageCollection>true</ServerGarbageCollection>
<AutoGenerateAssemblyVersion>true</AutoGenerateAssemblyVersion>

Expand Down Expand Up @@ -31,6 +31,7 @@
<ItemGroup>
<ProjectReference Include="..\MSBuild.Conversion.Project\MSBuild.Conversion.Project.csproj" />
<ProjectReference Include="..\MSBuild.Abstractions\MSBuild.Abstractions.csproj" />
<ProjectReference Include="..\MSBuild.Conversion.SDK\MSBuild.Conversion.SDK.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion tests/end-to-end/Smoke.Tests/BasicConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static (IProjectRootElement baselineRootElement, IProjectRootElement con

var item = conversionWorkspace.WorkspaceItems.Single();
var converter = new Converter(item.UnconfiguredProject, item.SdkBaselineProject, item.ProjectRootElement);
var convertedRootElement = converter.ConvertProjectFile();
var convertedRootElement = converter.ConvertProjectFile("netcoreapp3.1");

return (baselineRootElement, convertedRootElement);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/Smoke.Tests/Smoke.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
15 changes: 15 additions & 0 deletions try-convert.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PackageConversion.Tests", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Smoke.Tests", "tests\end-to-end\Smoke.Tests\Smoke.Tests.csproj", "{26176B40-732E-425C-A5F8-70B738C4778A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MSBuild.Conversion.SDK", "src\MSBuild.Conversion.SDK\MSBuild.Conversion.SDK.csproj", "{B56755BA-7992-4D2C-98E4-86F1C75A19B2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -132,6 +134,18 @@ Global
{26176B40-732E-425C-A5F8-70B738C4778A}.Release|x64.Build.0 = Release|Any CPU
{26176B40-732E-425C-A5F8-70B738C4778A}.Release|x86.ActiveCfg = Release|Any CPU
{26176B40-732E-425C-A5F8-70B738C4778A}.Release|x86.Build.0 = Release|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Debug|x64.ActiveCfg = Debug|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Debug|x64.Build.0 = Debug|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Debug|x86.ActiveCfg = Debug|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Debug|x86.Build.0 = Debug|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Release|Any CPU.Build.0 = Release|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Release|x64.ActiveCfg = Release|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Release|x64.Build.0 = Release|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Release|x86.ActiveCfg = Release|Any CPU
{B56755BA-7992-4D2C-98E4-86F1C75A19B2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{861A048E-B8C9-4079-A1D2-4851DA080648} = {FEF77564-1629-42D5-9183-18EF3C31AFE5}
Expand All @@ -142,5 +156,6 @@ Global
{30EE0DE2-7BED-4868-AE20-4BD27553380B} = {8F861E7D-283E-46F9-9A6A-DBF1BE8BC978}
{A3DDB4B7-F48E-40FA-A638-B03EECB48255} = {8F861E7D-283E-46F9-9A6A-DBF1BE8BC978}
{26176B40-732E-425C-A5F8-70B738C4778A} = {8F861E7D-283E-46F9-9A6A-DBF1BE8BC978}
{B56755BA-7992-4D2C-98E4-86F1C75A19B2} = {FEF77564-1629-42D5-9183-18EF3C31AFE5}
EndGlobalSection
EndGlobal

0 comments on commit 9ab68fa

Please sign in to comment.