static Driver() { L = Log.ForContext <Driver>(); AllLoadedAssemblies = Assembly.GetExecutingAssembly().LoadAllFrom("ClassifyBot.*.dll", "ClassifyBot.Base.dll", "ClassifyBot.Cli.dll"); foreach (string n in ExcludedAssemblyNames) { if (AllLoadedAssemblies.Any(a => a.FullName.StartsWith(n))) { AllLoadedAssemblies.RemoveAll(a => a.FullName.StartsWith(n)); } } if (AllLoadedAssemblies == null) { throw new Exception("Did not load assembly ClassifyBot.Core.dll"); } }
public static Type[] GetSubTypes <T>(string assemblyName = "") { IEnumerable <Assembly> assemblies = AllLoadedAssemblies; if (AllLoadedAssemblies.Count(a => assemblyName.IsNotEmpty() && a.GetName().Name == assemblyName) > 0) { assemblies = AllLoadedAssemblies.Where(a => a.FullName.StartsWith(assemblyName)); } else if (assemblyName.IsNotEmpty()) { return(null); } return(assemblies .Select(a => a.GetTypes()) .SelectMany(t => t) .Where(t => t.IsSubclassOf(typeof(T)) && !t.IsAbstract)? .ToArray()); }
public static StageResult MarshalOptionsForStage(string[] args, string explicitAssemblyName, out Stage stage, out string optionsHelp) { optionsHelp = string.Empty; Stage s = null; Parser p = new Parser(); Type[] types = GetSubTypes <Stage>(explicitAssemblyName); if ((types == null || types.Length == 0) && explicitAssemblyName.IsNotEmpty()) { stage = null; L.Error("No assemblies matching the name {0}.dll in directory {1} were found.".F(explicitAssemblyName, AssemblyExtensions.GetExecutingAssemblyDirectoryName())); optionsHelp = "You must enter an assembly name to explicitly load. Valid assembly names are : {0}." .F(string.Join(", ", AllLoadedAssemblies.Select(a => a.GetName().Name).ToArray())); return(StageResult.INVALID_OPTIONS); } else { ClassifyBotLoadedAssemblies = types.Select(t => t.Assembly).Distinct().ToList(); if (ClassifyBotLoadedAssemblies.Count == 1) { L.Information("Loaded 1 ClassifyBot assembly: {1}.", ClassifyBotLoadedAssemblies.Count(), ClassifyBotLoadedAssemblies.Select(a => a.FullName)); } else { L.Information("Loaded {0} ClassifyBot assemblies: {1}.", ClassifyBotLoadedAssemblies.Count(), ClassifyBotLoadedAssemblies.Select(a => a.FullName)); } } ParserResult <object> options = p.ParseArguments(args, types); string oh = string.Empty; StageResult sr = StageResult.INVALID_OPTIONS; options .WithNotParsed((errors) => { oh = GetHelpForInvalidOptions(options, errors); sr = StageResult.INVALID_OPTIONS; }) .WithParsed((o) => { s = (Stage)o; sr = StageResult.CREATED; }); optionsHelp = oh; stage = s; return(sr); }
public static void RunAndExit(string[] args) { if (args.Contains("--wait-for-attach")) { Console.WriteLine("Attach debugger and press any key to continue execution..."); Console.ReadKey(true); if (!Debugger.IsAttached) { Console.WriteLine("No debugger detected! Exiting."); return; } else { Debugger.Break(); } } if (args.Contains("--with-debug")) { WithDebugOutput = true; } if (args.Contains("--with-log-file")) { WithLogFile = true; } if (args.Contains("--without-console")) { WithoutConsole = true; } LoggerConfiguration = new LoggerConfiguration() .Enrich.FromLogContext() .Enrich.WithThreadId(); if (WithDebugOutput) { LoggerConfiguration = LoggerConfiguration.MinimumLevel.Debug(); } if (!WithoutConsole) { LoggerConfiguration = LoggerConfiguration.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} [{ThreadId:d2}][{Level:u3}] {Message}{NewLine}{Exception}"); } if (WithLogFile) { LogFileName = Path.Combine("logs", "ClassifyBot") + "-{Date}.log"; LoggerConfiguration = LoggerConfiguration.WriteTo.RollingFile(LogFileName, outputTemplate: "{Timestamp:HH:mm:ss}[{ThreadId:d2}] [{Level:u3}] {Message}{NewLine}{Exception}"); } Log.Logger = LoggerConfiguration.CreateLogger(); L = Log.ForContext <Driver>(); ExitToEnvironment = true; if (WithLogFile) { L.Information("Log file is at {0}.", LogFileName); } for (int i = 0; i < args.Length; i++) { if (args[i] == "-x" || args[i] == "--explicit") { if ((i + 1) <= args.Length - 1) { ExplicitAssemblyName = args[i + 1].StartsWith("ClassifyBot.") ? args[i + 1] : "ClassifyBot." + args[i + 1]; args = args.Except(new string[] { "-x", "--explicit", args[i + 1] }).ToArray(); } else { L.Error("You must enter an assembly name to explicitly load. Valid assembly names are : {0}." .F(string.Join(", ", AllLoadedAssemblies.Select(a => a.GetName().Name).ToArray()))); Exit(StageResult.INVALID_OPTIONS); } break; } } StageResult result = MarshalOptionsForStage(args, ExplicitAssemblyName, out Stage stage, out string optionsHelp); if (result == StageResult.INVALID_OPTIONS && stage == null && !optionsHelp.IsEmpty()) { L.Information(optionsHelp); } else if (result == StageResult.CREATED && stage != null && optionsHelp.IsEmpty()) { Exit(stage.Run()); } else { throw new Exception("Unknown stage state {0} {1}.".F(result, stage)); } }