示例#1
0
        private void HandleLifecycleMethod(MethodDeclarationSyntax methodDeclaration, WebFormsAppLifecycleEvent lifecycleEvent)
        {
            var statements       = methodDeclaration.Body.Statements;
            var lambdaExpression = LifecycleManagerService.ContentIsPreHandle(lifecycleEvent) ?
                                   MiddlewareSyntaxHelper.ConstructMiddlewareLambda(preHandleStatements: statements) :
                                   MiddlewareSyntaxHelper.ConstructMiddlewareLambda(postHandleStatements: statements);

            _lifecycleManager.RegisterMiddlewareLambda(lifecycleEvent, lambdaExpression);
        }
示例#2
0
        public void CheckMethodApplicationLifecycleHook_Returns_Null_For_Incorrect_Params()
        {
            var methodDeclaration = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)),
                SyntaxFactory.Identifier(BeginRequestMethodName));

            var result = LifecycleManagerService.CheckMethodApplicationLifecycleHook(methodDeclaration);

            Assert.IsNull(result);
        }
示例#3
0
        public void IsProcessRequestMethod_Returns_False_For_Incorrect_Method_Name()
        {
            var methodDeclaration = SyntaxFactory
                                    .MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)),
                SyntaxFactory.Identifier(IncorrectMethodName))
                                    .AddParameterListParameters(
                SyntaxFactory.Parameter(SyntaxFactory.Identifier(ProcessRequestParamName)).WithType(SyntaxFactory.ParseTypeName(ProcessRequestParamType)));

            var result = LifecycleManagerService.IsProcessRequestMethod(methodDeclaration);

            Assert.False(result);
        }
示例#4
0
        public ClassConverterFactory(string sourceProjectPath,
                                     LifecycleManagerService lcManager,
                                     TaskManagerService taskManager,
                                     WebFormMetricContext metricsContext)
        {
            _sourceProjectPath = sourceProjectPath;
            _lifecycleManager  = lcManager;
            _taskManager       = taskManager;
            _metricsContext    = metricsContext;

            // TODO: Receive services required for ClassConverters
            // via constructor parameters
        }
示例#5
0
        public void CheckMethodPageLifecycleHook_Returns_Null_For_Incorrect_Name()
        {
            var methodDeclaration = SyntaxFactory
                                    .MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)),
                SyntaxFactory.Identifier(IncorrectMethodName))
                                    .AddParameterListParameters(
                SyntaxFactory.Parameter(SyntaxFactory.Identifier(SenderParamName)).WithType(SyntaxFactory.ParseTypeName(SenderParamType)),
                SyntaxFactory.Parameter(SyntaxFactory.Identifier(EventArgsParamName)).WithType(SyntaxFactory.ParseTypeName(EventArgsParamType)));

            var result = LifecycleManagerService.CheckMethodPageLifecycleHook(methodDeclaration);

            Assert.IsNull(result);
        }
示例#6
0
        public void CheckMethodApplicationLifecycleHook_Returns_Correct_Lifecycle_Hook()
        {
            var methodDeclaration = SyntaxFactory
                                    .MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)),
                SyntaxFactory.Identifier(BeginRequestMethodName))
                                    .AddParameterListParameters(
                SyntaxFactory.Parameter(SyntaxFactory.Identifier(SenderParamName)).WithType(SyntaxFactory.ParseTypeName(SenderParamType)),
                SyntaxFactory.Parameter(SyntaxFactory.Identifier(EventArgsParamName)).WithType(SyntaxFactory.ParseTypeName(EventArgsParamType)));

            var result = LifecycleManagerService.CheckMethodApplicationLifecycleHook(methodDeclaration);

            Assert.AreEqual(WebFormsAppLifecycleEvent.BeginRequest, result);
        }
示例#7
0
 public HttpHandlerClassConverter(
     string relativePath,
     string sourceProjectPath,
     SemanticModel sourceFileSemanticModel,
     TypeDeclarationSyntax originalDeclarationSyntax,
     INamedTypeSymbol originalClassSymbol,
     LifecycleManagerService lifecycleManager,
     TaskManagerService taskManager,
     WebFormMetricContext metricsContext)
     : base(relativePath, sourceProjectPath, sourceFileSemanticModel, originalDeclarationSyntax, originalClassSymbol, taskManager)
 {
     _lifecycleManager = lifecycleManager;
     _lifecycleManager.NotifyExpectedMiddlewareSource();
     _metricsContext = metricsContext;
 }
示例#8
0
        public GlobalClassConverter(
            string relativePath,
            string sourceProjectPath,
            SemanticModel sourceFileSemanticModel,
            TypeDeclarationSyntax originalDeclarationSyntax,
            INamedTypeSymbol originalClassSymbol,
            LifecycleManagerService lifecycleManager,
            TaskManagerService taskManager,
            WebFormMetricContext metricsContext)
            : base(relativePath, sourceProjectPath, sourceFileSemanticModel, originalDeclarationSyntax, originalClassSymbol, taskManager)
        {
            _lifecycleManager = lifecycleManager;
            _lifecycleManager.NotifyExpectedMiddlewareSource();

            _configureMethodStatements = new List <StatementSyntax>();
            _keepableMethods           = new List <MethodDeclarationSyntax>();
            _endOfClassComments        = new List <string>();
            _metricsContext            = metricsContext;
        }
示例#9
0
        private void InitializeServices()
        {
            _taskManager            = new TaskManagerService();
            _lifecycleManager       = new LifecycleManagerService();
            _viewImportService      = new ViewImportService();
            _programCsService       = new ProgramCsService();
            _appRazorService        = new AppRazorService();
            _hostPageService        = new HostPageService();
            _blazorWorkspaceManager = new WorkspaceManagerService();

            // By convention, we expect the root namespace to share a name with the project
            // root folder, we normalize the folder name before using it in case the folder name
            // is not a valid identifier
            var rootNamespace = Utilities.NormalizeNamespaceIdentifier(_analyzerResult.ProjectResult.ProjectName);

            _programCsService.ProgramCsNamespace = rootNamespace;
            _hostPageService.HostNamespace       = rootNamespace;
            _blazorWorkspaceManager.CreateSolutionFile();
        }
示例#10
0
        private void ProcessMethods(IEnumerable <MethodDeclarationSyntax> orignalMethods)
        {
            foreach (var method in orignalMethods)
            {
                try
                {
                    var lifecycleEvent = LifecycleManagerService.CheckMethodApplicationLifecycleHook(method);

                    if (lifecycleEvent != null)
                    {
                        HandleLifecycleMethod(method, (WebFormsAppLifecycleEvent)lifecycleEvent);
                    }
                    else if (method.IsEventHandler(ApplicationStartMethodName))
                    {
                        var newStatements = method.Body.Statements
                                            // Make a note of where these lines came from
                                            .AddComment(string.Format(Constants.CodeOriginCommentTemplate, ApplicationStartMethodName))
                                            // Add blank line before new statements to give some separation from previous statements
                                            .Prepend(CodeSyntaxHelper.GetBlankLine());

                        _configureMethodStatements = _configureMethodStatements.Concat(newStatements);
                    }
                    else if (method.IsEventHandler(ApplicationEndMethodName) || method.IsEventHandler(SessionStartMethodName) || method.IsEventHandler(SessionEndMethodName))
                    {
                        CommentOutMethod(method);
                    }
                    else
                    {
                        _keepableMethods = _keepableMethods.Append(method);
                    }
                }
                catch (Exception e)
                {
                    LogHelper.LogError(e, $"{Rules.Config.Constants.WebFormsErrorTag}Failed to process {method.Identifier} method in {OriginalClassName} class at {_fullPath}");
                }
            }

            // We added all discovered middleware methods as lambdas so global has been
            // processed as a middleware source
            _lifecycleManager.NotifyMiddlewareSourceProcessed();
        }
示例#11
0
        private void ProcessLifecycleEventMethod(MethodDeclarationSyntax methodDeclaration, WebFormsPageLifecycleEvent lifecycleEvent)
        {
            var statements = (IEnumerable <StatementSyntax>)methodDeclaration.Body.Statements;

            // Dont do anything if the method is empty, no reason to move over nothing
            if (statements.Any())
            {
                statements = statements.AddComment(string.Format(Constants.NewEventRepresentationCommentTemplate, lifecycleEvent.ToString()));

                var blazorLifecycleEvent = LifecycleManagerService.GetEquivalentComponentLifecycleEvent(lifecycleEvent);

                if (_newLifecycleLines.ContainsKey(blazorLifecycleEvent))
                {
                    // Add spacing between last added method
                    statements = statements.Prepend(CodeSyntaxHelper.GetBlankLine());
                    _newLifecycleLines[blazorLifecycleEvent] = _newLifecycleLines[blazorLifecycleEvent].Concat(statements);
                }
                else
                {
                    _newLifecycleLines.Add(blazorLifecycleEvent, statements);
                }
            }
        }
示例#12
0
 public void GetEquivalentComponentLifecycleEvent_Returns_Correct_OnInitialized_Events()
 {
     Assert.AreEqual(BlazorComponentLifecycleEvent.OnInitialized, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.InitComplete));
     Assert.AreEqual(BlazorComponentLifecycleEvent.OnInitialized, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.LoadComplete));
 }
示例#13
0
 public void SetUp()
 {
     _lcManager = new LifecycleManagerService();
     _token     = new CancellationToken();
 }
示例#14
0
 public void CheckWebFormsLifecycleEventWithPrefix_Returns_Correct_Lifecycle_Event()
 {
     Assert.AreEqual(WebFormsAppLifecycleEvent.BeginRequest, LifecycleManagerService.CheckWebFormsLifecycleEventWithPrefix(PrefixedLifecycleEvent, LifecycleEventStringPrefix));
 }
示例#15
0
 public void CheckWebFormsLifecycleEventWithPrefix_Returns_Null_On_Failed_Match()
 {
     Assert.AreEqual(null, LifecycleManagerService.CheckWebFormsLifecycleEventWithPrefix(MisspelledPrefixedLifecycleEvent, LifecycleEventStringPrefix));
 }
示例#16
0
 public void GetEquivalentComponentLifecycleEvent_Returns_Correct_Dispose_Events()
 {
     Assert.AreEqual(BlazorComponentLifecycleEvent.Dispose, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.Unload));
 }
示例#17
0
 public void ContentIsPreHandle_Returns_True_For_First_Pre_Handle_Events()
 {
     Assert.True(LifecycleManagerService.ContentIsPreHandle(WebFormsAppLifecycleEvent.BeginRequest));
 }
示例#18
0
 public void GetEquivalentComponentLifecycleEvent_Returns_Correct_OnParametersSet_Events()
 {
     Assert.AreEqual(BlazorComponentLifecycleEvent.OnParametersSet, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.PreRender));
     Assert.AreEqual(BlazorComponentLifecycleEvent.OnParametersSet, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.PreRenderComplete));
 }
示例#19
0
 public void GetEquivalentComponentLifecycleEvent_Returns_Correct_OnAfterRender_Events()
 {
     Assert.AreEqual(BlazorComponentLifecycleEvent.OnAfterRender, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.SaveStateComplete));
 }
示例#20
0
        public override Task <IEnumerable <FileInformation> > MigrateClassAsync()
        {
            LogStart();

            _metricsContext.CollectActionMetrics(WebFormsActionType.ClassConversion, ActionName);
            var className     = _originalDeclarationSyntax.Identifier.ToString();
            var namespaceName = _originalClassSymbol.ContainingNamespace?.ToDisplayString();

            // NOTE: Removed temporarily until usings can be better determined, at the moment, too
            // many are being removed
            //var requiredNamespaceNames = _sourceFileSemanticModel
            //    .GetNamespacesReferencedByType(_originalDeclarationSyntax)
            //    .Select(namespaceSymbol => namespaceSymbol.ToDisplayString());

            var requiredNamespaceNames = _sourceFileSemanticModel.GetOriginalUsingNamespaces()
                                         .Union(MiddlewareSyntaxHelper.RequiredNamespaces);

            requiredNamespaceNames = CodeSyntaxHelper.RemoveFrameworkUsings(requiredNamespaceNames);

            // Make this call once now so we don't have to keep doing it later
            var originalDescendantNodes = _originalDeclarationSyntax.DescendantNodes();
            var keepableMethods         = originalDescendantNodes.OfType <MethodDeclarationSyntax>();

            var processRequestMethod = keepableMethods.Where(method => LifecycleManagerService.IsProcessRequestMethod(method)).SingleOrDefault();
            IEnumerable <StatementSyntax> preHandleStatements;

            if (processRequestMethod != null)
            {
                preHandleStatements = processRequestMethod.Body.Statements.AddComment(string.Format(Constants.CodeOriginCommentTemplate, Constants.ProcessRequestMethodName));
                keepableMethods     = keepableMethods.Where(method => !method.IsEquivalentTo(processRequestMethod));
                _lifecycleManager.RegisterMiddlewareClass(WebFormsAppLifecycleEvent.RequestHandlerExecute, className, namespaceName, className, false);
            }
            else
            {
                preHandleStatements = new[]
                {
                    CodeSyntaxHelper.GetBlankLine().AddComment(string.Format(Constants.IdentificationFailureCommentTemplate, ProcessRequestDiscovery, InvokePopulationOperation))
                };
            }

            // We have completed any possible registration by this point
            _lifecycleManager.NotifyMiddlewareSourceProcessed();

            var fileText = string.Empty;

            try
            {
                var middlewareClassDeclaration = MiddlewareSyntaxHelper.ConstructMiddlewareClass(
                    middlewareClassName: className,
                    shouldContinueAfterInvoke: false,
                    constructorAdditionalStatements: originalDescendantNodes.OfType <ConstructorDeclarationSyntax>().FirstOrDefault()?.Body?.Statements,
                    preHandleStatements: preHandleStatements,
                    additionalFieldDeclarations: originalDescendantNodes.OfType <FieldDeclarationSyntax>(),
                    additionalPropertyDeclarations: originalDescendantNodes.OfType <PropertyDeclarationSyntax>(),
                    additionalMethodDeclarations: keepableMethods);

                var namespaceNode = CodeSyntaxHelper.BuildNamespace(namespaceName, middlewareClassDeclaration);
                fileText = CodeSyntaxHelper.GetFileSyntaxAsString(namespaceNode, CodeSyntaxHelper.BuildUsingStatements(requiredNamespaceNames));
            }
            catch (Exception e)
            {
                LogHelper.LogError(e, $"{Rules.Config.Constants.WebFormsErrorTag}Failed to construct new HttpHandler file content from {OriginalClassName} class at {_fullPath}");
            }

            DoCleanUp();
            LogEnd();

            // Http modules are turned into middleware and so we use a new middleware directory
            var newRelativePath = FilePathHelper.RemoveDuplicateDirectories(Path.Combine(Constants.MiddlewareDirectoryName, FilePathHelper.AlterFileName(_relativePath, newFileName: className)));
            // TODO: Potentially remove certain folders from beginning of relative path
            var result = new[] { new FileInformation(newRelativePath, Encoding.UTF8.GetBytes(fileText)) };

            return(Task.FromResult((IEnumerable <FileInformation>)result));
        }
示例#21
0
 public void GetEquivalentComponentLifecycleEvent_Returns_Correct_SetParametersAsync_Events()
 {
     Assert.AreEqual(BlazorComponentLifecycleEvent.SetParametersAsync, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.PreInit));
     Assert.AreEqual(BlazorComponentLifecycleEvent.SetParametersAsync, LifecycleManagerService.GetEquivalentComponentLifecycleEvent(WebFormsPageLifecycleEvent.Init));
 }
示例#22
0
 public void ContentIsPreHandle_Returns_True_For_Last_Pre_Handle_Events()
 {
     Assert.True(LifecycleManagerService.ContentIsPreHandle(WebFormsAppLifecycleEvent.PreRequestHandlerExecute));
 }
示例#23
0
 public void ContentIsPreHandle_Returns_False_For_First_Post_Handle_Events()
 {
     Assert.False(LifecycleManagerService.ContentIsPreHandle(WebFormsAppLifecycleEvent.PostRequestHandlerExecute));
 }
示例#24
0
 public void ContentIsPreHandle_Returns_False_For_Last_Post_Handle_Events()
 {
     Assert.False(LifecycleManagerService.ContentIsPreHandle(WebFormsAppLifecycleEvent.PreSendRequestContent));
 }