Skip to content

Commit 8f24e45

Browse files
authored
dotnet-svcutil: resolve targetFramework from Directory.Build.props (#5428)
1 parent 21d9e40 commit 8f24e45

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/dotnet-svcutil/lib/src/Shared/FrameworkInfo.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static FrameworkInfo Parse(string fullFrameworkName)
3939
// framework spec form: 'netcore1.5' or 'net452'
4040
// framework spec form: 'net5.0'
4141
// framework spec form: '.NETCoreApp,Version=v6.0'
42+
// framework spec form: '.NETFramework,Version=v4.8'
4243
for (int i = 0; i < fullFrameworkName.Length; i++)
4344
{
4445
char c = fullFrameworkName[i];
@@ -66,13 +67,14 @@ public static FrameworkInfo Parse(string fullFrameworkName)
6667

6768
if (name.ToLower().Contains(Netversion))
6869
{
69-
if (version.Major >= 5)
70+
//netcoreapp3.1 and lower
71+
if (version.Major < 4)
7072
{
71-
name = Netfx;
73+
name = Netcoreapp;
7274
}
7375
else
7476
{
75-
name = Netcoreapp;
77+
name = Netfx;
7678
}
7779

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

src/dotnet-svcutil/lib/src/Shared/MSBuildProj.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.Tools.ServiceModel.Svcutil
1717
{
1818
internal class MSBuildProj : IDisposable
1919
{
20+
private const string DirBuildProps = "Directory.Build.props";
2021
private bool _isSaved;
2122
private bool _ownsDirectory;
2223
private readonly ProjectPropertyResolver _propertyResolver;
@@ -198,10 +199,15 @@ public static async Task<MSBuildProj> ParseAsync(string projectText, string proj
198199
if (targetFrameworkElements.Count() > 0)
199200
{
200201
// If property is specified more than once, MSBuild will resolve it by overwriting it with the last value.
201-
var targetFramework = targetFrameworkElements.Last().Value.Trim().ToLowerInvariant();
202+
var targetFramework = targetFrameworkElements.Last().Value.Trim();
202203
if (!string.IsNullOrWhiteSpace(targetFramework))
203204
{
204-
if(TargetFrameworkHelper.IsSupportedFramework(targetFramework, out FrameworkInfo fxInfo))
205+
if (targetFramework.ToString().StartsWith("$"))
206+
{
207+
targetFramework = GetValueFromDirBuildProps(targetFramework, msbuildProj.DirectoryPath);
208+
}
209+
210+
if (TargetFrameworkHelper.IsSupportedFramework(targetFramework, out FrameworkInfo fxInfo))
205211
{
206212
msbuildProj._targetFrameworks.Add(targetFramework);
207213
}
@@ -215,6 +221,11 @@ public static async Task<MSBuildProj> ParseAsync(string projectText, string proj
215221
if (targetFrameworksElements.Count() > 0)
216222
{
217223
var targetFrameworks = targetFrameworksElements.Last().Value;
224+
if (targetFrameworks.ToString().StartsWith("$"))
225+
{
226+
targetFrameworks = GetValueFromDirBuildProps(targetFrameworks, msbuildProj.DirectoryPath);
227+
}
228+
218229
foreach (var targetFx in targetFrameworks.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()))
219230
{
220231
if (!string.IsNullOrWhiteSpace(targetFx))
@@ -415,6 +426,27 @@ public static async Task<MSBuildProj> DotNetNewAsync(string fullPath, ILogger lo
415426
return project;
416427
}
417428

429+
private static string GetValueFromDirBuildProps(string elementStr, string dirPath)
430+
{
431+
try
432+
{
433+
//elementStr format: $(ElementName)
434+
elementStr = elementStr.Substring(2).TrimEnd(')');
435+
string filePath = Path.Combine(dirPath, DirBuildProps);
436+
XDocument doc = XDocument.Load(filePath);
437+
var ele = doc.Root?.Descendants(elementStr).FirstOrDefault();
438+
if (ele != null)
439+
{
440+
return ele.Value;
441+
}
442+
}
443+
catch
444+
{
445+
}
446+
447+
return "";
448+
}
449+
418450
private static IEnumerable<XElement> GetGroupValues(XElement projectElement, string group, bool createOnMissing = false)
419451
{
420452
// XElement.Elements() always returns a collection, no need to check for null.

0 commit comments

Comments
 (0)