Skip to content

Commit

Permalink
added ability to disable generation of the DebuggerNonUserCodeAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNav73 committed Jun 7, 2024
1 parent 474b9ef commit e9e66b3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 49 deletions.
8 changes: 4 additions & 4 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,28 @@ internal partial class Build : NukeBuild
DotNetPack(s => s
.SetProject(Solution.CoreCraft)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.15"))
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.16"))
.SetDescription("A core library to build cross-platform and highly customizable domain models")
.AddPackageTags("Model", "Domain"));
DotNetPack(s => s
.SetProject(Solution.CoreCraft_Generators)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.15"))
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.16"))
.SetDescription("Roslyn Source Generators for generating domain models using 'CoreCraft' library")
.AddPackageTags("Model", "Domain", "SourceGenerator", "Generator"));
DotNetPack(s => s
.SetProject(Solution.CoreCraft_Storage_Sqlite)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.15"))
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.16"))
.SetDescription("SQLite storage implementation for 'CoreCraft' library")
.AddPackageTags("Model", "Domain", "SQLite"));
DotNetPack(s => s
.SetProject(Solution.CoreCraft_Storage_Json)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.3.0", "0.3.15"))
.SetVersion(MakePreviewIfNeeded("0.3.0", "0.3.16"))
.SetDescription("Json storage implementation for 'CoreCraft' library")
.AddPackageTags("Model", "Domain", "Json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public static void EmptyLine(this IndentedTextWriter code)
code.WriteLineNoTabs(string.Empty);
}

public static void GeneratedClassAttributes(this IndentedTextWriter code)
public static void GeneratedClassAttributes(this IndentedTextWriter code, bool debug)
{
code.GeneratedCodeAttribute();
code.CompilerGeneratedCodeAttribute();
code.DebuggerNonUserCodeAttribute();
code.DebuggerNonUserCodeAttribute(debug);
code.ExcludeFromCodeCoverageAttribute();
}

Expand All @@ -119,9 +119,12 @@ public static void CompilerGeneratedCodeAttribute(this IndentedTextWriter code)
code.WriteLine("[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]");
}

public static void DebuggerNonUserCodeAttribute(this IndentedTextWriter code)
public static void DebuggerNonUserCodeAttribute(this IndentedTextWriter code, bool debug)
{
code.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
if (!debug)
{
code.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
}
}

public static void ExcludeFromCodeCoverageAttribute(this IndentedTextWriter code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void DefineEntities(ModelShard modelShard)

private void DefineEntityType(Entity entity)
{
code.GeneratedClassAttributes();
code.GeneratedClassAttributes(entity.Collection.Shard.Scheme.Debug);
code.WriteLine($"public sealed record {entity.Name}(global::System.Guid Id) : Entity(Id)");
code.Block(() =>
{
Expand All @@ -42,7 +42,7 @@ private void DefineEntityType(Entity entity)

private void DefineEntityPropertiesClass(Entity entity)
{
code.GeneratedClassAttributes();
code.GeneratedClassAttributes(entity.Collection.Shard.Scheme.Debug);
code.WriteLine($"public sealed partial record {entity.PropertiesType} : Properties");
code.Block(() =>
{
Expand Down
12 changes: 6 additions & 6 deletions src/CoreCraft.SourceGeneration/Generators/ModelShardGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void DefineModelShardInfoClass(ModelShard modelShard)
{
var visibility = GetInternalTypeVisibility(modelShard);

code.GeneratedClassAttributes();
code.GeneratedClassAttributes(modelShard.Scheme.Debug);
code.Class(visibility, "static", $"{modelShard.Name}ModelShardInfo",
() =>
{
Expand All @@ -67,16 +67,16 @@ private void DefineModelShardInfoClass(ModelShard modelShard)
}
code.EmptyLine();
foreach (var relation in modelShard.Relations)
foreach (var name in modelShard.Relations.Select(x => x.Name))
{
code.WriteLine($"public static readonly RelationInfo {relation.Name}Info = new(\"{modelShard.Name}\", \"{relation.Name}\");");
code.WriteLine($"public static readonly RelationInfo {name}Info = new(\"{modelShard.Name}\", \"{name}\");");
}
});
}

private void DefineModelShardClass(ModelShard modelShard)
{
code.GeneratedClassAttributes();
code.GeneratedClassAttributes(modelShard.Scheme.Debug);
code.Class(modelShard.Visibility, "sealed partial", $"{modelShard.Name}ModelShard", [$"I{modelShard.Name}ModelShard"], () =>
{
DefineCtor(modelShard);
Expand Down Expand Up @@ -262,7 +262,7 @@ private void DefineChangesFrameClass(ModelShard modelShard)
{
var visibility = GetInternalTypeVisibility(modelShard);

code.GeneratedClassAttributes();
code.GeneratedClassAttributes(modelShard.Scheme.Debug);
code.Class(visibility, "sealed", $"{modelShard.Name}ChangesFrame",
[
$"I{modelShard.Name}ChangesFrame", "IChangesFrameEx"
Expand Down Expand Up @@ -458,7 +458,7 @@ private void DefineMutableModelShardClass(ModelShard modelShard)
{
var visibility = GetInternalTypeVisibility(modelShard);

code.GeneratedClassAttributes();
code.GeneratedClassAttributes(modelShard.Scheme.Debug);
code.Class(visibility, "sealed", $"Mutable{modelShard.Name}ModelShard",
[
$"IMutable{modelShard.Name}ModelShard",
Expand Down
21 changes: 10 additions & 11 deletions src/CoreCraft.SourceGeneration/ModelScheme.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace CoreCraft.SourceGeneration;

internal sealed record ModelScheme(IEnumerable<ModelShard> Shards);
internal sealed record ModelScheme(bool Debug)
{
public IEnumerable<ModelShard> Shards { get; set; }
}

internal sealed record ModelShard
{
Expand All @@ -10,12 +13,14 @@ internal sealed record ModelShard

public Visibility Visibility { get; init; }

public ModelScheme Scheme { get; init; }

public IEnumerable<Collection> Collections { get; init; }

public IEnumerable<Relation> Relations { get; init; }
}

internal sealed record Collection(string Name, Entity Entity, bool LoadManually = false)
internal sealed record Collection(string Name, Entity Entity, ModelShard Shard, bool LoadManually = false)
{
public string Type => $"Collection<{Entity.Name}, {Entity.PropertiesType}>";

Expand All @@ -24,16 +29,8 @@ internal sealed record Collection(string Name, Entity Entity, bool LoadManually
public string ChangesType => $"CollectionChangeSet<{Entity.Name}, {Entity.PropertiesType}>";
}

internal sealed record Relation
internal sealed record Relation(string Name, Collection Parent, Collection Child, RelationType RelationType, ModelShard Shard)
{
public string Name { get; init; }

public Collection Parent { get; init; }

public Collection Child { get; init; }

public RelationType RelationType { get; init; }

public string Type => $"Relation<{Parent.Entity.Name}, {Child.Entity.Name}>";

public string MutableType => $"Mutable{Type}";
Expand Down Expand Up @@ -71,6 +68,8 @@ internal enum RelationType

internal sealed record Entity(string Name, IEnumerable<Property> Properties)
{
public Collection Collection { get; set; }

public string PropertiesType => $"{Name}Properties";
}

Expand Down
45 changes: 24 additions & 21 deletions src/CoreCraft.SourceGeneration/Serialization/DtoConverter.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
namespace CoreCraft.SourceGeneration.Serialization;

internal class DtoConverter
internal static class DtoConverter
{
public static ModelScheme Convert(ModelSchemeDto modelScheme)
{
return new ModelScheme(modelScheme.Shards.Select(Convert).ToArray());
var scheme = new ModelScheme(modelScheme.Debug);

scheme.Shards = modelScheme.Shards.Select(x => Convert(x, scheme)).ToArray();

return scheme;
}

private static ModelShard Convert(ModelShardDto modelShard)
private static ModelShard Convert(ModelShardDto modelShard, ModelScheme scheme)
{
var collections = new List<Collection>();
var relations = new List<Relation>();

var result = new ModelShard()
{
Name = modelShard.Name,
Visibility = Convert(modelShard.Visibility),
Collections = collections,
Relations = relations,
LoadManually = modelShard.LoadManually,
Scheme = scheme
};

var entities = modelShard.Entities.Select(Convert).ToArray();

foreach (var collection in modelShard.Collections)
foreach (var collectionDto in modelShard.Collections)
{
var entity = entities.Single(x => x.Name == collection.EntityType);
var entity = entities.Single(x => x.Name == collectionDto.EntityType);

entity.Collection = new Collection(collectionDto.Name, entity, result, collectionDto.LoadManually);

collections.Add(new Collection(collection.Name, entity, collection.LoadManually));
collections.Add(entity.Collection);
}

foreach (var relation in modelShard.Relations)
Expand All @@ -29,23 +45,10 @@ private static ModelShard Convert(ModelShardDto modelShard)
var childEntity = entities.Single(x => x.Name == relation.Child);
var childCollection = collections.Single(x => x.Entity == childEntity);

relations.Add(new Relation()
{
Name = relation.Name,
RelationType = Convert(relation.RelationType),
Parent = parentCollection,
Child = childCollection
});
relations.Add(new Relation(relation.Name, parentCollection, childCollection, Convert(relation.RelationType), result));
}

return new ModelShard()
{
Name = modelShard.Name,
Visibility = Convert(modelShard.Visibility),
Collections = collections,
Relations = relations,
LoadManually = modelShard.LoadManually
};
return result;
}

private static Entity Convert(EntityDto entity)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CoreCraft.SourceGeneration.Serialization;

internal sealed record ModelSchemeDto(IEnumerable<ModelShardDto> Shards);
internal sealed record ModelSchemeDto(bool Debug, IEnumerable<ModelShardDto> Shards);

internal sealed record ModelShardDto
{
Expand Down

0 comments on commit e9e66b3

Please sign in to comment.