Exemple #1
0
        public static string CompileToStream(bool enableLogging, string rootDir, string[] referenceAssemblies, string outputAssemblyName, Stream outputStream)
        {
            if (!rootDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                rootDir += Path.DirectorySeparatorChar.ToString();
            }

            EnableLogging = enableLogging;
            Log("Creating Razor engine...");
            var engine = RazorEngine.Create(builder =>
            {
                var defaultCSharpLoweringPhase = builder.Phases.OfType <IRazorCSharpLoweringPhase>().Single();
                builder.Phases.Remove(defaultCSharpLoweringPhase);
                builder.Phases.Add(new VirtualDomCSharpLoweringPhase(defaultCSharpLoweringPhase));
                builder.SetNamespace("Views");
                builder.SetBaseType(typeof(RazorComponent).FullName);
                builder.ConfigureClass((codeDoc, classNode) =>
                {
                    classNode.Name = RazorComponent.GetViewClassName(rootDir, codeDoc.Source.FileName);
                    if (!string.IsNullOrEmpty((string)codeDoc.Items["DetectedBaseClass"]))
                    {
                        classNode.BaseType = (string)codeDoc.Items["DetectedBaseClass"];
                    }

                    AddIComponentRazorViewFactoryImplementation(classNode);

                    var layoutProperty = new CSharpStatementIRNode
                    {
                        Parent = classNode,
                        Source = null
                    };
                    classNode.Children.Add(layoutProperty);

                    layoutProperty.Children.Add(new RazorIRToken
                    {
                        Kind    = RazorIRToken.TokenKind.CSharp,
                        Parent  = classNode,
                        Content = $"protected override string Layout {{ get {{ return \"{ codeDoc.Items["DetectedLayout"] ?? string.Empty }\"; }} }}"
                    });
                });
            });

            Log("Compiling Razor files to C#...");
            var filenames   = GetFilesToCompile(rootDir);
            var syntaxTrees = GetSyntaxTrees(engine, rootDir, filenames);

            Log("Compiling C#...");
            var modelAssemblyRefs = referenceAssemblies
                                    .Select(a => MetadataReference.CreateFromFile(Path.GetFullPath(a)))
                                    .Cast <MetadataReference>()
                                    .ToList();

            CompileToFile(syntaxTrees, modelAssemblyRefs, outputAssemblyName, outputStream);
            return($"Compiled {syntaxTrees.Count} view(s) as {outputAssemblyName}");
        }