Пример #1
0
        public string GetInterfaceServiceFileFromClass(
            ServiceFile classServiceFile,
            string serviceInterfaceName,
            string serviceNamespace)
        {
            var interfaceDeclaration = _serviceCommandParserService.GetInterfaceFromClass(
                classServiceFile.ServiceDeclaration,
                serviceInterfaceName);

            return(_serviceCommandStgService.RenderServiceFile(
                       usingDirectives: classServiceFile.UsingDirectives,
                       serviceNamespace: serviceNamespace,
                       service: interfaceDeclaration));
        }
        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.");
                }
            }
        }