public override void Initialize(RazorHost razorHost, IDictionary <string, string> directives)
        {
            base.Initialize(razorHost, directives);
            _languageUtil = razorHost.CodeLanguageUtil;

            _isSpecialPage = IsSpecialPage(razorHost.FullPath);
            FixupDefaultClassNameIfTemplate(razorHost);


            // The CSharpRazorCodeGenerator decides to generate line pragmas based on if the file path is available. Set it to an empty string if we
            // do not want to generate them.
            string path = razorHost.EnableLinePragmas ? razorHost.FullPath : String.Empty;

            switch (razorHost.CodeLanguage.LanguageName)
            {
            case "csharp":
                razorHost.CodeGenerator = new CSharpRazorCodeGenerator(razorHost.DefaultClassName, razorHost.DefaultNamespace, path, razorHost);
                razorHost.CodeGenerator.GenerateLinePragmas = razorHost.EnableLinePragmas;
                razorHost.Parser = new MvcCSharpRazorCodeParser();
                break;

            case "vb":
                razorHost.CodeGenerator = new VBRazorCodeGenerator(razorHost.DefaultClassName, razorHost.DefaultNamespace, path, razorHost);
                razorHost.CodeGenerator.GenerateLinePragmas = razorHost.EnableLinePragmas;
                razorHost.Parser = new MvcVBRazorCodeParser();
                break;

            default:
                throw new ArgumentOutOfRangeException("Unknown language - " + razorHost.CodeLanguage.LanguageName);
            }
        }
Exemple #2
0
        public IRazorHost CreateHost(string fullPath, string projectRelativePath, string vsNamespace)
        {
            CodeLanguageUtil langutil = CodeLanguageUtil.GetLanguageUtilFromFileName(fullPath);

            using (var codeDomProvider = langutil.GetCodeDomProvider())
            {
                return(CreateHost(fullPath, projectRelativePath, codeDomProvider, vsNamespace));
            }
        }
        public RazorHost(string baseRelativePath, string fullPath, IRazorCodeTransformer codeTransformer, CodeDomProvider codeDomProvider, IDictionary <string, string> directives)
            : base(RazorCodeLanguage.GetLanguageByExtension(CsHtmlLanguage))
        {
            if (codeTransformer == null)
            {
                throw new ArgumentNullException("codeTransformer");
            }
            if (baseRelativePath == null)
            {
                throw new ArgumentNullException("baseRelativePath");
            }
            if (fullPath == null)
            {
                throw new ArgumentNullException("fullPath");
            }
            if (codeDomProvider == null)
            {
                throw new ArgumentNullException("codeDomProvider");
            }
            _codeTransformer      = codeTransformer;
            _baseRelativePath     = baseRelativePath;
            _fullPath             = fullPath;
            _codeDomProvider      = codeDomProvider;
            _directives           = directives;
            _languageUtil         = Core.CodeLanguageUtil.GetLanguageUtilFromFileExtension(CsHtmlLanguage);
            base.DefaultNamespace = "ASP";
            EnableLinePragmas     = true;

            base.GeneratedClassContext = new GeneratedClassContext(
                executeMethodName: GeneratedClassContext.DefaultExecuteMethodName,
                writeMethodName: GeneratedClassContext.DefaultWriteMethodName,
                writeLiteralMethodName: GeneratedClassContext.DefaultWriteLiteralMethodName,
                writeToMethodName: "WriteTo",
                writeLiteralToMethodName: "WriteLiteralTo",
                templateTypeName: typeof(HelperResult).FullName,
                defineSectionMethodName: "DefineSection",
                beginContextMethodName: "BeginContext",
                endContextMethodName: "EndContext"
                )
            {
                ResolveUrlMethodName = "Href"
            };

            base.DefaultBaseClass = typeof(WebPage).FullName;
            foreach (var import in _defaultImports)
            {
                base.NamespaceImports.Add(import);
            }
        }
Exemple #4
0
        public string GenerateCode()
        {
            _codeTransformer.Initialize(this, _directives);

            // Create the engine
            RazorTemplateEngine engine = new RazorTemplateEngine(this);

            // Generate code
            GeneratorResults results = null;

            try
            {
                Stream stream = File.OpenRead(_fullPath);
                using (var reader = new StreamReader(stream, Encoding.Default, detectEncodingFromByteOrderMarks: true))
                {
                    results = engine.GenerateCode(reader, className: DefaultClassName, rootNamespace: DefaultNamespace, sourceFileName: _fullPath);
                }
            }
            catch (Exception e)
            {
                OnGenerateError(4, e.ToString(), 1, 1);
                //Returning null signifies that generation has failed
                return(null);
            }

            // Output errors
            foreach (RazorError error in results.ParserErrors)
            {
                OnGenerateError(4, error.Message, (uint)error.Location.LineIndex + 1, (uint)error.Location.CharacterIndex + 1);
            }

            try
            {
                OnCodeCompletion(50, 100);

                using (StringWriter writer = new StringWriter())
                {
                    CodeGeneratorOptions options = new CodeGeneratorOptions();
                    options.BlankLinesBetweenMembers = false;
                    options.BracingStyle             = "C";

                    //Generate the code
                    writer.WriteLine(CodeLanguageUtil.GetPreGeneratedCodeBlock());
                    _codeDomProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, writer, options);
                    writer.WriteLine(CodeLanguageUtil.GetPostGeneratedCodeBlock());

                    OnCodeCompletion(100, 100);
                    writer.Flush();

                    // Perform output transformations and return
                    string codeContent = writer.ToString();
                    codeContent = _codeTransformer.ProcessOutput(codeContent);
                    return(codeContent);
                }
            }
            catch (Exception e)
            {
                OnGenerateError(4, e.ToString(), 1, 1);
                //Returning null signifies that generation has failed
                return(null);
            }
        }