Skip to content

Commit 87b4fca

Browse files
committed
C#: Improve auto builder logic to detect Sdk reference.
1 parent a5aef8c commit 87b4fca

File tree

1 file changed

+31
-2
lines changed
  • csharp/autobuilder/Semmle.Autobuild.Shared

1 file changed

+31
-2
lines changed

csharp/autobuilder/Semmle.Autobuild.Shared/Project.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.IO;
44
using System.Linq;
55
using System.Xml;
6-
using Semmle.Util.Logging;
76

87
namespace Semmle.Autobuild.Shared
98
{
@@ -26,6 +25,36 @@ public class Project<TAutobuildOptions> : ProjectOrSolution<TAutobuildOptions> w
2625
private readonly Lazy<List<Project<TAutobuildOptions>>> includedProjectsLazy;
2726
public override IEnumerable<IProjectOrSolution> IncludedProjects => includedProjectsLazy.Value;
2827

28+
/// <summary>
29+
/// According to https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2022#reference-a-project-sdk
30+
/// there are three ways to reference a project SDK:
31+
/// 1. As an attribute on the <Project/>.
32+
/// 2. As a top level element of <Project>.
33+
/// 3. As an attribute on an <Import> element.
34+
///
35+
/// Returns true, if the Sdk attribute is used, otherwise false.
36+
/// </summary>
37+
private static bool UsesSdk(XmlElement xml)
38+
{
39+
// Case 1. and 2.
40+
if (xml.HasAttribute("Sdk") || xml.ChildNodes.OfType<XmlElement>().Any(e => e.Name == "Sdk"))
41+
{
42+
return true;
43+
}
44+
45+
var test = xml.GetElementsByTagName("Import");
46+
// Case 3.
47+
foreach (XmlElement e in xml.GetElementsByTagName("Import"))
48+
{
49+
if (e.HasAttribute("Sdk"))
50+
{
51+
return true;
52+
}
53+
}
54+
55+
return false;
56+
}
57+
2958
public Project(Autobuilder<TAutobuildOptions> builder, string path) : base(builder, path)
3059
{
3160
ToolsVersion = new Version();
@@ -49,7 +78,7 @@ public Project(Autobuilder<TAutobuildOptions> builder, string path) : base(build
4978

5079
if (root?.Name == "Project")
5180
{
52-
if (root.HasAttribute("Sdk"))
81+
if (UsesSdk(root))
5382
{
5483
DotNetProject = true;
5584
return;

0 commit comments

Comments
 (0)