Skip to content

Commit 31143b4

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

File tree

1 file changed

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

1 file changed

+21
-2
lines changed

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

Lines changed: 21 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,26 @@ 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+
private static bool HasSdkAttribute(XmlElement xml) =>
29+
xml.HasAttribute("Sdk");
30+
31+
private static bool AnyElement(XmlNodeList l, Func<XmlElement, bool> f) =>
32+
l.OfType<XmlElement>().Any(f);
33+
34+
/// <summary>
35+
/// According to https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2022#reference-a-project-sdk
36+
/// there are three ways to reference a project SDK:
37+
/// 1. As an attribute on the <Project/>.
38+
/// 2. As a top level element of <Project>.
39+
/// 3. As an attribute on an <Import> element.
40+
///
41+
/// Returns true, if the Sdk attribute is used, otherwise false.
42+
/// </summary>
43+
private static bool ReferencesSdk(XmlElement xml) =>
44+
HasSdkAttribute(xml) || // Case 1
45+
AnyElement(xml.ChildNodes, e => e.Name == "Sdk") || // Case 2
46+
AnyElement(xml.GetElementsByTagName("Import"), HasSdkAttribute); // Case 3
47+
2948
public Project(Autobuilder<TAutobuildOptions> builder, string path) : base(builder, path)
3049
{
3150
ToolsVersion = new Version();
@@ -49,7 +68,7 @@ public Project(Autobuilder<TAutobuildOptions> builder, string path) : base(build
4968

5069
if (root?.Name == "Project")
5170
{
52-
if (root.HasAttribute("Sdk"))
71+
if (ReferencesSdk(root))
5372
{
5473
DotNetProject = true;
5574
return;

0 commit comments

Comments
 (0)