Repository navigation
Expand file tree
/
Copy pathProjectDetector.cs
More file actions
97 lines (77 loc) · 3.74 KB
/
Copy pathProjectDetector.cs
File metadata and controls
97 lines (77 loc) · 3.74 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Xml.Linq;
using ShellUI.Native.Core.Models;
namespace ShellUI.Native.CLI.Services;
// Detects the type of native project in the current directory
public static class ProjectDetector
{
public static ProjectInfo DetectProject()
{
var csprojFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.csproj");
if (csprojFiles.Length == 0)
{
throw new Exception("No .csproj file found in current directory. Please run this command from your project root.");
}
var csprojPath = csprojFiles[0];
var projectName = Path.GetFileNameWithoutExtension(csprojPath);
var doc = XDocument.Load(csprojPath);
var platform = DetectPlatform(doc, csprojPath);
var rootNamespace = DetectRootNamespace(doc, projectName);
return new ProjectInfo
{
ProjectPath = csprojPath,
ProjectName = projectName,
RootNamespace = rootNamespace,
Platform = platform
};
}
internal static NativePlatform DetectPlatform(XDocument doc, string csprojPath)
{
var sdk = doc.Root?.Attribute("Sdk")?.Value ?? "";
// Check for MAUI workload
if (sdk.Contains("Maui", StringComparison.OrdinalIgnoreCase))
return NativePlatform.MAUI;
var useMaui = doc.Descendants("UseMaui").FirstOrDefault()?.Value;
if (useMaui?.Equals("true", StringComparison.OrdinalIgnoreCase) == true)
return NativePlatform.MAUI;
// Check for Avalonia (PackageReference-based, no dedicated SDK)
var hasAvaloniaPackage = doc.Descendants("PackageReference")
.Any(e => (e.Attribute("Include")?.Value ?? "").StartsWith("Avalonia", StringComparison.OrdinalIgnoreCase));
if (hasAvaloniaPackage)
return NativePlatform.Avalonia;
var projectDir = Path.GetDirectoryName(csprojPath) ?? ".";
if (File.Exists(Path.Combine(projectDir, "App.axaml")))
return NativePlatform.Avalonia;
// Check for WinUI
var useWinUI = doc.Descendants("UseWinUI").FirstOrDefault()?.Value;
if (useWinUI?.Equals("true", StringComparison.OrdinalIgnoreCase) == true)
return NativePlatform.WinUI;
// Check for WPF
if (sdk.Contains("Wpf", StringComparison.OrdinalIgnoreCase))
return NativePlatform.WPF;
var useWpf = doc.Descendants("UseWPF").FirstOrDefault()?.Value;
if (useWpf?.Equals("true", StringComparison.OrdinalIgnoreCase) == true)
return NativePlatform.WPF;
// Try to detect from output type and target framework
var targetFramework = doc.Descendants("TargetFramework").FirstOrDefault()?.Value ?? "";
var targetFrameworks = doc.Descendants("TargetFrameworks").FirstOrDefault()?.Value ?? "";
// MAUI typically targets multiple platforms
if (targetFrameworks.Contains("android") || targetFrameworks.Contains("ios"))
return NativePlatform.MAUI;
return NativePlatform.Unknown;
}
internal static string DetectRootNamespace(XDocument doc, string projectName)
{
var rootNamespace = doc.Descendants("RootNamespace").FirstOrDefault()?.Value;
return string.IsNullOrWhiteSpace(rootNamespace) ? SafeNamespace(projectName) : rootNamespace.Trim();
}
// What `dotnet new` does with a project name: "my-app" → "my_app", "2fa.app" → "_2fa.app".
internal static string SafeNamespace(string name)
{
var parts = name.Split('.').Select(part =>
{
var safe = new string(part.Select(c => char.IsLetterOrDigit(c) || c == '_' ? c : '_').ToArray());
return safe.Length == 0 || char.IsDigit(safe[0]) ? "_" + safe : safe;
});
return string.Join('.', parts);
}
}