Skip to content

Commit

Permalink
docs(changeset): fix attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
WoLfulus committed Apr 1, 2024
1 parent 3ec241b commit ae13c23
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/gentle-chefs-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"io.linefusion.unity.generator": patch
"io.linefusion.unity": patch
---

fix attributes
90 changes: 79 additions & 11 deletions src/Linefusion.Generator.Editor/src/Code/CodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,20 @@ public static bool IsTypeParameter(this MemberInfo member)

namespace Models
{
public class Attribute : IAttribute
public class CustomAttribute : IAttribute
{
private readonly CustomAttributeData data;

public Attribute(CustomAttributeData value)
public CustomAttribute(CustomAttributeData value)
{
this.data = value;
}

public string Name => data.AttributeType.Name;
public string FullName => data.AttributeType.FullName;

public CustomAttributeData Data => data;

public IClass? AttributeClass => Class.From(data.AttributeType.GetTypeInfo());
public IEnumerable<IAttributeArgument> Arguments =>
data
Expand All @@ -266,14 +268,59 @@ public static IEnumerable<IAttribute> From(IEnumerable<TypeInfo> types)
{
return types
.Where(type => type.IsAttribute())
.SelectMany(type => From(type.CustomAttributes))
.SelectMany(type => From(type.AsType().GetCustomAttributesData()))
.ToArray();
}

public static IEnumerable<IAttribute> From(IEnumerable<CustomAttributeData> attributes)
{
return attributes.Select(data => new Models.CustomAttribute(data)).ToArray();
}
}

public class Attribute : IAttribute
{
private readonly System.Attribute data;

public Attribute(System.Attribute value)
{
this.data = value;
}

public string Name => data.GetType().Name;
public string FullName => data.GetType().FullName;

public IClass? AttributeClass => Class.From(data.GetType().GetTypeInfo());
public IEnumerable<IAttributeArgument> Arguments => AttributeArgument.From(data);

public static IEnumerable<IAttribute> From(System.Type type)
{
return From(type.GetCustomAttributes(true)).ToArray();
}

public static IEnumerable<IAttribute> From(System.Reflection.TypeInfo type)
{
return From(type.AsType().GetCustomAttributes(true)).ToArray();
}

public static IEnumerable<IAttribute> From(IEnumerable<System.Type> types)
{
return From(types.Select(type => type.GetTypeInfo())).ToArray();
}

public static IEnumerable<IAttribute> From(IEnumerable<System.Attribute> attributes)
{
return attributes.Select(data => new Models.Attribute(data)).ToArray();
}

public static IEnumerable<IAttribute> From(IEnumerable<object> attributes)
{
return From(
attributes
.Where(obj => obj.GetType().IsAssignableTo<System.Attribute>())
.Cast<System.Attribute>()
);
}
}

public class AttributeArgument : IAttributeArgument
Expand Down Expand Up @@ -321,6 +368,19 @@ bool fromConstructor
fromConstructor
);
}

public static IEnumerable<IAttributeArgument> From(System.Attribute attr)
{
foreach (var prop in attr.GetType().GetProperties())
{
yield return new AttributeArgument(
prop.Name,
prop.GetValue(attr),
prop.PropertyType.GetTypeInfo(),
false
);
}
}
}

public class Class : NamedType, IClass
Expand Down Expand Up @@ -382,7 +442,11 @@ public Model(Assembly assembly)
}

public Assembly Assembly => assembly;
public IEnumerable<IAttribute> Attributes => Attribute.From(assembly.CustomAttributes);

public IEnumerable<IAttribute> Attributes =>
Attribute.From(assembly.GetCustomAttributes(true));
public IEnumerable<IAttribute> CustomAttributes =>
CustomAttribute.From(assembly.GetCustomAttributesData());

public IEnumerable<IClass> Classes => Class.From(assembly.DefinedTypes);
public IEnumerable<IDelegate> Delegates => Delegate.From(assembly.DefinedTypes);
Expand Down Expand Up @@ -546,7 +610,7 @@ public EnumValue(string name, object value, IEnumerable<IAttribute> attributes)
.Select(member => new EnumValue(
member!.Name,
member!.GetValue(null),
Attribute.From(member!.GetCustomAttributesData())
Attribute.From(member!.GetCustomAttributes(true))
))
.ToArray()!;
}
Expand Down Expand Up @@ -747,8 +811,6 @@ public class NamedType : Type, INamedType
{
private readonly TypeInfo value;



public NamedType(TypeInfo value)
: base(value)
{
Expand Down Expand Up @@ -871,6 +933,7 @@ public Struct(TypeInfo value)
public class Type : ISymbolBase, IType, ITypeReferencedByMember
{
private readonly TypeInfo value;

public Type(TypeInfo value)
{
this.value = value;
Expand Down Expand Up @@ -902,6 +965,7 @@ public Type(TypeInfo value)

public virtual string Name => value.Name;
public virtual string FullName => value.FullName;

/*
{
get
Expand All @@ -916,8 +980,10 @@ public Type(TypeInfo value)
}*/
public string Namespace => value.Namespace;


public IEnumerable<IAttribute> Attributes => Attribute.From(value.CustomAttributes);
public IEnumerable<IAttribute> Attributes =>
Attribute.From(value.AsType().GetCustomAttributes(true));
public IEnumerable<IAttribute> CustomAttributes =>
CustomAttribute.From(value.AsType().GetCustomAttributesData());
public INamedType? ContainingType => NamedType.From(value.DeclaringType.GetTypeInfo());
public IDocumentationCommentXml DocComment => new DocumentationCommentXml();

Expand Down Expand Up @@ -974,12 +1040,14 @@ public static IEnumerable<IType> From(IEnumerable<TypeInfo> type)
}
}


public class SymbolBase : ISymbolBase
{
private readonly MemberInfo member;

public IEnumerable<IAttribute> Attributes => Attribute.From(member.CustomAttributes);
public IEnumerable<IAttribute> Attributes =>
Attribute.From(member.GetCustomAttributes(true));
public IEnumerable<IAttribute> CustomAttributes =>
CustomAttribute.From(member.GetCustomAttributesData());
public INamedType? ContainingType => NamedType.From(member.DeclaringType.GetTypeInfo());
public IDocumentationCommentXml DocComment => new DocumentationCommentXml();

Expand Down
25 changes: 25 additions & 0 deletions src/Linefusion.Generator.Editor/src/Functions/PathFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Linefusion.Generator.IO;
using Scriban;
using Scriban.Runtime;
using UnityEditor;
using UnityEngine;

namespace Linefusion.Generators.Functions
{
public class PathFunctions
{
public static string combine(
TemplateContext context,
params string[] fragments
)
{
return Path.Combine(fragments).Replace('\\', '/');
}
}
}
1 change: 1 addition & 0 deletions src/Linefusion.Generator.Editor/src/TemplateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public static void Generate(Template? template, string path)
.Set<FileFunctions>("file")
.Set<DirFunctions>("directory")
.Set<CsharpFunctions>("csharp")
.Set<PathFunctions>("paths")
.Set(
"dotnet",
TemplateObject2
Expand Down
Binary file not shown.

0 comments on commit ae13c23

Please sign in to comment.