Example #1
0
        private static ParameterSyntax CreateParameter(WebIdlArgumentDefinition arg)
        {
            var item = SyntaxFactory.Identifier(NameService.GetValidIndentifier(arg.EscapedName));

            return(SyntaxFactory.Parameter(item)
                   .WithType(CreateType(arg.IdlType)));
        }
Example #2
0
        private static MemberDeclarationSyntax CreateInterfaceOperation(string proxyClass, WebIdlMemberDefinition memberDefinition)
        {
            var name = memberDefinition.Body.Name.Escaped;

            try
            {
                var returnTypeReference = memberDefinition.Body.IdlType;
                var isAsyncCall         = NameService.IsAsync(returnTypeReference);
                if (isAsyncCall)
                {
                    name += "Async";
                }

                var returnType = CreateType(returnTypeReference);
                var arguments  = memberDefinition.Body.Arguments.Select(CreateParameter);
                var body       = isAsyncCall
                    ? GenerateAsyncProxyCall(proxyClass, memberDefinition)
                    : GenerateSyncProxyCall(proxyClass, memberDefinition);
                var methodModifier = isAsyncCall
                    ? SyntaxFactory.TokenList(
                    SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                    SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
                    : SyntaxFactory.TokenList(
                    SyntaxFactory.Token(SyntaxKind.PublicKeyword));
                return(SyntaxFactory.MethodDeclaration(returnType, NameService.GetValidIndentifier(name))
                       .WithModifiers(methodModifier)
                       .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(arguments)))
                       .WithBody(body));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error during generation operation {name}", ex);
            }
        }
Example #3
0
        private static TypeSyntax CreateType(WebIdlTypeReference returnTypeReference)
        {
            var typeName   = NameService.GetTypeName(returnTypeReference);
            var returnType = SyntaxFactory.ParseTypeName(typeName);

            return(returnType);
        }
Example #4
0
        private static ClassDeclarationSyntax GenerateInterfaceCode(WebIdlTypeDefinition token)
        {
            var name = token.Name;

            try
            {
                var classDeclaration = SyntaxFactory.ClassDeclaration(name)
                                       .WithModifiers(SyntaxFactory.TokenList(
                                                          SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                                                          SyntaxFactory.Token(SyntaxKind.PartialKeyword)));

                var arguments = new[]
                {
                    Parameter(Identifier(NameService.GetValidIndentifier("asyncJsRunTime")))
                    .WithType(ParseTypeName("IJSRuntime")),
                };
                var constructorMember = ConstructorDeclaration(NameService.GetValidIndentifier(name))
                                        .WithModifiers(TokenList(
                                                           Token(SyntaxKind.PublicKeyword)))
                                        .WithParameterList(ParameterList(SeparatedList(arguments)))
                                        .WithBody(Block(
                                                      ParseStatement("this.asyncJsRunTime = asyncJsRunTime;")));
                classDeclaration = classDeclaration.AddMembers(constructorMember);
                var asyncJsRunTimeField = SyntaxFactory.FieldDeclaration(
                    VariableDeclaration(
                        ParseTypeName("IJSRuntime"))
                    .WithVariables(
                        SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                            SyntaxFactory.VariableDeclarator(
                                SyntaxFactory.Identifier(NameService.GetValidIndentifier("asyncJsRunTime"))))))
                                          .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)));
                classDeclaration = classDeclaration.AddMembers(asyncJsRunTimeField);

                var proxyClass = $"Blazor{token.Name}Proxy";
                foreach (var enumMemberDefinition in token.Members)
                {
                    var constField = CreateInterfaceMember(proxyClass, enumMemberDefinition);

                    // Add the field, the property and method to the class.
                    classDeclaration = classDeclaration.AddMembers(constField);
                }

                return(classDeclaration);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error during generation interface {name}", ex);
            }
        }
Example #5
0
        private static BlockSyntax GenerateAsyncProxyCall(string proxyClass, WebIdlMemberDefinition memberDefinition)
        {
            /*
             * public static async Task<string> MethodNameAsync(string arg1, string arg2)
             * {
             *  var asyncJsRunTime = JSRuntime.Current;
             *  return await asyncJsRunTime.InvokeAsync<string>("proxyClass.methodName", arg1, arg2);
             * }
             */
            var methodName            = memberDefinition.Body.Name.Escaped;
            var asyncInvocation       = SyntaxFactory.ParseExpression($@"await asyncJsRunTime.InvokeAsync<string>(""{proxyClass}.{methodName}"", message)");
            var syntax                = SyntaxFactory.ParseStatement("var asyncJsRunTime = this.asyncJsRunTime;");
            var proxyFunctionArgument = SyntaxFactory.Argument(
                SyntaxFactory.LiteralExpression(
                    SyntaxKind.StringLiteralExpression,
                    SyntaxFactory.Literal($"{proxyClass}.{methodName}")));
            var arguments = memberDefinition.Body.Arguments.Select(CreateArgumentCallExpression);
            var proxyCallArguments = new[] { proxyFunctionArgument }.Union(arguments);

            var typeName         = memberDefinition.Body.IdlType.TypeName ?? memberDefinition.Body.IdlType.IdlType[0].TypeName;
            var isResultTypeVoid = typeName == "void";
            var resultType       = isResultTypeVoid
                ? SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword))
                : SyntaxFactory.ParseTypeName(NameService.GetTypeName(memberDefinition.Body.IdlType.IdlType[0]));
            var awaitExpress = SyntaxFactory.AwaitExpression(
                SyntaxFactory.InvocationExpression(
                    SyntaxFactory.MemberAccessExpression(
                        kind: SyntaxKind.SimpleMemberAccessExpression,
                        expression: SyntaxFactory.IdentifierName("asyncJsRunTime"),
                        name: SyntaxFactory.GenericName(
                            identifier: SyntaxFactory.Identifier("InvokeAsync"),
                            typeArgumentList: SyntaxFactory.TypeArgumentList(
                                SyntaxFactory.SingletonSeparatedList <TypeSyntax>(resultType)))),
                    argumentList: SyntaxFactory.ArgumentList(
                        SyntaxFactory.SeparatedList <ArgumentSyntax>(
                            proxyCallArguments))));
            var lastStatement = isResultTypeVoid
                ? (StatementSyntax)SyntaxFactory.ExpressionStatement(awaitExpress)
                : SyntaxFactory.ReturnStatement(awaitExpress);

            return(SyntaxFactory.Block(
                       syntax,
                       lastStatement));
        }
Example #6
0
        private static DelegateDeclarationSyntax GenerateDelegate(WebIdlTypeDefinition memberDefinition)
        {
            var name = memberDefinition.Name;

            try
            {
                var returnTypeReference = memberDefinition.IdlType;
                var returnType          = CreateType(returnTypeReference);
                var arguments           = memberDefinition.Arguments.Select(CreateParameter);
                var methodModifier      = SyntaxFactory.TokenList(
                    SyntaxFactory.Token(SyntaxKind.PublicKeyword));
                return(SyntaxFactory.DelegateDeclaration(returnType, NameService.GetValidIndentifier(name))
                       .WithModifiers(methodModifier)
                       .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(arguments))));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error during generation operation {name}", ex);
            }
        }
Example #7
0
        private static MemberDeclarationSyntax CreateInterfaceField(WebIdlMemberDefinition memberDefinition)
        {
            var name = memberDefinition.Name;

            try
            {
                var constField = SyntaxFactory.FieldDeclaration(
                    SyntaxFactory.VariableDeclaration(
                        SyntaxFactory.ParseTypeName(NameService.GetTypeName(memberDefinition.IdlType)))
                    .WithVariables(
                        SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                            SyntaxFactory.VariableDeclarator(
                                SyntaxFactory.Identifier(NameService.GetValidIndentifier(memberDefinition.Name))))))
                                 .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)));
                return(constField);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error during generation field {name}", ex);
            }
        }
Example #8
0
        private static ClassDeclarationSyntax GenerateStringEnum(WebIdlTypeDefinition token)
        {
            //  Create a class: (class Order)
            var classDeclaration = SyntaxFactory.ClassDeclaration(token.Name)
                                   .WithModifiers(SyntaxFactory.TokenList(
                                                      SyntaxFactory.Token(SyntaxKind.PublicKeyword)));

            var enumType = SyntaxFactory.ParseTypeName(token.Name);
            //// Inherit BaseEntity<T> and implement IHaveIdentity: (public class Order : BaseEntity<T>, IHaveIdentity)
            //classDeclaration = classDeclaration.AddBaseListTypes(
            //    SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName("BaseEntity<Order>")),
            //    SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName("IHaveIdentity")));

            var backingField = SyntaxFactory.FieldDeclaration(
                SyntaxFactory.VariableDeclaration(
                    SyntaxFactory.PredefinedType(
                        SyntaxFactory.Token(SyntaxKind.StringKeyword)))
                .WithVariables(
                    SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                        SyntaxFactory.VariableDeclarator(
                            SyntaxFactory.Identifier("field")))))
                               .WithModifiers(SyntaxFactory.TokenList(
                                                  SyntaxFactory.Token(SyntaxKind.PrivateKeyword),
                                                  SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)));

            classDeclaration = classDeclaration.AddMembers(backingField);

            var constructorMember = SyntaxFactory.ConstructorDeclaration(token.Name)
                                    .WithParameterList(SyntaxFactory.ParameterList(
                                                           SyntaxFactory.SingletonSeparatedList <ParameterSyntax>(
                                                               SyntaxFactory.Parameter(
                                                                   SyntaxFactory.Identifier("value"))
                                                               .WithType(SyntaxFactory.PredefinedType(
                                                                             SyntaxFactory.Token(SyntaxKind.StringKeyword))))))
                                    .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxFactory.AssignmentExpression(
                        SyntaxKind.SimpleAssignmentExpression,
                        SyntaxFactory.IdentifierName("field"),
                        SyntaxFactory.IdentifierName("value"))))
                                    .WithModifiers(SyntaxFactory.TokenList(
                                                       SyntaxFactory.Token(SyntaxKind.PrivateKeyword)))
                                    .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));

            classDeclaration = classDeclaration.AddMembers(constructorMember);

            foreach (var enumMemberDefinition in token.Values)
            {
                var backingValue = SyntaxFactory.LiteralExpression(
                    SyntaxKind.StringLiteralExpression,
                    SyntaxFactory.Literal(enumMemberDefinition.Value));
                var initializationValue = SyntaxFactory.ObjectCreationExpression(enumType)
                                          .WithArgumentList(SyntaxFactory.ArgumentList(
                                                                SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(
                                                                    SyntaxFactory.Argument(backingValue))));
                var constField = SyntaxFactory.FieldDeclaration(
                    SyntaxFactory.VariableDeclaration(
                        enumType)
                    .WithVariables(
                        SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                            SyntaxFactory.VariableDeclarator(
                                SyntaxFactory.Identifier(NameService.ConvertFromWebIdlIdentifier(enumMemberDefinition.Value)))
                            .WithInitializer(
                                SyntaxFactory.EqualsValueClause(
                                    initializationValue)))))
                                 .WithModifiers(SyntaxFactory.TokenList(
                                                    SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                                                    SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword),
                                                    SyntaxFactory.Token(SyntaxKind.StaticKeyword)));

                // Add the field, the property and method to the class.
                classDeclaration = classDeclaration.AddMembers(constField);
            }

            return(classDeclaration);
        }
Example #9
0
 private static ArgumentSyntax CreateArgumentCallExpression(WebIdlArgumentDefinition argumentDefinition)
 {
     return(SyntaxFactory.Argument(
                SyntaxFactory.IdentifierName(NameService.GetValidIndentifier(argumentDefinition.Name))));
 }