Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (Environment.UserInteractive)
            {
                var options = new Options();
                CommandLine.Parser.Default.ParseArguments(args, options);

                if (options.LastParserState != null && options.LastParserState.Errors.Any())
                {
                    return;
                }

                if (options.Parameters != null && options.Parameters.Any())
                {
                    options.Parameters.ForEach(Console.WriteLine);
                }

                if (options.Init)
                {
                    Initializer.InitialiseCurrentDirectory();
                }
                else
                {
                    Initializer.StartBot(options);
                }
            }
            else
            {
                var options = new Options();
                CommandLine.Parser.Default.ParseArguments(args, options);
                ServiceBase.Run(new ServiceBase[] { new Service(options) });
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var options = new Options();
            CommandLine.Parser.Default.ParseArguments(args, options);

            if (options.ShowHelp)
            {
                return;
            }

            if (options.RunAsService)
            {
                ServiceBase.Run(new ServiceBase[] { new Service(options) });
            }
            else
            {
                if (options.LastParserState != null && options.LastParserState.Errors.Any())
                {
                    return;
                }

                if (options.Parameters != null && options.Parameters.Any())
                {
                    options.Parameters.ForEach(Console.WriteLine);
                }

                if (options.Init)
                {
                    Initializer.InitializeCurrentDirectory();
                    return;
                }

                SetupRobot(options);
            }
        }
Ejemplo n.º 3
0
        public static async Task<Robot> 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 null;
            }

            var logConfig = CreateLogConfig(options);
            ConfigurePath(options, logConfig.GetLogger());

            var builder = new RobotBuilder(logConfig).WithConfiguration(GetConfiguration(options));

            if (!string.IsNullOrWhiteSpace(options.Name))
            {
                builder.WithName(options.Name);
            }

            if (options.Test)
            {
                builder.DisableScriptDiscovery();
            }

            if (!string.IsNullOrEmpty(options.WorkingDirectory))
            {
                builder.UseWorkingDirectory(options.WorkingDirectory);
            }

            if (options.Watch)
            {
                builder.EnableScriptWatcher();
            }

            Robot robot = null;

            try
            {
                robot = builder.Build();
            }
            catch (Exception e)
            {
                logConfig.GetLogger().Fatal("Could not build robot. Try installing the latest version of any mmbot packages (mmbot.jabbr, mmbot.slack etc) if there was a breaking change.", e);
            }

            if (robot == null)
            {
                return null;
            }

            await 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");
                }
            });
            return robot;
        }
Ejemplo n.º 4
0
 public void Start(Options options)
 {
     _options = options;
     var robot = Initializer.StartBot(options).Result;
     var resetEvent = new AutoResetEvent(false);
     robot.ResetRequested += (sender, args) => resetEvent.Set();
     resetEvent.WaitOne();
 }
Ejemplo n.º 5
0
        private static void SetupRobot(Options options)
        {
            var childAppDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString("N"));
            var wrapper = childAppDomain.CreateInstanceAndUnwrap(typeof (RobotWrapper).Assembly.FullName,
                typeof (RobotWrapper).FullName) as RobotWrapper;

            wrapper.Start(options); //Blocks, waiting on a reset event.
            AppDomain.Unload(childAppDomain);
            SetupRobot(options);
        }
Ejemplo n.º 6
0
        public static Dictionary<string, string> GetConfiguration(Options options)
        {
            if (!options.SkipConfiguration && File.Exists("mmbot.ini"))
            {
                var config = new ConfigurationFileParser(Path.GetFullPath("mmbot.ini"));
                return config.GetConfiguration();
            }

            return new Dictionary<string, string>();
        }
Ejemplo n.º 7
0
        public void Start(Options options)
        {
            _robot = Initializer.StartBot(options).Result;

            if (_robot == null)
            {
                // Something went wrong. Abort
                Environment.Exit(-1);
            }

            _resetEvent = new AutoResetEvent(false);
            _robot.ResetRequested += (sender, args) => _resetEvent.Set();
            _resetEvent.WaitOne();
        }
Ejemplo n.º 8
0
        public  static void Run(Options options)
        {
            var childAppDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString("N"));
            _wrapper = childAppDomain.CreateInstanceAndUnwrap(typeof(RobotWrapper).Assembly.FullName,
                typeof(RobotWrapper).FullName) as RobotWrapper;

            _wrapper.Start(options); //Blocks, waiting on a reset event.

            //Select and ToList called to force re-instantiation of all strings and list itself inside of this outer AppDomain
            //or we will get crazy exceptions related to disposing/unloading of the child AppDomain in which the bot itself runs

            AppDomain.Unload(childAppDomain);

            PackageDirCleaner.CleanUpPackages();

            if (!_stopRequested)
            {
                Run(options);            
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            var options = new Options();
            CommandLine.Parser.Default.ParseArguments(args, options);

            if (options.ShowHelp)
            {
                return;
            }

            if (options.RunAsService)
            {
                ServiceBase.Run(new ServiceBase[] { new Service(options) });
            }
            else
            {
                if (options.LastParserState != null && options.LastParserState.Errors.Any())
                {
                    return;
                }

                if (options.Parameters != null && options.Parameters.Any())
                {
                    options.Parameters.ForEach(Console.WriteLine);
                }

                if (options.Init)
                {
                    Initializer.InitializeCurrentDirectory();
                    return;
                }

                Initializer.StartBot(options).Wait();

                while (true)
                {
                    // sit and spin?
                    Thread.Sleep(2000);
                }
            }
        }
Ejemplo n.º 10
0
 public Service(Options options)
 {
     _options = options;
     InitializeComponent();
 }
Ejemplo n.º 11
0
        public static LoggerConfigurator CreateLogConfig(Options options)
        {
            var logConfig = new LoggerConfigurator(options.Verbose ? LogLevel.Debug : LogLevel.Info);
            if (Environment.UserInteractive)
            {
                logConfig.ConfigureForConsole();
            }
            else
            {
                logConfig.AddTraceListener();
            }

            var logger = logConfig.GetLogger();

            if (!string.IsNullOrWhiteSpace(options.LogFile))
            {
                if (Directory.Exists(Path.GetDirectoryName(options.LogFile)))
                    logConfig.ConfigureForFile(options.LogFile);
                else
                    logger.Warn(string.Format("Failed to load log file.  Path for {0} does not exist.", options.LogFile));
            }
            return logConfig;
        }
Ejemplo n.º 12
0
 public static void ConfigurePath(Options options, ILog logger)
 {
     if (!string.IsNullOrWhiteSpace(options.WorkingDirectory))
     {
         if (!Directory.Exists(options.WorkingDirectory))
         {
             logger.Warn(string.Format("Could not find specified working directory {0}. Defaulting to current directory", options.WorkingDirectory));
         }
         else
         {
             Directory.SetCurrentDirectory(options.WorkingDirectory);
         }
     }
 }
Ejemplo n.º 13
0
        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);
            }
        }
Ejemplo n.º 14
0
 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?");
         }
     }
 }
Ejemplo n.º 15
0
        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;
        }