/// <summary>
        /// Creates an instance of wwScripting for this parser
        /// with the appropriate assemblies and namespaces set
        /// </summary>
        /// <returns></returns>
        private CSharpScriptExecution CreateScriptObject()
        {
            var scripting = new CSharpScriptExecution
            {
                GeneratedNamespace    = "MarkdownMonster.Commander.Scripting",
                ThrowExceptions       = false,
                AllowReferencesInCode = true
            };

            // Use loaded references so **all of MM is available**
            scripting.AddLoadedReferences();
            //.AddDefaultReferencesAndNamespaces();

            scripting.AddAssembly(typeof(CommanderAddin));


            scripting.AddNamespaces("System",
                                    "System.Threading.Tasks",
                                    "System.IO",
                                    "System.Reflection",
                                    "System.Text",
                                    "System.Drawing",
                                    "System.Diagnostics",
                                    "System.Data",
                                    "System.Data.SqlClient",
                                    "System.Linq",
                                    "System.Windows",
                                    "System.Windows.Controls",
                                    "System.Collections.Generic",

                                    "Newtonsoft.Json",
                                    "Newtonsoft.Json.Linq",

                                    "MarkdownMonster",
                                    "MarkdownMonster.Windows",
                                    "Westwind.Utilities",
                                    "CommanderAddin");

            scripting.SaveGeneratedCode = true;

            return(scripting);
        }
Beispiel #2
0
        /// <summary>
        /// Evaluates the embedded script parsing out {{ C# Expression }}
        /// blocks and evaluating the expressions and embedding the string
        /// output into the result string.
        ///
        ///
        /// </summary>
        /// <param name="snippet">The snippet template to expand</param>
        /// <param name="model">Optional model data accessible in Expressions as `Model`</param>
        /// <returns></returns>
        public string EvaluateScript(string snippet, object model = null)
        {
#if DEBUG
            var sw = new Stopwatch();
            sw.Start();
#endif
            var tokens = TokenizeString(ref snippet, "{{", "}}");

            snippet = snippet.Replace("\"", "\"\"");

            snippet = DetokenizeString(snippet, tokens);

            snippet = snippet.Replace("{{", "\" + ").Replace("}}", " + @\"");
            snippet = "@\"" + snippet + "\"";


            string code = "dynamic Model = parameters[0];\r\n" +
                          "return " + snippet + ";";

            //var scriptCompiler = new ScriptRunnerRoslyn();
            var scriptCompiler = new CSharpScriptExecution()
            {
                CompilerMode = ScriptCompilerModes.Roslyn
            };
            scriptCompiler.AddDefaultReferencesAndNamespaces();

            scriptCompiler.AddAssemblies("System.dll",
                                         "System.Core.dll",
                                         "System.Drawing.dll",
                                         "Microsoft.CSharp.dll",
                                         "System.Data.dll",
                                         "MarkdownMonster.exe",
                                         "Westwind.Utilities.dll",
                                         "System.Configuration.dll",
                                         "Newtonsoft.Json.dll");

            scriptCompiler.AddNamespaces("System",
                                         "System.IO",
                                         "System.Reflection",
                                         "System.Text",
                                         "System.Drawing",
                                         "System.Diagnostics",
                                         "System.Data",
                                         "System.Data.SqlClient",
                                         "System.Linq",
                                         "System.Collections.Generic",
                                         "Newtonsoft.Json",
                                         "Newtonsoft.Json.Linq",
                                         "MarkdownMonster",
                                         "Westwind.Utilities");

            string result = scriptCompiler.ExecuteCode(code, model) as string;

            if (result == null)
            {
                ErrorMessage = scriptCompiler.ErrorMessage;
            }
            else
            {
                ErrorMessage = null;
            }

#if DEBUG
            sw.Stop();
            Debug.WriteLine("ScriptParser Code: \r\n" + code);
            Debug.WriteLine("Snippet EvaluateScript Execution Time: " + sw.ElapsedMilliseconds + "ms");
#endif

            return(result);
        }