public override object VisitClass_member_declaration([NotNull] CSharpParser.Class_member_declarationContext context) { var method = context?.method_declaration(); // Find method "public void ConfigureServices(IServiceCollection services)" if (method != null && method.method_header().member_name().identifier().GetText() == "ConfigureServices" && string.Join(".", _currentClass.ToArray().Reverse()) == "Startup" && string.Join(".", _currentNamespace.ToArray().Reverse()) == _rootNamespace ) { _isFoundConfigureServices = true; var paramList = method?.method_header()?.formal_parameter_list()?.fixed_parameters()?.fixed_parameter(); if (paramList is null) { // throw } // Get name of IServiceCollection parameter (services) string serviceCollectionName = null; foreach (var param in paramList) { if (param.type_().GetText() == nameof(IServiceCollection)) { serviceCollectionName = param.identifier().GetText(); break; } } if (serviceCollectionName is null) { // throw } // Check each services.Add[Lifespan] method in the form // If TypeParameters: services.AddScoped(typeof(IMyService<>), typeof(MyService<>)); // Else: services.AddScoped<IMyService, MyService>(); // If found and Lifespan in services.Add[Lifespan] == Service.Lifespan: // No action taken // Else if found and Lifespan in services.Add[Lifespan] != Service.Lifespan: // Replace Add[Lifespan] with Add[Service.Lifespan] in token stream // Else: // Create StringTemplate for services.Add[Lifespan] statement // If TypeParameters: services.AddScoped(typeof(IMyService<>), typeof(MyService<>)); // Else: services.AddScoped<IMyService, MyService>(); // Use rewriter to insert services.Add[Lifespan] into token stream // Write parse tree as string to Startup.cs var missingRegInfo = new List <StartupRegistrationInfo>(); var body = _cSharpParserService.GetTextWithWhitespace(Tokens, method.method_body()); foreach (var regInfo in _startupRegInfoList) { if (!Regex.Match(body, ServiceRegistrationMatchString(regInfo)).Success) { missingRegInfo.Add(regInfo); } } if (missingRegInfo.Count > 0) { if (method?.method_body()?.block() is null) { // throw } else { var closeBranceIndex = method.method_body().block().CLOSE_BRACE().Symbol.TokenIndex; var preclassWhitespace = Tokens.GetHiddenTokensToLeft(closeBranceIndex, Lexer.Hidden); int tabLevels = (preclassWhitespace?.Count ?? 0) > 0 ? _stringUtilService.CalculateTabLevels(preclassWhitespace[0]?.Text ?? string.Empty, _tabString) : 0; var regCallStrings = new List <string>(); foreach (var regInfo in missingRegInfo) { var regCallString = _serviceCommandStgService.RenderServiceStartupRegistrationCall( serviceClassType: regInfo.ServiceClassType, serviceBaseType: regInfo.ServiceBaseType, hasTypeParameters: regInfo.HasTypeParameters, serviceLifespan: regInfo.ServiceLifespan.ToString()); regCallStrings.Add(serviceCollectionName + regCallString + ";"); } var registrationStatement = "\r\n" + _stringUtilService.TabString( string.Join("\r\n", regCallStrings), tabLevels + 1, _tabString) + "\r\n" + _stringUtilService.TabString(string.Empty, tabLevels, _tabString); IsModified = true; Rewriter.InsertBefore(closeBranceIndex, registrationStatement); return(null); } } } VisitChildren(context); return(null); }
public override object VisitClass_declaration([NotNull] CSharpParser.Class_declarationContext context) { bool matchNames = context.identifier().GetText() == ServiceClassName; bool isTypeParamsMatch = true; var typeParams = context?.type_parameter_list()?.type_parameter(); if (!(typeParams is null && (TypeParameters is null || TypeParameters.Count == 0))) { if ((typeParams?.Length ?? 0) != (TypeParameters?.Count ?? 0)) { isTypeParamsMatch = false; } else { for (int i = 0; i < typeParams.Length; ++i) { if (typeParams[i].identifier().GetText() != TypeParameters[i].TypeParam) { isTypeParamsMatch = false; break; } } } } if (matchNames && isTypeParamsMatch) { HasServiceClass = true; Results.ServiceNamespace = string.Join(".", _currentNamespace.ToArray().Reverse()); var classDeclaration = new ClassInterfaceDeclaration() { IsInterface = false, Attributes = _cSharpParserService.GetTextWithWhitespace(Tokens, context?.attributes()), Identifier = ServiceClassName }; //Results.ClassInterfaceDeclarations.Add(className, classDeclaration); Results.ServiceDeclaration = classDeclaration; if (context?.PARTIAL()?.GetText() != null) { classDeclaration.Modifiers.Add(context.PARTIAL().GetText()); } if (context?.class_modifier() != null) { foreach (var modifier in context.class_modifier()) { classDeclaration.Modifiers.Add(modifier.GetText()); } } var typeParameters = context?.type_parameter_list()?.type_parameter(); var constraintsClauses = context?.type_parameter_constraints_clauses()?.type_parameter_constraints_clause(); classDeclaration.TypeParameters = _cSharpParserService.ParseTypeParameterList(Tokens, typeParameters, constraintsClauses); var baseClassType = _cSharpParserService.GetTextWithWhitespaceMinifiedLite( Tokens, context?.class_base()?.class_type()); var interfaceTypes = context?.class_base()?.interface_type_list()?.interface_type(); if (interfaceTypes != null || baseClassType != null) { classDeclaration.Base = new ClassInterfaceBase(); if (baseClassType != null) { classDeclaration.Base.ClassType = baseClassType; } if (interfaceTypes != null) { foreach (var interfaceType in interfaceTypes) { classDeclaration.Base.InterfaceTypeList.Add( _cSharpParserService.GetTextWithWhitespaceMinifiedLite(Tokens, interfaceType)); } } } var memberDeclarations = context?.class_body()?.class_member_declarations()?.class_member_declaration(); if (memberDeclarations != null) { foreach (var memberDeclaration in memberDeclarations) { bool isPublic = false; var classMethodDeclaration = memberDeclaration?.method_declaration(); var classPropertyDeclaration = memberDeclaration?.property_declaration(); if (classMethodDeclaration != null) { var methodDeclaration = new MethodDeclaration() { Attributes = _cSharpParserService.GetTextWithWhitespace( Tokens, classMethodDeclaration?.method_header()?.attributes()), ReturnType = _cSharpParserService.GetTextWithWhitespaceMinifiedLite( Tokens, classMethodDeclaration.method_header().return_type()), Identifier = classMethodDeclaration.method_header().member_name().GetText(), Body = _cSharpParserService.GetTextWithWhitespaceUntab( Tokens, classMethodDeclaration.method_body()) }; var methodModifiers = classMethodDeclaration?.method_header()?.method_modifier(); if (methodModifiers != null) { foreach (var methodMod in methodModifiers) { if (methodMod.GetText() == Keywords.Public) { isPublic = true; } methodDeclaration.Modifiers.Add(methodMod.GetText()); } } if (!isPublic) { continue; } var formalParameterList = classMethodDeclaration?.method_header()?.formal_parameter_list(); methodDeclaration.FormalParameterList = _cSharpParserService.ParseFormalParameterList(Tokens, formalParameterList); var methodTypeParameters = classMethodDeclaration?.method_header()?.type_parameter_list()?.type_parameter(); var methodConstraintsClauses = classMethodDeclaration ?.method_header() ?.type_parameter_constraints_clauses() ?.type_parameter_constraints_clause(); methodDeclaration.TypeParameters = _cSharpParserService .ParseTypeParameterList(Tokens, methodTypeParameters, methodConstraintsClauses); classDeclaration.Body.MethodDeclarations.Add(methodDeclaration); } else if (classPropertyDeclaration != null) { var propertyDeclaration = new PropertyDeclaration() { Attributes = _cSharpParserService.GetTextWithWhitespace( Tokens, classPropertyDeclaration?.attributes()), Type = _cSharpParserService.GetTextWithWhitespaceMinifiedLite( Tokens, classPropertyDeclaration.type_()), Identifier = classPropertyDeclaration.member_name().GetText(), Body = new PropertyBody() { Text = _cSharpParserService.GetTextWithWhitespaceUntab( Tokens, classPropertyDeclaration.property_body()), HasGetAccessor = classPropertyDeclaration.property_body().expression() != null | classPropertyDeclaration.property_body() ?.accessor_declarations() ?.get_accessor_declaration() != null, HasSetAccessor = classPropertyDeclaration.property_body() ?.accessor_declarations() ?.set_accessor_declaration() != null } }; var propertyModifiers = classPropertyDeclaration?.property_modifier(); if (propertyModifiers != null) { foreach (var propModifier in propertyModifiers) { if (propModifier.GetText() == Keywords.Public) { isPublic = true; } propertyDeclaration.Modifiers.Add(propModifier.GetText()); } } if (!isPublic) { continue; } classDeclaration.Body.PropertyDeclarations.Add(propertyDeclaration); } } } } VisitChildren(context); return(null); }
public override object VisitInterface_declaration([NotNull] CSharpParser.Interface_declarationContext context) { bool matchNames = context.identifier().GetText() == ServiceInterfaceName; bool isTypeParamsMatch = true; var variantTypeParameters = context?.variant_type_parameter_list()?.variant_type_parameter(); if (!(variantTypeParameters is null && (TypeParameters is null || TypeParameters.Count == 0))) { if ((variantTypeParameters?.Length ?? 0) != (TypeParameters?.Count ?? 0)) { isTypeParamsMatch = false; } else { for (int i = 0; i < variantTypeParameters.Length; ++i) { if (variantTypeParameters[i].identifier().GetText() != TypeParameters[i].TypeParam) { isTypeParamsMatch = false; break; } } } } if (matchNames && isTypeParamsMatch) { HasServiceInterface = true; Results.ServiceNamespace = string.Join(".", _currentNamespace.ToArray().Reverse()); var interfaceDeclaration = new ClassInterfaceDeclaration() { IsInterface = true, Attributes = _cSharpParserService.GetTextWithWhitespace(Tokens, context?.attributes()), Identifier = ServiceInterfaceName }; //Results.ClassInterfaceDeclarations.Add(interfaceName, interfaceDeclaration); Results.ServiceDeclaration = interfaceDeclaration; if (context?.PARTIAL()?.GetText() != null) { interfaceDeclaration.Modifiers.Add(context.PARTIAL().GetText()); } if (context?.interface_modifier() != null) { foreach (var modifier in context.interface_modifier()) { interfaceDeclaration.Modifiers.Add(modifier.GetText()); } } var constraintsClauses = context?.type_parameter_constraints_clauses()?.type_parameter_constraints_clause(); interfaceDeclaration.TypeParameters = _cSharpParserService.ParseVariantTypeParameterList( Tokens, variantTypeParameters, constraintsClauses); var interfaceTypes = context?.interface_base()?.interface_type_list()?.interface_type(); if (interfaceTypes != null) { interfaceDeclaration.Base = new ClassInterfaceBase(); foreach (var interfaceType in interfaceTypes) { interfaceDeclaration.Base.InterfaceTypeList.Add( _cSharpParserService.GetTextWithWhitespaceMinifiedLite(Tokens, interfaceType)); } } var memberDeclarations = context?.interface_body()?.interface_member_declaration(); if (memberDeclarations != null) { foreach (var memberDeclaration in memberDeclarations) { var interfaceMethodDeclaration = memberDeclaration?.interface_method_declaration(); var interfacePropertyDeclaration = memberDeclaration?.interface_property_declaration(); if (interfaceMethodDeclaration != null) { var methodDeclaration = new MethodDeclaration() { Attributes = _cSharpParserService.GetTextWithWhitespace( Tokens, interfaceMethodDeclaration?.attributes()), ReturnType = _cSharpParserService.GetTextWithWhitespaceMinifiedLite( Tokens, interfaceMethodDeclaration.return_type()), Identifier = interfaceMethodDeclaration.identifier().GetText() }; if (interfaceMethodDeclaration?.NEW()?.GetText() != null) { methodDeclaration.Modifiers.Add(interfaceMethodDeclaration.NEW().GetText()); } var formalParameterList = interfaceMethodDeclaration?.formal_parameter_list(); methodDeclaration.FormalParameterList = _cSharpParserService.ParseFormalParameterList(Tokens, formalParameterList); var methodTypeParameters = interfaceMethodDeclaration?.type_parameter_list()?.type_parameter(); var methodConstraintsClauses = interfaceMethodDeclaration ?.type_parameter_constraints_clauses() ?.type_parameter_constraints_clause(); methodDeclaration.TypeParameters = _cSharpParserService .ParseTypeParameterList(Tokens, methodTypeParameters, methodConstraintsClauses); interfaceDeclaration.Body.MethodDeclarations.Add(methodDeclaration); } else if (interfacePropertyDeclaration != null) { var propertyDeclaration = new PropertyDeclaration() { Attributes = _cSharpParserService.GetTextWithWhitespace( Tokens, interfacePropertyDeclaration?.attributes()), Type = _cSharpParserService.GetTextWithWhitespaceMinifiedLite( Tokens, interfacePropertyDeclaration.type_()), Identifier = interfacePropertyDeclaration.identifier().GetText(), Body = new PropertyBody() { Text = _cSharpParserService.GetTextWithWhitespaceUntab( Tokens, interfacePropertyDeclaration.interface_accessors()), HasGetAccessor = interfacePropertyDeclaration.interface_accessors() .interface_get_accessor() != null, HasSetAccessor = interfacePropertyDeclaration.interface_accessors() .interface_set_accessor() != null } }; if (interfacePropertyDeclaration?.NEW() != null) { propertyDeclaration.Modifiers.Add(interfacePropertyDeclaration?.NEW()?.GetText()); } interfaceDeclaration.Body.PropertyDeclarations.Add(propertyDeclaration); } } } } VisitChildren(context); return(null); }