Skip to content

Commit 55c7bc1

Browse files
scottdurowjanis-veinbergs
authored andcommitted
Changed OneTypePerFile to be consitent with other config fields
Renamed CrmSvcUtilHelper folder to CrmSvcUtil Changed classes to be descriptive names: SourceCodeManipulator->SourceCodeSplitter SourceCodeRegexUtility -> SourceCodeTypeExtractor Added tracing Updated nuget spec to reference 9.0.0.7 CrmSvcUtil Added oneTypePerFile to sample spkl.json
1 parent c886809 commit 55c7bc1

13 files changed

+270
-256
lines changed

spkl/SparkleXrm.Tasks.Tests/EarlyBoundTypes.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void TestGenerateGlobalOptionsets_OneTypePerFile()
7272
generateGlobalOptionsets = true,
7373
entities ="socialprofile,socialactivity",
7474
filename="entities.cs",
75-
OneTypePerFile = true
75+
oneTypePerFile = true
7676
}
7777
},
7878
filePath = tempFolder
@@ -156,7 +156,7 @@ public void TestNotGeneratingGlobalOptionsets_OneTypePerFile()
156156
generateGlobalOptionsets = false,
157157
entities ="socialprofile,socialactivity",
158158
filename="entities.cs",
159-
OneTypePerFile = true
159+
oneTypePerFile = true
160160
}
161161
},
162162
filePath = tempFolder
@@ -241,7 +241,7 @@ public void TestNotGeneratingAnyOptionsets_OneTypePerFile()
241241
generateGlobalOptionsets = false,
242242
entities ="socialprofile,socialactivity",
243243
filename="entities.cs",
244-
OneTypePerFile = true
244+
oneTypePerFile = true
245245
}
246246
},
247247
filePath = tempFolder

spkl/SparkleXrm.Tasks.Tests/SparkleXrm.Tasks.Tests.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.22.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5757
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.22.302111727\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
5858
</Reference>
59-
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
59+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
60+
<Private>False</Private>
61+
</Reference>
6062
<Reference Include="Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6163
<HintPath>..\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.0.7\lib\net452\Microsoft.Xrm.Sdk.dll</HintPath>
6264
</Reference>

spkl/SparkleXrm.Tasks/Config/EarlyBoundTypeConfig.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public class EarlyBoundTypeConfig
1717
public string filename;
1818
public string classNamespace;
1919
public string serviceContextName;
20-
public bool OneTypePerFile { get; set; }
20+
public bool oneTypePerFile;
2121
}
22-
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace SparkleXrm.Tasks.CrmSvcUtil
2+
{
3+
public enum ContainerType
4+
{
5+
/// <summary>
6+
/// Class containers to be split into different files
7+
/// </summary>
8+
ClassContainer,
9+
10+
/// <summary>
11+
/// Enum containers to be split into different files
12+
/// </summary>
13+
EnumContainer,
14+
15+
/// <summary>
16+
/// OrganizationServiceContext containers to be split into different files
17+
/// </summary>
18+
OrganizationServiceContextContainer,
19+
20+
/// <summary>
21+
/// OrganizationRequest containers to be split into different files
22+
/// </summary>
23+
OrganizationRequestContainer,
24+
25+
/// <summary>
26+
/// OrganizationResponse containers to be split into different files
27+
/// </summary>
28+
OrganizationResponseContainer
29+
}
30+
}

spkl/SparkleXrm.Tasks/CrmSvcUtilHelper/SourceCodeManipulator.cs renamed to spkl/SparkleXrm.Tasks/CrmSvcUtil/SourceCodeSplitter.cs

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
using System.Collections.Generic;
2-
using System.IO;
3-
using System.Linq;
4-
using System.Text;
5-
6-
namespace SparkleXrm.Tasks.CrmSvcUtilHelper
1+
namespace SparkleXrm.Tasks.CrmSvcUtil
72
{
8-
public class SourceCodeManipulator
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
8+
public class SourceCodeSplitter
99
{
1010
private const string CustomActions = "CustomActions";
1111
private const string Entities = "Entities";
1212
private const string OptionSets = "OptionSets";
13+
protected ITrace _trace;
14+
15+
public SourceCodeSplitter(ITrace trace)
16+
{
17+
_trace = trace;
18+
}
1319

14-
public void ProcessSourceCode(string destinationDirectoryPath, string sourceCode, string typeNamespace)
20+
public void WriteToSeparateFiles(string destinationDirectoryPath, string sourceCode, string typeNamespace)
1521
{
1622
if (!destinationDirectoryPath.Trim().EndsWith("\\"))
1723
{
1824
destinationDirectoryPath = $"{destinationDirectoryPath.Trim()}\\";
1925
}
2026

27+
var regex = new SourceCodeTypeExtractor();
28+
var result = regex.ExtractTypes(sourceCode);
2129

22-
var scru = new SourceCodeRegexUtility();
23-
var result = scru.ExtractTypes(sourceCode);
24-
25-
var enumDeclarations = result.Where(x => x.Type == ContainerType.EnumContainer).ToList();
26-
var classDeclarations = result.Where(x => x.Type != ContainerType.EnumContainer).ToList();
30+
var enumDeclarations = result.Where(x => x.Type == ContainerType.EnumContainer).ToList();
31+
var classDeclarations = result.Where(x => x.Type != ContainerType.EnumContainer).ToList();
2732

2833
CreateDirectories(destinationDirectoryPath, new List<string>() { CustomActions, Entities, OptionSets });
2934

3035
foreach (var entry in enumDeclarations)
3136
{
32-
WriteTypeContentToFile(entry.Name, typeNamespace, $"{destinationDirectoryPath}{OptionSets}\\", entry.Content);
37+
WriteTypeContentToFile(entry.Name, typeNamespace, $"{destinationDirectoryPath}{OptionSets}\\", entry.Content);
3338
}
3439

3540
foreach (var entry in classDeclarations)
@@ -48,16 +53,16 @@ public void ProcessSourceCode(string destinationDirectoryPath, string sourceCode
4853
destination = $"{destinationDirectoryPath}{Entities}\\";
4954
break;
5055
}
51-
52-
WriteTypeContentToFile(entry.Name, typeNamespace, destination,
53-
entry.Content);
54-
}
56+
57+
WriteTypeContentToFile(entry.Name, typeNamespace, destination, entry.Content);
58+
}
5559
}
5660

57-
5861
private void WriteTypeContentToFile(string typeName, string typeNamespace, string directoryPath, string content)
5962
{
60-
using (var streamWriter = new StreamWriter($"{directoryPath}{typeName}.cs", false))
63+
var fileName = $"{directoryPath}{typeName}.cs";
64+
_trace.WriteLine($"Writing code file {fileName}");
65+
using (var streamWriter = new StreamWriter(fileName, false))
6166
{
6267
var result = streamWriter.WriteAsync(GenerateTypeText(typeNamespace, content));
6368
result.Wait();
@@ -67,16 +72,16 @@ private void WriteTypeContentToFile(string typeName, string typeNamespace, strin
6772
private string GenerateTypeText(string typeNamespace, string content)
6873
{
6974
var stringBuilder = new StringBuilder();
70-
71-
if (!string.IsNullOrWhiteSpace(typeNamespace))
75+
var namespaceContent = !string.IsNullOrWhiteSpace(typeNamespace);
76+
if (namespaceContent)
7277
{
73-
stringBuilder.AppendLine($"public namespace {typeNamespace}");
78+
stringBuilder.AppendLine($"namespace {typeNamespace}");
7479
stringBuilder.AppendLine("{");
7580
}
7681

7782
stringBuilder.AppendLine(content);
7883

79-
if (!string.IsNullOrWhiteSpace(typeNamespace))
84+
if (namespaceContent)
8085
{
8186
stringBuilder.AppendLine("}");
8287
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace SparkleXrm.Tasks.CrmSvcUtil
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text.RegularExpressions;
6+
7+
public class SourceCodeTypeExtractor
8+
{
9+
private const string ClassPattern = @"([a-zA-Z0-9\(\"",\s\.\)\]\s\n\[:])+public\spartial[a-zA-Z0-9\s:\.,_]+{(?:[^{}]|(?<open>{)|(?<-open>}))+(?(open)(?!))}";
10+
private const string EnumPattern = @"([a-zA-Z0-9\(\"",\s\.\)\]\s\n\[:])+public\senum[a-zA-Z0-9\s_]+{(?:[^{}]|(?<open>{)|(?<-open>}))+(?(open)(?!))}";
11+
12+
public List<TypeContainer> ExtractTypes(string input)
13+
{
14+
var classMatches = new Regex(ClassPattern).Matches(input);
15+
var result = (from Match match in classMatches
16+
select new TypeContainer(ContainerType.ClassContainer, match.Value))
17+
.ToList();
18+
19+
var enumMatches = new Regex(EnumPattern).Matches(input);
20+
result.AddRange(from Match match in enumMatches
21+
select new TypeContainer(ContainerType.EnumContainer, match.Value));
22+
23+
return result;
24+
}
25+
}
26+
}

spkl/SparkleXrm.Tasks/CrmSvcUtilHelper/TypeContainer.cs renamed to spkl/SparkleXrm.Tasks/CrmSvcUtil/TypeContainer.cs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
using System;
2-
using System.Linq;
3-
using System.Text.RegularExpressions;
4-
5-
namespace SparkleXrm.Tasks.CrmSvcUtilHelper
1+
namespace SparkleXrm.Tasks.CrmSvcUtil
62
{
3+
using System;
4+
using System.Linq;
5+
using System.Text.RegularExpressions;
6+
77
public class TypeContainer
88
{
9-
const string ClassName = @"public(\s)+partial(\s)+class(\s)+[a-zA-Z0-9_]+";
10-
const string EnumName = @"public(\s)+enum(\s)+[a-zA-Z0-9_]+";
9+
private const string ClassName = @"public(\s)+partial(\s)+class(\s)+[a-zA-Z0-9_]+";
10+
private const string EnumName = @"public(\s)+enum(\s)+[a-zA-Z0-9_]+";
11+
1112
public TypeContainer(ContainerType type, string content)
1213
{
1314
Type = type;
1415
Content = content;
1516
ComputeName();
1617
}
18+
1719
public string Content { get; }
20+
1821
public string Name { get; set; }
22+
1923
public ContainerType Type { get; private set; }
2024

2125
private void ComputeName()

spkl/SparkleXrm.Tasks/CrmSvcUtilHelper/ContainerType.cs

-11
This file was deleted.

spkl/SparkleXrm.Tasks/CrmSvcUtilHelper/SourceCodeRegexUtility.cs

-38
This file was deleted.

spkl/SparkleXrm.Tasks/SparkleXrm.Tasks.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@
100100
<ItemGroup>
101101
<Compile Include="componenttype.cs" />
102102
<Compile Include="Config\IConfigFileService.cs" />
103-
<Compile Include="CrmSvcUtilHelper\ContainerType.cs" />
104-
<Compile Include="CrmSvcUtilHelper\SourceCodeManipulator.cs" />
105-
<Compile Include="CrmSvcUtilHelper\SourceCodeRegexUtility.cs" />
106-
<Compile Include="CrmSvcUtilHelper\TypeContainer.cs" />
103+
<Compile Include="CrmSvcUtil\ContainerType.cs" />
104+
<Compile Include="CrmSvcUtil\SourceCodeSplitter.cs" />
105+
<Compile Include="CrmSvcUtil\SourceCodeTypeExtractor.cs" />
106+
<Compile Include="CrmSvcUtil\TypeContainer.cs" />
107107
<Compile Include="IDirectoryService.cs" />
108108
<Compile Include="ServiceLocator.cs" />
109109
<Compile Include="CodeParser.cs" />

0 commit comments

Comments
 (0)