<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Threading;
using Knapcode.NuGetTools.Logic;
using Knapcode.NuGetTools.Logic.Direct;
using Knapcode.NuGetTools.Logic.Direct.Wrappers;
using Knapcode.NuGetTools.Logic.Wrappers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NuGet.Common;
using Version = Knapcode.NuGetTools.Logic.Direct.Wrappers.Version;

namespace Knapcode.NuGetTools.Website
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            HostingEnvironment = env;

            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            if (env.IsDevelopment())
            {
                builder.AddApplicationInsightsSettings(developerMode: true);
            }

            Configuration = builder.Build();
        }

        public IHostingEnvironment HostingEnvironment { get; }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(serviceProvider =>
            {
                var hostingEnvironment = serviceProvider.GetService<IHostingEnvironment>();

                var settings = new InMemorySettings();
                var nuGetSettings = new NuGetSettings(settings);
                nuGetSettings.GlobalPackagesFolder = Path.Combine(hostingEnvironment.ContentRootPath, "packages");

                return nuGetSettings;
            });

            services.AddTransient<IAlignedVersionsDownloader, AlignedVersionsDownloader>();
            services.AddTransient<IAssemblyLoader, AssemblyLoader>();
            services.AddTransient<IFrameworkEnumerator<Framework>, FrameworkEnumerator>();
            services.AddTransient<IFrameworkList, FrameworkList<Framework>>();
            services.AddTransient<IPackageLoader, PackageLoader>();
            services.AddTransient<IPackageRangeDownloader, PackageRangeDownloader>();

            services.AddSingleton<IToolsFactory, ToolsFactory>();

            try
            {
                // Try to construct the reflection-based tools factory.
                var serviceProvider = services.BuildServiceProvider();
                var toolsFactory = serviceProvider.GetRequiredService<IToolsFactory>();

                var versions = toolsFactory.GetAvailableVersionsAsync(CancellationToken.None).Result;

                if (!versions.Any())
                {
                    throw new InvalidOperationException("At least one version is required.");
                }
            }
            catch
            {
                // Fallback to using the NuGet version directly referenced by this project.
                var serviceDescriptor = services.First(x => x.ImplementationType == typeof(ToolsFactory));
                services.Remove(serviceDescriptor);

                services.AddTransient<IFrameworkLogic<Framework>, FrameworkLogic>();
                services.AddTransient<IVersionLogic<Version>, VersionLogic>();
                services.AddTransient<IVersionRangeLogic<Version, VersionRange>, VersionRangeLogic>();

                services.AddTransient<IToolsService>(serviceProvider =>
                {
                    return new ToolsService<Framework, Version, VersionRange>(
                        ClientVersionUtility.GetNuGetAssemblyVersion(),
                        serviceProvider.GetRequiredService<IFrameworkLogic<Framework>>(),
                        serviceProvider.GetRequiredService<IVersionLogic<Version>>(),
                        serviceProvider.GetRequiredService<IVersionRangeLogic<Version, VersionRange>>());
                });

                services.AddTransient<IFrameworkPrecedenceService>(serviceProvider =>
                {
                    return new FrameworkPrecedenceService<Framework>(
                        ClientVersionUtility.GetNuGetAssemblyVersion(),
                        serviceProvider.GetRequiredService<IFrameworkList>(),
                        serviceProvider.GetRequiredService<IFrameworkLogic<Framework>>());
                });

                services.AddSingleton<IToolsFactory, SingletonToolsFactory>();
            }

            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            // Add Application Insights monitoring to the request pipeline as a very first middleware.
            app.UseApplicationInsightsRequestTelemetry();

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");<commit_msg>Hard code client version since it does not always match the package version (and it should)<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Threading;
using Knapcode.NuGetTools.Logic;
using Knapcode.NuGetTools.Logic.Direct;
using Knapcode.NuGetTools.Logic.Direct.Wrappers;
using Knapcode.NuGetTools.Logic.Wrappers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Version = Knapcode.NuGetTools.Logic.Direct.Wrappers.Version;

namespace Knapcode.NuGetTools.Website
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            HostingEnvironment = env;

            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            if (env.IsDevelopment())
            {
                builder.AddApplicationInsightsSettings(developerMode: true);
            }

            Configuration = builder.Build();
        }

        public IHostingEnvironment HostingEnvironment { get; }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(serviceProvider =>
            {
                var hostingEnvironment = serviceProvider.GetService<IHostingEnvironment>();

                var settings = new InMemorySettings();
                var nuGetSettings = new NuGetSettings(settings);
                nuGetSettings.GlobalPackagesFolder = Path.Combine(hostingEnvironment.ContentRootPath, "packages");

                return nuGetSettings;
            });

            services.AddTransient<IAlignedVersionsDownloader, AlignedVersionsDownloader>();
            services.AddTransient<IAssemblyLoader, AssemblyLoader>();
            services.AddTransient<IFrameworkEnumerator<Framework>, FrameworkEnumerator>();
            services.AddTransient<IFrameworkList, FrameworkList<Framework>>();
            services.AddTransient<IPackageLoader, PackageLoader>();
            services.AddTransient<IPackageRangeDownloader, PackageRangeDownloader>();

            services.AddSingleton<IToolsFactory, ToolsFactory>();

            try
            {
                // Try to construct the reflection-based tools factory.
                var serviceProvider = services.BuildServiceProvider();
                var toolsFactory = serviceProvider.GetRequiredService<IToolsFactory>();

                var versions = toolsFactory.GetAvailableVersionsAsync(CancellationToken.None).Result;

                if (!versions.Any())
                {
                    throw new InvalidOperationException("At least one version is required.");
                }
            }
            catch
            {
                // Fallback to using the NuGet version directly referenced by this project.
                var serviceDescriptor = services.First(x => x.ImplementationType == typeof(ToolsFactory));
                services.Remove(serviceDescriptor);

                services.AddTransient<IFrameworkLogic<Framework>, FrameworkLogic>();
                services.AddTransient<IVersionLogic<Version>, VersionLogic>();
                services.AddTransient<IVersionRangeLogic<Version, VersionRange>, VersionRangeLogic>();

                // TODO: determine this by reading the .deps.json file.
                var clientVersion = "3.5.0-rc1-final";

                services.AddTransient<IToolsService>(serviceProvider =>
                {
                    return new ToolsService<Framework, Version, VersionRange>(
                        clientVersion,
                        serviceProvider.GetRequiredService<IFrameworkLogic<Framework>>(),
                        serviceProvider.GetRequiredService<IVersionLogic<Version>>(),
                        serviceProvider.GetRequiredService<IVersionRangeLogic<Version, VersionRange>>());
                });

                services.AddTransient<IFrameworkPrecedenceService>(serviceProvider =>
                {
                    return new FrameworkPrecedenceService<Framework>(
                        clientVersion,
                        serviceProvider.GetRequiredService<IFrameworkList>(),
                        serviceProvider.GetRequiredService<IFrameworkLogic<Framework>>());
                });

                services.AddSingleton<IToolsFactory, SingletonToolsFactory>();
            }

            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            // Add Application Insights monitoring to the request pipeline as a very first middleware.
            app.UseApplicationInsightsRequestTelemetry();

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
{'fraction_non_alphanumeric': 0.07990056818181818, 'fraction_numerical': 0.0003551136363636364, 'mean_word_length': 2.2919345412039744, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4429700', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2474, 'n_words': 503}