-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCsprojParserService.cs
More file actions
56 lines (46 loc) · 1.88 KB
/
Copy pathCsprojParserService.cs
File metadata and controls
56 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.IO;
using System.Linq;
using System.Xml.Linq;
using GeneralUpdate.Tools.Models;
namespace GeneralUpdate.Tools.Services;
public static class CsprojParserService
{
public static CsprojInfo? Parse(string csprojPath)
{
if (string.IsNullOrWhiteSpace(csprojPath) || !File.Exists(csprojPath))
return null;
var doc = XDocument.Load(csprojPath);
var projectDir = Path.GetDirectoryName(Path.GetFullPath(csprojPath)) ?? "";
var projectName = Path.GetFileNameWithoutExtension(csprojPath);
var assemblyName = GetElementValue(doc, "AssemblyName")
?? projectName;
var outputType = GetElementValue(doc, "OutputType") ?? "WinExe";
var targetFramework = GetElementValue(doc, "TargetFramework")
?? GetElementValue(doc, "TargetFrameworks");
return new CsprojInfo
{
ProjectName = projectName,
AssemblyName = assemblyName.EndsWith(".exe", System.StringComparison.OrdinalIgnoreCase)
? assemblyName
: assemblyName + ".exe",
OutputType = outputType,
TargetFramework = targetFramework ?? "",
CsprojPath = Path.GetFullPath(csprojPath),
ProjectDir = projectDir
};
}
private static string? GetElementValue(XContainer doc, string elementName)
{
var ns = doc.Document?.Root?.GetDefaultNamespace() ?? XNamespace.None;
var el = doc.Descendants(ns + elementName).FirstOrDefault();
if (el != null && !string.IsNullOrWhiteSpace(el.Value))
return el.Value.Trim();
// Fallback: search without namespace
foreach (var d in doc.Descendants())
{
if (d.Name.LocalName == elementName && !string.IsNullOrWhiteSpace(d.Value))
return d.Value.Trim();
}
return null;
}
}