Пример #1
0
 private void ConfigList(IGeneratorConfiguration generatorConfiguration)
 {
     Console.WriteLine($"Config File: {ConfigUtils.FullConfigFilePath}");
     Console.WriteLine($"Namespace:  {generatorConfiguration.Namespace}");
     Console.WriteLine($"PascalCase: {generatorConfiguration.PascalCase}");
     Console.WriteLine($"Term Attribute: {generatorConfiguration.TermAttribute}");
     Console.WriteLine($"Output:     {generatorConfiguration.Output}");
     Console.WriteLine($"Map method: {generatorConfiguration.MapMethod}");
     Console.WriteLine($"Usings:");
     generatorConfiguration.Usings.ToList().ForEach(n => Console.WriteLine($"          {n}"));
     Console.WriteLine();
     Console.WriteLine($"Name      Type      Include Term");
     Console.WriteLine(new string('-', 80));
     foreach (var property in generatorConfiguration.Properties)
     {
         var propertyConfig = generatorConfiguration.GetPropertyConfiguration(property.Key);
         Console.WriteLine($"{propertyConfig.PropertyName,-10}{propertyConfig.TypeName,-10}{propertyConfig.Include,-8}{property.Key}");
     }
 }
Пример #2
0
        private static MethodDeclarationSyntax MapMethodSyntax(IFileMetaData fileMetaData, IGeneratorConfiguration config, bool useExtensionSyntax = false)
        {
            var roslynGeneratorUtils = new RoslynGeneratorUtils();
            var className            = roslynGeneratorUtils.NormalizeIdentifiers(Path.GetFileNameWithoutExtension(fileMetaData.FileName),
                                                                                 config.PascalCase);

            var objParam = SyntaxFactory.Parameter(SyntaxFactory.Identifier("obj"))
                           .WithType(SyntaxFactory.ParseTypeName(className));

            if (useExtensionSyntax)
            {
                objParam = objParam.AddModifiers(SyntaxFactory.Token(SyntaxKind.ThisKeyword));
            }
            var parameterList = new List <ParameterSyntax>()
            {
                objParam,
                SyntaxFactory.Parameter(SyntaxFactory.Identifier("row"))
                .WithType(SyntaxFactory.ParseTypeName("IRow"))
            };

            var statements = new List <StatementSyntax>();

            foreach (var field in fileMetaData.Fields)
            {
                var propertyConfig = config.GetPropertyConfiguration(field.Term);
                var propertyName   = propertyConfig.PropertyName ?? roslynGeneratorUtils.NormalizeIdentifiers(field.Term, config.PascalCase);
                if (propertyConfig.Include)
                {
                    var statement = CreateStatement(propertyName, field.Term, propertyConfig)
                                    .WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed);
                    statements.Add(statement);
                }
            }

            var methodSyntax = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName("void"), "MapRow")
                               .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                               .AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
                               .AddParameterListParameters(parameterList.ToArray())
                               .WithBody(SyntaxFactory.Block(statements.ToArray()));

            return(methodSyntax);
        }
Пример #3
0
        public ClassDeclarationSyntax GeneratClassSyntax(IFileMetaData fileMetaData, IGeneratorConfiguration config)
        {
            var roslynGeneratorUtils = new RoslynGeneratorUtils();
            var className            = roslynGeneratorUtils.NormalizeIdentifiers(Path.GetFileNameWithoutExtension(fileMetaData.FileName),
                                                                                 config.PascalCase);
            var classDeclaration = SyntaxFactory
                                   .ClassDeclaration(className)
                                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));

            foreach (var metaData in fileMetaData.Fields)
            {
                var propertyConfiguration = config.GetPropertyConfiguration(metaData.Term);

                if (propertyConfiguration.Include)
                {
                    var propertyDeclaration = GenerateProperty(metaData, propertyConfiguration, roslynGeneratorUtils, config);
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }
            return(classDeclaration);
        }