/// <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();
            }
        }
Пример #2
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());
            }
        }