Example #1
0
        public static Property FromContentType(string codename, string contentType)
        {
            if (!IsContentTypeSupported(contentType))
            {
                throw new ArgumentException($"Unknown Content Type {contentType}", nameof(contentType));
            }

            return(new Property(codename, null)
            {
                Identifier = TextHelpers.GetValidPascalCaseIdentifierName(codename),
                TypeName = contentTypeToTypeName[contentType],
            });
        }
Example #2
0
        public string GenerateCode()
        {
            var usings = new[]
            {
                SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System")),
                SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Collections.Generic")),
                SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("KenticoCloud.Delivery"))
            };

            var properties = ClassDefinition.Properties.Select(element =>
                                                               SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName(element.TypeName), element.Identifier)
                                                               .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                                               .AddAccessorListAccessors(
                                                                   SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                                                                   .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                                                                   SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                                                                   .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                                                                   )
                                                               ).ToArray();

            var classCodenameConstant = SyntaxFactory.FieldDeclaration(
                SyntaxFactory.VariableDeclaration(
                    SyntaxFactory.ParseTypeName("string"),
                    SyntaxFactory.SeparatedList(new[] {
                SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.Identifier("Codename"),
                    null,
                    SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(ClassDefinition.Codename)))
                    )
            })
                    )
                )
                                        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                        .AddModifiers(SyntaxFactory.Token(SyntaxKind.ConstKeyword));

            var propertyCodenameConstants = ClassDefinition.PropertyCodenameConstants.Select(element =>
                                                                                             SyntaxFactory.FieldDeclaration(
                                                                                                 SyntaxFactory.VariableDeclaration(
                                                                                                     SyntaxFactory.ParseTypeName("string"),
                                                                                                     SyntaxFactory.SeparatedList(new[] {
                SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.Identifier($"{TextHelpers.GetValidPascalCaseIdentifierName(element.Name)}Codename"),
                    null,
                    SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(element.Codename)))
                    )
            })
                                                                                                     )
                                                                                                 )
                                                                                             .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                                                                             .AddModifiers(SyntaxFactory.Token(SyntaxKind.ConstKeyword))
                                                                                             ).ToArray();

            var classDeclaration = SyntaxFactory.ClassDeclaration(ClassDefinition.ClassName)
                                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword))
                                   .AddMembers(classCodenameConstant)
                                   .AddMembers(propertyCodenameConstants)
                                   .AddMembers(properties);

            var description = SyntaxFactory.Comment(
                @"// This code was generated by a cloud-generators-net tool 
// (see https://github.com/Kentico/cloud-generators-net).
// 
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. 
// For further modifications of the class, create a separate file with the partial class." + Environment.NewLine + Environment.NewLine
                );

            CompilationUnitSyntax cu = SyntaxFactory.CompilationUnit()
                                       .AddUsings(usings)
                                       .AddMembers(
                SyntaxFactory.NamespaceDeclaration(SyntaxFactory.IdentifierName(Namespace))
                .AddMembers(classDeclaration)
                )
                                       .WithLeadingTrivia(description);

            AdhocWorkspace cw = new AdhocWorkspace();

            return(Formatter.Format(cu, cw).ToFullString());
        }
 public ClassDefinition(string codeName)
 {
     ClassName = TextHelpers.GetValidPascalCaseIdentifierName(codeName);
     Codename  = codeName;
 }
 public Property(string codename, string typeName)
 {
     Identifier = TextHelpers.GetValidPascalCaseIdentifierName(codename);
     TypeName   = typeName;
 }