private async Task <IEnumerable <UsingDirectiveSyntax> > GetAllUsingStatements() { // NOTE: Removed temporarily until usings can be better determined, at the moment, too // many are being removed //var typeRequiredNamespaceNames = _sourceFileSemanticModel // .GetNamespacesReferencedByType(_originalDeclarationSyntax) // .Select(namespaceSymbol => namespaceSymbol.ToDisplayString()); var typeRequiredNamespaceNames = _sourceFileSemanticModel.GetOriginalUsingNamespaces(); typeRequiredNamespaceNames = CodeSyntaxHelper.RemoveFrameworkUsings(typeRequiredNamespaceNames); // Merging required using statements for middleware and source type contents try { var middlewareNamespaceNames = await _taskManager.ManagedRun(_taskId, (token) => _lifecycleManager.GetMiddlewareNamespaces(token)); return(CodeSyntaxHelper.BuildUsingStatements(typeRequiredNamespaceNames.Union(middlewareNamespaceNames).Union(StartupSyntaxHelper.RequiredNamespaces))); } catch (OperationCanceledException e) { LogHelper.LogError(e, string.Format( Constants.CaneledServiceCallLogTemplate, Rules.Config.Constants.WebFormsErrorTag, GetType().Name, typeof(LifecycleManagerService).Name, GetMiddlewareNamespacesLogCall)); return(CodeSyntaxHelper.BuildUsingStatements(typeRequiredNamespaceNames) .AddComment(string.Format(Constants.OperationFailedCommentTemplate, AddMiddlewareUsingsOperation))); } }
public override Task <IEnumerable <FileInformation> > MigrateClassAsync() { LogStart(); _metricsContext.CollectActionMetrics(WebFormsActionType.ClassConversion, ActionName); // NOTE: Removed temporarily until usings can be better determined, at the moment, too // many are being removed //var requiredNamespaces = _sourceFileSemanticModel.GetNamespacesReferencedByType(_originalDeclarationSyntax); //var namespaceNames = requiredNamespaces.Select(namespaceSymbol => namespaceSymbol.ToDisplayString()).Append(Constants.BlazorComponentsNamespace); var requiredNamespaces = _sourceFileSemanticModel.GetOriginalUsingNamespaces().Append(Constants.BlazorComponentsNamespace); requiredNamespaces = CodeSyntaxHelper.RemoveFrameworkUsings(requiredNamespaces); var usingStatements = CodeSyntaxHelper.BuildUsingStatements(requiredNamespaces); var modifiedClass = ((ClassDeclarationSyntax)_originalDeclarationSyntax) // Remove outdated base type references // TODO: Scan and remove specific base types in the future .ClearBaseTypes() // ComponentBase base class is added because user controls become // normal razor components .AddBaseType(Constants.ComponentBaseClass); var namespaceNode = CodeSyntaxHelper.BuildNamespace(_originalClassSymbol.ContainingNamespace?.ToDisplayString(), modifiedClass); DoCleanUp(); LogEnd(); var newRelativePath = GetNewRelativePath(); var fileContent = CodeSyntaxHelper.GetFileSyntaxAsString(namespaceNode, usingStatements); var result = new[] { new FileInformation(newRelativePath, Encoding.UTF8.GetBytes(fileContent)) }; return(Task.FromResult((IEnumerable <FileInformation>)result)); }
public override Task <IEnumerable <FileInformation> > MigrateClassAsync() { LogStart(); _metricsContext.CollectActionMetrics(WebFormsActionType.ClassConversion, ActionName); // NOTE: We could just read the file from the disk and retrieve the bytes like // that but instead I opted to "rebuild" the type in case we wanted to add comments // or something else to these undefined code files, most likely though we may still // want to scan parts of these files and remove/alter/take note of certain lines/info var newRelativePath = FilePathHelper.RemoveDuplicateDirectories(FilePathHelper.AlterFileName(_relativePath, newFileName: _originalClassSymbol.Name)); // NOTE: Removed temporarily until usings can be better determined, at the moment, too // many are being removed //var sourceClassComponents = GetSourceClassComponents(); var namespaceNames = _sourceFileSemanticModel.GetOriginalUsingNamespaces().Append(Constants.BlazorComponentsNamespace); namespaceNames = CodeSyntaxHelper.RemoveFrameworkUsings(namespaceNames); var usingStatements = CodeSyntaxHelper.BuildUsingStatements(namespaceNames); var namespaceNode = CodeSyntaxHelper.BuildNamespace(_originalClassSymbol.ContainingNamespace?.ToDisplayString(), _originalDeclarationSyntax); var fileText = CodeSyntaxHelper.GetFileSyntaxAsString(namespaceNode, usingStatements); DoCleanUp(); LogEnd(); var result = new[] { new FileInformation(newRelativePath, Encoding.UTF8.GetBytes(fileText)) }; return(Task.FromResult((IEnumerable <FileInformation>)result)); }
public override Task <IEnumerable <FileInformation> > MigrateClassAsync() { LogStart(); _metricsContext.CollectActionMetrics(WebFormsActionType.ClassConversion, ActionName); // 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()) // // This is so we can use ComponentBase base class // .Append(Constants.BlazorComponentsNamespace); var requiredNamespaceNames = _sourceFileSemanticModel.GetOriginalUsingNamespaces().Append(Constants.BlazorComponentsNamespace); requiredNamespaceNames = CodeSyntaxHelper.RemoveFrameworkUsings(requiredNamespaceNames); var allMethods = _originalDeclarationSyntax.DescendantNodes().OfType <MethodDeclarationSyntax>(); var currentClassDeclaration = ((ClassDeclarationSyntax)_originalDeclarationSyntax) // Need to track methods so modifications can be made one after another .TrackNodes(allMethods) // Remove outdated base type references // TODO: Scan and remove specific base types in the future .ClearBaseTypes() // ComponentBase base class is required to use lifecycle events .AddBaseType(Constants.ComponentBaseClass); var orderedMethods = allMethods .Select(method => (method, LifecycleManagerService.CheckMethodPageLifecycleHook(method))) // Filter out non-lifecycle methods .Where(methodTuple => methodTuple.Item2 != null) // Order matters within new events so we order before processing .OrderBy(methodTuple => { return((int)methodTuple.Item2); }); // Remove old lifecycle methods, sort, and record their content foreach (var methodTuple in orderedMethods) { try { // This records the statements in the proper collection ProcessLifecycleEventMethod(methodTuple.Item1, (WebFormsPageLifecycleEvent)methodTuple.Item2); } catch (Exception e) { LogHelper.LogError(e, $"{Rules.Config.Constants.WebFormsErrorTag}Failed to process WebForms lifecycle event method {methodTuple.Item1.Identifier} " + $"from {OriginalClassName} class at {_fullPath}"); } // Refresh node before removing var currentMethodNode = currentClassDeclaration.GetCurrentNode(methodTuple.Item1); currentClassDeclaration = currentClassDeclaration.RemoveNode(currentMethodNode, SyntaxRemoveOptions.AddElasticMarker); } // Construct new lifecycle methods and add them to the class foreach (var newLifecycleEventKvp in _newLifecycleLines) { var newLifecycleEvent = newLifecycleEventKvp.Key; var newLifecycleEventStatements = newLifecycleEventKvp.Value; try { var newMethodDeclaration = ComponentSyntaxHelper.ConstructComponentLifecycleMethod(newLifecycleEvent, newLifecycleEventStatements); currentClassDeclaration = currentClassDeclaration.AddMembers(newMethodDeclaration); } catch (Exception e) { LogHelper.LogError(e, $"{Rules.Config.Constants.WebFormsErrorTag}Failed to construct new lifecycle event method for {newLifecycleEvent} Blazor event " + $"using {OriginalClassName} class at {_fullPath}"); } } // If we need to make use of the dispose method, add the IDisposable // interface to the class, usings are fine as is because this come from // the System namespace if (_newLifecycleLines.ContainsKey(BlazorComponentLifecycleEvent.Dispose)) { currentClassDeclaration = currentClassDeclaration.AddBaseType(Constants.DisposableInterface); } var namespaceNode = CodeSyntaxHelper.BuildNamespace(_originalClassSymbol.ContainingNamespace?.ToDisplayString(), currentClassDeclaration); var fileText = CodeSyntaxHelper.GetFileSyntaxAsString(namespaceNode, CodeSyntaxHelper.BuildUsingStatements(requiredNamespaceNames)); DoCleanUp(); LogEnd(); var result = new[] { new FileInformation(GetNewRelativePath(), Encoding.UTF8.GetBytes(fileText)) }; return(Task.FromResult((IEnumerable <FileInformation>)result)); }
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)); }