public string GetClassServiceFileFromInterface( ServiceFile interfaceServiceFile, string serviceClassName, string serviceNamespace, List <string> appendUsingDirectives, ClassInterfaceBody appendClassBody) { var usingSet = interfaceServiceFile.UsingDirectives.ToHashSet(); usingSet.UnionWith(appendUsingDirectives); usingSet.Remove(serviceNamespace); var classDeclaration = _serviceCommandParserService.GetClassFromInterface( interfaceServiceFile.ServiceDeclaration, serviceClassName); var fieldDeclarations = appendClassBody?.FieldDeclarations; classDeclaration.Body.ConstructorDeclaration = appendClassBody?.ConstructorDeclaration; if (fieldDeclarations != null) { classDeclaration.Body.FieldDeclarations = fieldDeclarations; } return(_serviceCommandStgService.RenderServiceFile( usingDirectives: usingSet.ToList(), serviceNamespace: serviceNamespace, service: classDeclaration)); }
private void ConsolidateServiceClassAndInterface( SourceOfTruth sourceOfTruth, string serviceClassName, string serviceInterfaceName, List <TypeParameter> typeParameters, List <InjectedService> injectedServices, string serviceClassFilePath, string serviceInterfaceFilePath, string serviceNamespace, string outServiceClassFilePath, string outServiceInterfaceFilePath) { var usingDirectives = new HashSet <string>(); var fieldDeclarations = new List <FieldDeclaration>(); var ctorParameters = new FormalParameterList(); var fixedParams = ctorParameters.FixedParameters; var ctorBody = new ConstructorBody(); var statements = ctorBody.Statements; var appendBody = new ClassInterfaceBody() { FieldDeclarations = fieldDeclarations, ConstructorDeclaration = new ConstructorDeclaration() { FormalParameterList = ctorParameters, Body = ctorBody } }; if (injectedServices != null && injectedServices.Count > 0) { foreach (var injectedService in injectedServices) { var injectedServiceIdentifier = injectedService.ServiceIdentifier ?? Regex.Replace( Regex.Replace( injectedService.Type, @"^I?([A-Z])", "$1"), @"^[A-Z]", m => m.ToString().ToLower()); if (!Regex.Match(serviceNamespace, "^" + Regex.Escape(injectedService.Namespace)).Success) { usingDirectives.Add(injectedService.Namespace); } fieldDeclarations.Add( new FieldDeclaration() { Modifiers = new List <string>() { Keywords.Private, Keywords.Readonly }, Type = injectedService.Type, VariableDeclarator = new VariableDeclarator() { Identifier = "_" + injectedServiceIdentifier } }); fixedParams.Add( new FixedParameter() { Type = injectedService.Type, Identifier = injectedServiceIdentifier }); statements.Add( new Statement() { SimpleAssignment = new SimpleAssignment() { LeftHandSide = "_" + injectedServiceIdentifier, RightHandSide = injectedServiceIdentifier } }); } } CSharpParserWrapper serviceClassParser = null; CSharpParserWrapper serviceInterfaceParser = null; ServiceFile classScraperResults = null; ServiceFile interfaceScraperResults = null; //Check if <service> class file exists: if (File.Exists(serviceClassFilePath)) { _logger.LogDebug("Service class file found. Pulling data from service class."); // Else: // Parse <service> class file // Extract list of existing public method signatures serviceClassParser = new CSharpParserWrapper(GetPathFromWrittenTo(serviceClassFilePath)); classScraperResults = _serviceCommandService.ScrapeServiceClass( serviceClassParser, serviceClassName, serviceNamespace, typeParameters); } else { _logger.LogDebug($"No service class file found at {serviceClassFilePath}."); } //Check if <service> interface file exists: if (File.Exists(serviceInterfaceFilePath)) { _logger.LogDebug("Service interface file found. Pulling data from service interface."); // Else: // Parse <service> interface file // Extract list of existing method signatures serviceInterfaceParser = new CSharpParserWrapper(GetPathFromWrittenTo(serviceInterfaceFilePath)); interfaceScraperResults = _serviceCommandService.ScrapeServiceInterface( serviceInterfaceParser, serviceInterfaceName, serviceNamespace, typeParameters); } else { _logger.LogDebug($"No service interface file found at {serviceInterfaceFilePath}."); } if (classScraperResults is null && interfaceScraperResults is null) { _logger.LogDebug("Creating new service class and interface and writing them to file."); // Create <service> class file // Create serviceClass StringTemplate with empty class body var classDeclaration = new ClassInterfaceDeclaration() { IsInterface = false, Modifiers = new List <string>() { Keywords.Public }, Identifier = serviceClassName, TypeParameters = typeParameters.Copy(), Base = new ClassInterfaceBase() { InterfaceTypeList = new List <string>() { serviceInterfaceName + _cSharpCommonStgService.RenderTypeParamList(typeParameters.Copy()) } }, Body = new ClassInterfaceBody() { FieldDeclarations = fieldDeclarations, ConstructorDeclaration = new ConstructorDeclaration() { Modifiers = new List <string>() { Keywords.Public }, Identifier = serviceClassName, FormalParameterList = ctorParameters, Body = ctorBody } } }; UpdateWrittenTo(serviceClassFilePath, outServiceClassFilePath); _ioUtilService.WriteStringToFile( _serviceCommandStgService.RenderServiceFile( usingDirectives: usingDirectives.ToList(), serviceNamespace: serviceNamespace, service: classDeclaration), outServiceClassFilePath); // Create <service> interface file // Create serviceInterface StringTemplate with empty interface body var interfaceDeclaration = new ClassInterfaceDeclaration() { IsInterface = true, Modifiers = new List <string>() { Keywords.Public }, Identifier = serviceInterfaceName, TypeParameters = typeParameters.Copy() }; UpdateWrittenTo(serviceInterfaceFilePath, outServiceInterfaceFilePath); _ioUtilService.WriteStringToFile( _serviceCommandStgService.RenderServiceFile( serviceNamespace: serviceNamespace, service: interfaceDeclaration), outServiceInterfaceFilePath); }
private void ValidateServiceClass( string serviceClassName, List <TypeParameter> typeParameters, List <InjectedService> injectedServices, string serviceClassFilePath, string serviceNamespace, string outServiceClassFilePath) { var usingDirectives = new HashSet <string>(); var fieldDeclarations = new List <FieldDeclaration>(); var ctorParameters = new FormalParameterList(); var fixedParams = ctorParameters.FixedParameters; var ctorBody = new ConstructorBody(); var statements = ctorBody.Statements; var appendBody = new ClassInterfaceBody() { FieldDeclarations = fieldDeclarations, ConstructorDeclaration = new ConstructorDeclaration() { FormalParameterList = ctorParameters, Body = ctorBody } }; if (injectedServices != null && injectedServices.Count > 0) { foreach (var injectedService in injectedServices) { var injectedServiceIdentifier = injectedService.ServiceIdentifier ?? Regex.Replace( Regex.Replace( injectedService.Type, @"^I?([A-Z])", "$1"), @"^[A-Z]", m => m.ToString().ToLower()); if (!Regex.Match(serviceNamespace, "^" + Regex.Escape(injectedService.Namespace)).Success) { usingDirectives.Add(injectedService.Namespace); } fieldDeclarations.Add( new FieldDeclaration() { Modifiers = new List <string>() { Keywords.Private, Keywords.Readonly }, Type = injectedService.Type, VariableDeclarator = new VariableDeclarator() { Identifier = "_" + injectedServiceIdentifier } }); fixedParams.Add( new FixedParameter() { Type = injectedService.Type, Identifier = injectedServiceIdentifier }); statements.Add( new Statement() { SimpleAssignment = new SimpleAssignment() { LeftHandSide = "_" + injectedServiceIdentifier, RightHandSide = injectedServiceIdentifier } }); } } var serviceClassFile = new ServiceFile() { UsingDirectives = usingDirectives.ToList(), ServiceNamespace = serviceNamespace, ServiceDeclaration = new ClassInterfaceDeclaration() { IsInterface = false, Modifiers = new List <string>() { Keywords.Public }, Identifier = serviceClassName, TypeParameters = typeParameters.Copy(), Body = new ClassInterfaceBody() { FieldDeclarations = fieldDeclarations, ConstructorDeclaration = new ConstructorDeclaration() { Modifiers = new List <string>() { Keywords.Public }, Identifier = serviceClassName, FormalParameterList = ctorParameters, Body = ctorBody } } } }; if (!File.Exists(serviceClassFilePath)) { // Create <service> class file // Create serviceClass StringTemplate with empty class body _logger.LogDebug("Creating new service class and writing it to file."); UpdateWrittenTo(serviceClassFilePath, outServiceClassFilePath); _ioUtilService.WriteStringToFile( _serviceCommandStgService.RenderServiceFile( usingDirectives: usingDirectives.ToList(), serviceNamespace: serviceNamespace, service: serviceClassFile.ServiceDeclaration), outServiceClassFilePath); } else { var serviceClassParser = new CSharpParserWrapper(GetPathFromWrittenTo(serviceClassFilePath)); var serviceClassRewrite = _serviceCommandService.InjectDataIntoServiceClass( serviceClassFile, serviceClassParser, serviceClassName, _userSettings.Value.TabString); if (serviceClassRewrite != null) { _logger.LogDebug("Overwriting service class file."); UpdateWrittenTo(serviceClassFilePath, outServiceClassFilePath); _ioUtilService.WriteStringToFile( serviceClassRewrite, outServiceClassFilePath); } else { _logger.LogDebug("Service class file was already up to date."); } } }