<commit_before>﻿using BitCodeGenerator.Implementations;
using BitCodeGenerator.Implementations.TypeScriptClientProxyGenerator;
using BitTools.Core.Contracts;
using BitTools.Core.Model;
using Fclp;
using Microsoft.Build.Construction;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace BitCodeGeneratorTaskImpl
{
    public class BitCodeGeneratorTaskImplArgs
    {
        public string SolutionPath { get; set; }

        public string ProjectPath { get; set; }
    }

    public class Program
    {
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Bit Code Generator implementation started...");

            if (!MSBuildLocator.IsRegistered)
                MSBuildLocator.RegisterDefaults();

            FluentCommandLineParser<BitCodeGeneratorTaskImplArgs> commandLineParser = new FluentCommandLineParser<BitCodeGeneratorTaskImplArgs>();

            commandLineParser.Setup(arg => arg.SolutionPath)
                .As('s', "solutionPath")
                .Required();

            commandLineParser.Setup(arg => arg.ProjectPath)
                .As('p', "projectPath")
                .Required();

            ICommandLineParserResult result = commandLineParser.Parse(args);

            if (result.HasErrors == true)
            {
                throw new Exception(result.ErrorText);
            }
            else
            {
                InitProps(commandLineParser);

                if (SolutionPath == "*Undefined*") // dotnet commands using portable msbuild of dotnet sdk-cli will pass solution name like that! But Visual Studio's msbuild passes solution path correctly!
                {
                    DirectoryInfo projDir = new DirectoryInfo(Path.GetDirectoryName(ProjectPath));

                    while (projDir.Parent != null)
                    {
                        string filePath = Path.Combine(projDir.FullName, "BitConfigV1.json");

                        if (File.Exists(filePath))
                        {
                            SolutionPath = Directory.EnumerateFiles(projDir.FullName, "*.sln").ExtendedSingleOrDefault($"Finding solution for project {ProjectName} {ProjectPath}");
                            break;
                        }

                        projDir = projDir.Parent;
                    }
                }

                MSBuildWorkspace workspace = MSBuildWorkspace.Create();

                workspace.SkipUnrecognizedProjects = workspace.LoadMetadataForReferencedProjects = true;

                workspace.WorkspaceFailed += MSBuildWorkspace_WorkspaceFailed;

                await CallGenerateCodes(workspace, ProjectName);
            }
        }

        static void InitProps(FluentCommandLineParser<BitCodeGeneratorTaskImplArgs> commandLineParser)
        {
            SolutionPath = commandLineParser.Object.SolutionPath;
            ProjectPath = commandLineParser.Object.ProjectPath;
            SolutionProjects = SolutionFile.Parse(SolutionPath).ProjectsInOrder;
            ProjectName = SolutionProjects.ExtendedSingle($"Finding {ProjectPath}'s name in solution.", p => p.AbsolutePath == ProjectPath).ProjectName;
        }

        public static IReadOnlyList<ProjectInSolution> SolutionProjects { get; set; }
        public static string SolutionPath { get; set; }
        public static string ProjectPath { get; set; }
        public static string ProjectName { get; set; }

        static async Task CallGenerateCodes(MSBuildWorkspace workspace, string beingCompiledProjectName)
        {<commit_msg>Fixed small issues related to BitCodeGeneratorTaskImpl & .net core projects<commit_after>﻿using BitCodeGenerator.Implementations;
using BitCodeGenerator.Implementations.TypeScriptClientProxyGenerator;
using BitTools.Core.Contracts;
using BitTools.Core.Model;
using Fclp;
using Microsoft.Build.Construction;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace BitCodeGeneratorTaskImpl
{
    public class BitCodeGeneratorTaskImplArgs
    {
        public string SolutionPath { get; set; }

        public string ProjectPath { get; set; }
    }

    public class Program
    {
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Bit Code Generator implementation started...");

            if (!MSBuildLocator.IsRegistered)
                MSBuildLocator.RegisterDefaults();

            FluentCommandLineParser<BitCodeGeneratorTaskImplArgs> commandLineParser = new FluentCommandLineParser<BitCodeGeneratorTaskImplArgs>();

            commandLineParser.Setup(arg => arg.SolutionPath)
                .As('s', "solutionPath")
                .Required();

            commandLineParser.Setup(arg => arg.ProjectPath)
                .As('p', "projectPath")
                .Required();

            ICommandLineParserResult result = commandLineParser.Parse(args);

            if (result.HasErrors == true)
            {
                throw new Exception(result.ErrorText);
            }
            else
            {
                SolutionPath = commandLineParser.Object.SolutionPath;
                ProjectPath = commandLineParser.Object.ProjectPath;

                if (SolutionPath == "*Undefined*" || SolutionPath == null) // dotnet commands using portable msbuild of dotnet sdk-cli will pass solution name like that! But Visual Studio's msbuild passes solution path correctly!
                {
                    DirectoryInfo projDir = new DirectoryInfo(Path.GetDirectoryName(ProjectPath));

                    while (projDir.Parent != null)
                    {
                        string filePath = Path.Combine(projDir.FullName, "BitConfigV1.json");

                        if (File.Exists(filePath))
                        {
                            SolutionPath = Directory.EnumerateFiles(projDir.FullName, "*.sln").ExtendedSingleOrDefault($"Finding solution for project {ProjectName} {ProjectPath}");
                            break;
                        }

                        projDir = projDir.Parent;
                    }
                }

                InitPropjects(commandLineParser);

                MSBuildWorkspace workspace = MSBuildWorkspace.Create();

                workspace.SkipUnrecognizedProjects = workspace.LoadMetadataForReferencedProjects = true;

                workspace.WorkspaceFailed += MSBuildWorkspace_WorkspaceFailed;

                await CallGenerateCodes(workspace, ProjectName);
            }
        }

        static void InitPropjects(FluentCommandLineParser<BitCodeGeneratorTaskImplArgs> commandLineParser)
        {
            SolutionProjects = SolutionFile.Parse(SolutionPath).ProjectsInOrder;
            ProjectName = SolutionProjects.ExtendedSingle($"Finding {ProjectPath}'s name in solution.", p => p.AbsolutePath == ProjectPath).ProjectName;
        }

        public static IReadOnlyList<ProjectInSolution> SolutionProjects { get; set; }
        public static string SolutionPath { get; set; }
        public static string ProjectPath { get; set; }
        public static string ProjectName { get; set; }

        static async Task CallGenerateCodes(MSBuildWorkspace workspace, string beingCompiledProjectName)
        {
{'fraction_non_alphanumeric': 0.07736123989932442, 'fraction_numerical': 0.00026493575307987813, 'mean_word_length': 2.288327526132404, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '3215418', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1741, 'n_words': 400}