Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add automatic addition of registerattribute to nsobject subtypes
- Loading branch information
Showing
4 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...onents/tbc.host/Components/IncrementalCompiler/iOSDynamicRegistrationAttributeRewriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Roslyn.Reflection; | ||
|
||
namespace Tbc.Host.Components.IncrementalCompiler; | ||
|
||
// adds a unique [Register] attribute to every nsobject-derived type | ||
public class iOSDynamicRegistrationAttributeRewriter : CSharpSyntaxRewriter | ||
{ | ||
private readonly MetadataLoadContext _metadataLoadContext; | ||
private readonly SemanticModel _semanticModel; | ||
private readonly Type _nsObject; | ||
|
||
public iOSDynamicRegistrationAttributeRewriter(MetadataLoadContext metadataLoadContext, SemanticModel semanticModel) | ||
{ | ||
_metadataLoadContext = metadataLoadContext; | ||
_semanticModel = semanticModel; | ||
|
||
_nsObject = _metadataLoadContext.ResolveType("Foundation.NSObject"); | ||
} | ||
|
||
public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node) | ||
{ | ||
var symbol = _semanticModel.GetDeclaredSymbolForNode(node); | ||
if (symbol is null) return base.VisitClassDeclaration(node); | ||
|
||
var symbolReference = $"{symbol.ContainingNamespace}.{symbol.MetadataName}"; | ||
var type = _metadataLoadContext.ResolveType(symbolReference); | ||
if (type is null) | ||
return base.VisitClassDeclaration(node); | ||
|
||
if (type.IsSubclassOf(_nsObject)) | ||
{ | ||
var attributeArgument = | ||
SyntaxFactory.AttributeList | ||
(SyntaxFactory.SingletonSeparatedList( | ||
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("Register"), | ||
SyntaxFactory.AttributeArgumentList( | ||
SyntaxFactory.SeparatedList(new[] { | ||
SyntaxFactory.AttributeArgument( | ||
SyntaxFactory.LiteralExpression( | ||
SyntaxKind.StringLiteralExpression, | ||
SyntaxFactory.Literal($"{symbol.MetadataName}_{Guid.NewGuid()}")) | ||
) | ||
}))))); | ||
|
||
var updatedNode = node.WithAttributeLists(node.AttributeLists.Add(attributeArgument)); | ||
|
||
return updatedNode; | ||
} | ||
|
||
return base.VisitClassDeclaration(node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/components/tbc.host/Config/iOSDynamicRegistrationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Tbc.Host.Config; | ||
|
||
public class iOSDynamicRegistrationOptions | ||
{ | ||
public bool Enabled { get; set; } | ||
} |