diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md index bf1b4f6..9ae8870 100644 --- a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md @@ -14,6 +14,32 @@ Implementing assemblies include the specific implementation and private fields o ## Preheating Examples +## 链式预热 + +从 V9 版本起,Natasha 支持链式预热: + +```cs +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //使用内存中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //过滤哪些元数据是不能用的,被排除的 + .WithExcludeReferences((asm, asmStr) => { return false; }) + //注册域构造器 + .Preheating(); +``` + +如果不指定相关 API ,预热将跳过此行为,例如只写 WithXXXReference 不写 using 相关的 API, 那么 Natasha 预热时将只对元数据进行操作,不会缓存 using code. 这样做的好处是实现了高度定制化,按需预热。 + ### Ordinary Preheating 1. The generic preheating method will automatically create a singleton for the compilation domain creator. @@ -22,17 +48,39 @@ Implementing assemblies include the specific implementation and private fields o 4. The second parameter indicates whether to extract 'Metadata' from memory assemblies. Set it to true to extract 'Metadata' from implementing assemblies. ```cs -//Register the compilation domain and preheat method +//注册编译域并预热方法 NatashaManagement.Preheating(true, true); +//或者 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //注册域构造器 + .Preheating(); ``` ### Using Cache Preheating -The first generation will write the 'Using Code' into the cache file 'Natasha.Namespace.cache', and subsequent restarts will automatically load it from the file. +指定第三个参数,第一次生成将 Using Code 写入缓存文件 Natasha.Namespace.cache 中,后续重启会自动从文件中加载。 ```cs -//Register the compilation domain and preheat method -NatashaManagement.Preheating(true, true, true); +//注册编译域并预热方法 +NatashaManagement.Preheating(true, true,true); +//或者 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //注册域构造器 + .Preheating(); ``` ### Separate Preheating diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md index 7929d8e..1f324e7 100644 --- a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md @@ -15,6 +15,32 @@ Reference Assembly does not include the implementation and will not cause unexpe ## Preheating Examples +### 链式预热 + +从 V9 版本起,Natasha 支持链式预热: + +```cs +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //使用内存中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //过滤哪些元数据是不能用的,被排除的 + .WithExcludeReferences((asm, asmStr) => { return false; }) + //注册域构造器 + .Preheating(); +``` + +如果不指定相关 API ,预热将跳过此行为,例如只写 WithXXXReference 不写 using 相关的 API, 那么 Natasha 预热时将只对元数据进行操作,不会缓存 using code. 这样做的好处是实现了高度定制化,按需预热。 + ### Ordinary Preheating 1. The generic preheating method will automatically create a singleton for the compilation domain creator. @@ -23,8 +49,18 @@ Reference Assembly does not include the implementation and will not cause unexpe 4. The second parameter specifies whether to extract the metadata from the memory assembly. Setting it to false will extract the metadata from the reference assembly. ```cs -//Register the compilation domain and preheating method +//注册编译域并预热方法 NatashaManagement.Preheating(false, false); +//或 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //注册域构造器 + .Preheating(); ``` ### Using Cache Preheating @@ -32,8 +68,20 @@ NatashaManagement.Preheating(false, false); The first generation will write the 'Using Code' into the cache file 'Natasha.Namespace.cache', and subsequent restarts will automatically load it from the file. ```cs -//Register the compilation domain and preheating method -NatashaManagement.Preheating(false, false, true); +//注册编译域并预热方法 +NatashaManagement.Preheating(false, false,true); +//或 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //注册域构造器 + .Preheating(); ``` ### Separate Preheating diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/004-Metadata.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/004-Metadata.md index f307a68..659f90c 100644 --- a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/004-Metadata.md +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/004-Metadata.md @@ -33,6 +33,8 @@ loadContext.AddReferenceAndUsingCode(refAssemblyFilePath); Add [metadata references] separately ```cs +loadContext.AddReferences() +//或 loadContext.ReferenceRecorder.AddReference( AssemblyName assemblyName, MetadataReference reference, @@ -44,6 +46,8 @@ The purpose of the third parameter is: when the assemblyName already exists in t ### Add [Using Code] separately ```cs +loadContext.AddUsing() +//或 loadContext.UsingRecorder.Using(string? @using); loadContext.UsingRecorder.Using(IEnumerable @using); loadContext.UsingRecorder.Using(Assembly assembly); diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/006-Advanced.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/006-Advanced.md index a203044..4405b91 100644 --- a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/006-Advanced.md +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/006-Advanced.md @@ -111,18 +111,18 @@ builder.SetDllFilePath/SetPdbFilePath/SetCommentFilePath(); Natasha has assembly-related configurations ```cs -//Output the full assembly +//输出完整程序集 builder.OutputAsFullAssembly(); -//Output the referenced assembly, which by default does not include private members +//输出引用程序集,此时将默认不包含私有成员 builder.OutputAsRefAssembly(); -//Include private members when outputting +//输出时包含私有成员 builder.WithPrivateMembers(); -//Do not include private members when outputting +//输出时不包含私有成员 builder.WithoutPrivateMembers(); -//The compilation result is a referenced assembly, written to a file, and not loaded into the domain. +//编译结果为引用程序集,且写入文件,且不会加载到域。 builder .OutputAsRefAssembly(); .WithFileOutput() - .WithoutInjectToDomain(); + .CompileWithoutAssembly(); ``` diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/007-EvenAndException.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/007-EvenAndException.md new file mode 100644 index 0000000..08d5464 --- /dev/null +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/007-EvenAndException.md @@ -0,0 +1,47 @@ +--- +title: 7. Natasha 的异常 +--- + +## 使用方法 + +Natasha 在编译时出错会抛出异常,异常 Model 如下: + +```cs +public sealed class NatashaException : Exception +{ + + //格式化后的脚本字符串 + public string Formatter; + + //错误类型 + public NatashaExceptionKind ErrorKind; + + //roslyn诊断集合 + public List Diagnostics; + + /// + /// 详细的编译信息 + /// + public string CompileMessage; + +} +``` + +### 如何监控和获取异常 + +Natasha 的事件执行流程如下: + +1. 添加语法树时: + +- 使用 FastAddScriptWithoutCheck,则不会抛出异常。 +- 使用 Add 则会进行语法检查,并抛出异常。 + +2. 编译时: + +- 编译后先触发 LogCompilationEvent 事件,用来获取编译后的信息。 +- 如果编译成功会继续引发 CompileSucceedEvent 事件。 +- 如果编译失败会继续引发 CompileFailedEvent 事件。 + +3. 编译周期之外: + +- 编译过后,可以通过 GetException() 获取异常(可能为空)。 diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/008-ExtensionType.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/008-ExtensionType.md new file mode 100644 index 0000000..f8ae671 --- /dev/null +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/008-ExtensionType.md @@ -0,0 +1,43 @@ +--- +title: 8. 使用 Natasha 的类型扩展 +--- + +```cs + +Example: + + + typeof(Dictionary>[]).GetRuntimeName(); + //result: "Dictionary>[]" + + + typeof(Dictionary>[]).GetDevelopName(); + //result: "System.Collections.Generic.Dictionary>[]" + + typeof(Dictionary<,>).GetDevelopNameWithoutFlag(); + //result: "System.Collections.Generic.Dictionary<,>" + + + typeof(Dictionary>[]).GetAvailableName(); + //result: "Dictionary_String_List_Int32____" + + + typeof(Dictionary>).GetAllGenericTypes(); + //result: [string,list<>,int] + + + typeof(Dictionary>).IsImplementFrom(); + //result: true + + + typeof(Dictionary>).IsSimpleType(); + //result: false + + + typeof(List<>).With(typeof(int)); + //result: List + +``` + +
+
diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/009-Codecov.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/009-Codecov.md new file mode 100644 index 0000000..41a404a --- /dev/null +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/009-Codecov.md @@ -0,0 +1,66 @@ +--- +title: 9. 使用 Codecov 的扩展 +--- + +## 使用方法 + +1. 引入 `DotNetCore.Natasha.CSharp.Extension.Codecov` 扩展包。 +2. 编码。 + +```cs +builder.WithCodecov(); +Assembly asm = builder.GetAssembly(); +List<(string MethodName, bool[] Usage)>? list = asm.GetCodecovCollection(); +Show(list); + + +public static void Show(List<(string MethodName, bool[] Usage)> list) +{ + for (int i = 0; i < list!.Count; i++) + { + if (list[i].Usage != null) + { + if (list[i].Usage.Length == 0) + { + Console.WriteLine($"{list[i].MethodName} 执行:100%"); + } + else + { + var executeCount = list[i].Usage.Count(item => item); + Console.WriteLine($"{list[i].MethodName} 执行:{((double)executeCount / list[i].Usage.Length).ToString("P")}"); + } + } + else + { + Console.WriteLine($"{list[i].MethodName} 未执行!"); + } + } +} +``` + +## Description + +CodeCov 将作为 Natasha 扩展库出现 + +使用方法: + +```cs +List<(string MethodName, bool[] Usage)>? result = Assembly.GetCodecovCollection(); +``` + +其中 result 将存放方法以及方法所执行行数的集合。 +比如 A 类中有方法 Method , Method 方法体共 6 行代码逻辑,在执行过程中仅执行了前4行。 +result 集合中将有: + +```cs +"MyNamespace.A.Method": + [0] = true, + [1] = true, + [2] = true, + [3] = true, + [4] = false, + [5] = false, +``` + +
+
diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/010-CompileDirector.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/010-CompileDirector.md new file mode 100644 index 0000000..67b694f --- /dev/null +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/010-CompileDirector.md @@ -0,0 +1,55 @@ +--- +title: 10. 使用 CompileDirector 的扩展 +--- + +## Introduction + +这是一个可以让编译单元不断学习和矫正 using code 的库,在重复的编译场景中,几乎每次 using code 和元数据引用都大同小异,甚至不会改变, +该库允编译单元在编译成功时学习有用的 using code 以备后用。 + +## 使用方法 + +1. 引入 `DotNetCore.Natasha.CSharp.Extension.CompileDirector` 扩展包。 +2. 编码。 + +### 编译导演 + +```cs +//传入采样计数阈值,不传默认为 2. +//采样计数会影响 CompileDirector 的“学习”效率. +//每次编译成功,该编译单元成功的 using 计数 + 1. +//当 using 计数大于采样计数阈值时,将被 CompileDirector 内部的 UsingCache 录用为正式 using. +//正式 using 每次会优先覆盖脚本 +CompileDirector director = new CompileDirector(3); + +//配置每次从该场景中产生的 编译单元。 +director.ConfigBuilder(builder => builder +.ConfigSyntaxOptions(opt => opt.WithPreprocessorSymbols("DEBUG")) +.WithDebugCompile() +.ConfigLoadContext(ctx => ctx + .AddReferenceAndUsingCode() + .AddReferenceAndUsingCode(typeof(Math)) + .AddReferenceAndUsingCode(typeof(File)) +)); + +//生产编译单元 +//由 director 生产的编译单元会自动覆盖已经被录用的 using code. +var builder = director.CreateBuilder(); + +//编译单元 +builder.Add(@"public static class A +{ + public static void Show() + { + File.WriteAllText(""1.txt"", ""1""); + Console.WriteLine(Math.Abs(-4)); + } +}"); + +//通过 director 获取程序集 +// 成功时继续挑选和计数录用 using code +// 失败时将使用对应的 using 覆盖策略进行重编译 +// 通过 ConfigUsingConverSrategy API 配置失败时的覆盖策略 +var asm = director.GetAssembly(builder); +asm.GetDelegateFromShortName("A", "Show")!(); +``` diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/011-MethodCreator.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/011-MethodCreator.md new file mode 100644 index 0000000..aedee1d --- /dev/null +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/compile/011-MethodCreator.md @@ -0,0 +1,36 @@ +--- +title: 11. 使用 MethodCreator 的扩展 +--- + +## Introduction + +MethodCreator 扩展库允许开发者快速构建动态委托。 + +## 使用方法 + +1. 引入 `DotNetCore.Natasha.CSharp.Extension.MethodCreator` 扩展包。 +2. 编码。 + +### 共分为两种模式 + +#### 精简构造模式 + +精简构造模式需要自己管理元数据和using + +```cs + var func = "return arg1 + arg2 + 0.1;" + .WithMetadata() + .ToFunc(); +``` + +#### 智能构造模式 + +智能构造模式将自动覆盖元数据和 using + +```cs + var func = "return arg1 + arg2 + 0.1;" + .ToFunc(); +``` + +
+
diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/faq/index.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/faq/index.md index 67dba3e..19fef95 100644 --- a/src/i18n/en-US/docusaurus-plugin-content-docs/current/faq/index.md +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/faq/index.md @@ -67,7 +67,7 @@ A: The dynamic construction ability of Natasha is very powerful. It can be used ### I don't want to compile every time, can I cache the result of the script? -A: No, caching the result needs to be done by you. Natasha follows the lightweight route. `AssemblyCSharpBuilder` is the smallest and most basic compilation unit. If you want to cache, you can implement a `ConcurrentDictionary` to cache the result.Natasha is not responsible for anything other than the compilation responsibility. +A: No, you cannot. Caching the result requires your action. Natasha follows a lightweight approach, where `AssemblyCSharpBuilder` is the smallest and most basic compilation unit. If you want to cache, you can implement your own `ConcurrentDictionary` to cache the result.Natasha is not responsible for anything other than the compilation responsibility. ### I only want to use the using statement, but I don't want to add so many references. diff --git a/src/i18n/en-US/docusaurus-plugin-content-docs/current/template/001-static-init.md b/src/i18n/en-US/docusaurus-plugin-content-docs/current/template/001-static-init.md index d2adb5d..c142134 100644 --- a/src/i18n/en-US/docusaurus-plugin-content-docs/current/template/001-static-init.md +++ b/src/i18n/en-US/docusaurus-plugin-content-docs/current/template/001-static-init.md @@ -2,6 +2,8 @@ title: 1. Template initialization operation --- +## 该文档已停更,若需要请提 ISSUE + Natasha 的所有模板均继承自 ComplierTemplate ,ComplierTemplate 本身会提供静态构造方法。因此上层 API 也会被支持。Therefore, the upper-level API is also supported.Therefore, the upper-level API will also be supported.
diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md index cb1df0e..088bf13 100644 --- a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Full.md @@ -14,6 +14,32 @@ title: 2.1 スマートコンパイルモード - 実装アセンブリ ## プレヒートの例 +## 链式预热 + +从 V9 版本起,Natasha 支持链式预热: + +```cs +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //使用内存中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //过滤哪些元数据是不能用的,被排除的 + .WithExcludeReferences((asm, asmStr) => { return false; }) + //注册域构造器 + .Preheating(); +``` + +如果不指定相关 API ,预热将跳过此行为,例如只写 WithXXXReference 不写 using 相关的 API, 那么 Natasha 预热时将只对元数据进行操作,不会缓存 using code. 这样做的好处是实现了高度定制化,按需预热。 + ### 通常のプレヒート 1. ジェネリックなプレヒートメソッドでコンパイルドメインクリエーターのシングルトンを自動的に作成します。 @@ -22,17 +48,39 @@ title: 2.1 スマートコンパイルモード - 実装アセンブリ 4. 2番目のパラメータは、メモリアセンブリからメタデータを抽出するかどうかを指定します。true を設定すると、実装アセンブリからメタデータを抽出します。 ```cs -//コンパイルドメインの登録と事前準備のメソッド +//注册编译域并预热方法 NatashaManagement.Preheating(true, true); +//或者 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //注册域构造器 + .Preheating(); ``` ### Usingキャッシュのプレヒート -最初の生成時にUsing Codeをキャッシュファイル`Natasha.Namespace.cache`に書き込み、後続の再起動ではファイルから自動的に読み込みます。 +指定第三个参数,第一次生成将 Using Code 写入缓存文件 Natasha.Namespace.cache 中,后续重启会自动从文件中加载。 ```cs -//コンパイルドメインの登録と事前準備のメソッド -NatashaManagement.Preheating(true, true, true); +//注册编译域并预热方法 +NatashaManagement.Preheating(true, true,true); +//或者 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //注册域构造器 + .Preheating(); ``` ### 分離プレヒート diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md index 55bf973..cc82dde 100644 --- a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/002-SmartMode-Ref.md @@ -15,6 +15,32 @@ title: 2.2 スマートコンパイルモード-アセンブリの参照 ## プレヒートの例 +### 链式预热 + +从 V9 版本起,Natasha 支持链式预热: + +```cs +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //使用内存中的命名空间 + .WithMemoryUsing() + //使用内存中的元数据 + .WithMemoryReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //过滤哪些元数据是不能用的,被排除的 + .WithExcludeReferences((asm, asmStr) => { return false; }) + //注册域构造器 + .Preheating(); +``` + +如果不指定相关 API ,预热将跳过此行为,例如只写 WithXXXReference 不写 using 相关的 API, 那么 Natasha 预热时将只对元数据进行操作,不会缓存 using code. 这样做的好处是实现了高度定制化,按需预热。 + ### 通常のプレヒート 1. ジェネリックなプレヒートメソッドでコンパイルドメインクリエーターのシングルトンを自動的に作成します。 @@ -23,8 +49,18 @@ title: 2.2 スマートコンパイルモード-アセンブリの参照 4. 2番目のパラメータは、メモリアセンブリからメタデータを抽出するかどうかを指定します。falseに設定すると、アセンブリの参照からメタデータを抽出します。 ```cs -//コンパイルドメインを登録してプレヒートメソッドを使用 +//注册编译域并预热方法 NatashaManagement.Preheating(false, false); +//或 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //注册域构造器 + .Preheating(); ``` ### Usingキャッシュのプレヒート @@ -32,8 +68,20 @@ NatashaManagement.Preheating(false, false); 最初の生成時にUsing Codeをキャッシュファイル`Natasha.Namespace.cache`に書き込み、後続の再起動ではファイルから自動的に読み込みます。 ```cs -//コンパイルドメインを登録してプレヒートメソッドを使用 +//注册编译域并预热方法 NatashaManagement.Preheating(false, false,true); +//或 V9 版本 +NatashaManagement + //获取链式构造器 + .GetInitializer() + //使用引用程序集中的命名空间 + .WithRefUsing() + //使用引用程序集中的元数据 + .WithRefReference() + //使用文件来持久化缓存 命名空间 + .WithFileUsingCache() + //注册域构造器 + .Preheating(); ``` ### 分離プレヒート diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/004-Metadata.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/004-Metadata.md index dc87073..fb59a41 100644 --- a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/004-Metadata.md +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/004-Metadata.md @@ -33,6 +33,8 @@ loadContext.AddReferenceAndUsingCode(refAssemblyFilePath); 单独添加 [元数据引用] ```cs +loadContext.AddReferences() +//或 loadContext.ReferenceRecorder.AddReference( AssemblyName assemblyName, MetadataReference reference, @@ -44,6 +46,8 @@ loadContext.ReferenceRecorder.AddReference( ### 单独增加 [Using Code] ```cs +loadContext.AddUsing() +//或 loadContext.UsingRecorder.Using(string? @using); loadContext.UsingRecorder.Using(IEnumerable @using); loadContext.UsingRecorder.Using(Assembly assembly); diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/006-Advanced.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/006-Advanced.md index d04e514..ebbb3d5 100644 --- a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/006-Advanced.md +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/006-Advanced.md @@ -111,18 +111,18 @@ builder.SetDllFilePath/SetPdbFilePath/SetCommentFilePath(); Natashaには、アセンブリに関連する設定があります ```cs -//完全なアセンブリを出力 +//输出完整程序集 builder.OutputAsFullAssembly(); -//参照アセンブリを出力し、この時点ではプライベートメンバーはデフォルトで含まれません +//输出引用程序集,此时将默认不包含私有成员 builder.OutputAsRefAssembly(); -//プライベートメンバーを含むように出力 +//输出时包含私有成员 builder.WithPrivateMembers(); -//プライベートメンバーを含まないように出力 +//输出时不包含私有成员 builder.WithoutPrivateMembers(); -//コンパイル結果は参照アセンブリとして出力され、ファイルに書き込まれ、ドメインにはロードされません。 +//编译结果为引用程序集,且写入文件,且不会加载到域。 builder .OutputAsRefAssembly(); .WithFileOutput() - .WithoutInjectToDomain(); + .CompileWithoutAssembly(); ``` diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/007-EvenAndException.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/007-EvenAndException.md new file mode 100644 index 0000000..08d5464 --- /dev/null +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/007-EvenAndException.md @@ -0,0 +1,47 @@ +--- +title: 7. Natasha 的异常 +--- + +## 使用方法 + +Natasha 在编译时出错会抛出异常,异常 Model 如下: + +```cs +public sealed class NatashaException : Exception +{ + + //格式化后的脚本字符串 + public string Formatter; + + //错误类型 + public NatashaExceptionKind ErrorKind; + + //roslyn诊断集合 + public List Diagnostics; + + /// + /// 详细的编译信息 + /// + public string CompileMessage; + +} +``` + +### 如何监控和获取异常 + +Natasha 的事件执行流程如下: + +1. 添加语法树时: + +- 使用 FastAddScriptWithoutCheck,则不会抛出异常。 +- 使用 Add 则会进行语法检查,并抛出异常。 + +2. 编译时: + +- 编译后先触发 LogCompilationEvent 事件,用来获取编译后的信息。 +- 如果编译成功会继续引发 CompileSucceedEvent 事件。 +- 如果编译失败会继续引发 CompileFailedEvent 事件。 + +3. 编译周期之外: + +- 编译过后,可以通过 GetException() 获取异常(可能为空)。 diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/008-ExtensionType.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/008-ExtensionType.md new file mode 100644 index 0000000..f8ae671 --- /dev/null +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/008-ExtensionType.md @@ -0,0 +1,43 @@ +--- +title: 8. 使用 Natasha 的类型扩展 +--- + +```cs + +Example: + + + typeof(Dictionary>[]).GetRuntimeName(); + //result: "Dictionary>[]" + + + typeof(Dictionary>[]).GetDevelopName(); + //result: "System.Collections.Generic.Dictionary>[]" + + typeof(Dictionary<,>).GetDevelopNameWithoutFlag(); + //result: "System.Collections.Generic.Dictionary<,>" + + + typeof(Dictionary>[]).GetAvailableName(); + //result: "Dictionary_String_List_Int32____" + + + typeof(Dictionary>).GetAllGenericTypes(); + //result: [string,list<>,int] + + + typeof(Dictionary>).IsImplementFrom(); + //result: true + + + typeof(Dictionary>).IsSimpleType(); + //result: false + + + typeof(List<>).With(typeof(int)); + //result: List + +``` + +
+
diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/009-Codecov.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/009-Codecov.md new file mode 100644 index 0000000..a60e9d5 --- /dev/null +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/009-Codecov.md @@ -0,0 +1,66 @@ +--- +title: 9. 使用 Codecov 的扩展 +--- + +## 使用方法 + +1. 引入 `DotNetCore.Natasha.CSharp.Extension.Codecov` 扩展包。 +2. 编码。 + +```cs +builder.WithCodecov(); +Assembly asm = builder.GetAssembly(); +List<(string MethodName, bool[] Usage)>? list = asm.GetCodecovCollection(); +Show(list); + + +public static void Show(List<(string MethodName, bool[] Usage)> list) +{ + for (int i = 0; i < list!.Count; i++) + { + if (list[i].Usage != null) + { + if (list[i].Usage.Length == 0) + { + Console.WriteLine($"{list[i].MethodName} 执行:100%"); + } + else + { + var executeCount = list[i].Usage.Count(item => item); + Console.WriteLine($"{list[i].MethodName} 执行:{((double)executeCount / list[i].Usage.Length).ToString("P")}"); + } + } + else + { + Console.WriteLine($"{list[i].MethodName} 未执行!"); + } + } +} +``` + +## 说明 + +CodeCov 将作为 Natasha 扩展库出现 + +使用方法: + +```cs +List<(string MethodName, bool[] Usage)>? result = Assembly.GetCodecovCollection(); +``` + +其中 result 将存放方法以及方法所执行行数的集合。 +比如 A 类中有方法 Method , Method 方法体共 6 行代码逻辑,在执行过程中仅执行了前4行。 +result 集合中将有: + +```cs +"MyNamespace.A.Method": + [0] = true, + [1] = true, + [2] = true, + [3] = true, + [4] = false, + [5] = false, +``` + +
+
diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/010-CompileDirector.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/010-CompileDirector.md new file mode 100644 index 0000000..0770c58 --- /dev/null +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/010-CompileDirector.md @@ -0,0 +1,55 @@ +--- +title: 10. 使用 CompileDirector 的扩展 +--- + +## 介绍 + +这是一个可以让编译单元不断学习和矫正 using code 的库,在重复的编译场景中,几乎每次 using code 和元数据引用都大同小异,甚至不会改变, +该库允编译单元在编译成功时学习有用的 using code 以备后用。 + +## 使用方法 + +1. 引入 `DotNetCore.Natasha.CSharp.Extension.CompileDirector` 扩展包。 +2. 编码。 + +### 编译导演 + +```cs +//传入采样计数阈值,不传默认为 2. +//采样计数会影响 CompileDirector 的“学习”效率. +//每次编译成功,该编译单元成功的 using 计数 + 1. +//当 using 计数大于采样计数阈值时,将被 CompileDirector 内部的 UsingCache 录用为正式 using. +//正式 using 每次会优先覆盖脚本 +CompileDirector director = new CompileDirector(3); + +//配置每次从该场景中产生的 编译单元。 +director.ConfigBuilder(builder => builder +.ConfigSyntaxOptions(opt => opt.WithPreprocessorSymbols("DEBUG")) +.WithDebugCompile() +.ConfigLoadContext(ctx => ctx + .AddReferenceAndUsingCode() + .AddReferenceAndUsingCode(typeof(Math)) + .AddReferenceAndUsingCode(typeof(File)) +)); + +//生产编译单元 +//由 director 生产的编译单元会自动覆盖已经被录用的 using code. +var builder = director.CreateBuilder(); + +//编译单元 +builder.Add(@"public static class A +{ + public static void Show() + { + File.WriteAllText(""1.txt"", ""1""); + Console.WriteLine(Math.Abs(-4)); + } +}"); + +//通过 director 获取程序集 +// 成功时继续挑选和计数录用 using code +// 失败时将使用对应的 using 覆盖策略进行重编译 +// 通过 ConfigUsingConverSrategy API 配置失败时的覆盖策略 +var asm = director.GetAssembly(builder); +asm.GetDelegateFromShortName("A", "Show")!(); +``` diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/011-MethodCreator.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/011-MethodCreator.md new file mode 100644 index 0000000..e43d059 --- /dev/null +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/compile/011-MethodCreator.md @@ -0,0 +1,36 @@ +--- +title: 11. 使用 MethodCreator 的扩展 +--- + +## 介绍 + +MethodCreator 扩展库允许开发者快速构建动态委托。 + +## 使用方法 + +1. 引入 `DotNetCore.Natasha.CSharp.Extension.MethodCreator` 扩展包。 +2. 编码。 + +### 共分为两种模式 + +#### 精简构造模式 + +精简构造模式需要自己管理元数据和using + +```cs + var func = "return arg1 + arg2 + 0.1;" + .WithMetadata() + .ToFunc(); +``` + +#### 智能构造模式 + +智能构造模式将自动覆盖元数据和 using + +```cs + var func = "return arg1 + arg2 + 0.1;" + .ToFunc(); +``` + +
+
diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/faq/index.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/faq/index.md index 349946b..5d6294b 100644 --- a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/faq/index.md +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/faq/index.md @@ -67,7 +67,7 @@ var simpleFunc = "return Math.Floor(arg1/0.3);" ### スクリプトの結果をキャッシュしたくありません。毎回コンパイルするのは嫌ですか? -答え:できません。結果をキャッシュするには、自分で`ConcurrentDictionary`を実装する必要があります。Natashaは軽量化路線を歩んでおり、`AssemblyCSharpBuilder`が最小限の基本的なコンパイルユニットです。Natasha 不负责编译职责之外的事情。 +答:不可以,缓存結果需要您來完成,Natasha 走的是輕量化路線,`AssemblyCSharpBuilder` 為最小最基礎的編譯單元,如果你想緩存,可以自己實現一個 `ConcurrentDictionary` 來緩存結果。Natasha 不负责编译职责之外的事情。 ### 我只想使用 using 全集,但不想添加那么多的引用。 diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/001-static-init.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/001-static-init.md index 8a082ad..d432f12 100644 --- a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/001-static-init.md +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/001-static-init.md @@ -2,6 +2,8 @@ title: 1. テンプレートの初期化操作 --- +## 该文档已停更,若需要请提 ISSUE + Natasha のすべてのテンプレートは ComplierTemplate を継承しています。ComplierTemplate は静的な構築メソッドを提供します。そのため、上位の API もサポートされます。
diff --git a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/004-development-specification.md b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/004-development-specification.md index a2f140b..ccd023a 100644 --- a/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/004-development-specification.md +++ b/src/i18n/ja-JP/docusaurus-plugin-content-docs/current/template/004-development-specification.md @@ -43,7 +43,7 @@ Package(Builder) + API + Extension => Operator
-直接使用Natasha内置的Builder可以快速实现定制,例如:`OopBuilder`,`MethodBuilder`泛型方法。 +直接使用Natasha内置的Builder可以快速实现定制,例如:OopBuilder,MethodBuilder泛型方法。 前者为其提供对象构造模板,后者专注构建方法。