|
3 | 3 |
|
4 | 4 | using System.Text.Json; |
5 | 5 | using System.Text.Json.Nodes; |
| 6 | +using System.Text.RegularExpressions; |
6 | 7 | using System.Xml.Linq; |
7 | 8 | using Microsoft.Extensions.Logging; |
8 | 9 |
|
9 | 10 | namespace Aspire.Cli.Projects; |
10 | 11 |
|
11 | 12 | /// <summary> |
12 | | -/// Provides fallback XML parsing capabilities when MSBuild evaluation fails. |
| 13 | +/// Provides fallback parsing capabilities when MSBuild evaluation fails. |
| 14 | +/// Supports both .csproj XML files and .cs single-file apphost files. |
13 | 15 | /// Used primarily for AppHost projects with unresolvable SDK versions. |
14 | 16 | /// </summary> |
15 | | -internal sealed class FallbackProjectParser |
| 17 | +internal sealed partial class FallbackProjectParser |
16 | 18 | { |
17 | 19 | private readonly ILogger<FallbackProjectParser> _logger; |
18 | 20 |
|
| 21 | + [GeneratedRegex(@"#:sdk\s+Aspire\.AppHost\.Sdk@([\d\.\-a-zA-Z]+|\*)")] |
| 22 | + private static partial Regex SdkDirectiveRegex(); |
| 23 | + |
| 24 | + [GeneratedRegex(@"#:package\s+([a-zA-Z0-9\._]+)@([\d\.\-a-zA-Z]+|\*)")] |
| 25 | + private static partial Regex PackageDirectiveRegex(); |
| 26 | + |
19 | 27 | public FallbackProjectParser(ILogger<FallbackProjectParser> logger) |
20 | 28 | { |
21 | 29 | _logger = logger; |
22 | 30 | } |
23 | 31 |
|
24 | 32 | /// <summary> |
25 | | - /// Parses a project file using direct XML parsing to extract basic project information. |
| 33 | + /// Parses a project file using direct parsing to extract basic project information. |
26 | 34 | /// Returns a synthetic JsonDocument that mimics MSBuild's GetProjectItemsAndProperties output. |
| 35 | + /// Supports both .csproj XML files and .cs single-file apphost files. |
27 | 36 | /// </summary> |
28 | 37 | public JsonDocument ParseProject(FileInfo projectFile) |
29 | 38 | { |
30 | 39 | try |
31 | 40 | { |
32 | | - _logger.LogDebug("Parsing project file '{ProjectFile}' using fallback XML parser", projectFile.FullName); |
33 | | - |
34 | | - var doc = XDocument.Load(projectFile.FullName); |
35 | | - var root = doc.Root; |
| 41 | + _logger.LogDebug("Parsing project file '{ProjectFile}' using fallback parser", projectFile.FullName); |
36 | 42 |
|
37 | | - if (root?.Name.LocalName != "Project") |
| 43 | + // Detect file type and route to appropriate parser |
| 44 | + if (string.Equals(projectFile.Extension, ".csproj", StringComparison.OrdinalIgnoreCase)) |
38 | 45 | { |
39 | | - throw new InvalidOperationException($"Invalid project file format: {projectFile.FullName}"); |
| 46 | + return ParseCsprojProjectFile(projectFile); |
40 | 47 | } |
41 | | - |
42 | | - // Extract SDK information |
43 | | - var aspireHostingSdkVersion = ExtractAspireHostingSdkVersion(root); |
44 | | - |
45 | | - // Extract package references |
46 | | - var packageReferences = ExtractPackageReferences(root); |
47 | | - |
48 | | - // Extract project references |
49 | | - var projectReferences = ExtractProjectReferences(root, projectFile); |
50 | | - |
51 | | - // Build the synthetic JSON structure using JsonObject |
52 | | - var rootObject = new JsonObject(); |
53 | | - |
54 | | - // Items section |
55 | | - var itemsObject = new JsonObject(); |
56 | | - |
57 | | - // PackageReference items |
58 | | - var packageRefArray = new JsonArray(); |
59 | | - foreach (var pkg in packageReferences) |
| 48 | + else if (string.Equals(projectFile.Extension, ".cs", StringComparison.OrdinalIgnoreCase)) |
60 | 49 | { |
61 | | - var packageObj = new JsonObject(); |
62 | | - packageObj["Identity"] = JsonValue.Create(pkg.Identity); |
63 | | - packageObj["Version"] = JsonValue.Create(pkg.Version); |
64 | | - packageRefArray.Add((JsonNode?)packageObj); |
| 50 | + return ParseCsAppHostFile(projectFile); |
65 | 51 | } |
66 | | - itemsObject["PackageReference"] = packageRefArray; |
67 | | - |
68 | | - // ProjectReference items |
69 | | - var projectRefArray = new JsonArray(); |
70 | | - foreach (var proj in projectReferences) |
| 52 | + else |
71 | 53 | { |
72 | | - var projectObj = new JsonObject(); |
73 | | - projectObj["Identity"] = JsonValue.Create(proj.Identity); |
74 | | - projectObj["FullPath"] = JsonValue.Create(proj.FullPath); |
75 | | - projectRefArray.Add((JsonNode?)projectObj); |
| 54 | + throw new ProjectUpdaterException($"Unsupported project file type: {projectFile.Extension}. Expected .csproj or .cs file."); |
76 | 55 | } |
77 | | - itemsObject["ProjectReference"] = projectRefArray; |
78 | | - |
79 | | - rootObject["Items"] = itemsObject; |
80 | | - |
81 | | - // Properties section |
82 | | - var propertiesObject = new JsonObject(); |
83 | | - propertiesObject["AspireHostingSDKVersion"] = JsonValue.Create(aspireHostingSdkVersion); |
84 | | - rootObject["Properties"] = propertiesObject; |
85 | | - |
86 | | - // Fallback flag |
87 | | - rootObject["Fallback"] = JsonValue.Create(true); |
88 | | - |
89 | | - // Convert JsonObject to JsonDocument |
90 | | - return JsonDocument.Parse(rootObject.ToJsonString()); |
| 56 | + } |
| 57 | + catch (ProjectUpdaterException) |
| 58 | + { |
| 59 | + // Re-throw our custom exceptions |
| 60 | + throw; |
91 | 61 | } |
92 | 62 | catch (Exception ex) |
93 | 63 | { |
94 | | - _logger.LogError(ex, "Failed to parse project file '{ProjectFile}' using fallback XML parser", projectFile.FullName); |
95 | | - throw new ProjectUpdaterException($"Failed to parse project file '{projectFile.FullName}' using fallback XML parser: {ex.Message}", ex); |
| 64 | + _logger.LogError(ex, "Failed to parse project file '{ProjectFile}' using fallback parser", projectFile.FullName); |
| 65 | + throw new ProjectUpdaterException($"Failed to parse project file '{projectFile.FullName}' using fallback parser: {ex.Message}", ex); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Parses a .csproj XML project file to extract SDK and package information. |
| 71 | + /// </summary> |
| 72 | + private static JsonDocument ParseCsprojProjectFile(FileInfo projectFile) |
| 73 | + { |
| 74 | + var doc = XDocument.Load(projectFile.FullName); |
| 75 | + var root = doc.Root; |
| 76 | + |
| 77 | + if (root?.Name.LocalName != "Project") |
| 78 | + { |
| 79 | + throw new InvalidOperationException($"Invalid project file format: {projectFile.FullName}"); |
96 | 80 | } |
| 81 | + |
| 82 | + // Extract SDK information |
| 83 | + var aspireHostingSdkVersion = ExtractAspireHostingSdkVersion(root); |
| 84 | + |
| 85 | + // Extract package references |
| 86 | + var packageReferences = ExtractPackageReferences(root); |
| 87 | + |
| 88 | + // Extract project references |
| 89 | + var projectReferences = ExtractProjectReferences(root, projectFile); |
| 90 | + |
| 91 | + return BuildJsonDocument(aspireHostingSdkVersion, packageReferences, projectReferences); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Parses a .cs single-file apphost to extract SDK and package information from directives. |
| 96 | + /// </summary> |
| 97 | + private static JsonDocument ParseCsAppHostFile(FileInfo projectFile) |
| 98 | + { |
| 99 | + var fileContent = File.ReadAllText(projectFile.FullName); |
| 100 | + |
| 101 | + // Extract SDK version from #:sdk directive |
| 102 | + var aspireHostingSdkVersion = ExtractSdkVersionFromDirective(fileContent); |
| 103 | + |
| 104 | + // Extract package references from #:package directives |
| 105 | + var packageReferences = ExtractPackageReferencesFromDirectives(fileContent); |
| 106 | + |
| 107 | + // Single-file apphost projects don't have project references |
| 108 | + var projectReferences = Array.Empty<ProjectReferenceInfo>(); |
| 109 | + |
| 110 | + return BuildJsonDocument(aspireHostingSdkVersion, packageReferences, projectReferences); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Builds a synthetic JsonDocument from extracted project information. |
| 115 | + /// </summary> |
| 116 | + private static JsonDocument BuildJsonDocument( |
| 117 | + string? aspireHostingSdkVersion, |
| 118 | + PackageReferenceInfo[] packageReferences, |
| 119 | + ProjectReferenceInfo[] projectReferences) |
| 120 | + { |
| 121 | + var rootObject = new JsonObject(); |
| 122 | + |
| 123 | + // Items section |
| 124 | + var itemsObject = new JsonObject(); |
| 125 | + |
| 126 | + // PackageReference items |
| 127 | + var packageRefArray = new JsonArray(); |
| 128 | + foreach (var pkg in packageReferences) |
| 129 | + { |
| 130 | + var packageObj = new JsonObject(); |
| 131 | + packageObj["Identity"] = JsonValue.Create(pkg.Identity); |
| 132 | + packageObj["Version"] = JsonValue.Create(pkg.Version); |
| 133 | + packageRefArray.Add((JsonNode?)packageObj); |
| 134 | + } |
| 135 | + itemsObject["PackageReference"] = packageRefArray; |
| 136 | + |
| 137 | + // ProjectReference items |
| 138 | + var projectRefArray = new JsonArray(); |
| 139 | + foreach (var proj in projectReferences) |
| 140 | + { |
| 141 | + var projectObj = new JsonObject(); |
| 142 | + projectObj["Identity"] = JsonValue.Create(proj.Identity); |
| 143 | + projectObj["FullPath"] = JsonValue.Create(proj.FullPath); |
| 144 | + projectRefArray.Add((JsonNode?)projectObj); |
| 145 | + } |
| 146 | + itemsObject["ProjectReference"] = projectRefArray; |
| 147 | + |
| 148 | + rootObject["Items"] = itemsObject; |
| 149 | + |
| 150 | + // Properties section |
| 151 | + var propertiesObject = new JsonObject(); |
| 152 | + propertiesObject["AspireHostingSDKVersion"] = JsonValue.Create(aspireHostingSdkVersion); |
| 153 | + rootObject["Properties"] = propertiesObject; |
| 154 | + |
| 155 | + // Fallback flag |
| 156 | + rootObject["Fallback"] = JsonValue.Create(true); |
| 157 | + |
| 158 | + // Convert JsonObject to JsonDocument |
| 159 | + return JsonDocument.Parse(rootObject.ToJsonString()); |
97 | 160 | } |
98 | 161 |
|
99 | 162 | private static string? ExtractAspireHostingSdkVersion(XElement projectRoot) |
@@ -172,6 +235,51 @@ private static ProjectReferenceInfo[] ExtractProjectReferences(XElement projectR |
172 | 235 |
|
173 | 236 | return projectReferences.ToArray(); |
174 | 237 | } |
| 238 | + |
| 239 | + /// <summary> |
| 240 | + /// Extracts the Aspire.AppHost.Sdk version from the #:sdk directive in a single-file apphost. |
| 241 | + /// </summary> |
| 242 | + private static string? ExtractSdkVersionFromDirective(string fileContent) |
| 243 | + { |
| 244 | + // Match: #:sdk Aspire.AppHost.Sdk@<version> |
| 245 | + // Where version can be a semantic version or wildcard (*) |
| 246 | + var match = SdkDirectiveRegex().Match(fileContent); |
| 247 | + |
| 248 | + if (match.Success) |
| 249 | + { |
| 250 | + return match.Groups[1].Value; |
| 251 | + } |
| 252 | + |
| 253 | + return null; |
| 254 | + } |
| 255 | + |
| 256 | + /// <summary> |
| 257 | + /// Extracts package references from #:package directives in a single-file apphost. |
| 258 | + /// </summary> |
| 259 | + private static PackageReferenceInfo[] ExtractPackageReferencesFromDirectives(string fileContent) |
| 260 | + { |
| 261 | + var packageReferences = new List<PackageReferenceInfo>(); |
| 262 | + |
| 263 | + // Match: #:package <PackageId>@<version> |
| 264 | + // Where version can be a semantic version or wildcard (*) |
| 265 | + var matches = PackageDirectiveRegex().Matches(fileContent); |
| 266 | + |
| 267 | + foreach (Match match in matches) |
| 268 | + { |
| 269 | + var identity = match.Groups[1].Value; |
| 270 | + var version = match.Groups[2].Value; |
| 271 | + |
| 272 | + var packageRef = new PackageReferenceInfo |
| 273 | + { |
| 274 | + Identity = identity, |
| 275 | + Version = version |
| 276 | + }; |
| 277 | + |
| 278 | + packageReferences.Add(packageRef); |
| 279 | + } |
| 280 | + |
| 281 | + return packageReferences.ToArray(); |
| 282 | + } |
175 | 283 | } |
176 | 284 |
|
177 | 285 | internal record PackageReferenceInfo |
|
0 commit comments