diff --git a/src/MSBuild.Conversion.SDK/TargetFrameworkHelper.cs b/src/MSBuild.Conversion.SDK/TargetFrameworkHelper.cs index cba9ba597..701b29171 100644 --- a/src/MSBuild.Conversion.SDK/TargetFrameworkHelper.cs +++ b/src/MSBuild.Conversion.SDK/TargetFrameworkHelper.cs @@ -26,43 +26,51 @@ public static string FindHighestInstalledTargetFramework(bool usePreviewSDK) throw; } - // 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)) + try { - if (NuGetVersion.TryParse(Path.GetFileName(templateDirectory), out var templatesVersion) && - templatesVersion > largestVersion) + // 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 (usePreviewSDK) + if (NuGetVersion.TryParse(Path.GetFileName(templateDirectory), out var templatesVersion) && + templatesVersion > largestVersion) { - largestVersion = templatesVersion; - templatePath = Path.GetFullPath(templateDirectory); - } - else if (!templatesVersion.IsPrerelease) - { - largestVersion = templatesVersion; - templatePath = Path.GetFullPath(templateDirectory); + 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(); + // 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(); + // 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"; + } - return doc.RootElement.GetProperty("baselines").GetProperty("app").GetProperty("defaultOverrides").GetProperty("Framework").GetString(); } } } diff --git a/src/try-convert/Program.cs b/src/try-convert/Program.cs index dbfb429f0..def1b2397 100644 --- a/src/try-convert/Program.cs +++ b/src/try-convert/Program.cs @@ -56,10 +56,6 @@ public static int Run(string project, string workspace, string msbuildPath, stri if (tfm is null) { tfm = TargetFrameworkHelper.FindHighestInstalledTargetFramework(allowPreviews); - if (tfm is null) - { - tfm = "netcoreapp3.1"; - } } var currentDirectory = Environment.CurrentDirectory;