Skip to content

Commit 82c52a6

Browse files
committed
SourceCodeTransformation now makes enums internal and contains logging messages to the console
1 parent e4edd4a commit 82c52a6

4 files changed

Lines changed: 38 additions & 10 deletions

File tree

Code/Light.GuardClauses.SourceCodeTransformation/Light.GuardClauses.SourceCodeTransformation.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.1" />
1515
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
1616
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.8.2" />
17-
<PackageReference Include="Light.Undefine" Version="0.1.1" />
17+
<PackageReference Include="Light.Undefine" Version="0.2.0" />
1818
</ItemGroup>
1919

2020
<ItemGroup>
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.Extensions.Configuration;
34

45
namespace Light.GuardClauses.SourceCodeTransformation
56
{
67
public static class Program
78
{
8-
public static async Task Main(string[] args)
9+
public static async Task<int> Main(string[] args)
910
{
11+
Console.WriteLine("Creating configuration...");
1012
var configuration =
1113
new ConfigurationBuilder()
1214
.AddJsonFile("settings.json", true)
@@ -16,7 +18,19 @@ public static async Task Main(string[] args)
1618
var mergeOptionsBuilder = new SourceFileMergeOptions.Builder();
1719
configuration.Bind(mergeOptionsBuilder);
1820
var merger = new SourceFileMerger(mergeOptionsBuilder.Build());
19-
await merger.CreateSingleSourceFileAsync();
21+
try
22+
{
23+
Console.WriteLine("Merging source files...");
24+
await merger.CreateSingleSourceFileAsync();
25+
Console.WriteLine("Source file export completed successfully.");
26+
return 0;
27+
}
28+
catch (Exception ex)
29+
{
30+
Console.WriteLine("Exception occured during source file export:");
31+
Console.WriteLine(ex);
32+
return -1;
33+
}
2034
}
2135
}
2236
}

Code/Light.GuardClauses.SourceCodeTransformation/SourceFileMerger.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ public async Task CreateSingleSourceFileAsync()
2626
var stringBuilder = new StringBuilder();
2727

2828
if (_options.IncludeVersionComment)
29+
{
30+
Console.WriteLine("Appending version header...");
2931
stringBuilder.AppendLine("/* ------------------------------")
3032
.AppendLine($" Light.GuardClauses {typeof(SourceFileMerger).Assembly.GetName().Version.ToString(3)}")
3133
.AppendLine(" ------------------------------")
3234
.AppendLine();
33-
35+
}
36+
37+
Console.WriteLine("Creating default file layout...");
3438
stringBuilder.AppendLineIf(!_options.IncludeVersionComment, "/*")
3539
.AppendLine($@"License information for Light.GuardClauses
3640
@@ -76,8 +80,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
7680
{(_options.IncludeJetBrainsAnnotationsUsing ? "using JetBrains.Annotations;" + Environment.NewLine : string.Empty)}using {_options.BaseNamespace}.Exceptions;
7781
using {_options.BaseNamespace}.FrameworkExtensions;
7882
79-
// ReSharper disable StaticMemberInGenericType
80-
8183
namespace {_options.BaseNamespace}
8284
{{
8385
}}
@@ -146,6 +148,7 @@ namespace JetBrains.Annotations
146148
.ToDictionary(f => f.Name);
147149

148150
// Start with Check.CommonAssertions before all other files to prepare the Check class
151+
Console.WriteLine("Merging source files of the Check class...");
149152
var currentFile = allSourceFiles["Check.CommonAssertions.cs"];
150153

151154
var sourceSyntaxTree = CSharpSyntaxTree.ParseText(await currentFile.ReadContentAsync());
@@ -155,6 +158,7 @@ namespace JetBrains.Annotations
155158
checkClassDeclaration = checkClassDeclaration.WithModifiers(checkClassDeclaration.Modifiers.Remove(checkClassDeclaration.Modifiers.First(token => token.Kind() == SyntaxKind.PartialKeyword)));
156159

157160
// Process all other files
161+
Console.WriteLine("Merging remaining files...");
158162
foreach (var fileName in allSourceFiles.Keys)
159163
{
160164
if (fileName == "Check.CommonAssertions.cs")
@@ -227,10 +231,12 @@ namespace JetBrains.Annotations
227231
// Make types internal if necessary
228232
if (_options.ChangePublicTypesToInternalTypes)
229233
{
234+
Console.WriteLine("Types are changed from public to internal...");
230235
var changedTypeDeclarations = new Dictionary<BaseTypeDeclarationSyntax, BaseTypeDeclarationSyntax>();
231236

232237
foreach (var typeDeclaration in targetRoot.DescendantNodes().Where(node => node.Kind() == SyntaxKind.ClassDeclaration ||
233-
node.Kind() == SyntaxKind.StructDeclaration)
238+
node.Kind() == SyntaxKind.StructDeclaration ||
239+
node.Kind() == SyntaxKind.EnumDeclaration)
234240
.Cast<BaseTypeDeclarationSyntax>())
235241
{
236242
var publicModifier = typeDeclaration.Modifiers[0];
@@ -245,6 +251,9 @@ namespace JetBrains.Annotations
245251

246252
else if (typeDeclaration is StructDeclarationSyntax structDeclaration)
247253
changedTypeDeclarations[structDeclaration] = structDeclaration.WithModifiers(adjustedModifiers);
254+
255+
else if (typeDeclaration is EnumDeclarationSyntax enumDeclaration)
256+
changedTypeDeclarations[enumDeclaration] = enumDeclaration.WithModifiers(adjustedModifiers);
248257
}
249258

250259
targetRoot = targetRoot.ReplaceNodes(changedTypeDeclarations.Keys, (originalNode, _) => changedTypeDeclarations[originalNode]);
@@ -253,11 +262,16 @@ namespace JetBrains.Annotations
253262
// Remove preprocessor directives if necessary
254263
var targetFileContent = targetRoot.ToFullString();
255264
if (_options.RemovePreprocessorDirectives)
265+
{
266+
Console.WriteLine("Preprocessor directives are removed...");
256267
targetFileContent = new UndefineTransformation().Undefine(targetFileContent, _options.DefinedPreprocessorSymbols).ToString();
268+
}
257269

270+
Console.WriteLine("File is cleaned up...");
258271
targetFileContent = CleanupStep.Cleanup(targetFileContent, _options.RemoveContractAnnotations).ToString();
259272

260273
// Write the target file
274+
Console.WriteLine("File is written to disk...");
261275
await File.WriteAllTextAsync(_options.TargetFile, targetFileContent);
262276
}
263277
}

Code/Light.GuardClauses/EnumInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
using System.Runtime.CompilerServices;
1515
#endif
1616

17-
// ReSharper disable StaticMemberInGenericType
18-
1917
namespace Light.GuardClauses
2018
{
2119
/// <summary>
@@ -28,6 +26,8 @@ public static class EnumInfo<T> where T : Enum
2826
, IConvertible
2927
#endif
3028
{
29+
// ReSharper disable StaticMemberInGenericType
30+
3131
/// <summary>
3232
/// Gets the value indicating whether the enum type is marked with the flags attribute.
3333
/// </summary>

0 commit comments

Comments
 (0)