|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Reflection; |
| 4 | +using System.Text; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using RazorLight; |
| 7 | +using RazorLight.Generation; |
| 8 | + |
| 9 | +namespace MSBuildRazorCompiler |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + private static readonly Regex LeadingSlash = new Regex(@"^[\\/]", RegexOptions.Compiled); |
| 14 | + |
| 15 | + static int Main(string[] args) |
| 16 | + { |
| 17 | + var directoryToApply = args.Length > 0 ? args[0] : null; |
| 18 | + var rootNamespace = args.Length > 1 ? args[1] : null; |
| 19 | + |
| 20 | + if (string.IsNullOrEmpty(directoryToApply) || !Directory.Exists(directoryToApply) || !Path.IsPathFullyQualified(directoryToApply)) |
| 21 | + { |
| 22 | + Console.Error.WriteLine($"Invalid directory passed in as commandline argument: {directoryToApply}"); |
| 23 | + return 1; |
| 24 | + } |
| 25 | + |
| 26 | + if (string.IsNullOrEmpty(rootNamespace)) |
| 27 | + { |
| 28 | + Console.Error.WriteLine($"Invalid namespace passed in as commandline argument: {rootNamespace}"); |
| 29 | + } |
| 30 | + |
| 31 | + var engine = new RazorLightEngineBuilder() |
| 32 | + .UseFileSystemProject(directoryToApply, ".cshtml") |
| 33 | + .Build(); |
| 34 | + |
| 35 | + // todo: submit PR to RazorGenerator to remove the need for reflection |
| 36 | + var sourceGenerator = (RazorSourceGenerator)(engine.Handler.Compiler.GetType() |
| 37 | + .GetField("_razorSourceGenerator", BindingFlags.Instance | BindingFlags.NonPublic) |
| 38 | + .GetValue(engine.Handler.Compiler)); |
| 39 | + |
| 40 | + foreach(var file in Directory.EnumerateFiles(directoryToApply, "*.cshtml", SearchOption.AllDirectories)) |
| 41 | + { |
| 42 | + Console.WriteLine($"Processing {file}..."); |
| 43 | + |
| 44 | + var itemKey = LeadingSlash.Replace(file.Replace(directoryToApply, ""), ""); |
| 45 | + var source = sourceGenerator.GenerateCodeAsync(itemKey).GetAwaiter().GetResult(); |
| 46 | + |
| 47 | + var (namespacePath, className) = GetNamespaceAndClassName(itemKey, rootNamespace); |
| 48 | + Console.WriteLine($" Classname will be `{className}` and namespace will be `{namespacePath}`"); |
| 49 | + |
| 50 | + var code = source.GeneratedCode |
| 51 | + .Replace("public class GeneratedTemplate", $"public class {className}") |
| 52 | + .Replace("namespace RazorLight.CompiledTemplates", $"namespace {namespacePath}") |
| 53 | + .Replace("typeof(RazorLight.CompiledTemplates.GeneratedTemplate)", $"typeof({namespacePath}.{className})"); |
| 54 | + |
| 55 | + Console.WriteLine($" Writing {file}.generated.cs"); |
| 56 | + File.WriteAllText(file + ".generated.cs", code, Encoding.UTF8); |
| 57 | + } |
| 58 | + |
| 59 | + return 0; |
| 60 | + } |
| 61 | + |
| 62 | + // todo: implement this as a Razor Feature (as pe https://github.com/toddams/RazorLight/blob/master/src/RazorLight/DefaultRazorEngine.cs) |
| 63 | + private static (string, string) GetNamespaceAndClassName(string itemKey, string rootNamespace) |
| 64 | + { |
| 65 | + var finalSlash = itemKey.LastIndexOf(Path.DirectorySeparatorChar); |
| 66 | + var className = ""; |
| 67 | + var namespacePath = ""; |
| 68 | + if (finalSlash > -1) |
| 69 | + { |
| 70 | + className = itemKey.Substring(finalSlash + 1).Replace(".cshtml", ""); |
| 71 | + namespacePath = rootNamespace + "." + itemKey.Substring(0, finalSlash).Replace(Path.DirectorySeparatorChar, '.'); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + className = itemKey.Replace(".cshtml", ""); |
| 76 | + namespacePath = rootNamespace; |
| 77 | + } |
| 78 | + className = className[0].ToString().ToUpper() + className.Substring(1); |
| 79 | + |
| 80 | + return (namespacePath, className); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments