99using System . Threading . Tasks ;
1010using contentstack . CMA ;
1111using contentstack . model . generator . Model ;
12+ using Contentstack . Model . Generator . Model ;
1213using McMaster . Extensions . CommandLineUtils ;
1314using Newtonsoft . Json ;
1415using Newtonsoft . Json . Linq ;
@@ -38,13 +39,13 @@ public class ModelGenerator
3839 [ Option ( CommandOptionType . SingleValue , Description = "The Modular block Class Prefix." ) ]
3940 public string ModularBlockPrefix { get ; } = "MB" ;
4041
41- [ Option ( CommandOptionType . SingleValue , Description = "The Modular block Class Prefix." ) ]
42+ [ Option ( CommandOptionType . SingleValue , Description = "The Group Class Prefix." ) ]
4243 public string GroupPrefix { get ; } = "Group" ;
4344
4445 [ Option ( CommandOptionType . SingleValue , Description = "Path to the file or directory to create files in" ) ]
4546 public string Path { get ; }
4647
47- [ VersionOption ( "0.2.1 " ) ]
48+ [ VersionOption ( "0.2.3 " ) ]
4849 public bool Version { get ; }
4950
5051 private string _templateStart = @"using System;
@@ -57,7 +58,7 @@ public class ModelGenerator
5758using Newtonsoft.Json.Converters;
5859using Newtonsoft.Json.Linq;" ;
5960
60- private List < Contenttype > _contentTypes ;
61+ private List < Contenttype > _contentTypes = new List < Contenttype > ( ) ;
6162 private Dictionary < string , List < Contenttype > > _modularBlocks = new Dictionary < string , List < Contenttype > > ( ) ;
6263
6364 public async Task < int > OnExecute ( CommandLineApplication app , IConsole console )
@@ -73,18 +74,32 @@ public async Task<int> OnExecute(CommandLineApplication app, IConsole console)
7374
7475 try
7576 {
77+
7678 Console . WriteLine ( $ "Fetching Content Types from { ApiKey } ") ;
77- var contentType = await client . GetContentTypes ( ) ;
78- var ContentTypeJson = JsonConvert . SerializeObject ( contentType ) ;
79- _contentTypes = JsonConvert . DeserializeObject < List < Contenttype > > ( ContentTypeJson ) ;
80- Console . WriteLine ( $ "Found { _contentTypes . Count } content types.") ;
79+ var totalCount = await getContentTypes ( client , 0 ) ;
80+ var skip = 100 ;
81+ Console . WriteLine ( $ "Found { totalCount } Content Types .") ;
82+
83+ while ( totalCount > skip )
84+ {
85+ Console . WriteLine ( $ "{ skip } Content Types Fetched.") ;
86+ totalCount = await getContentTypes ( client , skip ) ;
87+ skip += 100 ;
88+ }
89+ Console . WriteLine ( $ "Total { totalCount } Content Types fetched.") ;
8190
8291 Console . WriteLine ( $ "Fetching Global Fields from { ApiKey } ") ;
83- var globalField = await client . GetGlobalFields ( ) ;
84- var globalFieldsJson = JsonConvert . SerializeObject ( globalField ) ;
85- var globalFields = JsonConvert . DeserializeObject < List < Contenttype > > ( globalFieldsJson ) ;
86- _contentTypes . AddRange ( globalFields ) ;
87- Console . WriteLine ( $ "Found { globalFields . Count } Global Fields.") ;
92+ totalCount = await getGlobalFields ( client , 0 ) ;
93+ skip = 100 ;
94+ Console . WriteLine ( $ "Found { totalCount } Global Fields.") ;
95+
96+ while ( totalCount > skip )
97+ {
98+ Console . WriteLine ( $ "{ skip } Global Fields Fetched.") ;
99+ totalCount = await getGlobalFields ( client , skip ) ;
100+ skip += 100 ;
101+ }
102+ Console . WriteLine ( $ "Total { totalCount } Global Fields fetched.") ;
88103 }
89104 catch ( Exception e )
90105 {
@@ -117,6 +132,7 @@ public async Task<int> OnExecute(CommandLineApplication app, IConsole console)
117132 CreateLinkClass ( Namespace , dir ) ;
118133 CreateHelperClass ( Namespace , dir ) ;
119134 CreateStringHelperClass ( Namespace , dir ) ;
135+ CreateDisplayAttributeClass ( Namespace , dir ) ;
120136 foreach ( var contentType in _contentTypes )
121137 {
122138 CreateFile ( FormatClassName ( contentType . Title ) , Namespace , contentType , dir , null , true ) ;
@@ -130,6 +146,19 @@ public async Task<int> OnExecute(CommandLineApplication app, IConsole console)
130146 return Program . OK ;
131147 }
132148
149+ private async Task < int > getContentTypes ( ContentstackClient client , int skip )
150+ {
151+ ContentstackResponse contentstackResponse = await client . GetContentTypes ( skip : skip ) ;
152+ _contentTypes . AddRange ( contentstackResponse . listContentTypes ) ;
153+ return contentstackResponse . Count ;
154+ }
155+
156+ private async Task < int > getGlobalFields ( ContentstackClient client , int skip )
157+ {
158+ ContentstackResponse contentstackResponse = await client . GetGlobalFields ( skip : skip ) ;
159+ _contentTypes . AddRange ( contentstackResponse . listContentTypes ) ;
160+ return contentstackResponse . Count ;
161+ }
133162 private string GetDatatype ( Field field , string contentTypeName )
134163 {
135164 switch ( field . DataType )
@@ -244,6 +273,48 @@ private string GetDataTypeForGroup(Field field, string contentTypeName)
244273 return $ "{ GroupPrefix } { contentTypeName } { FormatClassName ( field . DisplayName ) } ". Replace ( " " , "" ) ;
245274 }
246275
276+ private void CreateDisplayAttributeClass ( string NameSpace , DirectoryInfo directoryInfo )
277+ {
278+ // Create File for DisplayAttribute
279+ string contentstackLinkClass = "DisplayNameAttribute" ;
280+ var file = shouldCreateFile ( contentstackLinkClass , directoryInfo ) ;
281+ if ( file != null )
282+ {
283+ using ( var sw = file . CreateText ( ) )
284+ {
285+ var sb = new StringBuilder ( ) ;
286+ // Adding using at start of file
287+ AddUsingDirectives ( null , sb ) ;
288+
289+ // Creating namespace
290+ AddNameSpace ( $ "{ NameSpace } .{ directoryInfo . Name } ", sb ) ;
291+
292+ sb . AppendLine ( " [AttributeUsage(AttributeTargets.Field)]" ) ;
293+ // Creating Class
294+ sb . AppendLine ( $ " public partial class { contentstackLinkClass } : Attribute") ;
295+ sb . AppendLine ( " {" ) ;
296+ sb . AppendLine ( " private string displayName;" ) ;
297+ sb . AppendLine ( " public string DisplayName" ) ;
298+ sb . AppendLine ( " {" ) ;
299+ sb . AppendLine ( " get" ) ;
300+ sb . AppendLine ( " {" ) ;
301+ sb . AppendLine ( " return displayName;" ) ;
302+ sb . AppendLine ( " }" ) ;
303+ sb . AppendLine ( " }" ) ;
304+
305+ sb . AppendLine ( $ " public { contentstackLinkClass } (string displayName)") ;
306+ sb . AppendLine ( " {" ) ;
307+ sb . AppendLine ( " this.displayName = displayName;" ) ;
308+ sb . AppendLine ( " }" ) ;
309+
310+ // End of namespace and class
311+ AddEnd ( sb ) ;
312+
313+ // write to file
314+ sw . WriteLine ( sb . ToString ( ) ) ;
315+ }
316+ }
317+ }
247318 private void CreateLinkClass ( string NameSpace , DirectoryInfo directoryInfo )
248319 {
249320 // Create File for LinkClass
@@ -790,11 +861,13 @@ private void CreateModularBlockConverter(string nameSpace, string className, Dic
790861 sb . AppendLine ( "using System.Reflection;" ) ;
791862 sb . AppendLine ( "using Newtonsoft.Json.Linq;" ) ;
792863 sb . AppendLine ( "using System.ComponentModel;" ) ;
793-
864+ sb . AppendLine ( "using Contentstack.Core;" ) ;
794865 // Creating namespace
795866 AddNameSpace ( $ "{ nameSpace } .{ directoryInfo . Name } ", sb ) ;
796867 // Creating Enum
797- AddClass ( $ "{ className } Converter : JsonConverter<{ className } >", sb ) ;
868+ var ConverterName = $ "{ className } Converter";
869+ sb . AppendLine ( $ " [CSJsonConverter(\" { ConverterName } \" )]") ;
870+ AddClass ( $ "{ ConverterName } : JsonConverter<{ className } >", sb ) ;
798871
799872 sb . AppendLine ( $ " protected { className } Create(Type objectType, JObject jObject)") ;
800873 sb . AppendLine ( " {" ) ;
0 commit comments