Exemplo n.º 1
0
        /// <summary>
        /// Initializes an instance of <see cref="InteractiveMode"/>.
        /// </summary>
        public InteractiveMode(IOptions <InteractiveModeOptions> options,
                               IConsole console,
                               ILogger <InteractiveMode> logger,
                               IRootSchemaAccessor rootSchemaAccessor,
                               ApplicationMetadata metadata,
                               ApplicationConfiguration configuration,
                               IServiceProvider serviceProvider)
        {
            _options = options.Value;

            _console         = console;
            _logger          = logger;
            _metadata        = metadata;
            _configuration   = configuration;
            _serviceProvider = serviceProvider;

            if (_options.IsAdvancedInputAvailable && !_console.Input.IsRedirected)
            {
                _autoCompleteInput = new AutoCompleteInput(_console, _options.UserDefinedShortcuts)
                {
                    AutoCompletionHandler = new AutoCompletionHandler(rootSchemaAccessor),
                };

                _autoCompleteInput.History.IsEnabled = true;
            }
        }
Exemplo n.º 2
0
 public CliCommandExecutor(IServiceScopeFactory serviceScopeFactory,
                           IRootSchemaAccessor rootSchemaAccessor,
                           ILogger <CliCommandExecutor> logger)
 {
     _serviceScopeFactory = serviceScopeFactory;
     _rootSchemaAccessor  = rootSchemaAccessor;
     _logger = logger;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes an instance of <see cref="CliApplication"/>.
        /// </summary>
        internal CliApplication(IServiceProvider serviceProvider,
                                IConsole console,
                                EnvironmentVariablesAccessor environmentVariablesAccessor,
                                ApplicationMetadata metadata,
                                Action <ApplicationMetadata, IConsole>?startupMessage)
        {
            _serviceProvider = serviceProvider;

            _environmentVariablesAccessor = environmentVariablesAccessor;
            _metadata       = metadata;
            _startupMessage = startupMessage;
            _console        = console;

            _logger              = serviceProvider.GetRequiredService <ILogger <CliApplication> >();
            _cliCommandExecutor  = serviceProvider.GetRequiredService <ICliCommandExecutor>();
            _rootSchemaAccessor  = serviceProvider.GetRequiredService <IRootSchemaAccessor>();
            _applicationLifetime = (CliApplicationLifetime)serviceProvider.GetRequiredService <ICliApplicationLifetime>();
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes an instance of <see cref="AutoCompletionHandler"/>.
 /// </summary>
 public AutoCompletionHandler(IRootSchemaAccessor rootSchemaAccessor)
 {
     _rootSchemaAccessor = rootSchemaAccessor;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates an instance of <see cref="CliApplication"/> using configured parameters.
        /// Default values are used in place of parameters that were not specified.
        /// A scope is defined as a lifetime of a command execution pipeline that includes directives handling.
        /// </summary>
        public CliApplication Build()
        {
            if (_cliApplicationBuilt)
            {
                throw new InvalidOperationException("Build can only be called once.");
            }

            _cliApplicationBuilt = true;

            // Set default values
            _title ??= AssemblyUtils.TryGetDefaultTitle() ?? "App";
            _executableName ??= AssemblyUtils.TryGetDefaultExecutableName() ?? "app";
            _versionText ??= AssemblyUtils.TryGetDefaultVersionText() ?? "v1.0";
            _console ??= new SystemConsole();

            if (_startupMode is null || _modeTypes.Count == 0)
            {
                this.UseDirectMode(true);
            }

            // Add core middlewares to the end of the pipeline
            this.AddAfterUserMiddlewares();

            // Create context
            var _serviceCollection = new ServiceCollection();

            var metadata      = new ApplicationMetadata(_title, _executableName, _versionText, _description);
            var configuration = new ApplicationConfiguration(_modeTypes,
                                                             _commandTypes,
                                                             _directivesTypes,
                                                             _middlewareTypes,
                                                             _startupMode !,
                                                             _serviceCollection);

            var environmentVariablesAccessor = new EnvironmentVariablesAccessor();

            // Add core services
            _serviceCollection.AddOptions();
            _serviceCollection.AddSingleton(typeof(ApplicationMetadata), metadata);
            _serviceCollection.AddSingleton(typeof(ApplicationConfiguration), configuration);
            _serviceCollection.AddSingleton(typeof(IConsole), _console);
            _serviceCollection.AddSingleton(typeof(IEnvironmentVariablesAccessor), environmentVariablesAccessor);
            _serviceCollection.AddSingleton <IRootSchemaAccessor, RootSchemaAccessor>();
            _serviceCollection.AddSingleton <ICliCommandExecutor, CliCommandExecutor>();
            _serviceCollection.AddSingleton <ICliApplicationLifetime, CliApplicationLifetime>();

            _serviceCollection.AddScoped(typeof(ICliContext), (provider) =>
            {
                IRootSchemaAccessor rootSchemaAccessor = provider.GetRequiredService <IRootSchemaAccessor>();

                return(new CliContext(metadata,
                                      configuration,
                                      rootSchemaAccessor.RootSchema,
                                      environmentVariablesAccessor.EnvironmentVariables,
                                      _console));
            });

            _serviceCollection.AddLogging(cfg =>
            {
                cfg.ClearProviders();
                cfg.AddDebug();
                cfg.SetMinimumLevel(LogLevel.Information);
            });

            IServiceProvider serviceProvider = CreateServiceProvider(_serviceCollection);

            return(new CliApplication(serviceProvider, _console, environmentVariablesAccessor, metadata, _startupMessage));
        }