Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dotnet-svcutil error when project targetframework is defined in D… #4893

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/dotnet-svcutil/lib/src/Shared/FrameworkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class FrameworkInfo
public const string Netstandard = "netstandard";
public const string Netcoreapp = "netcoreapp";
public const string Netfx = "net";
public const string Netversion = "version";

private FrameworkInfo()
{
Expand All @@ -37,6 +38,7 @@ public static FrameworkInfo Parse(string fullFrameworkName)

// framework spec form: 'netcore1.5' or 'net452'
// framework spec form: 'net5.0'
// framework spec form: '.NETCoreApp,Version=v6.0'
for (int i = 0; i < fullFrameworkName.Length; i++)
{
char c = fullFrameworkName[i];
Expand All @@ -62,6 +64,20 @@ public static FrameworkInfo Parse(string fullFrameworkName)
}
}

if (name.ToLower().Contains(Netversion))
{
if (version.Major >= 5)
{
name = Netfx;
}
else
{
name = Netcoreapp;
}

fullFrameworkName = string.Concat(name, version.ToString());
}

if (version == null || name == null)
{
throw new FormatException(fullFrameworkName);
Expand Down
29 changes: 22 additions & 7 deletions src/dotnet-svcutil/lib/src/Shared/MSBuildProj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ public static async Task<MSBuildProj> FromPathAsync(string filePath, ILogger log
return project;
}

public static async Task<MSBuildProj> ParseAsync(string projectText, string projectFullPath, ILogger logger, CancellationToken cancellationToken)
internal static async Task<MSBuildProj> FromPathAsync(string filePath, ILogger logger, string tfMonitor, CancellationToken cancellationToken)
imcarolwang marked this conversation as resolved.
Show resolved Hide resolved
{
var project = await ParseAsync(File.ReadAllText(filePath), filePath, logger, cancellationToken, tfMonitor).ConfigureAwait(false);
project._isSaved = true;
return project;
}

public static async Task<MSBuildProj> ParseAsync(string projectText, string projectFullPath, ILogger logger, CancellationToken cancellationToken, string tfMonitor = "")
{
using (var safeLogger = await SafeLogger.WriteStartOperationAsync(logger, $"Parsing project {Path.GetFileName(projectFullPath)}").ConfigureAwait(false))
{
Expand Down Expand Up @@ -159,12 +166,6 @@ public static async Task<MSBuildProj> ParseAsync(string projectText, string proj
var targetFramework = targetFrameworkElements.Last().Value.Trim().ToLowerInvariant();
if (!string.IsNullOrWhiteSpace(targetFramework))
{
var tfx = targetFramework.Split('-');
if (tfx.Length > 1 && (tfx[0] == "net5.0" || tfx[0] == "net6.0"))
{
targetFramework = tfx[0];
}

msbuildProj._targetFrameworks.Add(targetFramework);
}
}
Expand All @@ -188,6 +189,20 @@ public static async Task<MSBuildProj> ParseAsync(string projectText, string proj

msbuildProj._targetFramework = TargetFrameworkHelper.GetBestFitTargetFramework(msbuildProj._targetFrameworks);

if(string.IsNullOrEmpty(msbuildProj._targetFramework))
{
if(!string.IsNullOrEmpty(tfMonitor))
{
msbuildProj._targetFramework = tfMonitor;
}
else
{
msbuildProj._targetFramework = string.Concat("net", TargetFrameworkHelper.NetCoreVersionReferenceTable.LastOrDefault().Key.ToString());
}

msbuildProj._targetFrameworks.Add(msbuildProj._targetFramework);
}

// Ensure target framework is valid.
FrameworkInfo frameworkInfo = TargetFrameworkHelper.GetValidFrameworkInfo(msbuildProj.TargetFramework);

Expand Down
13 changes: 12 additions & 1 deletion src/dotnet-svcutil/lib/src/Shared/TargetFrameworkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static IEnumerable<ProjectDependency> GetWcfProjectReferences(string targ
public static string GetBestFitTargetFramework(IEnumerable<string> targetFrameworks)
{
string targetFramework = string.Empty;
FrameworkInfo fxInfo;
FrameworkInfo fxInfo = null;

if (targetFrameworks != null)
{
Expand All @@ -147,6 +147,11 @@ public static string GetBestFitTargetFramework(IEnumerable<string> targetFramewo
}
}

if (fxInfo != null)
{
return fxInfo.FullName;
}

return targetFramework;
}

Expand Down Expand Up @@ -204,6 +209,12 @@ public static bool IsSupportedFramework(string fullFrameworkName, out FrameworkI
{
bool isSupported = false;

var tfx = fullFrameworkName.Split('-');
if (tfx.Length > 1)
{
fullFrameworkName = tfx[0];
}

if (FrameworkInfo.TryParse(fullFrameworkName, out frameworkInfo))
{
isSupported = (frameworkInfo.Name == FrameworkInfo.Netstandard && frameworkInfo.Version >= MinSupportedNetStandardVersion) ||
Expand Down