コード例 #1
0
        /// <summary>
        /// Handles the apply using the specified path
        /// </summary>
        /// <param name="path">The path</param>
        /// <param name="verbosity">The verbosity</param>
        /// <param name="config">The config</param>
        /// <param name="dryRun">The dry run</param>
        /// <param name="console">The console</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <exception cref="Exception">No implementation found for service {nameof(IApplyDocumentHandler)}</exception>
        /// <returns>A task containing the int</returns>
        private static async Task <int> HandleApplyAsync(string path, string verbosity, string config,
                                                         bool dryRun, IConsole console, CancellationToken cancellationToken)
        {
            // Configure the logger
            var logger = LoggingUtils.ConfigureLogger(verbosity);

            logger.Verbose("dotnet-runtime version: {Version}", VersionUtils.GetRuntimeVersion());

            if (VersionUtils.TryGetVersion(out var version))
            {
                logger.Verbose("dotnet-document version: {Version}", version);
            }

            logger.Verbose("Verbosity {verbosity} converted to log level {level}",
                           verbosity, LoggingUtils.ParseLogLevel(verbosity));
            logger.Verbose("Path to document: {path}", path);
            logger.Verbose("Is dry run: {dryRun}", dryRun);
            logger.Verbose("Config file from args: {config}", config);

            var configFilePath = IdentifyConfigFileToUse(config);

            // Declare a new service collection
            var services = new ServiceCollection();

            services.AddLogging(o => o.AddSerilog(logger));

            // Configure services collection
            services.ConfigureFromFile(configFilePath);
            services.AddDotnetDocument();

            // Build the service provider
            var serviceProvider = services.BuildServiceProvider();

            var handler = serviceProvider.GetService <IApplyDocumentHandler>();

            if (handler is null)
            {
                logger.Error("No implementation found for service {Type}", nameof(IApplyDocumentHandler));

                throw new Exception($"No implementation found for service {nameof(IApplyDocumentHandler)}");
            }

            var result = handler.Apply(path, dryRun);

            return(await Task.FromResult((int)result));
        }