コード例 #1
0
        public GeneratorResults Generate()
        {
            lock (this)
            {
                _codeTransformer.Initialize(this, _directives);

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

                // Generate code
                GeneratorResults results = null;
                try
                {
                    using (var stream = File.OpenRead())
                        using (var reader = new StreamReader(stream, Encoding.Default, detectEncodingFromByteOrderMarks: true))
                        {
                            results = engine.GenerateCode(reader, className: DefaultClassName, rootNamespace: DefaultNamespace, sourceFileName: this.File.RealPath);
                        }
                }
                catch (Exception e)
                {
                    OnGenerateError(4, e.Message, 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);
                }

                return(results);
            }
        }
コード例 #2
0
        public GeneratorResults Generate()
        {
            lock (this)
            {
                _codeTransformer.Initialize(this, _directives);

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

                // Generate code
                GeneratorResults results = null;
                try
                {
                    using (var stream = File.OpenRead())
                    {
                        var reader = new StreamReader(stream, Encoding.Default, detectEncodingFromByteOrderMarks: true);
                        results = engine.GenerateCode(reader, className: DefaultClassName, rootNamespace: DefaultNamespace, sourceFileName: this.File.RealPath);
                    }
                }
                catch (Exception e)
                {
                    throw new HttpParseException(e.Message, e, GetAbsoluteErrorPath(this.File.VirtualPath), null, 1);
                }

                //Throw the first parser message to generate the YSOD
                //TODO: Is there a way to output all errors at once?
                if (results.ParserErrors.Count > 0)
                {
                    var error = results.ParserErrors[0];
                    throw new HttpParseException(error.Message, null, GetAbsoluteErrorPath(this.File.VirtualPath), null, error.Location.LineIndex + 1);
                }

                return(results);
            }
        }
コード例 #3
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.UTF8))
                {
                    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);
            }
        }
コード例 #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.UTF8))
                {
                    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("#pragma warning disable 1591");
                    _codeDomProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, writer, options);
                    writer.WriteLine("#pragma warning restore 1591");

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

                    // Perform output transformations and return
                    string codeContent = writer.ToString();
                    codeContent = _codeTransformer.ProcessOutput(codeContent);

                    //TridionVSRazorExtension hack : transform into partial class

                    string baseNamespace = _directives["VsNamespace"].Split('.')[0];

                    codeContent = codeContent.Replace("namespace " + DefaultNamespace, "namespace " + baseNamespace);

                    codeContent = codeContent.Replace(
                        "public partial class " + DefaultClassName + " : " + baseNamespace + ".WrappedTridionRazorTemplate<dynamic>",
                        "public partial class WrappedTridionRazorTemplate"
                        );

                    int intConstructorStart = codeContent.IndexOf("public " + DefaultClassName + "()");
                    if (intConstructorStart > -1)
                    {
                        string strConstructor = codeContent.Substring(intConstructorStart, codeContent.IndexOf("}", intConstructorStart) - intConstructorStart + 1);
                        codeContent = codeContent.Replace(strConstructor, "");
                    }

                    int intExecuteStart = codeContent.IndexOf("public override void Execute()");
                    if (intExecuteStart > -1)
                    {
                        string strExecute = codeContent.Substring(intExecuteStart, codeContent.IndexOf("}", intExecuteStart) - intExecuteStart + 1);
                        codeContent = codeContent.Replace(strExecute, "");
                    }

                    int intAttribute1Start = codeContent.IndexOf("[System.CodeDom.Compiler.GeneratedCodeAttribute(");
                    if (intAttribute1Start > -1)
                    {
                        string strAttribute1 = codeContent.Substring(intAttribute1Start, codeContent.IndexOf("]", intAttribute1Start) - intAttribute1Start + 1);
                        codeContent = codeContent.Replace(strAttribute1, "");
                    }

                    int intAttribute2Start = codeContent.IndexOf("[System.Web.WebPages.PageVirtualPathAttribute(");
                    if (intAttribute2Start > -1)
                    {
                        string strAttribute2 = codeContent.Substring(intAttribute2Start, codeContent.IndexOf("]", intAttribute2Start) - intAttribute2Start + 1);
                        codeContent = codeContent.Replace(strAttribute2, "");
                    }

                    return(codeContent);
                }
            }
            catch (Exception e)
            {
                OnGenerateError(4, e.ToString(), 1, 1);
                //Returning null signifies that generation has failed
                return(null);
            }
        }