-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulled out IAssemblyGenerator and moved more logic to JasperFx from J…
…asperFx.RuntimeCompiler. Closes GH-17
- Loading branch information
1 parent
adf72ac
commit c3caa32
Showing
5 changed files
with
167 additions
and
21 deletions.
There are no files selected for viewing
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
4 changes: 1 addition & 3 deletions
4
...eCompiler/ExpectedTypeMissingException.cs → ...eneration/ExpectedTypeMissingException.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
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,40 @@ | ||
using System.Reflection; | ||
using JasperFx.CodeGeneration.Model; | ||
|
||
namespace JasperFx.CodeGeneration; | ||
|
||
public interface IAssemblyGenerator | ||
{ | ||
string AssemblyName { get; set; } | ||
string Code { get; } | ||
|
||
/// <summary> | ||
/// Tells Roslyn to reference the given assembly and any of its dependencies | ||
/// when compiling code | ||
/// </summary> | ||
/// <param name="assembly"></param> | ||
void ReferenceAssembly(Assembly assembly); | ||
|
||
/// <summary> | ||
/// Reference the assembly containing the type "T" | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
void ReferenceAssemblyContainingType<T>(); | ||
|
||
/// <summary> | ||
/// Compile code built up by using an ISourceWriter to a new assembly in memory | ||
/// </summary> | ||
/// <param name="write"></param> | ||
/// <returns></returns> | ||
Assembly Generate(Action<ISourceWriter> write); | ||
|
||
/// <summary> | ||
/// Compile the code passed into this method to a new assembly in memory | ||
/// </summary> | ||
/// <param name="code"></param> | ||
/// <returns></returns> | ||
/// <exception cref="InvalidOperationException"></exception> | ||
Assembly Generate(string code); | ||
|
||
void Compile(GeneratedAssembly generatedAssembly, IServiceVariableSource services = null); | ||
} |
101 changes: 101 additions & 0 deletions
101
src/JasperFx/Events/CodeGeneration/NewGeneratedProjection.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,101 @@ | ||
using System.Reflection; | ||
using JasperFx.CodeGeneration; | ||
using JasperFx.Core.Reflection; | ||
|
||
namespace JasperFx.Events.CodeGeneration; | ||
|
||
/* | ||
public abstract class NewGeneratedProjection<TOperations, TStore, TDatabase, TOptions> : ICodeFile | ||
{ | ||
private readonly Type _projectionType; | ||
private bool _hasGenerated; | ||
public NewGeneratedProjection(Type projectionType) | ||
{ | ||
_projectionType = projectionType; | ||
} | ||
internal TOptions StoreOptions { get; set; } | ||
bool ICodeFile.AttachTypesSynchronously(GenerationRules rules, Assembly assembly, IServiceProvider services, | ||
string containingNamespace) | ||
{ | ||
return tryAttachTypes(assembly, StoreOptions); | ||
} | ||
public string FileName => GetType().ToSuffixedTypeName("RuntimeSupport"); | ||
void ICodeFile.AssembleTypes(GeneratedAssembly assembly) | ||
{ | ||
if (_hasGenerated) | ||
return; | ||
lock (_assembleLocker) | ||
{ | ||
if (_hasGenerated) | ||
return; | ||
assembleTypes(assembly, StoreOptions); | ||
_hasGenerated = true; | ||
} | ||
} | ||
Task<bool> ICodeFile.AttachTypes(GenerationRules rules, Assembly assembly, IServiceProvider services, | ||
string containingNamespace) | ||
{ | ||
var attached = tryAttachTypes(assembly, StoreOptions); | ||
return Task.FromResult(attached); | ||
} | ||
public Type ProjectionType => GetType(); | ||
protected abstract void assembleTypes(GeneratedAssembly assembly, TOptions options); | ||
protected abstract bool tryAttachTypes(Assembly assembly, TOptions options); | ||
private void generateIfNecessary(TStore store) | ||
{ | ||
lock (_assembleLocker) | ||
{ | ||
if (_hasGenerated) | ||
{ | ||
return; | ||
} | ||
generateIfNecessaryLocked(); | ||
_hasGenerated = true; | ||
} | ||
return; | ||
void generateIfNecessaryLocked() | ||
{ | ||
StoreOptions = store.Options; | ||
var rules = store.Options.CreateGenerationRules(); | ||
rules.ReferenceTypes(GetType()); | ||
this.As<ICodeFile>().InitializeSynchronously(rules, store.Options.EventGraph, null); | ||
if (!needsSettersGenerated()) | ||
{ | ||
return; | ||
} | ||
var generatedAssembly = new GeneratedAssembly(rules); | ||
assembleTypes(generatedAssembly, store.Options); | ||
// This will force it to create any setters or dynamic funcs | ||
generatedAssembly.GenerateCode(); | ||
} | ||
} | ||
/// <summary> | ||
/// Prevent code generation bugs when multiple aggregates are code generated in parallel | ||
/// Happens more often on dynamic code generation | ||
/// </summary> | ||
protected static object _assembleLocker = new(); | ||
protected abstract bool needsSettersGenerated(); | ||
} | ||
*/ |