/// <summary>
        ///     Executes all parsed code and returns the resulting document.
        /// </summary>
        /// <param name="outputPath">The path of the file to store the resulting text in.</param>
        /// <returns>The resulting document.</returns>
        private string Execute(string outputPath)
        {
            scriptBuilder.AppendPropertiesScript(scriptProperties);

            string script = scriptBuilder.ToString();

            if (!string.IsNullOrEmpty(outputPath))
            {
                WriteScript(script, outputPath);
            }

            Assembly assembly;
            string cacheKey = GetHashCode(script);

            lock (assemblyCacheLock)
            {
                if (!assemblyCache.ContainsKey(cacheKey))
                {
                    CompilerResults results = CompileScript(script);

                    foreach (CompilerError error in results.Errors.Cast<CompilerError>().OrderBy(n => n.Line))
                    {
                        TextTemplateFileSourceReference sourceReference = new TextTemplateFileSourceReference()
                        {
                            Path = GetSourceFilePath(error.FileName),
                            Line = error.Line
                        };

                        if (!error.IsWarning)
                        {
                            TextTemplateFileException templateFileException = new TextTemplateFileException(GetCompilerErrorMessage(error), sourceReference);

                            throw templateFileException;
                        }
                    }

                    assembly = results.CompiledAssembly;
                    assemblyCache[cacheKey] = assembly;
                }
                else
                {
                    assembly = assemblyCache[cacheKey];
                }

                TextTemplateOutputWriter outputWriter = new TextTemplateOutputWriter();
                object documentScriptsInstance = assembly.CreateInstance(Constants.NamespaceName + "." + Constants.ClassName);

                // Call the property intialization method.
                Type classType = assembly.GetType(Constants.NamespaceName + "." + Constants.ClassName);
                MethodInfo initMethod = classType.GetMethod(Constants.PropertyInitializationMethodName);
                initMethod.Invoke(documentScriptsInstance, propertyValues);

                // Call the intialization method.
                classType = assembly.GetType(Constants.NamespaceName + "." + Constants.ClassName);
                initMethod = classType.GetMethod(Constants.InitializationMethodName);
                initMethod.Invoke(documentScriptsInstance, new[] { outputWriter, context, this });

                // Execute the main method.
                outputWriter.StartCapture();

                MethodInfo mainMethod = classType.GetMethod(scriptBuilder.MainMethodName);
                mainMethod.Invoke(documentScriptsInstance, new object[] { });

                return outputWriter.EndCapture();
            }
        }
Example #2
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="TextTemplateFileException"/> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="sourceReference">The source reference.</param>
 public TextTemplateFileException(string message, TextTemplateFileSourceReference sourceReference)
     : this(message)
 {
     this.SourceReference = sourceReference;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="TextTemplateFileException"/> class.
 /// </summary>
 /// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
 /// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
 protected TextTemplateFileException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     SourceReference = (TextTemplateFileSourceReference)info.GetValue("SourceReference", typeof(TextTemplateFileSourceReference));
 }
Example #4
0
        /// <summary>
        ///     Executes all parsed code and returns the resulting document.
        /// </summary>
        /// <param name="outputPath">The path of the file to store the resulting text in.</param>
        /// <returns>The resulting document.</returns>
        private string Execute(string outputPath)
        {
            scriptBuilder.AppendPropertiesScript(scriptProperties);

            string script = scriptBuilder.ToString();

            if (!string.IsNullOrEmpty(outputPath))
            {
                WriteScript(script, outputPath);
            }

            Assembly assembly;
            string   cacheKey = GetHashCode(script);

            lock (assemblyCacheLock)
            {
                if (!assemblyCache.ContainsKey(cacheKey))
                {
                    CompilerResults results = CompileScript(script);

                    foreach (CompilerError error in results.Errors.Cast <CompilerError>().OrderBy(n => n.Line))
                    {
                        TextTemplateFileSourceReference sourceReference = new TextTemplateFileSourceReference()
                        {
                            Path = GetSourceFilePath(error.FileName),
                            Line = error.Line
                        };

                        if (!error.IsWarning)
                        {
                            TextTemplateFileException templateFileException = new TextTemplateFileException(GetCompilerErrorMessage(error), sourceReference);

                            throw templateFileException;
                        }
                    }

                    assembly = results.CompiledAssembly;
                    assemblyCache[cacheKey] = assembly;
                }
                else
                {
                    assembly = assemblyCache[cacheKey];
                }

                TextTemplateOutputWriter outputWriter = new TextTemplateOutputWriter();
                object documentScriptsInstance        = assembly.CreateInstance(Constants.NamespaceName + "." + Constants.ClassName);

                // Call the property intialization method.
                Type       classType  = assembly.GetType(Constants.NamespaceName + "." + Constants.ClassName);
                MethodInfo initMethod = classType.GetMethod(Constants.PropertyInitializationMethodName);
                initMethod.Invoke(documentScriptsInstance, propertyValues);

                // Call the intialization method.
                classType  = assembly.GetType(Constants.NamespaceName + "." + Constants.ClassName);
                initMethod = classType.GetMethod(Constants.InitializationMethodName);
                initMethod.Invoke(documentScriptsInstance, new[] { outputWriter, context, this });

                // Execute the main method.
                outputWriter.StartCapture();

                MethodInfo mainMethod = classType.GetMethod(scriptBuilder.MainMethodName);
                mainMethod.Invoke(documentScriptsInstance, new object[] { });

                return(outputWriter.EndCapture());
            }
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="TextTemplateFileException"/> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="sourceReference">The source reference.</param>
 public TextTemplateFileException(string message, TextTemplateFileSourceReference sourceReference)
     : this(message)
 {
     this.SourceReference = sourceReference;
 }