<commit_before>﻿using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using WebApiToTypeScript.Block;
using WebApiToTypeScript.Endpoints;
using WebApiToTypeScript.Enums;
using WebApiToTypeScript.Interfaces;
using WebApiToTypeScript.Resources;
using WebApiToTypeScript.Types;
using WebApiToTypeScript.Views;
using WebApiToTypeScript.WebApi;

namespace WebApiToTypeScript
{
    public class WebApiToTypeScript : AppDomainIsolatedTask
    {
        private Stopwatch stopwatch;

        public const string IHaveQueryParams = nameof(IHaveQueryParams);
        public const string IEndpoint = nameof(IEndpoint);

        public static Config.Config Config { get; private set; }
        public static EnumsService EnumsService { get; private set; }
        public static TypeService TypeService { get; private set; }
        public static InterfaceService InterfaceService { get; private set; }

        public static EndpointsService EndpointsService { get; private set; }
        public static AngularEndpointsService AngularEndpointsService { get; private set; }

        public static ViewsService ViewsService { get; private set; }
        public static ResourceService ResourceService { get; private set; }

        [Required]
        public string ConfigFilePath { get; set; }

        public override bool Execute()
        {
            InitializeServices();

            var apiControllers = TypeService.GetControllers(Config.WebApiModuleFileName);

            var endpointBlock = EndpointsService.CreateEndpointBlock();
            var serviceBlock = AngularEndpointsService.CreateServiceBlock();

            if (Config.GenerateViews)
            {
                StartAnalysis("views");

                foreach (var viewsBlock in ViewsService.GetBlocksForViews())
                    CreateFileForBlock(viewsBlock.TypeScriptBlock, Config.ViewsOutputDirectory, viewsBlock.Filename);

                StopAnalysis();
            }

            if (Config.GenerateResources)
            {
                StartAnalysis("resources");

                foreach (var resourceBlock in ResourceService.GetBlocksForResources())
                    CreateFileForBlock(resourceBlock.TypeScriptBlock, Config.ResourcesOutputDirectory, resourceBlock.Filename);

                StopAnalysis();
            }

            StartAnalysis("controllers and actions");

            foreach (var apiController in apiControllers)
            {
                var webApiController = new WebApiController(apiController);

                EndpointsService.WriteEndpointClassToBlock(endpointBlock, webApiController);
                AngularEndpointsService.WriteServiceObjectToBlock(serviceBlock.Children.First() as TypeScriptBlock, webApiController);
            }

            StopAnalysis();

            CreateFileForBlock(endpointBlock, Config.EndpointsOutputDirectory, Config.EndpointsFileName);
            CreateFileForBlock(serviceBlock, Config.ServiceOutputDirectory, Config.ServiceFileName);

            var enumsBlock = EnumsService.CreateEnumsBlock();

            if (Config.GenerateInterfaces)
            {
                StartAnalysis("interfaces");

                var interfacesBlock = InterfaceService.CreateInterfacesBlock();
                InterfaceService.AddMatchingInterfaces();
                InterfaceService.WriteInterfacesToBlock(interfacesBlock);

                StopAnalysis();

                CreateFileForBlock(interfacesBlock, Config.InterfacesOutputDirectory, Config.InterfacesFileName);
            }

            if (Config.GenerateEnums)
            {
                StartAnalysis("enumerations");

                EnumsService.WriteEnumsToBlock(enumsBlock);

                StopAnalysis();

                CreateFileForBlock(enumsBlock, Config.EnumsOutputDirectory, Config.EnumsFileName);
            }

            return true;
        }

        private void InitializeServices()
        {
            Config = GetConfig(ConfigFilePath);

            TypeService = new TypeService();
            TypeService.LoadAllTypes(Config.WebApiModuleFileName);

            EnumsService = new EnumsService();
            InterfaceService = new InterfaceService();

            EndpointsService = new EndpointsService();
            AngularEndpointsService = new AngularEndpointsService();

            ViewsService = new ViewsService();
            ResourceService = new ResourceService();
        }

        private Config.Config GetConfig(string configFilePath)
        {
            var configFileContent = File.ReadAllText(configFilePath);

            return JsonConvert.DeserializeObject<Config.Config>(configFileContent);
        }

        private void CreateFileForBlock(TypeScriptBlock typeScriptBlock, string outputDirectory, string fileName)
        {
            CreateOuputDirectory(outputDirectory);

            var filePath = Path.Combine(outputDirectory, fileName);

            using (var endpointFileWriter = new StreamWriter(filePath, false))
            {
                endpointFileWriter.Write(typeScriptBlock.ToString());
            }
        }

        private void CreateOuputDirectory(string directory)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }

        private void StartAnalysis(string ofWhat)
        {
            LogMessage("");
            LogMessage($"Analyzing {ofWhat}...");

            stopwatch = Stopwatch.StartNew();
        }

        private void StopAnalysis()
        {
            LogMessage($"Analysis complete. Took {stopwatch.ElapsedMilliseconds}ms.");

            stopwatch = null;
        }

        private void LogMessage(string log)
        {
            try
            {
                Log.LogMessage(log);
            }
            catch (Exception)
            {
                Console.WriteLine(log);
            }
        }
    }
}<commit_msg>This is needed by the logging added to service aware commit - oops. Adds logging and supports config values correctly<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Newtonsoft.Json;
using WebApiToTypeScript.Block;
using WebApiToTypeScript.Endpoints;
using WebApiToTypeScript.Enums;
using WebApiToTypeScript.Interfaces;
using WebApiToTypeScript.Resources;
using WebApiToTypeScript.Types;
using WebApiToTypeScript.Views;
using WebApiToTypeScript.WebApi;

namespace WebApiToTypeScript
{
    public class WebApiToTypeScript : AppDomainIsolatedTask
    {
        private Stopwatch stopwatch;

        public const string IHaveQueryParams = nameof(IHaveQueryParams);
        public const string IEndpoint = nameof(IEndpoint);

        public static Config.Config Config { get; private set; }
        public static EnumsService EnumsService { get; private set; }
        public static TypeService TypeService { get; private set; }
        public static InterfaceService InterfaceService { get; private set; }

        public static EndpointsService EndpointsService { get; private set; }
        public static AngularEndpointsService AngularEndpointsService { get; private set; }

        public static ViewsService ViewsService { get; private set; }
        public static ResourceService ResourceService { get; private set; }

        public static List<string> LogMessages { get; }
            = new List<string>();

        [Required]
        public string ConfigFilePath { get; set; }

        public override bool Execute()
        {
            InitializeServices();

            var apiControllers = TypeService.GetControllers(Config.WebApiModuleFileName);

            if (Config.GenerateViews)
            {
                StartAnalysis("views");

                foreach (var viewsBlock in ViewsService.GetBlocksForViews())
                    CreateFileForBlock(viewsBlock.TypeScriptBlock, Config.ViewsOutputDirectory, viewsBlock.Filename);

                StopAnalysis();
            }

            if (Config.GenerateResources)
            {
                StartAnalysis("resources");

                foreach (var resourceBlock in ResourceService.GetBlocksForResources())
                    CreateFileForBlock(resourceBlock.TypeScriptBlock, Config.ResourcesOutputDirectory, resourceBlock.Filename);

                StopAnalysis();
            }

            StartAnalysis("controllers and actions");

            var endpointBlock = EndpointsService.CreateEndpointBlock();
            var serviceBlock = AngularEndpointsService.CreateServiceBlock();

            foreach (var apiController in apiControllers)
            {
                var webApiController = new WebApiController(apiController);

                if (Config.GenerateEndpoints || Config.GenerateService)
                    EndpointsService.WriteEndpointClassToBlock(endpointBlock, webApiController);

                if (Config.GenerateService)
                    AngularEndpointsService.WriteServiceObjectToBlock(serviceBlock.Children.First() as TypeScriptBlock, webApiController);
            }

            CreateFileForBlock(endpointBlock, Config.EndpointsOutputDirectory, Config.EndpointsFileName);

            if (Config.GenerateService)
                CreateFileForBlock(serviceBlock, Config.ServiceOutputDirectory, Config.ServiceFileName);

            StopAnalysis();

            var enumsBlock = EnumsService.CreateEnumsBlock();

            if (Config.GenerateInterfaces)
            {
                StartAnalysis("interfaces");

                var interfacesBlock = InterfaceService.CreateInterfacesBlock();
                InterfaceService.AddMatchingInterfaces();
                InterfaceService.WriteInterfacesToBlock(interfacesBlock);

                StopAnalysis();

                CreateFileForBlock(interfacesBlock, Config.InterfacesOutputDirectory, Config.InterfacesFileName);
            }

            if (Config.GenerateEnums)
            {
                StartAnalysis("enumerations");

                EnumsService.WriteEnumsToBlock(enumsBlock);

                StopAnalysis();

                CreateFileForBlock(enumsBlock, Config.EnumsOutputDirectory, Config.EnumsFileName);
            }

            WriteServiceLogMessages();

            return true;
        }

        private void WriteServiceLogMessages()
        {
            LogMessage("");

            foreach (var message in LogMessages)
                LogMessage(message);
        }

        private void InitializeServices()
        {
            Config = GetConfig(ConfigFilePath);

            TypeService = new TypeService();
            TypeService.LoadAllTypes(Config.WebApiModuleFileName);

            EnumsService = new EnumsService();
            InterfaceService = new InterfaceService();

            EndpointsService = new EndpointsService();
            AngularEndpointsService = new AngularEndpointsService();

            ViewsService = new ViewsService();
            ResourceService = new ResourceService();
        }

        private Config.Config GetConfig(string configFilePath)
        {
            var configFileContent = File.ReadAllText(configFilePath);

            return JsonConvert.DeserializeObject<Config.Config>(configFileContent);
        }

        private void CreateFileForBlock(TypeScriptBlock typeScriptBlock, string outputDirectory, string fileName)
        {
            CreateOuputDirectory(outputDirectory);

            var filePath = Path.Combine(outputDirectory, fileName);

            using (var endpointFileWriter = new StreamWriter(filePath, false))
            {
                endpointFileWriter.Write(typeScriptBlock.ToString());
            }
        }

        private void CreateOuputDirectory(string directory)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }

        private void StartAnalysis(string ofWhat)
        {
            LogMessage("");
            LogMessage($"Analyzing {ofWhat}...");

            stopwatch = Stopwatch.StartNew();
        }

        private void StopAnalysis()
        {
            LogMessage($"Analysis complete. Took {stopwatch.ElapsedMilliseconds}ms.");

            stopwatch = null;
        }

        private void LogMessage(string message)
        {
            try
            {
                Log.LogMessage(message);
            }
            catch (Exception)
            {
                Console.WriteLine(message);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07167530224525043, 'fraction_numerical': 0.0, 'mean_word_length': 2.237357052096569, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6021106', 'n_tokens_mistral': 2978, 'n_tokens_neox': 2809, 'n_words': 623}