Ejemplo n.º 1
0
        private async Task InsertRequestPipelineMiddlewareRegistrations()
        {
            try
            {
                var pipelineAdditions = await _taskManager.ManagedRun(_taskId, (token) => _lifecycleManager.GetMiddlewarePipelineAdditions(token));

                _configureMethodStatements = _configureMethodStatements.Concat(pipelineAdditions);
            }
            catch (OperationCanceledException e)
            {
                LogHelper.LogError(e, string.Format(
                                       Constants.CaneledServiceCallLogTemplate,
                                       Rules.Config.Constants.WebFormsErrorTag,
                                       GetType().Name,
                                       typeof(LifecycleManagerService).Name,
                                       GetMiddlewarePipelineAdditionsLogCall));

                var failureComment = string.Format(Constants.OperationFailedCommentTemplate, ConfigureRequestPipelineOperation);

                if (!_configureMethodStatements.Any())
                {
                    // If we don't have any extra statements for configure() add a blank
                    // one to attach our comment to
                    _configureMethodStatements = _configureMethodStatements.Append(CodeSyntaxHelper.GetBlankLine().AddComment(failureComment, isLeading: false));
                }
                else
                {
                    _configureMethodStatements = _configureMethodStatements.AddComment(failureComment, isLeading: false);
                }
            }
        }
Ejemplo n.º 2
0
        public override async Task <IEnumerable <FileInformation> > MigrateClassAsync()
        {
            LogStart();
            _metricsContext.CollectActionMetrics(WebFormsActionType.ClassConversion, ActionName);

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

            // Currently not implementing service layer so add blank line with a comment instead
            var configureServicesLines = new[]
            {
                CodeSyntaxHelper.GetBlankLine().AddComment(string.Format(Constants.OperationUnattemptedCommentTemplate, MigrateServiceLayerOperation), isLeading: false)
            };

            // Iterate through methods and process them as needed
            ProcessMethods(originalDescendantNodes.OfType <MethodDeclarationSyntax>());

            // With middleware lambdas added we need to retrieve registrations
            // before we have all statements required to build startup class
            await InsertRequestPipelineMiddlewareRegistrations();

            var fileText = string.Empty;

            try
            {
                var startupClassDeclaration = StartupSyntaxHelper.ConstructStartupClass(
                    constructorAdditionalStatements: originalDescendantNodes.OfType <ConstructorDeclarationSyntax>().FirstOrDefault()?.Body?.Statements,
                    configureAdditionalStatements: _configureMethodStatements,
                    configureServicesAdditionalStatements: configureServicesLines,
                    additionalFieldDeclarations: originalDescendantNodes.OfType <FieldDeclarationSyntax>(),
                    additionalPropertyDeclarations: originalDescendantNodes.OfType <PropertyDeclarationSyntax>(),
                    additionalMethodDeclarations: _keepableMethods)
                                              .AddClassBlockComment(_endOfClassComments, false);

                var containingNamespace = CodeSyntaxHelper.BuildNamespace(_originalClassSymbol.ContainingNamespace?.ToDisplayString(), startupClassDeclaration);
                fileText = CodeSyntaxHelper.GetFileSyntaxAsString(containingNamespace, await GetAllUsingStatements());
            }
            catch (Exception e)
            {
                LogHelper.LogError(e, $"{Rules.Config.Constants.WebFormsErrorTag}Failed to construct new Startup file content from {OriginalClassName} class at {_fullPath}");
            }

            DoCleanUp();
            LogEnd();

            // Global.asax.cs turns into Startup.cs
            var newRelativePath = FilePathHelper.RemoveDuplicateDirectories(Path.Combine(Path.GetDirectoryName(_relativePath), Constants.StartupFileName));

            return(new[] { new FileInformation(newRelativePath, Encoding.UTF8.GetBytes(fileText)) });
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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);
                }
            }
        }
Ejemplo n.º 5
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));
        }