Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add event handler #247

Open
wants to merge 1 commit into
base: v4/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ build.out/

# eof
src/.idea
*.nupkg
16 changes: 16 additions & 0 deletions src/Our.ModelsBuilder/Building/CodeCompilationsArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp;

namespace Our.ModelsBuilder.Building
{
public class CodeCompilationsArgs : EventArgs
{
public LanguageVersion OptionsLanguageVersion { get; }

public CodeCompilationsArgs(LanguageVersion optionsLanguageVersion, Dictionary<string, string> dictionary)
{
OptionsLanguageVersion = optionsLanguageVersion;
}
}
}
17 changes: 17 additions & 0 deletions src/Our.ModelsBuilder/Building/CodeGenerationArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Our.ModelsBuilder.Options;

namespace Our.ModelsBuilder.Building
{
public class CodeGenerationArgs : EventArgs
{
public ICodeFactory CodeFactory;
public ModelsBuilderOptions Options;

public CodeGenerationArgs(ICodeFactory codeFactory, ModelsBuilderOptions options)
{
CodeFactory = codeFactory;
Options = options;
}
}
}
21 changes: 16 additions & 5 deletions src/Our.ModelsBuilder/Building/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ public class Generator
{
private readonly ICodeFactory _codeFactory;
private readonly ModelsBuilderOptions _options;

public static EventHandler<CodeGenerationArgs> CustomActionOnGeneration;
protected virtual void OnGeneration(CodeGenerationArgs e)
{
CustomActionOnGeneration?.Invoke(this, e);
}
public static EventHandler<CodeCompilationsArgs> CustomActionOnCompiliation;
protected virtual void OnCompiliation(CodeCompilationsArgs e)
{
CustomActionOnCompiliation?.Invoke(this, e);
}
public Generator(ICodeFactory codeFactory, ModelsBuilderOptions options)
{
_codeFactory = codeFactory;
_options = options;
}

public void GenerateModels(string modelsDirectory, string modelsNamespace, string bin)
{
if (!Directory.Exists(modelsDirectory))
Expand All @@ -35,7 +44,7 @@ public void GenerateModels(string modelsDirectory, string modelsNamespace, strin
var filename = Path.Combine(modelsDirectory, name + ".generated.cs");
File.WriteAllText(filename, code);
});

OnGeneration(new CodeGenerationArgs(_codeFactory,_options));
// the idea was to calculate the current hash and to add it as an extra file to the compilation,
// in order to be able to detect whether a DLL is consistent with an environment - however the
// environment *might not* contain the local partial files, and thus it could be impossible to
Expand All @@ -46,17 +55,19 @@ public void GenerateModels(string modelsDirectory, string modelsNamespace, strin
[assembly:ModelsBuilderAssembly(SourceHash = ""{currentHash}"")]
";
*/

if (bin != null)
{
// build
foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
files[file] = File.ReadAllText(file);
var compiler = new Compiler(_options.LanguageVersion);
OnCompiliation(new CodeCompilationsArgs(_options.LanguageVersion, files));
// FIXME what is the name of the DLL as soon as we accept several namespaces = an option?
compiler.Compile(codeModel.AssemblyName, files, bin);

}

OutOfDateModelsStatus.Clear();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Our.ModelsBuilder/Our.ModelsBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Building\CodeCompilationsArgs.cs" />
<Compile Include="Building\CodeGenerationArgs.cs" />
<Compile Include="Building\CodeModel.cs" />
<Compile Include="Building\CodeModelBuilder.cs" />
<Compile Include="Building\CodeModelData.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Our.ModelsBuilder/ReferencedAssemblies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Our.ModelsBuilder
{
internal static class ReferencedAssemblies
public sealed class ReferencedAssemblies
{
private static readonly Lazy<IEnumerable<string>> LazyLocations;
private static readonly Lazy<IEnumerable<PortableExecutableReference>> LazyReferences;
Expand Down