<commit_before>﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.Extensions.DependencyModel;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;

namespace Natasha
{

    public class ScriptComplier
    {
        public static string LibPath;
        public static ConcurrentBag<string> DynamicDlls;
        public static ConcurrentBag<PortableExecutableReference> References;
        public static bool IsComplete;
        
        static ScriptComplier()
        {
            IsComplete = false;
            LibPath = AppDomain.CurrentDomain.BaseDirectory + "lib\\";
            if (Directory.Exists(LibPath))
            {
                Directory.Delete(LibPath, true);
            }
            Directory.CreateDirectory(LibPath);
            
            var _ref = DependencyContext.Default.CompileLibraries
                                .SelectMany(cl => cl.ResolveReferencePaths())
                                .Select(asm => MetadataReference.CreateFromFile(asm));


            DynamicDlls = new ConcurrentBag<string>();
            References = new ConcurrentBag<PortableExecutableReference>(_ref);
            
            IsComplete = true;
        }


        public static void Init()
        {
            if (Directory.Exists(LibPath))
            {
                Directory.Delete(LibPath, true);
            }
            Directory.CreateDirectory(LibPath);
        }

        /// <summary>
        /// 通过语法解析获取脚本内容中的第一个类
        /// </summary>
        /// <param name="content">脚本内容</param>
        /// <returns></returns>
        public static string GetClassName(string content)
        {
            SyntaxTree tree = CSharpSyntaxTree.ParseText(content);
            CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
            MemberDeclarationSyntax firstMember = root.Members[0];
            var NameSpaceDeclaration = (NamespaceDeclarationSyntax)firstMember;
            var ClassDeclaration = (ClassDeclarationSyntax)NameSpaceDeclaration.Members[0];
            var identifier = (IdentifierNameSyntax)NameSpaceDeclaration.Name;
            return $"{identifier.Identifier.Text.Trim()}.{ClassDeclaration.Identifier.Text}";
        }

        /// <summary>
        /// 脚本编译，根据类名生成dll
        /// </summary>
        /// <param name="content">脚本内容</param>
        /// <param name="className">指定类名</param>
        /// <param name="errorAction">发生错误执行委托</param>
        /// <returns></returns>
        public static Assembly Complier(string content, string className = null, Action<string> errorAction = null)
        {
            while (!IsComplete) { }
            //写入分析树
            SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(content);

            //添加程序及引用
          

            //创建dll
            if (className == null)
            {
                className = GetClassName(content);<commit_msg>Update ScriptComplier.cs<commit_after>﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.Extensions.DependencyModel;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;

namespace Natasha
{

    public class ScriptComplier
    {
        public static string LibPath;
        public static ConcurrentBag<string> DynamicDlls;
        public static ConcurrentBag<PortableExecutableReference> References;
        //public static bool IsComplete;
        
        static ScriptComplier()
        {
           // IsComplete = false;
            LibPath = AppDomain.CurrentDomain.BaseDirectory + "lib\\";
            if (Directory.Exists(LibPath))
            {
                Directory.Delete(LibPath, true);
            }
            Directory.CreateDirectory(LibPath);
            
            var _ref = DependencyContext.Default.CompileLibraries
                                .SelectMany(cl => cl.ResolveReferencePaths())
                                .Select(asm => MetadataReference.CreateFromFile(asm));


            DynamicDlls = new ConcurrentBag<string>();
            References = new ConcurrentBag<PortableExecutableReference>(_ref);
            
            //IsComplete = true;
        }


        public static void Init()
        {
            if (Directory.Exists(LibPath))
            {
                Directory.Delete(LibPath, true);
            }
            Directory.CreateDirectory(LibPath);
        }

        /// <summary>
        /// 通过语法解析获取脚本内容中的第一个类
        /// </summary>
        /// <param name="content">脚本内容</param>
        /// <returns></returns>
        public static string GetClassName(string content)
        {
            SyntaxTree tree = CSharpSyntaxTree.ParseText(content);
            CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
            MemberDeclarationSyntax firstMember = root.Members[0];
            var NameSpaceDeclaration = (NamespaceDeclarationSyntax)firstMember;
            var ClassDeclaration = (ClassDeclarationSyntax)NameSpaceDeclaration.Members[0];
            var identifier = (IdentifierNameSyntax)NameSpaceDeclaration.Name;
            return $"{identifier.Identifier.Text.Trim()}.{ClassDeclaration.Identifier.Text}";
        }

        /// <summary>
        /// 脚本编译，根据类名生成dll
        /// </summary>
        /// <param name="content">脚本内容</param>
        /// <param name="className">指定类名</param>
        /// <param name="errorAction">发生错误执行委托</param>
        /// <returns></returns>
        public static Assembly Complier(string content, string className = null, Action<string> errorAction = null)
        {

            //写入分析树
            SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(content);

            //添加程序及引用
          

            //创建dll
            if (className == null)
            {
                className = GetClassName(content);
{'fraction_non_alphanumeric': 0.0988296488946684, 'fraction_numerical': 0.0006501950585175553, 'mean_word_length': 2.0340236686390534, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, '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': '7036057', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1579, 'n_words': 290}