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

Limit the amount of types to process #816

Merged
merged 1 commit into from
Oct 22, 2024
Merged
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
38 changes: 12 additions & 26 deletions src/KubeOps.Cli/Transpilation/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,56 +147,42 @@ public static Task<MetadataLoadContext> ForSolution(
return mlc;
});

public static IEnumerable<Type> GetEntities(this MetadataLoadContext context) => context.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
public static IEnumerable<Type> GetEntities(this MetadataLoadContext context) => context
.GetTypesToInspect()
.Select(t => (t, attrs: CustomAttributeData.GetCustomAttributes(t)))
.Where(e => e.attrs.Any(a => a.AttributeType.Name == nameof(KubernetesEntityAttribute)) &&
e.attrs.All(a => a.AttributeType.Name != nameof(IgnoreAttribute)))
.Select(e => e.t);

public static IEnumerable<CustomAttributeData> GetRbacAttributes(this MetadataLoadContext context)
{
foreach (var type in context.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.SelectMany(t =>
t.GetCustomAttributesData<GenericRbacAttribute>()))
{
yield return type;
}

foreach (var type in context.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.SelectMany(t =>
t.GetCustomAttributesData<EntityRbacAttribute>()))
{
yield return type;
}
}
public static IEnumerable<CustomAttributeData> GetRbacAttributes(this MetadataLoadContext context) => context
.GetTypesToInspect().SelectMany(t => t.GetCustomAttributesData<GenericRbacAttribute>().Concat(t.GetCustomAttributesData<EntityRbacAttribute>()));

public static IEnumerable<ValidationWebhook> GetValidatedEntities(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.GetTypesToInspect()
.Where(t => t.BaseType?.Name == typeof(ValidationWebhook<>).Name &&
t.BaseType?.Namespace == typeof(ValidationWebhook<>).Namespace)
.Distinct()
.Select(t => new ValidationWebhook(t, context.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata));

public static IEnumerable<MutationWebhook> GetMutatedEntities(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.GetTypesToInspect()
.Where(t => t.BaseType?.Name == typeof(MutationWebhook<>).Name &&
t.BaseType?.Namespace == typeof(MutationWebhook<>).Namespace)
.Distinct()
.Select(t => new MutationWebhook(t, context.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata));

public static IEnumerable<EntityMetadata> GetConvertedEntities(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.GetTypesToInspect()
.Where(t => t.BaseType?.Name == typeof(ConversionWebhook<>).Name &&
t.BaseType?.Namespace == typeof(ConversionWebhook<>).Namespace)
.Distinct()
.Select(t => context.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata);

private static IEnumerable<TypeInfo> GetTypesToInspect(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.Where(t => !t.IsInterface && !t.IsAbstract && !t.IsGenericType);

#if NET7_0_OR_GREATER
[GeneratedRegex(".*")]
private static partial Regex DefaultRegex();
Expand Down
Loading