|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using Apollo.Contracts.Compilation; |
| 5 | +using Microsoft.AspNetCore.Razor.Language; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Solution = Apollo.Contracts.Solutions.Solution; |
| 9 | + |
| 10 | +namespace Apollo.Compilation; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Compiles Razor component files (.razor) to assemblies. |
| 14 | +/// </summary> |
| 15 | +public class RazorCompilationService |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Compiles a solution containing Razor files to an assembly. |
| 19 | + /// </summary> |
| 20 | + public CompilationReferenceResult Compile(Solution solution, IEnumerable<MetadataReference> references) |
| 21 | + { |
| 22 | + var stopwatch = Stopwatch.StartNew(); |
| 23 | + var syntaxTrees = new List<SyntaxTree>(); |
| 24 | + var diagnostics = new List<string>(); |
| 25 | + |
| 26 | + diagnostics.Add($"Starting Razor compilation for {solution.Name} with {solution.Items.Count} items"); |
| 27 | + |
| 28 | + var importFiles = solution.Items |
| 29 | + .Where(item => item.Path.EndsWith("_Imports.razor", StringComparison.OrdinalIgnoreCase)) |
| 30 | + .ToList(); |
| 31 | + |
| 32 | + diagnostics.Add($"Found {importFiles.Count} _Imports.razor file(s)"); |
| 33 | + |
| 34 | + var importSourceDocuments = importFiles |
| 35 | + .Select(import => RazorSourceDocument.Create(import.Content, import.Path)) |
| 36 | + .ToImmutableArray(); |
| 37 | + |
| 38 | + foreach (var item in solution.Items) |
| 39 | + { |
| 40 | + if (item.Path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase) && |
| 41 | + !item.Path.EndsWith("_Imports.razor", StringComparison.OrdinalIgnoreCase)) |
| 42 | + { |
| 43 | + diagnostics.Add($"Processing Razor file: {item.Path}"); |
| 44 | + |
| 45 | + var (generatedCode, genDiagnostics) = GenerateComponentCode(item.Path, item.Content, importSourceDocuments); |
| 46 | + diagnostics.AddRange(genDiagnostics); |
| 47 | + |
| 48 | + if (!string.IsNullOrEmpty(generatedCode)) |
| 49 | + { |
| 50 | + generatedCode = EnsureEssentialUsings(generatedCode); |
| 51 | + syntaxTrees.Add(CSharpSyntaxTree.ParseText(generatedCode, path: item.Path + ".g.cs")); |
| 52 | + diagnostics.Add($"Generated {generatedCode.Length} chars of C# code for {Path.GetFileName(item.Path)}"); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + diagnostics.Add($"Failed to generate code for {item.Path}"); |
| 57 | + } |
| 58 | + } |
| 59 | + else if (item.Path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) |
| 60 | + { |
| 61 | + syntaxTrees.Add(CSharpSyntaxTree.ParseText(item.Content, path: item.Path)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (syntaxTrees.Count == 0) |
| 66 | + { |
| 67 | + stopwatch.Stop(); |
| 68 | + diagnostics.Add("No compilable files found"); |
| 69 | + return new CompilationReferenceResult(false, null, diagnostics, stopwatch.Elapsed); |
| 70 | + } |
| 71 | + |
| 72 | + diagnostics.Add($"Compiling {syntaxTrees.Count} syntax trees with {references.Count()} references"); |
| 73 | + |
| 74 | + var compilation = CSharpCompilation.Create( |
| 75 | + solution.Name, |
| 76 | + syntaxTrees, |
| 77 | + references, |
| 78 | + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, concurrentBuild: true) |
| 79 | + ); |
| 80 | + |
| 81 | + using var memoryStream = new MemoryStream(); |
| 82 | + var emitResult = compilation.Emit(memoryStream); |
| 83 | + |
| 84 | + stopwatch.Stop(); |
| 85 | + |
| 86 | + if (!emitResult.Success) |
| 87 | + { |
| 88 | + var emitDiagnostics = emitResult.Diagnostics |
| 89 | + .Where(d => d.Severity == DiagnosticSeverity.Error) |
| 90 | + .Select(d => $"{d.Location}: {d.GetMessage()}") |
| 91 | + .ToList(); |
| 92 | + diagnostics.AddRange(emitDiagnostics); |
| 93 | + diagnostics.Add($"Compilation failed with {emitDiagnostics.Count} errors"); |
| 94 | + return new CompilationReferenceResult(false, null, diagnostics, stopwatch.Elapsed); |
| 95 | + } |
| 96 | + |
| 97 | + memoryStream.Seek(0, SeekOrigin.Begin); |
| 98 | + var assemblyBytes = memoryStream.ToArray(); |
| 99 | + |
| 100 | + diagnostics.Add($"Compilation successful, assembly size: {assemblyBytes.Length} bytes"); |
| 101 | + |
| 102 | + return new CompilationReferenceResult(true, assemblyBytes, diagnostics, stopwatch.Elapsed); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Generates C# code from a Razor component file. |
| 107 | + /// </summary> |
| 108 | + private (string code, List<string> diagnostics) GenerateComponentCode(string filePath, string razorContent, ImmutableArray<RazorSourceDocument> importSources) |
| 109 | + { |
| 110 | + var diagnostics = new List<string>(); |
| 111 | + |
| 112 | + try |
| 113 | + { |
| 114 | + var fileSystem = new VirtualRazorProjectFileSystem(); |
| 115 | + |
| 116 | + var projectEngine = RazorProjectEngine.Create( |
| 117 | + RazorConfiguration.Default, |
| 118 | + fileSystem, |
| 119 | + builder => |
| 120 | + { |
| 121 | + builder.SetRootNamespace("UserComponents"); |
| 122 | + }); |
| 123 | + |
| 124 | + var sourceDocument = RazorSourceDocument.Create(razorContent, filePath); |
| 125 | + |
| 126 | + var codeDocument = projectEngine.Process( |
| 127 | + sourceDocument, |
| 128 | + FileKinds.Component, |
| 129 | + importSources, |
| 130 | + tagHelpers: null); |
| 131 | + |
| 132 | + var csharpDocument = codeDocument.GetCSharpDocument(); |
| 133 | + |
| 134 | + if (csharpDocument == null) |
| 135 | + { |
| 136 | + diagnostics.Add("Razor compiler returned null C# document, using fallback"); |
| 137 | + return (GenerateSimpleComponentCode(filePath, razorContent), diagnostics); |
| 138 | + } |
| 139 | + |
| 140 | + foreach (var diag in csharpDocument.Diagnostics) |
| 141 | + { |
| 142 | + diagnostics.Add($"Razor: {diag.GetMessage()}"); |
| 143 | + } |
| 144 | + |
| 145 | + var generatedCode = csharpDocument.GeneratedCode; |
| 146 | + |
| 147 | + if (string.IsNullOrEmpty(generatedCode)) |
| 148 | + { |
| 149 | + diagnostics.Add("Razor compiler returned empty code, using fallback"); |
| 150 | + return (GenerateSimpleComponentCode(filePath, razorContent), diagnostics); |
| 151 | + } |
| 152 | + |
| 153 | + diagnostics.Add("Razor compiler succeeded"); |
| 154 | + return (generatedCode, diagnostics); |
| 155 | + } |
| 156 | + catch (Exception ex) |
| 157 | + { |
| 158 | + diagnostics.Add($"Razor compiler exception: {ex.Message}, using fallback"); |
| 159 | + return (GenerateSimpleComponentCode(filePath, razorContent), diagnostics); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Fallback simple code generation when Razor compiler fails. |
| 165 | + /// Generates a minimal but functional component. |
| 166 | + /// </summary> |
| 167 | + private string GenerateSimpleComponentCode(string filePath, string razorContent) |
| 168 | + { |
| 169 | + var componentName = Path.GetFileNameWithoutExtension(filePath); |
| 170 | + |
| 171 | + var codeBlockMatch = Regex.Match( |
| 172 | + razorContent, |
| 173 | + @"@code\s*\{([\s\S]*)\}\s*$", |
| 174 | + RegexOptions.Multiline); |
| 175 | + |
| 176 | + var codeBlock = codeBlockMatch.Success ? codeBlockMatch.Groups[1].Value.Trim() : ""; |
| 177 | + |
| 178 | + var usings = new List<string>(); |
| 179 | + var usingMatches = Regex.Matches(razorContent, @"@using\s+([\w\.]+)"); |
| 180 | + foreach (Match match in usingMatches) |
| 181 | + { |
| 182 | + usings.Add($"using {match.Groups[1].Value};"); |
| 183 | + } |
| 184 | + |
| 185 | + var usingsBlock = string.Join("\n", usings); |
| 186 | + |
| 187 | + return $$""" |
| 188 | +// <auto-generated/> |
| 189 | +#pragma warning disable |
| 190 | +using System; |
| 191 | +using System.Collections.Generic; |
| 192 | +using System.Linq; |
| 193 | +using System.Threading.Tasks; |
| 194 | +using Microsoft.AspNetCore.Components; |
| 195 | +using Microsoft.AspNetCore.Components.Web; |
| 196 | +using Microsoft.AspNetCore.Components.Rendering; |
| 197 | +{{usingsBlock}} |
| 198 | +
|
| 199 | +namespace UserComponents |
| 200 | +{ |
| 201 | + public partial class {{componentName}} : ComponentBase |
| 202 | + { |
| 203 | + protected override void BuildRenderTree(RenderTreeBuilder __builder) |
| 204 | + { |
| 205 | + __builder.OpenElement(0, "div"); |
| 206 | + __builder.AddAttribute(1, "class", "component-preview"); |
| 207 | + __builder.AddContent(2, "{{componentName}} Component"); |
| 208 | + __builder.CloseElement(); |
| 209 | + } |
| 210 | +
|
| 211 | +{{codeBlock}} |
| 212 | + } |
| 213 | +} |
| 214 | +#pragma warning restore |
| 215 | +"""; |
| 216 | + } |
| 217 | + |
| 218 | + /// <summary> |
| 219 | + /// Ensures essential using statements are present in the generated C# code. |
| 220 | + /// The Razor compiler may not emit all necessary usings for async/Task support. |
| 221 | + /// </summary> |
| 222 | + private static string EnsureEssentialUsings(string generatedCode) |
| 223 | + { |
| 224 | + const string taskUsing = "using System.Threading.Tasks;"; |
| 225 | + |
| 226 | + if (generatedCode.Contains(taskUsing)) |
| 227 | + return generatedCode; |
| 228 | + |
| 229 | + var insertIndex = 0; |
| 230 | + var pragmaIndex = generatedCode.IndexOf("#pragma warning disable", StringComparison.Ordinal); |
| 231 | + if (pragmaIndex >= 0) |
| 232 | + { |
| 233 | + var lineEnd = generatedCode.IndexOf('\n', pragmaIndex); |
| 234 | + if (lineEnd >= 0) |
| 235 | + insertIndex = lineEnd + 1; |
| 236 | + } |
| 237 | + |
| 238 | + return generatedCode.Insert(insertIndex, taskUsing + "\n"); |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +/// <summary> |
| 243 | +/// A virtual file system implementation for the Razor project engine. |
| 244 | +/// </summary> |
| 245 | +internal class VirtualRazorProjectFileSystem : RazorProjectFileSystem |
| 246 | +{ |
| 247 | + public override IEnumerable<RazorProjectItem> EnumerateItems(string basePath) |
| 248 | + { |
| 249 | + return Enumerable.Empty<RazorProjectItem>(); |
| 250 | + } |
| 251 | + |
| 252 | + public override RazorProjectItem GetItem(string path) |
| 253 | + { |
| 254 | + return new NotFoundProjectItem(string.Empty, path, FileKinds.Component); |
| 255 | + } |
| 256 | + |
| 257 | + public override RazorProjectItem GetItem(string path, string? fileKind) |
| 258 | + { |
| 259 | + return new NotFoundProjectItem(string.Empty, path, fileKind ?? FileKinds.Component); |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +/// <summary> |
| 264 | +/// Represents a project item that was not found. |
| 265 | +/// </summary> |
| 266 | +internal class NotFoundProjectItem : RazorProjectItem |
| 267 | +{ |
| 268 | + public NotFoundProjectItem(string basePath, string path, string fileKind) |
| 269 | + { |
| 270 | + BasePath = basePath; |
| 271 | + FilePath = path; |
| 272 | + FileKind = fileKind; |
| 273 | + } |
| 274 | + |
| 275 | + public override string BasePath { get; } |
| 276 | + public override string FilePath { get; } |
| 277 | + public override string FileKind { get; } |
| 278 | + public override bool Exists => false; |
| 279 | + public override string PhysicalPath => FilePath; |
| 280 | + |
| 281 | + public override Stream Read() |
| 282 | + { |
| 283 | + throw new InvalidOperationException("Item does not exist"); |
| 284 | + } |
| 285 | +} |
0 commit comments