Exemplo n.º 1
0
        /// <summary>
        /// Generates code for a razor template in the specified namespace.
        /// </summary>
        /// <param name="cshtmlFilePath">Full path to razor template.</param>
        /// <param name="rootNamespace">Root namespace for razor-generated class.</param>
        private void GenerateCodeFile(string cshtmlFilePath, string rootNamespace)
        {
            var basePath = Path.GetDirectoryName(cshtmlFilePath);
            var fileName = Path.GetFileName(cshtmlFilePath);
            var fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName);
            var codeLang = new CSharpRazorCodeLanguage();
            var host = new RazorEngineHost(codeLang);
            host.GeneratedClassContext = new GeneratedClassContext(
                executeMethodName: GeneratedClassContext.DefaultExecuteMethodName,
                writeMethodName: GeneratedClassContext.DefaultWriteMethodName,
                writeLiteralMethodName: GeneratedClassContext.DefaultWriteLiteralMethodName,
                writeToMethodName: "WriteTo",
                writeLiteralToMethodName: "WriteLiteralTo",
                templateTypeName: "HelperResult",
                defineSectionMethodName: "DefineSection",
                generatedTagHelperContext: new GeneratedTagHelperContext());
            var engine = new RazorTemplateEngine(host);

            using (var fileStream = File.OpenText(cshtmlFilePath))
            {
                var code = engine.GenerateCode(
                    input: fileStream,
                    className: fileNameNoExtension,
                    rootNamespace: rootNamespace,
                    sourceFileName: fileName);

                var source = code.GeneratedCode;
                source = CopyrightHeader + "\r\n\r\n" + source;
                var startIndex = 0;
                while (startIndex < source.Length)
                {
                    var startMatch = @"<%$ include: ";
                    var endMatch = @" %>";
                    startIndex = source.IndexOf(startMatch, startIndex);
                    if (startIndex == -1)
                    {
                        break;
                    }
                    var endIndex = source.IndexOf(endMatch, startIndex);
                    if (endIndex == -1)
                    {
                        break;
                    }
                    var includeFileName = source.Substring(startIndex + startMatch.Length,
                        endIndex - (startIndex + startMatch.Length));
                    Console.WriteLine("    Inlining file {0}", includeFileName);
                    var replacement =
                        File.ReadAllText(Path.Combine(basePath, includeFileName))
                            .Replace("\"", "\\\"")
                            .Replace("\r\n", "\\r\\n");
                    source = source.Substring(0, startIndex) + replacement +
                             source.Substring(endIndex + endMatch.Length);
                    startIndex = startIndex + replacement.Length;
                }
                File.WriteAllText(Path.Combine(basePath, string.Format("{0}.cs", fileNameNoExtension)), source);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            const string rootNamespace = "RazorOnConsole";
            var viewPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\Index.cshtml");
            var basePath = Path.GetDirectoryName(viewPath);
            var fileName = Path.GetFileName(viewPath);
            var fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName);
            using (var file = File.Create(fileNameNoExtension + ".html")) { new Index { Model = "foobarfoo" }.ExecuteAsync(file).Wait(); }

            var codeLang = new CSharpRazorCodeLanguage();
            var host = new RazorEngineHost(codeLang)
            {
                DefaultBaseClass = "RazorOnConsole.Views.BaseView",
                GeneratedClassContext = new GeneratedClassContext(
                    executeMethodName: GeneratedClassContext.DefaultExecuteMethodName,
                    writeMethodName: GeneratedClassContext.DefaultWriteMethodName,
                    writeLiteralMethodName: GeneratedClassContext.DefaultWriteLiteralMethodName,
                    writeToMethodName: "WriteTo",
                    writeLiteralToMethodName: "WriteLiteralTo",
                    templateTypeName: "HelperResult",
                    defineSectionMethodName: "DefineSection",
                    generatedTagHelperContext: new GeneratedTagHelperContext())
            };

            host.NamespaceImports.Add("System");

            var engine = new RazorTemplateEngine(host);

            using (var fileStream = File.OpenText(viewPath))
            {
                GeneratorResults code = engine.GenerateCode(
                    input: fileStream,
                    className: fileNameNoExtension,
                    rootNamespace: rootNamespace,
                    sourceFileName: fileName);

                string source = code.GeneratedCode;
                File.WriteAllText(Path.Combine(basePath, @"..\..\..\", "Views", string.Format("{0}.cs", fileNameNoExtension)), source);
            }
        }
Exemplo n.º 3
0
        private static string GetRazorSyntaxTree()
        {
            var viewPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\Index.cshtml");
            var fileName = Path.GetFileName(viewPath);
            var fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName);
            var className = MainClassNamePrefix + fileNameNoExtension;

            var codeLang = new CSharpRazorCodeLanguage();
            var host = new RazorEngineHost(codeLang)
            {
                DefaultBaseClass = typeof(BaseView).FullName,
                GeneratedClassContext = new GeneratedClassContext(
                    executeMethodName: GeneratedClassContext.DefaultExecuteMethodName,
                    writeMethodName: GeneratedClassContext.DefaultWriteMethodName,
                    writeLiteralMethodName: GeneratedClassContext.DefaultWriteLiteralMethodName,
                    writeToMethodName: "WriteTo",
                    writeLiteralToMethodName: "WriteLiteralTo",
                    templateTypeName: "HelperResult",
                    defineSectionMethodName: "DefineSection",
                    generatedTagHelperContext: new GeneratedTagHelperContext())
            };

            host.NamespaceImports.Add("System");

            var engine = new RazorTemplateEngine(host);

            using (var fileStream = File.OpenText(viewPath))
            {
                GeneratorResults code = engine.GenerateCode(
                    input: fileStream,
                    className: className,
                    rootNamespace: RootNamespace,
                    sourceFileName: fileName);

                return code.GeneratedCode;
            }
        }