Skip to content

Commit

Permalink
dotnet-svcutil: resolve targetFramework from Directory.Build.props (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
imcarolwang authored Mar 21, 2024
1 parent 21d9e40 commit 8f24e45
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/dotnet-svcutil/lib/src/Shared/FrameworkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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'
// framework spec form: '.NETFramework,Version=v4.8'
for (int i = 0; i < fullFrameworkName.Length; i++)
{
char c = fullFrameworkName[i];
Expand Down Expand Up @@ -66,13 +67,14 @@ public static FrameworkInfo Parse(string fullFrameworkName)

if (name.ToLower().Contains(Netversion))
{
if (version.Major >= 5)
//netcoreapp3.1 and lower
if (version.Major < 4)
{
name = Netfx;
name = Netcoreapp;
}
else
{
name = Netcoreapp;
name = Netfx;
}

fullFrameworkName = string.Concat(name, version.ToString());
Expand Down
36 changes: 34 additions & 2 deletions src/dotnet-svcutil/lib/src/Shared/MSBuildProj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Microsoft.Tools.ServiceModel.Svcutil
{
internal class MSBuildProj : IDisposable
{
private const string DirBuildProps = "Directory.Build.props";
private bool _isSaved;
private bool _ownsDirectory;
private readonly ProjectPropertyResolver _propertyResolver;
Expand Down Expand Up @@ -198,10 +199,15 @@ public static async Task<MSBuildProj> ParseAsync(string projectText, string proj
if (targetFrameworkElements.Count() > 0)
{
// If property is specified more than once, MSBuild will resolve it by overwriting it with the last value.
var targetFramework = targetFrameworkElements.Last().Value.Trim().ToLowerInvariant();
var targetFramework = targetFrameworkElements.Last().Value.Trim();
if (!string.IsNullOrWhiteSpace(targetFramework))
{
if(TargetFrameworkHelper.IsSupportedFramework(targetFramework, out FrameworkInfo fxInfo))
if (targetFramework.ToString().StartsWith("$"))
{
targetFramework = GetValueFromDirBuildProps(targetFramework, msbuildProj.DirectoryPath);
}

if (TargetFrameworkHelper.IsSupportedFramework(targetFramework, out FrameworkInfo fxInfo))
{
msbuildProj._targetFrameworks.Add(targetFramework);
}
Expand All @@ -215,6 +221,11 @@ public static async Task<MSBuildProj> ParseAsync(string projectText, string proj
if (targetFrameworksElements.Count() > 0)
{
var targetFrameworks = targetFrameworksElements.Last().Value;
if (targetFrameworks.ToString().StartsWith("$"))
{
targetFrameworks = GetValueFromDirBuildProps(targetFrameworks, msbuildProj.DirectoryPath);
}

foreach (var targetFx in targetFrameworks.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()))
{
if (!string.IsNullOrWhiteSpace(targetFx))
Expand Down Expand Up @@ -415,6 +426,27 @@ public static async Task<MSBuildProj> DotNetNewAsync(string fullPath, ILogger lo
return project;
}

private static string GetValueFromDirBuildProps(string elementStr, string dirPath)
{
try
{
//elementStr format: $(ElementName)
elementStr = elementStr.Substring(2).TrimEnd(')');
string filePath = Path.Combine(dirPath, DirBuildProps);
XDocument doc = XDocument.Load(filePath);
var ele = doc.Root?.Descendants(elementStr).FirstOrDefault();
if (ele != null)
{
return ele.Value;
}
}
catch
{
}

return "";
}

private static IEnumerable<XElement> GetGroupValues(XElement projectElement, string group, bool createOnMissing = false)
{
// XElement.Elements() always returns a collection, no need to check for null.
Expand Down

0 comments on commit 8f24e45

Please sign in to comment.