Skip to content

Commit 690f3a6

Browse files
committed
Properties and classes PascalCase formattings
1 parent 4e6a4e0 commit 690f3a6

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

Compentio.SourceConfig/Compentio.SourceConfig.Generator/Compentio.SourceConfig.csproj

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@
1515
<None Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
1616
<None Include="$(PKGSystem_Text_Encodings_Web)\lib\netstandard2.0\System.Text.Encodings.Web.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
1717
</ItemGroup>
18-
19-
<PropertyGroup>
20-
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
21-
</PropertyGroup>
22-
23-
<Target Name="GetDependencyTargetPaths">
24-
<ItemGroup>
25-
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" IncludeRuntimeDependency="false" />
26-
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Text_Encodings_Web)\lib\netstandard2.0\System.Text.Encodings.Web.dll" IncludeRuntimeDependency="false" />
27-
</ItemGroup>
28-
</Target>
2918

3019
<ItemGroup>
3120
<None Include="..\..\Compentio.Assets\Logo.png">
@@ -39,7 +28,7 @@
3928
<LangVersion>9.0</LangVersion>
4029
<IsRoslynComponent>true</IsRoslynComponent>
4130
<Nullable>enable</Nullable>
42-
<Version>1.0.9</Version>
31+
<Version>1.0.10</Version>
4332
<Description>Code generator for objects that are based on *.json configuration files: when developer adds some file or new properties to existng json configuration file the POCO objects for this configuration generated.</Description>
4433
<PackageIcon>Logo.png</PackageIcon>
4534
<PackageIconUrl />

Compentio.SourceConfig/Compentio.SourceConfig.Generator/Context/ConfigurationFileContext.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Compentio.SourceConfig.Extensions;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -99,11 +100,7 @@ public bool ShouldBeMerged(string filePath)
99100
private string FormatClassName(string className)
100101
{
101102
var originName = className.Split('.').Where(item => !string.IsNullOrWhiteSpace(item)).First();
102-
103-
if (string.IsNullOrWhiteSpace(originName))
104-
return string.Concat(className[0].ToString().ToUpper(), className.Substring(1));
105-
106-
return string.Concat(originName[0].ToString().ToUpper(), originName.Substring(1)).Replace("settings", "Settings");
103+
return originName.FromatClassName();
107104
}
108105

109106
private Dictionary<string, object> Deserialize(string content)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Compentio.SourceConfig.Extensions
2+
{
3+
static class FormatingExtensions
4+
{
5+
public static string FromatClassName(this string input)
6+
{
7+
return ToPascalCase(input).Replace("settings", "Settings");
8+
}
9+
10+
public static string FromatPropertyName(this string input)
11+
{
12+
var underscore = "_";
13+
var tmp = input
14+
.Replace(".", underscore)
15+
.Replace("$", underscore);
16+
17+
if (tmp[0].Equals(underscore))
18+
tmp.Replace(underscore, string.Empty);
19+
20+
return ToPascalCase(tmp);
21+
}
22+
23+
private static string ToPascalCase(string input)
24+
{
25+
if (string.IsNullOrWhiteSpace(input))
26+
return string.Empty;
27+
28+
if (input.Length == 1)
29+
return input.ToUpper();
30+
31+
return string.Concat(input[0].ToString().ToUpper(), input.Substring(1));
32+
}
33+
}
34+
}

Compentio.SourceConfig/Compentio.SourceConfig.Generator/Generators/ICodeGenerator.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Compentio.SourceConfig.Context;
2+
using Compentio.SourceConfig.Extensions;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.CSharp;
45
using Microsoft.CodeAnalysis.Text;
@@ -66,18 +67,18 @@ public class {_configurationFileContext.ClassName}
6667
if (value is JsonElement element && element.ValueKind == JsonValueKind.Array)
6768
{
6869
var propertyType = GetPropertyTypeName(element.EnumerateArray().FirstOrDefault());
69-
sourceBuilder.Append($"public IEnumerable<{propertyType}> {FormatPropertyName(key)} {{ get; set; }}");
70+
sourceBuilder.Append($"public IEnumerable<{propertyType}> {key.FromatPropertyName()} {{ get; set; }}");
7071
}
7172
else
7273
{
73-
sourceBuilder.Append($"public string {FormatPropertyName(key)} {{ get; set; }}");
74+
sourceBuilder.Append($"public string {key.FromatPropertyName()} {{ get; set; }}");
7475
}
7576
}
7677

7778
foreach (var item in _configurationFileContext.ConfigClasses)
7879
{
7980
var key = item.Key;
80-
sourceBuilder.Append($"public {FormatPropertyName(key)} {FormatPropertyName(key)}{{ get; set; }}");
81+
sourceBuilder.Append($"public {key.FromatPropertyName()} {key.FromatPropertyName()}{{ get; set; }}");
8182
}
8283

8384
sourceBuilder.Append("}");
@@ -94,14 +95,15 @@ private void BuildConfigClass(KeyValuePair<string, object> classInfo, StringBuil
9495
{
9596
var nestedClasses = new StringBuilder();
9697

97-
stringBuilder.Append($"public class {classInfo.Key}");
98+
stringBuilder.Append("[ExcludeFromCodeCoverage]");
99+
stringBuilder.Append($"public class {classInfo.Key.FromatClassName()}");
98100
stringBuilder.Append("{");
99101

100102
foreach (var item in (Dictionary<string, object>)classInfo.Value)
101103
{
102104
if (item.Value is Dictionary<string, object>)
103105
{
104-
stringBuilder.Append($"public {item.Key} {FormatPropertyName(item.Key)} {{ get; set; }}");
106+
stringBuilder.Append($"public {item.Key} {item.Key.FromatPropertyName()} {{ get; set; }}");
105107
BuildConfigClass(item, nestedClasses);
106108
}
107109
else
@@ -110,11 +112,11 @@ private void BuildConfigClass(KeyValuePair<string, object> classInfo, StringBuil
110112
var propertyType = GetPropertyTypeName(prop);
111113
if (prop.ValueKind == JsonValueKind.Array)
112114
{
113-
stringBuilder.Append($"public IEnumerable<{propertyType}> {FormatPropertyName(item.Key)} {{ get; set; }}");
115+
stringBuilder.Append($"public IEnumerable<{propertyType}> {item.Key.FromatPropertyName()} {{ get; set; }}");
114116
}
115117
else
116118
{
117-
stringBuilder.Append($"public {propertyType} {FormatPropertyName(item.Key)} {{ get; set; }}");
119+
stringBuilder.Append($"public {propertyType} {item.Key.FromatPropertyName()} {{ get; set; }}");
118120
}
119121
}
120122
}
@@ -132,13 +134,5 @@ private string GetPropertyTypeName(JsonElement value)
132134
_ => "string",
133135
};
134136
}
135-
136-
private string FormatPropertyName(string origin)
137-
{
138-
var underscore = "_";
139-
return origin
140-
.Replace(".", underscore)
141-
.Replace("$", underscore);
142-
}
143137
}
144138
}

0 commit comments

Comments
 (0)