private bool RunScriptFile(string path) { string hash; using (var stream = File.OpenRead(path)) { hash = Encoding.UTF8.GetString(System.Security.Cryptography.MD5.Create().ComputeHash(stream)); } var scriptName = Path.GetFileNameWithoutExtension(path); string value; if (scriptName != null && scriptHashes.TryGetValue(scriptName, out value) && value == hash) { return(false); } using (StartScriptProcessingSession(new ScriptSource(scriptName, path))) { try { ParseScriptComments(path); } catch (Exception ex) { _logger.Warn(string.Format("Could not parse comments: {0}", ex.Message)); } var console = new ScriptConsole(); var scriptServicesBuilder = new ScriptServicesBuilder(console, _logger); scriptServicesBuilder.Cache(false); scriptServicesBuilder.LoadModules("csx"); var scriptServiceRoot = scriptServicesBuilder.Build(); var defaultReferences = ScriptExecutor.DefaultReferences.ToArray(); var fileSystem = new FileSystem(); //where clause hack using the exact same code that the hack in scriptCS sues to filter their list of assemblies in ShouldLoadAssembly in RuntimeServices.cs var packageReferences = scriptServiceRoot.PackageAssemblyResolver.GetAssemblyNames(Environment.CurrentDirectory).Where(fileSystem.IsPathRooted); scriptServiceRoot.Executor.AddReferences(defaultReferences.Concat(NuGetPackageAssemblyResolver.FilterAssembliesToMostRecent(packageReferences)).ToArray()); scriptServiceRoot.Executor.ImportNamespaces( ScriptExecutor.DefaultNamespaces.Concat(new[] { "MMBot", "Newtonsoft.Json", "Newtonsoft.Json.Linq", "HtmlAgilityPack", "System.Xml", "System.Net", "System.Net.Http" }).ToArray()); scriptServiceRoot.Executor.AddReference <Robot>(); scriptServiceRoot.Executor.AddReference <ILog>(); scriptServiceRoot.Executor.AddReference <JArray>(); scriptServiceRoot.Executor.AddReference <HtmlDocument>(); scriptServiceRoot.Executor.AddReference <HttpResponseMessage>(); scriptServiceRoot.Executor.AddReference <IScriptPackContext>(); scriptServiceRoot.Executor.AddReference <OwinContext>(); scriptServiceRoot.Executor.Initialize(new string[0], new IScriptPack[] { new MMBot2ScriptPackInternal(_robot), }); var result = scriptServiceRoot.Executor.Execute(path); if (!result.IsCompleteSubmission) { _logger.Error(string.Format("{0}: error compiling script - {1}", path, result.CompileExceptionInfo.SourceException.Message)); } if (result.CompileExceptionInfo != null) { _logger.Error(result.CompileExceptionInfo.SourceException.Message); _logger.Debug(result.CompileExceptionInfo.SourceException); } if (result.ExecuteExceptionInfo != null) { _logger.Error(result.ExecuteExceptionInfo.SourceException); } scriptHashes[CurrentScriptSource.Name] = hash; return(result.IsCompleteSubmission && result.CompileExceptionInfo == null && result.ExecuteExceptionInfo == null); } }
public static void StartBot(Options options) { if (options.Test && (options.ScriptFiles == null || !options.ScriptFiles.Any())) { Console.WriteLine("You need to specify at least one script file to test."); return; } var logger = CreateLogger(options); ConfigurePath(options, logger); var nugetResolver = new NuGetPackageAssemblyResolver(logger); AppDomain.CurrentDomain.AssemblyResolve += nugetResolver.OnAssemblyResolve; var adapters = DiscoverAdapters(options, nugetResolver, logger); var configuration = GetConfiguration(options); string name; var robot = Robot.Create(configuration.TryGetValue("MMBOT_ROBOT_NAME", out name) ? name : "mmbot", configuration, logger, adapters.Concat(new []{typeof(ConsoleAdapter)}).ToArray()); ConfigureRouter(robot, nugetResolver); LoadScripts(options, robot, nugetResolver, logger); robot.Run().ContinueWith(t => { if (!t.IsFaulted) { Console.WriteLine(IntroText); Console.WriteLine((options.Test ? "The test console is ready. " : "mmbot is running. ") + "Press CTRL+C at any time to exit" ); } }); while (true) { // sit and spin? Thread.Sleep(2000); } }
private bool RunScriptFile(string path) { using (StartScriptProcessingSession(new ScriptSource(Path.GetFileNameWithoutExtension(path), path))) { try { ParseScriptComments(path); } catch (Exception ex) { _logger.Warn(string.Format("Could not parse comments: {0}", ex.Message)); } var console = new ScriptConsole(); var scriptServicesBuilder = new ScriptServicesBuilder(console, _logger); scriptServicesBuilder.Cache(false); scriptServicesBuilder.LoadModules("csx", new string[0]); var scriptServiceRoot = scriptServicesBuilder.Build(); var defaultReferences = ScriptExecutor.DefaultReferences.ToArray(); var packageReferences = scriptServiceRoot.PackageAssemblyResolver.GetAssemblyNames(Environment.CurrentDirectory); scriptServiceRoot.Executor.AddReferences(defaultReferences.Concat(NuGetPackageAssemblyResolver.FilterAssembliesToMostRecent(packageReferences)).ToArray()); scriptServiceRoot.Executor.ImportNamespaces( ScriptExecutor.DefaultNamespaces.Concat(new[] { "MMBot", "Newtonsoft.Json", "Newtonsoft.Json.Linq", "HtmlAgilityPack", "System.Xml", "System.Net", "System.Net.Http" }).ToArray()); scriptServiceRoot.Executor.AddReference <Robot>(); scriptServiceRoot.Executor.AddReference <ILog>(); scriptServiceRoot.Executor.AddReference <JArray>(); scriptServiceRoot.Executor.AddReference <HtmlDocument>(); scriptServiceRoot.Executor.AddReference <HttpResponseMessage>(); scriptServiceRoot.Executor.AddReference <IScriptPackContext>(); scriptServiceRoot.Executor.AddReference <OwinContext>(); scriptServiceRoot.Executor.Initialize(new string[0], new IScriptPack[] { new MMBot2ScriptPackInternal(_robot), }); var result = scriptServiceRoot.Executor.Execute(path); if (result.CompileExceptionInfo != null) { _logger.Error(result.CompileExceptionInfo.SourceException.Message); _logger.Debug(result.CompileExceptionInfo.SourceException); } if (result.ExecuteExceptionInfo != null) { _logger.Error(result.ExecuteExceptionInfo.SourceException); } return(result.CompileExceptionInfo == null && result.ExecuteExceptionInfo == null); } }
private static void LoadScripts(Options options, Robot robot, NuGetPackageAssemblyResolver nugetResolver, ILog logger) { if (options.Test) { robot.AutoLoadScripts = false; options.ScriptFiles.ForEach(robot.LoadScriptFile); } else { robot.LoadScripts(nugetResolver.GetCompiledScriptsFromPackages()); if (!Directory.Exists(Path.Combine(Environment.CurrentDirectory, "scripts"))) { logger.Warn( "There is no scripts folder. Have you forgotten to run 'mmbot init' to initialise the current running directory?"); } } }
private static IEnumerable<Type> DiscoverAdapters(Options options, NuGetPackageAssemblyResolver nugetResolver, ILog logger) { var adapters = new Type[0]; if (!options.Test) { adapters = LoadAdapters(nugetResolver, logger); } return adapters; }
private static void ConfigureRouter(Robot robot, NuGetPackageAssemblyResolver nugetResolver) { var robotEnabledVar = robot.GetConfigVariable("MMBOT_ROUTER_ENABLED"); if (robotEnabledVar != null && robotEnabledVar.ToLower() == "true" || robotEnabledVar == "yes") { var routerType = nugetResolver.GetCompiledRouterFromPackages(); if (routerType != null) { robot.Logger.Info(string.Format("Loading router '{0}'", routerType.Name)); robot.ConfigureRouter(routerType); } else { robot.Logger.Warn("The router was enabled but no implementation was found. Make sure you have installed the relevant router package"); } } }
internal static Type[] LoadAdapters(NuGetPackageAssemblyResolver nugetResolver, ILog logger) { var adapters = nugetResolver.GetCompiledAdaptersFromPackages().ToArray(); if (!adapters.Any()) { logger.Warn("Could not find any adapters. Loading the default console adapter only"); } return adapters; }
private bool RunScriptFile(string path) { string hash; using (var stream = File.OpenRead(path)) { hash = Encoding.UTF8.GetString(System.Security.Cryptography.MD5.Create().ComputeHash(stream)); } var scriptName = Path.GetFileNameWithoutExtension(path); if (scriptName != null && scriptHashes.ContainsKey(scriptName) && scriptHashes[scriptName] == hash) { return(false); } using (StartScriptProcessingSession(new ScriptSource(scriptName, path))) { try { ParseScriptComments(path); } catch (Exception ex) { _logger.Warn(string.Format("Could not parse comments: {0}", ex.Message)); } var console = new ScriptConsole(); var scriptServicesBuilder = new ScriptServicesBuilder(console, _logger); scriptServicesBuilder.Cache(false); scriptServicesBuilder.LoadModules("csx", new string[0]); var scriptServiceRoot = scriptServicesBuilder.Build(); var defaultReferences = ScriptExecutor.DefaultReferences.ToArray(); var packageReferences = scriptServiceRoot.PackageAssemblyResolver.GetAssemblyNames(Environment.CurrentDirectory); scriptServiceRoot.Executor.AddReferences(defaultReferences.Concat(NuGetPackageAssemblyResolver.FilterAssembliesToMostRecent(packageReferences)).ToArray()); scriptServiceRoot.Executor.ImportNamespaces( ScriptExecutor.DefaultNamespaces.Concat(new[] { "MMBot", "Newtonsoft.Json", "Newtonsoft.Json.Linq", "HtmlAgilityPack", "System.Xml", "System.Net", "System.Net.Http" }).ToArray()); scriptServiceRoot.Executor.AddReference <Robot>(); scriptServiceRoot.Executor.AddReference <ILog>(); scriptServiceRoot.Executor.AddReference <JArray>(); scriptServiceRoot.Executor.AddReference <HtmlDocument>(); scriptServiceRoot.Executor.AddReference <HttpResponseMessage>(); scriptServiceRoot.Executor.AddReference <IScriptPackContext>(); scriptServiceRoot.Executor.AddReference <OwinContext>(); scriptServiceRoot.Executor.Initialize(new string[0], new IScriptPack[] { new MMBot2ScriptPackInternal(_robot), }); var result = scriptServiceRoot.Executor.Execute(path); if (result.ExpectingClosingChar.HasValue) { _logger.Error(string.Format("{0}: closing {1} expected", path, result.ExpectingClosingChar.Value)); } if (result.CompileExceptionInfo != null) { _logger.Error(result.CompileExceptionInfo.SourceException.Message); _logger.Debug(result.CompileExceptionInfo.SourceException); } if (result.ExecuteExceptionInfo != null) { _logger.Error(result.ExecuteExceptionInfo.SourceException); } scriptHashes[CurrentScriptSource.Name] = hash; return(!result.ExpectingClosingChar.HasValue && result.CompileExceptionInfo == null && result.ExecuteExceptionInfo == null); } }