Skip to content

Commit

Permalink
new environment check for expected pre-built-types
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Sep 11, 2023
1 parent 192b4c9 commit 8f9b866
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/GeneratorTarget/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using JasperFx.CodeGeneration;
using JasperFx.CodeGeneration.Commands;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Oakton;
Expand All @@ -17,6 +18,8 @@ static Task<int> Main(string[] args)
{
services.AddSingleton<ICodeFileCollection>(new GreeterGenerator());
services.AddSingleton<ICodeFileCollection>(new GreeterGenerator2());
services.AssertAllExpectedPreBuiltTypesExistOnStartUp();
})
.RunOaktonCommands(args);

Expand Down
23 changes: 23 additions & 0 deletions src/JasperFx.CodeGeneration.Commands/AllPreGeneratedTypesExist.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Oakton.Environment;

namespace JasperFx.CodeGeneration.Commands;

public class AllPreGeneratedTypesExist : IEnvironmentCheck
{
public async Task Assert(IServiceProvider services, CancellationToken cancellation)
{
var collections = services.GetServices<ICodeFileCollection>().ToArray();
foreach (var collection in collections)
{
collection.AssertPreBuildTypesExist(services);
}
}

public string Description { get; } =
"Asserting that all expected pre-built generated types exist in the configured assembly";
}
2 changes: 1 addition & 1 deletion src/JasperFx.CodeGeneration.Commands/CodeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public enum CodeAction
preview,
write,
test,
delete
delete,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public override bool Execute(GenerateCodeInput input)

case CodeAction.delete:
builder.DeleteAllGeneratedCode();

break;

case CodeAction.write:
Expand Down
11 changes: 11 additions & 0 deletions src/JasperFx.CodeGeneration.Commands/VerificationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using JasperFx.RuntimeCompiler;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Oakton.Environment;

namespace JasperFx.CodeGeneration.Commands;

Expand Down Expand Up @@ -59,4 +60,14 @@ public static void AssertAllGeneratedCodeCanCompile(this IHost host)
throw new AggregateException($"Compilation failures for:\n{failures.Join("\n")}", exceptions);
}
}

/// <summary>
/// Add an environment check that all expected pre-built generated
/// types exist in the configured assembly
/// </summary>
/// <param name="services"></param>
public static void AssertAllExpectedPreBuiltTypesExistOnStartUp(this IServiceCollection services)
{
services.AddSingleton<IEnvironmentCheck, AllPreGeneratedTypesExist>();
}
}
36 changes: 36 additions & 0 deletions src/JasperFx.CodeGeneration/CodeGenerationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -8,6 +9,13 @@

namespace JasperFx.CodeGeneration;

public class MissingTypeException : Exception
{
public MissingTypeException(string? message) : base(message)
{
}
}

public static class CodeGenerationExtensions
{
/// <summary>
Expand Down Expand Up @@ -95,4 +103,32 @@ public static void ReturnNewStringConstant(this FramesCollection frames,
var setter = frames.ParentMethod.ParentType.AddStringConstant(constantName, value);
frames.Return(setter);
}

/// <summary>
/// Assert that all the expected pre-generated types exist in the configured assembly
/// </summary>
/// <param name="collection"></param>
/// <param name="services"></param>
/// <exception cref="MissingTypeException"></exception>
public static void AssertPreBuildTypesExist(this ICodeFileCollection collection, IServiceProvider services)
{
var missing = new List<ICodeFile>();

foreach (var file in collection.BuildFiles())
{
var @namespace = $"{collection.Rules.GeneratedNamespace}.{collection.ChildNamespace}";
if (!file.AttachTypesSynchronously(collection.Rules, collection.Rules.ApplicationAssembly, services,
@namespace))
{
missing.Add(file);
}
}

if (missing.Any())
{
throw new MissingTypeException(
$"Missing expected pre-generated type(s) from assembly {collection.Rules.ApplicationAssembly.FullName}:\n" +
missing.Select(x => x.ToString()).Join("\n"));

Check warning on line 131 in src/JasperFx.CodeGeneration/CodeGenerationExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Argument of type 'IEnumerable<string?>' cannot be used for parameter 'values' of type 'IEnumerable<string>' in 'string StringExtensions.Join(IEnumerable<string> values, string separator)' due to differences in the nullability of reference types.

Check warning on line 131 in src/JasperFx.CodeGeneration/CodeGenerationExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Argument of type 'IEnumerable<string?>' cannot be used for parameter 'values' of type 'IEnumerable<string>' in 'string StringExtensions.Join(IEnumerable<string> values, string separator)' due to differences in the nullability of reference types.
}
}
}

0 comments on commit 8f9b866

Please sign in to comment.