Пример #1
0
        public void LoadModulesInExecutionFolder()
        {
            ConsoleApplicationController.WriteToConsole("Loading Modules");
            var    codeBase = Assembly.GetExecutingAssembly().Location;
            var    uri      = new UriBuilder(codeBase);
            string path     = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));

            foreach (string dll in Directory.GetFiles(path, "*.dll"))
            {
                var loadIt = Assembly.LoadFile(dll);
                foreach (var type in loadIt.GetTypes())
                {
                    if (type.IsTypeOf(typeof(ModuleBase)) && !type.IsGenericType && !type.IsAbstract)
                    {
                        AddModule(type);
                    }
                }
            }

            var commands = GetCommandOptions();

            if (!commands.Any())
            {
                throw new NullReferenceException(string.Format("No {0} Implementing {1} Was Found In The Execution Folder '{2}'", nameof(ModuleBase), nameof(ICommandLineExecutable), codeBase));
            }
        }
        public void LoadModulesInExecutionFolder()
        {
            ConsoleApplicationController.WriteToConsole("Loading Modules");
            var    codeBase = Assembly.GetExecutingAssembly().Location;
            var    uri      = new UriBuilder(codeBase);
            string path     = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));

            var ignoreThese = new[]
            {
                "Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter",
                "Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices",
                "Microsoft.VisualStudio.GraphModel",
                "Microsoft.VisualStudio.Shell.Framework",
                "Microsoft.VisualStudio.Utilities",
                "Microsoft.VisualStudio.Telemetry",
                "StreamJsonRpc"
            };

            foreach (string dll in Directory.GetFiles(path, "*.dll"))
            {
                var loadIt = Assembly.LoadFile(dll);
                if (!ignoreThese.Any(s => loadIt.FullName.Contains(s)))
                {
                    try
                    {
                        foreach (var type in loadIt.GetTypes())
                        {
                            if (type.IsTypeOf(typeof(ModuleBase)) && !type.IsGenericType && !type.IsAbstract)
                            {
                                AddModule(type);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Error Loading Modules In Assembly {loadIt.FullName}", ex);
                    }
                }
            }

            var commands = GetCommandOptions();

            if (!commands.Any())
            {
                throw new NullReferenceException(string.Format("No {0} Implementing {1} Was Found In The Execution Folder '{2}'", nameof(ModuleBase), nameof(ICommandLineExecutable), codeBase));
            }
        }
        public void Run(string[] args)
        {
            //need to run as per the arguments or display the console options
            if (args == null || !args.Any())
            {
                ConsoleApplicationController.WriteToConsole("The Valid Commands For This Application Are\n");
                ConsoleApplicationController.WriteToConsole(GetCommandLineSwitchString());
            }
            else
            {
                var command = args.First();
                if (command == "?" || command.ToLower() == "help")
                {
                    ConsoleApplicationController.WriteToConsole("The Valid Commands For This Application Are\n");
                    ConsoleApplicationController.WriteToConsole(GetCommandLineSwitchString());
                }
                else
                {
                    var matchingOptions = GetCommandOptions().Where(o => o.CommandName.ToLower() == command.ToLower());
                    if (!matchingOptions.Any())
                    {
                        ConsoleApplicationController.WriteToConsole(string.Format("No Matching Command Found For '{0}'\n", command));
                        ConsoleApplicationController.WriteToConsole(GetCommandLineSwitchString());
                    }
                    else
                    {
                        var matchingOption = matchingOptions.First();
                        var arguments      = ParseCommandLineArguments(args);
                        if (!arguments.Any() || arguments.First().Key == "?" || arguments.First().Key == "help")
                        {
                            ConsoleApplicationController.WriteToConsole("The Commands Arguments Are");
                            ConsoleApplicationController.WriteToConsole(GetCommandLineArgumentsString(command));
                        }
                        else
                        {
                            ConsoleApplicationController.WriteToConsole(string.Format("Loading {0} Command", command));
                            var commandArgs = matchingOption.GetArgs();

                            foreach (var arg in arguments.Where(a => !StandardCommandArguments.Any(sca => sca.CommandName == a.Key)))
                            {
                                var matchingCommand = commandArgs.Where(c => c.CommandName == arg.Key);
                                if (!matchingCommand.Any())
                                {
                                    throw new ArgumentOutOfRangeException(arg.Key, string.Format("'{0}' is not a valid argument name for the command. The valid arguments are\n{1}", arg.Key, GetCommandLineArgumentsString(command)));
                                }
                                matchingCommand.First().LoadAction(arg.Value);
                            }
                            //this needs to run after others which load the log path
                            LoadActiveXrmConnection(arguments);

                            ConsoleApplicationController.WriteToConsole(string.Format("Starting {0} Process", command));
                            matchingOption.Command();
                        }
                    }
                }
            }
        }