<commit_before>﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nito.AsyncEx;
using System;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;

namespace TIKSN.PowerShell
{
    public abstract class CommandBase : PSCmdlet
    {
        protected CancellationTokenSource cancellationTokenSource;
        private IServiceScope serviceScope;

        protected CommandBase()
        {
            cancellationTokenSource = new CancellationTokenSource();
        }

        protected IServiceProvider ServiceProvider => serviceScope.ServiceProvider;

        protected override void BeginProcessing()
        {
            cancellationTokenSource = new CancellationTokenSource();

            base.BeginProcessing();

            var topServiceProvider = GetServiceProvider();
            serviceScope = topServiceProvider.CreateScope();
            ServiceProvider.GetRequiredService<ICurrentCommandStore>().SetCurrentCommand(this);
            ConfigureLogger(ServiceProvider.GetRequiredService<ILoggerFactory>());
        }

        protected virtual void ConfigureLogger(ILoggerFactory loggerFactory)
        {
            loggerFactory.AddPowerShell(ServiceProvider);
        }

        protected abstract IServiceProvider GetServiceProvider();

        protected sealed override void ProcessRecord()
        {
            AsyncContext.Run(async () => await ProcessRecordAsync(cancellationTokenSource.Token));
        }

        protected abstract Task ProcessRecordAsync(CancellationToken cancellationToken);

        protected override void StopProcessing()
        {
            cancellationTokenSource.Cancel();
            base.StopProcessing();
            serviceScope.Dispose();
        }
    }
}<commit_msg>Update CommandBase.cs<commit_after>﻿using System;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nito.AsyncEx;

namespace TIKSN.PowerShell
{
    public abstract class CommandBase : PSCmdlet
    {
        protected CancellationTokenSource cancellationTokenSource;
        private IServiceScope serviceScope;

        protected CommandBase() => this.cancellationTokenSource = new CancellationTokenSource();

        protected IServiceProvider ServiceProvider => this.serviceScope.ServiceProvider;

        protected override void BeginProcessing()
        {
            this.cancellationTokenSource = new CancellationTokenSource();

            base.BeginProcessing();

            var topServiceProvider = this.GetServiceProvider();
            this.serviceScope = topServiceProvider.CreateScope();
            this.ServiceProvider.GetRequiredService<ICurrentCommandStore>().SetCurrentCommand(this);
            this.ConfigureLogger(this.ServiceProvider.GetRequiredService<ILoggerFactory>());
        }

        protected virtual void ConfigureLogger(ILoggerFactory loggerFactory) =>
            loggerFactory.AddPowerShell(this.ServiceProvider);

        protected abstract IServiceProvider GetServiceProvider();

        protected sealed override void ProcessRecord() => AsyncContext.Run(async () =>
            await this.ProcessRecordAsync(this.cancellationTokenSource.Token));

        protected abstract Task ProcessRecordAsync(CancellationToken cancellationToken);

        protected override void StopProcessing()
        {
            this.cancellationTokenSource.Cancel();
            base.StopProcessing();
            this.serviceScope.Dispose();
        }
    }
}

{'fraction_non_alphanumeric': 0.07263654006099252, 'fraction_numerical': 0.0, 'mean_word_length': 3.2051282051282053, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3087198', 'n_tokens_mistral': 793, 'n_tokens_neox': 804, 'n_words': 163}