Beispiel #1
0
 public ICompiledRazorTemplate Compile(string razorTemplateText, RazorEngineCompileSettings settings, string templateName = null)
 {
     Contract.Requires(razorTemplateText != null);
     Contract.Requires(settings != null);
     Contract.Ensures(Contract.Result <ICompiledRazorTemplate>() != null);
     throw new System.NotImplementedException();
 }
        public RazorEngineCompileSettings Clone()
        {
            RazorEngineCompileSettings clone = new RazorEngineCompileSettings();

            clone.defaultNamespace = defaultNamespace;
            clone.defaultClassName = defaultClassName;
            clone.defaultBaseClass = defaultBaseClass;
            clone.namespaceImports.AddRange(namespaceImports);
            clone.referenceAssemblies.AddRange(referenceAssemblies);
            clone.DebugMode = DebugMode;
            return(clone);
        }
Beispiel #3
0
        private void RegisterViewPrivate(string viewName, ICompiledRazorTemplate layoutTemplate, string nspace)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Registering Razor view '{0}'", viewName);
            }

            RazorEngineCompileSettings viewCompileSettings = compileSettings.Clone();

            viewCompileSettings.DefaultClassName = viewName + compileSettings.DefaultClassName;

            string fileName;

            string containerDir;

            Contract.Assume(viewsRootDirectory != null);

            if (nspace == null)
            {
                containerDir = viewsRootDirectory;
            }
            else
            {
                containerDir = Path.Combine(viewsRootDirectory, nspace);
            }

            string viewContents = FetchViewContents(containerDir, viewName, out fileName);

            try
            {
                ICompiledRazorTemplate template = razorCompiler.Compile(viewContents, viewCompileSettings, viewName);
                template.LayoutTemplate = layoutTemplate;
                viewTemplates.Add(viewName, template);
            }
            catch (RazorException ex)
            {
                LogTemplateErrors(ex, fileName);
                throw;
            }
        }
Beispiel #4
0
        public ICompiledRazorTemplate Compile(string razorTemplateText, RazorEngineCompileSettings settings, string templateName = null)
        {
            RazorEngineHost razorEngineHost = new RazorEngineHost(new CSharpRazorCodeLanguage());

            razorEngineHost.DefaultNamespace      = settings.DefaultNamespace;
            razorEngineHost.DefaultClassName      = settings.DefaultClassName;
            razorEngineHost.DefaultBaseClass      = settings.DefaultBaseClass;
            razorEngineHost.GeneratedClassContext = new GeneratedClassContext(
                GeneratedClassContext.DefaultExecuteMethodName,
                GeneratedClassContext.DefaultWriteMethodName,
                GeneratedClassContext.DefaultWriteLiteralMethodName,
                "WriteTo",
                "WriteLiteralTo",
                "Syborg.Razor.HelperResult",
                "DefineSection");

            foreach (string namespaceImport in settings.NamespaceImports)
            {
                razorEngineHost.NamespaceImports.Add(namespaceImport);
            }

            RazorTemplateEngine razorTemplateEngine = new RazorTemplateEngine(razorEngineHost);

            using (StringReader templateReader = new StringReader(razorTemplateText))
            {
                GeneratorResults generatorResults = razorTemplateEngine.GenerateCode(templateReader);

                Contract.Assume(generatorResults != null);

                if (!generatorResults.Success)
                {
                    throw new RazorException(generatorResults);
                }

                // do this only for debug purposes
                if (templateName != null && settings.DebugMode)
                {
                    lock (log)
                    {
                        string generatedTemplateFileName = Path.Combine(Path.GetTempPath(), templateName + ".cs");
                        using (StreamWriter sourceCodeWriter = new StreamWriter(generatedTemplateFileName))
                            using (CSharpCodeProvider provider = new CSharpCodeProvider())
                            {
                                CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions();
                                provider.GenerateCodeFromCompileUnit(generatorResults.GeneratedCode, sourceCodeWriter, codeGeneratorOptions);
                                if (log.IsDebugEnabled)
                                {
                                    log.DebugFormat("Writing the generated template to '{0}", generatedTemplateFileName);
                                }
                            }
                    }
                }

                CompilerParameters compilerParameters = new CompilerParameters();
                compilerParameters.GenerateInMemory        = true;
                compilerParameters.IncludeDebugInformation = true;
                compilerParameters.ReferencedAssemblies.Add(typeof(InMemoryRazorCompiler).Assembly.Location);
                compilerParameters.TreatWarningsAsErrors = true;
                foreach (Assembly referenceAssembly in settings.ReferenceAssemblies)
                {
                    compilerParameters.ReferencedAssemblies.Add(referenceAssembly.Location);
                }

                using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
                {
                    CompilerResults compilerResults = codeProvider.CompileAssemblyFromDom(compilerParameters, generatorResults.GeneratedCode);

                    Contract.Assume(compilerResults != null);

                    if (compilerResults.Errors.HasErrors)
                    {
                        throw new RazorException(generatorResults, compilerResults);
                    }

                    return(new CompiledRazorTemplate(generatorResults, compilerResults));
                }
            }
        }
 public void Initialize(string viewsRootDirectory, RazorEngineCompileSettings settings)
 {
     Contract.Requires(viewsRootDirectory != null);
     Contract.Requires(settings != null);
     throw new System.NotImplementedException();
 }
Beispiel #6
0
 public void Initialize(string viewsRootDirectory, RazorEngineCompileSettings settings)
 {
     this.viewsRootDirectory = viewsRootDirectory;
     compileSettings         = settings;
 }