-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
271 additions
and
349 deletions.
There are no files selected for viewing
371 changes: 35 additions & 336 deletions
371
src/Natasha.CSharp/Natasha.CSharp.Compiler/CompileUnit/AssemblyCSharpBuilder.Compile.cs
Large diffs are not rendered by default.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
src/Natasha.CSharp/Natasha.CSharp.Compiler/CompileUnit/AssemblyCSharpBuilder.Emit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.CodeAnalysis.Emit; | ||
using System; | ||
using System.Collections.Concurrent; | ||
/// <summary> | ||
/// 程序集编译构建器 - EMIT选项 | ||
/// </summary> | ||
public sealed partial class AssemblyCSharpBuilder | ||
{ | ||
private ConcurrentQueue<Func<EmitOptions, EmitOptions>>? _emitOptionHandle; | ||
/// <summary> | ||
/// 追加对 emitOption 的处理逻辑. | ||
/// <list type="bullet"> | ||
/// <item>一次性配置,不可重用.</item> | ||
/// <item>多次调用会进入配置队列.</item> | ||
/// <item>调用 <see cref="GetAssembly"/> 后清空队列.</item> | ||
/// <item>调用 <see cref="Clear"/> 后清空队列.</item> | ||
/// <item>调用 <see cref="ClearEmitOptionCache"/> 后清空队列.</item> | ||
/// </list> | ||
/// </summary> | ||
/// <remarks> | ||
/// 注:该配置属于一次性配置,若重复使用该配置逻辑,请在这次编译后重新调用该方法. | ||
/// </remarks> | ||
/// <param name="handleAndReturnNewEmitOption"></param> | ||
/// <returns>链式对象(调用方法的实例本身).</returns> | ||
public AssemblyCSharpBuilder ConfigEmitOptions(Func<EmitOptions, EmitOptions> handleAndReturnNewEmitOption) | ||
{ | ||
_emitOptionHandle ??= new ConcurrentQueue<Func<EmitOptions, EmitOptions>>(); | ||
_emitOptionHandle.Enqueue(handleAndReturnNewEmitOption); | ||
return this; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Natasha.CSharp/Natasha.CSharp.Compiler/CompileUnit/AssemblyCSharpBuilder.Event.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Reflection; | ||
|
||
/// <summary> | ||
/// 程序集编译构建器 - 事件 | ||
/// </summary> | ||
public sealed partial class AssemblyCSharpBuilder | ||
{ | ||
/// <summary> | ||
/// 监听编译日志事件,默认不监听. | ||
/// </summary> | ||
/// <remarks> | ||
/// 注:该事件会被缓存,复用时无需重复添加方法. | ||
/// </remarks> | ||
public event Action<NatashaCompilationLog>? LogCompilationEvent; | ||
/// <summary> | ||
/// 流编译成功之后触发的事件. | ||
/// </summary> | ||
/// <remarks> | ||
/// 此时已编译结束,程序集已经生成并加载. | ||
/// </remarks> | ||
public event Action<CSharpCompilation, Assembly>? CompileSucceedEvent; | ||
|
||
|
||
/// <summary> | ||
/// 流编译失败之后触发的事件. | ||
/// </summary> | ||
/// <remarks> | ||
/// 此时已经编译结束, 但是编译失败. | ||
/// </remarks> | ||
public event Action<CSharpCompilation, ImmutableArray<Diagnostic>>? CompileFailedEvent; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/Natasha.CSharp/Natasha.CSharp.Compiler/CompileUnit/AssemblyCSharpBuilder.References.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System; | ||
using System.Collections.Generic; | ||
/// <summary> | ||
/// 程序集编译构建器 - 引用选项 | ||
/// </summary> | ||
public sealed partial class AssemblyCSharpBuilder | ||
{ | ||
private Func<IEnumerable<MetadataReference>, IEnumerable<MetadataReference>>? _referencesFilter; | ||
private CombineReferenceBehavior _combineReferenceBehavior = CombineReferenceBehavior.UseCurrent; | ||
private readonly ReferenceConfiguration _referenceConfiguration = new(); | ||
private readonly List<MetadataReference> _specifiedReferences; | ||
|
||
/// <summary> | ||
/// 该方法允许共享域参与编译. | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description>[共享域] 元数据 [参与] 编译.</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>[当前域] 元数据 [参与] 编译.</description> | ||
/// </item> | ||
/// </list> | ||
/// </summary> | ||
/// <remarks> | ||
/// 注:若两个域不同,且存在相同名称元数据,默认优先使用主域的元数据. | ||
/// </remarks> | ||
/// <param name="action">配置同名元数据的解决策略</param> | ||
/// <returns>链式对象(调用方法的实例本身).</returns> | ||
public AssemblyCSharpBuilder WithCombineReferences(Action<ReferenceConfiguration>? action = null) | ||
{ | ||
action?.Invoke(_referenceConfiguration); | ||
_combineReferenceBehavior = CombineReferenceBehavior.CombineDefault; | ||
return this; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 配置编译元数据的合并行为. | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description>[共享域] 元数据 [不参与] 编译.</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>[当前域] 元数据 [参与] 编译.</description> | ||
/// </item> | ||
/// </list> | ||
/// </summary> | ||
/// <returns>链式对象(调用方法的实例本身).</returns> | ||
public AssemblyCSharpBuilder WithCurrentReferences() | ||
{ | ||
_combineReferenceBehavior = CombineReferenceBehavior.UseCurrent; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 使用外部指定的元数据引用进行编译. | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description>[共享域] 元数据 [不参与] 编译.</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>[当前域] 元数据 [不参与] 编译.</description> | ||
/// </item> | ||
/// </list> | ||
/// </summary> | ||
/// <remarks> | ||
/// 使用 ClearOutsideReferences 可以清除本次传递的元数据引用. | ||
/// </remarks> | ||
/// <param name="metadataReferences"></param> | ||
/// <returns>链式对象(调用方法的实例本身).</returns> | ||
public AssemblyCSharpBuilder WithSpecifiedReferences(IEnumerable<MetadataReference> metadataReferences) | ||
{ | ||
lock (_specifiedReferences) | ||
{ | ||
_specifiedReferences.AddRange(metadataReferences); | ||
} | ||
_combineReferenceBehavior = CombineReferenceBehavior.UseSpecified; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 清除由 WithSpecifiedReferences 方法传入的元数据引用. | ||
/// </summary> | ||
/// <returns>链式对象(调用方法的实例本身).</returns> | ||
public AssemblyCSharpBuilder ClearOutsideReferences() | ||
{ | ||
lock (_specifiedReferences) | ||
{ | ||
_specifiedReferences.Clear(); | ||
} | ||
return this; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 配置元数据引用过滤策略. | ||
/// </summary> | ||
/// <param name="referencesFilter"></param> | ||
/// <returns>链式对象(调用方法的实例本身).</returns> | ||
public AssemblyCSharpBuilder SetReferencesFilter(Func<IEnumerable<MetadataReference>, IEnumerable<MetadataReference>>? referencesFilter) | ||
{ | ||
_referencesFilter = referencesFilter; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Natasha.CSharp/Natasha.CSharp.Compiler/Utils/NatashaFileRepeateHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Natasha.CSharp.Compiler.Utils | ||
{ | ||
internal static class NatashaFileRepeateHelper | ||
{ | ||
public static string GetAvaliableFilePath(string file) | ||
{ | ||
var tempOutputFolder = Path.GetDirectoryName(file); | ||
if (!Directory.Exists(tempOutputFolder)) | ||
{ | ||
Directory.CreateDirectory(tempOutputFolder); | ||
} | ||
var tempFileName = Path.GetFileName(file); | ||
return Path.Combine(tempOutputFolder, $"repeate.{Guid.NewGuid():N}.{tempFileName}"); | ||
} | ||
} | ||
} |