Esempio n. 1
0
        public static MethodDeclarationSyntax ConstructStartupConfigureMethod(IEnumerable <StatementSyntax> additonalStatements = null)
        {
            var devOnlyStatements = new List <StatementSyntax>()
            {
                SyntaxFactory.ParseStatement(AppUseDevExceptionPageCall)
            };

            var devEnvCondition = SyntaxFactory.ParseExpression(DevEnvConditionText);

            var statements = new List <StatementSyntax>()
            {
                // Standard blazor configuration statements
                SyntaxFactory.ParseStatement(AppUseStaticFilesText),
                SyntaxFactory.ParseStatement(AppUseRoutingText),
                SyntaxFactory.ParseStatement(AppUseEndpointsCall),
                // Dev env dependent statements
                SyntaxFactory.IfStatement(devEnvCondition, CodeSyntaxHelper.GetStatementsAsBlock(devOnlyStatements))
            };

            if (additonalStatements != null)
            {
                statements.AddRange(additonalStatements);
            }

            return(SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), StartupConfigureMethodName)
                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                   // Accept runtime injected application builder
                   .AddParameterListParameters(RuntimeInjectable.AppBuilderInjectable.AsParameter)
                   .WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements)));
        }
Esempio n. 2
0
        public static MethodDeclarationSyntax ConstructDisposeMethod(IEnumerable <StatementSyntax> statements)
        {
            var method = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)),
                BlazorComponentLifecycleEvent.Dispose.ToString());

            method = method.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

            method = method.WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements));

            // Should be public void Dispose()
            return(method);
        }
Esempio n. 3
0
        public static MethodDeclarationSyntax ConstructOnParametersSetMethod(IEnumerable <StatementSyntax> statements)
        {
            var method = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)),
                BlazorComponentLifecycleEvent.OnParametersSet.ToString());

            method = method.AddModifiers(
                SyntaxFactory.Token(SyntaxKind.ProtectedKeyword),
                SyntaxFactory.Token(SyntaxKind.OverrideKeyword));

            method = method.WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements));

            // Should be protected override void OnParametersSet()
            return(method);
        }
Esempio n. 4
0
        public static MethodDeclarationSyntax ConstructOnAfterRenderMethod(IEnumerable <StatementSyntax> statements)
        {
            var method = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)),
                BlazorComponentLifecycleEvent.OnAfterRender.ToString());

            method = method.AddModifiers(
                SyntaxFactory.Token(SyntaxKind.ProtectedKeyword),
                SyntaxFactory.Token(SyntaxKind.OverrideKeyword));

            method = method.AddParameterListParameters(SyntaxFactory
                                                       .Parameter(SyntaxFactory.Identifier(ComponentOnAfterRenderParamName))
                                                       .WithType(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword))));
            method = method.WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements));

            // Should be protected override void OnAfterRender(bool firstRender)
            return(method);
        }
Esempio n. 5
0
        public static MethodDeclarationSyntax ConstructStartupConfigureServicesMethod(IEnumerable <StatementSyntax> additonalStatements = null)
        {
            var statements = new List <StatementSyntax>()
            {
                // Standard blazor configuration statements
                SyntaxFactory.ParseStatement(ServiceAddRazorPagesText),
                SyntaxFactory.ParseStatement(ServiceAddServerSideBlazorText)
            };


            if (additonalStatements != null)
            {
                statements.AddRange(additonalStatements);
            }

            return(SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), StartupConfigureServicesMethodName)
                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                   // Accept runtime injected service collection
                   .AddParameterListParameters(RuntimeInjectable.ServiceCollectionInjectable.AsParameter)
                   .WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements)));
        }
Esempio n. 6
0
        public static ConstructorDeclarationSyntax ConstructStartupConstructor(IEnumerable <StatementSyntax> additonalStatements = null)
        {
            var statements = new List <StatementSyntax>()
            {
                // We guarantee that config and env will be assigned values from
                // constructor params
                RuntimeInjectable.ConfigInjectable.PropertyAssignment,
                RuntimeInjectable.EnvInjectable.PropertyAssignment
            };

            if (additonalStatements != null)
            {
                statements.AddRange(additonalStatements);
            }

            return(SyntaxFactory.ConstructorDeclaration(StartupClassName)
                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                   // Accept runtime injected config and env services
                   .AddParameterListParameters(RuntimeInjectable.ConfigInjectable.AsParameter, RuntimeInjectable.EnvInjectable.AsParameter)
                   .WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements)));
        }
Esempio n. 7
0
        public static ConstructorDeclarationSyntax ConstructMiddlewareConstructor(
            string middlewareClassName,
            IEnumerable <StatementSyntax> additionalStatements = null)
        {
            var statements = new List <StatementSyntax>()
            {
                // We guarantee that request delegate will be assigned to _next
                RuntimeInjectable.RequestDelegateInjectable.PrivateFieldAssignment
            };

            if (additionalStatements != null)
            {
                statements.AddRange(additionalStatements);
            }

            return(SyntaxFactory.ConstructorDeclaration(middlewareClassName)
                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                   // Accept runtime injected request delegate as next
                   .AddParameterListParameters(RuntimeInjectable.RequestDelegateInjectable.AsParameter)
                   .WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements)));
        }
Esempio n. 8
0
        public static MethodDeclarationSyntax ConstructSetParametersAsyncMethod(IEnumerable <StatementSyntax> statements)
        {
            var method = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.ParseTypeName(typeof(Task).Name),
                BlazorComponentLifecycleEvent.SetParametersAsync.ToString());

            method = method.AddModifiers(
                SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                SyntaxFactory.Token(SyntaxKind.OverrideKeyword),
                SyntaxFactory.Token(SyntaxKind.AsyncKeyword));

            method = method.AddParameterListParameters(RuntimeInjectable.ParameterViewInjectable.AsParameter);

            // Adding the call to ComponentBase.SetParametersAsync function, technically not
            // required but doesn't hurt to add and covers more cases
            var baseCall = SyntaxFactory.ParseStatement(string.Format(SetParametersBaseCallTemplate, RuntimeInjectable.ParameterViewInjectable.ParamName));

            // Adding an extra space so that the base call is separated from other method calls
            method = method.WithBody(CodeSyntaxHelper.GetStatementsAsBlock(statements.Append(CodeSyntaxHelper.GetBlankLine()).Append(baseCall)));

            // Should be public override async Task SetParametersAsync(ParameterView parameters)
            return(method);
        }
Esempio n. 9
0
        public static BlockSyntax ConstructMiddlewareMainContentBlock(
            string nextCallText,
            bool shouldContinue = true,
            IEnumerable <StatementSyntax> preHandleStatements  = null,
            IEnumerable <StatementSyntax> postHandleStatements = null)
        {
            IEnumerable <StatementSyntax> statements = preHandleStatements ?? new List <StatementSyntax>();

            // In most cases, shouldContinue will be true, but if the middleware was generated from
            // an http handler, then this will not be the case
            if (shouldContinue)
            {
                // Ensure that call to next occurs between pre-handle statements and post-handle
                // statements if any exist (usually exactly one will)
                statements = statements.Append(SyntaxFactory.ParseStatement(nextCallText));
            }

            if (postHandleStatements != null)
            {
                statements = statements.Concat(postHandleStatements);
            }

            return(CodeSyntaxHelper.GetStatementsAsBlock(statements));
        }