Skip to content

Commit 04d1496

Browse files
authored
Merge pull request #9 from contentstack/modular_with_global
Autoload JsonConverter support
2 parents 40a1d5f + d85fb89 commit 04d1496

8 files changed

Lines changed: 136 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
### Version: 0.2.3
3+
#### Date: Aug-12-2020
4+
- DisplayNameAttribute added for Modular block Enum
5+
- Skip limit functionality added to support Stack with more than 100 Content Type and Global Field
6+
- Contentstack Response Class added
7+
- Modular block converter to implement CSJsonConverterAttribute to autoload converters
8+
29
### Version: 0.2.2
310
#### Date: June-17-2020
411
- Modular block with Global field

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Contentstack
3+
Copyright (c) 2020 Contentstack
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,25 @@ To create classes with namespace run following command:
4141
contentstack.model.generator -a <stack_api_key> -d <delivery_token> -n YourProject.Models
4242
```
4343

44+
### MIT License
4445

46+
Copyright (c) 2012-2020 Contentstack
4547

48+
Permission is hereby granted, free of charge, to any person obtaining a copy
49+
of this software and associated documentation files (the "Software"), to deal
50+
in the Software without restriction, including without limitation the rights
51+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52+
copies of the Software, and to permit persons to whom the Software is
53+
furnished to do so, subject to the following conditions:
4654

55+
The above copyright notice and this permission notice shall be included in all
56+
copies or substantial portions of the Software.
57+
58+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
64+
SOFTWARE.
4765

48-
##

contentstack.model.generator/LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright © 2012-2019 Contentstack. All Rights Reserved
3+
Copyright © 2012-2020 Contentstack. All Rights Reserved
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using contentstack.model.generator.Model;
4+
5+
namespace Contentstack.Model.Generator.Model
6+
{
7+
public class ContentstackResponse
8+
{
9+
public List<Contenttype> listContentTypes;
10+
11+
public int Count;
12+
13+
public ContentstackResponse()
14+
{
15+
16+
}
17+
}
18+
}

contentstack.model.generator/ModelGenerator.cs

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using contentstack.CMA;
1111
using contentstack.model.generator.Model;
12+
using Contentstack.Model.Generator.Model;
1213
using McMaster.Extensions.CommandLineUtils;
1314
using Newtonsoft.Json;
1415
using 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
5758
using Newtonsoft.Json.Converters;
5859
using 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(" {");

contentstack.model.generator/contentstack.model.generator.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<Copyright>Copyright © 2012-2020 Contentstack. All Rights Reserved</Copyright>
88
<NeutralLanguage>en-US</NeutralLanguage>
99
<PackageLicense>https://github.com/contentstack/contentstack-model-generator/blob/master/LICENSE</PackageLicense>
10-
<Owners>Contentstack</Owners>
10+
<Owners>Contentstacks</Owners>
1111
<PackageProjectUrl>https://github.com/contentstack/contentstack-model-generator.git</PackageProjectUrl>
1212
<PackAsTool>true</PackAsTool>
1313
<PackageOutputPath>./nupkg</PackageOutputPath>
1414
<PackOnBuild>true</PackOnBuild>
15-
<PackageVersion>0.2.2</PackageVersion>
15+
<PackageVersion>0.2.3</PackageVersion>
1616
<Authors>Contentstack</Authors>
17-
<ReleaseVersion>0.2.2</ReleaseVersion>
17+
<ReleaseVersion>0.2.3</ReleaseVersion>
1818
<RootNamespace>Contentstack.Model.Generator</RootNamespace>
1919
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2020
<PackageReleaseNotes>Global field support in modular block</PackageReleaseNotes>

contentstack.model.generator/contentstack.model.generator.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ Global
2929
$4.scope = text/plain
3030
$0.StandardHeader = $5
3131
$0.VersionControlPolicy = $6
32-
version = 0.2.2
32+
version = 0.2.3
3333
EndGlobalSection
3434
EndGlobal

0 commit comments

Comments
 (0)