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
7
2
{
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
9
9
{
10
10
private const string CustomActions = "CustomActions" ;
11
11
private const string Entities = "Entities" ;
12
12
private const string OptionSets = "OptionSets" ;
13
+ protected ITrace _trace ;
14
+
15
+ public SourceCodeSplitter ( ITrace trace )
16
+ {
17
+ _trace = trace ;
18
+ }
13
19
14
- public void ProcessSourceCode ( string destinationDirectoryPath , string sourceCode , string typeNamespace )
20
+ public void WriteToSeparateFiles ( string destinationDirectoryPath , string sourceCode , string typeNamespace )
15
21
{
16
22
if ( ! destinationDirectoryPath . Trim ( ) . EndsWith ( "\\ " ) )
17
23
{
18
24
destinationDirectoryPath = $ "{ destinationDirectoryPath . Trim ( ) } \\ ";
19
25
}
20
26
27
+ var regex = new SourceCodeTypeExtractor ( ) ;
28
+ var result = regex . ExtractTypes ( sourceCode ) ;
21
29
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 ( ) ;
27
32
28
33
CreateDirectories ( destinationDirectoryPath , new List < string > ( ) { CustomActions , Entities , OptionSets } ) ;
29
34
30
35
foreach ( var entry in enumDeclarations )
31
36
{
32
- WriteTypeContentToFile ( entry . Name , typeNamespace , $ "{ destinationDirectoryPath } { OptionSets } \\ ", entry . Content ) ;
37
+ WriteTypeContentToFile ( entry . Name , typeNamespace , $ "{ destinationDirectoryPath } { OptionSets } \\ ", entry . Content ) ;
33
38
}
34
39
35
40
foreach ( var entry in classDeclarations )
@@ -48,16 +53,16 @@ public void ProcessSourceCode(string destinationDirectoryPath, string sourceCode
48
53
destination = $ "{ destinationDirectoryPath } { Entities } \\ ";
49
54
break ;
50
55
}
51
-
52
- WriteTypeContentToFile ( entry . Name , typeNamespace , destination ,
53
- entry . Content ) ;
54
- }
56
+
57
+ WriteTypeContentToFile ( entry . Name , typeNamespace , destination , entry . Content ) ;
58
+ }
55
59
}
56
60
57
-
58
61
private void WriteTypeContentToFile ( string typeName , string typeNamespace , string directoryPath , string content )
59
62
{
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 ) )
61
66
{
62
67
var result = streamWriter . WriteAsync ( GenerateTypeText ( typeNamespace , content ) ) ;
63
68
result . Wait ( ) ;
@@ -67,16 +72,16 @@ private void WriteTypeContentToFile(string typeName, string typeNamespace, strin
67
72
private string GenerateTypeText ( string typeNamespace , string content )
68
73
{
69
74
var stringBuilder = new StringBuilder ( ) ;
70
-
71
- if ( ! string . IsNullOrWhiteSpace ( typeNamespace ) )
75
+ var namespaceContent = ! string . IsNullOrWhiteSpace ( typeNamespace ) ;
76
+ if ( namespaceContent )
72
77
{
73
- stringBuilder . AppendLine ( $ "public namespace { typeNamespace } ") ;
78
+ stringBuilder . AppendLine ( $ "namespace { typeNamespace } ") ;
74
79
stringBuilder . AppendLine ( "{" ) ;
75
80
}
76
81
77
82
stringBuilder . AppendLine ( content ) ;
78
83
79
- if ( ! string . IsNullOrWhiteSpace ( typeNamespace ) )
84
+ if ( namespaceContent )
80
85
{
81
86
stringBuilder . AppendLine ( "}" ) ;
82
87
}
0 commit comments