Пример #1
0
        public async Task GetMiddlewarePipelineAdditions_Returns_On_All_Sources_Processed()
        {
            _lcManager.NotifyExpectedMiddlewareSource();
            _lcManager.NotifyExpectedMiddlewareSource();

            var result = _lcManager.GetMiddlewarePipelineAdditions(_token);
            await Task.Delay(ProcessingAllowanceDelay);

            Assert.False(result.IsCompleted);

            _lcManager.NotifyMiddlewareSourceProcessed();
            await Task.Delay(ProcessingAllowanceDelay);

            Assert.False(result.IsCompleted);

            _lcManager.NotifyMiddlewareSourceProcessed();
            await Task.Delay(ProcessingAllowanceDelay);

            Assert.True(result.IsCompletedSuccessfully);
        }
Пример #2
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();
        }
Пример #3
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));
        }