public string GenerateSourceCode() { var razorResults = Generate(); using (var writer = new StringWriter()) { var options = new CodeGeneratorOptions { BlankLinesBetweenMembers = false, BracingStyle = "C" }; //Generate the code writer.WriteLine("#pragma warning disable 1591"); _codeDomProvider.GenerateCodeFromCompileUnit(razorResults.GeneratedCode, writer, options); writer.WriteLine("#pragma warning restore 1591"); OnCodeCompletion(); writer.Flush(); // Perform output transformations and return string codeContent = writer.ToString(); codeContent = _codeTransformer.ProcessOutput(codeContent); return(codeContent); } }
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); } }
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); } }