Skip to content

Commit 2170f7f

Browse files
committed
Audit service
1 parent 8c09c39 commit 2170f7f

File tree

7 files changed

+428
-16
lines changed

7 files changed

+428
-16
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file contains a list of the files generated by the Database.tt file.
2+
# Please do not edit this file. It is used to delete files that may get filtered out during the next run.
3+
# Time start = 14/12/2022 11:12:28 PM
4+
# Time end = 14/12/2022 11:12:29 PM, duration = 1.33 seconds.

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,23 +1675,94 @@
16751675
}
16761676
}
16771677

1678+
/// <summary>
1679+
/// This class is responsible for:
1680+
/// 1. Recording what files were created.
1681+
/// 2. Write this log to into a text file, same name as the database.tt filename, but with .txt file extension.
1682+
/// 3. Read this file back in at the start, and to delete the previous runs files prior to re-generating.
1683+
/// </summary>
1684+
public class FileAuditService
1685+
{
1686+
// List of file, including paths, relative to Settings.Root
1687+
private readonly List<string> _files;
1688+
private readonly DateTime _start;
1689+
1690+
public FileAuditService()
1691+
{
1692+
_start = DateTime.Now;
1693+
_files = new List<string>
1694+
{
1695+
"# This file contains a list of the files generated by the " + Settings.TemplateFile + ".tt file.",
1696+
"# Please do not edit this file. It is used to delete files that may get filtered out during the next run.",
1697+
"# Time start = " + _start.ToLocalTime()
1698+
};
1699+
}
1700+
1701+
public void AddFile(string file)
1702+
{
1703+
_files.Add(file);
1704+
}
1705+
1706+
public void WriteAuditFile()
1707+
{
1708+
var end = DateTime.Now;
1709+
_files.Add($"# Time end = {end.ToLocalTime()}, duration = {(end - _start).TotalSeconds:F} seconds.");
1710+
var filename = GetFilename();
1711+
DeleteFilesNotRecreated(filename);
1712+
File.WriteAllLines(filename, _files);
1713+
}
1714+
1715+
private void DeleteFilesNotRecreated(string filename)
1716+
{
1717+
if (!File.Exists(filename))
1718+
return;
1719+
1720+
var filesToDelete = File.ReadAllLines(filename)
1721+
.Where(x => !x.StartsWith("#") &&
1722+
!string.IsNullOrWhiteSpace(x) &&
1723+
!_files.Contains(x))
1724+
.ToList();
1725+
1726+
foreach (var file in filesToDelete)
1727+
{
1728+
try
1729+
{
1730+
var path = Path.Combine(Settings.Root, file);
1731+
if (File.Exists(path))
1732+
File.Delete(path);
1733+
}
1734+
catch (Exception)
1735+
{
1736+
// Ignore if cannot delete, or file is missing
1737+
}
1738+
}
1739+
}
1740+
1741+
private static string GetFilename()
1742+
{
1743+
return Path.Combine(Settings.Root, Settings.TemplateFile + "Audit.txt");
1744+
}
1745+
}
1746+
16781747
public class FileManagementService : IFileManager
16791748
{
16801749
private readonly GeneratedTextTransformation _outer;
16811750
private readonly Dictionary<string, IFileManager> _fileManagers;
16821751
private IFileManager _fileManager;
1683-
private bool _writeToOuter;
16841752
private VisualStudioFileManager _visualStudioFileManager;
1753+
private FileAuditService _auditService;
1754+
private bool _writeToOuter;
16851755
public bool ForceWriteToOuter;
16861756

16871757
public FileManagementService(GeneratedTextTransformation outer)
16881758
{
16891759
if (outer == null) throw new ArgumentNullException(nameof(outer));
16901760

1691-
_outer = outer;
1692-
_fileManagers = new Dictionary<string, IFileManager>();
1693-
_fileManager = null;
1761+
_outer = outer;
1762+
_fileManagers = new Dictionary<string, IFileManager>();
1763+
_fileManager = null;
16941764
_visualStudioFileManager = null;
1765+
_auditService = null;
16951766
}
16961767

16971768
public static void DeleteFile(string filename)
@@ -1712,6 +1783,7 @@
17121783
Settings.FilterCount = filters.Count;
17131784

17141785
_writeToOuter = Settings.GenerateSingleDbContext && !Settings.GenerateSeparateFiles;
1786+
_auditService = new FileAuditService();
17151787

17161788
// For debug
17171789
/*var a = _writeToOuter;
@@ -1723,7 +1795,7 @@
17231795

17241796
if (fileManagerType == typeof(VisualStudioFileManager))
17251797
{
1726-
_visualStudioFileManager = (VisualStudioFileManager) Activator.CreateInstance(fileManagerType);
1798+
_visualStudioFileManager = (VisualStudioFileManager)Activator.CreateInstance(fileManagerType);
17271799
_visualStudioFileManager.Init(_outer);
17281800

17291801
// Switch to the EfCoreFileManager for the rest
@@ -1732,7 +1804,7 @@
17321804

17331805
foreach (var filter in filters)
17341806
{
1735-
var fileManager = (IFileManager) Activator.CreateInstance(fileManagerType);
1807+
var fileManager = (IFileManager)Activator.CreateInstance(fileManagerType);
17361808
fileManager.Init(_outer);
17371809
if (!string.IsNullOrWhiteSpace(filter.Key))
17381810
fileManager.StartNewFile(filter.Key + Settings.FileExtension);
@@ -1781,6 +1853,8 @@
17811853

17821854
public void Process(bool split)
17831855
{
1856+
_auditService.WriteAuditFile();
1857+
17841858
foreach (var fileManager in _fileManagers)
17851859
{
17861860
if (_visualStudioFileManager == null)
@@ -1789,8 +1863,8 @@
17891863
continue;
17901864
}
17911865

1792-
if(fileManager.Value.GetType() == typeof(EfCoreFileManager))
1793-
((EfCoreFileManager) fileManager.Value).ProcessToAnotherFileManager(_visualStudioFileManager, _outer);
1866+
if (fileManager.Value.GetType() == typeof(EfCoreFileManager))
1867+
((EfCoreFileManager)fileManager.Value).ProcessToAnotherFileManager(_visualStudioFileManager, _outer);
17941868
else
17951869
fileManager.Value.Process(split);
17961870
}
@@ -1801,6 +1875,7 @@
18011875
public void StartNewFile(string name)
18021876
{
18031877
_fileManager.StartNewFile(name);
1878+
_auditService.AddFile(name);
18041879
}
18051880
}
18061881

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
7+
namespace Efrpg.FileManagement
8+
{
9+
/// <summary>
10+
/// This class is responsible for:
11+
/// 1. Recording what files were created.
12+
/// 2. Write this log to into a text file, same name as the database.tt filename, but with .txt file extension.
13+
/// 3. Read this file back in at the start, and to delete the previous runs files prior to re-generating.
14+
/// </summary>
15+
public class FileAuditService
16+
{
17+
// List of file, including paths, relative to Settings.Root
18+
private readonly List<string> _files;
19+
private readonly DateTime _start;
20+
21+
public FileAuditService()
22+
{
23+
_start = DateTime.Now;
24+
_files = new List<string>
25+
{
26+
"# This file contains a list of the files generated by the " + Settings.TemplateFile + ".tt file.",
27+
"# Please do not edit this file. It is used to delete files that may get filtered out during the next run.",
28+
"# Time start = " + _start.ToLocalTime()
29+
};
30+
}
31+
32+
public void AddFile(string file)
33+
{
34+
_files.Add(file);
35+
}
36+
37+
public void WriteAuditFile()
38+
{
39+
var end = DateTime.Now;
40+
_files.Add($"# Time end = {end.ToLocalTime()}, duration = {(end - _start).TotalSeconds:F} seconds.");
41+
var filename = GetFilename();
42+
DeleteFilesNotRecreated(filename);
43+
File.WriteAllLines(filename, _files);
44+
}
45+
46+
private void DeleteFilesNotRecreated(string filename)
47+
{
48+
if (!File.Exists(filename))
49+
return;
50+
51+
var filesToDelete = File.ReadAllLines(filename)
52+
.Where(x => !x.StartsWith("#") &&
53+
!string.IsNullOrWhiteSpace(x) &&
54+
!_files.Contains(x))
55+
.ToList();
56+
57+
foreach (var file in filesToDelete)
58+
{
59+
try
60+
{
61+
var path = Path.Combine(Settings.Root, file);
62+
if (File.Exists(path))
63+
File.Delete(path);
64+
}
65+
catch (Exception)
66+
{
67+
// Ignore if cannot delete, or file is missing
68+
}
69+
}
70+
}
71+
72+
private static string GetFilename()
73+
{
74+
return Path.Combine(Settings.Root, Settings.TemplateFile + "Audit.txt");
75+
}
76+
}
77+
}

Generator/FileManagement/FileManagementService.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ public class FileManagementService : IFileManager
1010
private readonly GeneratedTextTransformation _outer;
1111
private readonly Dictionary<string, IFileManager> _fileManagers;
1212
private IFileManager _fileManager;
13-
private bool _writeToOuter;
1413
private VisualStudioFileManager _visualStudioFileManager;
14+
private FileAuditService _auditService;
15+
private bool _writeToOuter;
1516
public bool ForceWriteToOuter;
1617

1718
public FileManagementService(GeneratedTextTransformation outer)
1819
{
1920
if (outer == null) throw new ArgumentNullException(nameof(outer));
2021

21-
_outer = outer;
22-
_fileManagers = new Dictionary<string, IFileManager>();
23-
_fileManager = null;
22+
_outer = outer;
23+
_fileManagers = new Dictionary<string, IFileManager>();
24+
_fileManager = null;
2425
_visualStudioFileManager = null;
26+
_auditService = null;
2527
}
2628

2729
public static void DeleteFile(string filename)
@@ -42,6 +44,7 @@ public void Init(Dictionary<string, IDbContextFilter> filters, Type fileManagerT
4244
Settings.FilterCount = filters.Count;
4345

4446
_writeToOuter = Settings.GenerateSingleDbContext && !Settings.GenerateSeparateFiles;
47+
_auditService = new FileAuditService();
4548

4649
// For debug
4750
/*var a = _writeToOuter;
@@ -53,7 +56,7 @@ public void Init(Dictionary<string, IDbContextFilter> filters, Type fileManagerT
5356

5457
if (fileManagerType == typeof(VisualStudioFileManager))
5558
{
56-
_visualStudioFileManager = (VisualStudioFileManager) Activator.CreateInstance(fileManagerType);
59+
_visualStudioFileManager = (VisualStudioFileManager)Activator.CreateInstance(fileManagerType);
5760
_visualStudioFileManager.Init(_outer);
5861

5962
// Switch to the EfCoreFileManager for the rest
@@ -62,7 +65,7 @@ public void Init(Dictionary<string, IDbContextFilter> filters, Type fileManagerT
6265

6366
foreach (var filter in filters)
6467
{
65-
var fileManager = (IFileManager) Activator.CreateInstance(fileManagerType);
68+
var fileManager = (IFileManager)Activator.CreateInstance(fileManagerType);
6669
fileManager.Init(_outer);
6770
if (!string.IsNullOrWhiteSpace(filter.Key))
6871
fileManager.StartNewFile(filter.Key + Settings.FileExtension);
@@ -111,6 +114,8 @@ public void EndBlock()
111114

112115
public void Process(bool split)
113116
{
117+
_auditService.WriteAuditFile();
118+
114119
foreach (var fileManager in _fileManagers)
115120
{
116121
if (_visualStudioFileManager == null)
@@ -119,8 +124,8 @@ public void Process(bool split)
119124
continue;
120125
}
121126

122-
if(fileManager.Value.GetType() == typeof(EfCoreFileManager))
123-
((EfCoreFileManager) fileManager.Value).ProcessToAnotherFileManager(_visualStudioFileManager, _outer);
127+
if (fileManager.Value.GetType() == typeof(EfCoreFileManager))
128+
((EfCoreFileManager)fileManager.Value).ProcessToAnotherFileManager(_visualStudioFileManager, _outer);
124129
else
125130
fileManager.Value.Process(split);
126131
}
@@ -131,6 +136,7 @@ public void Process(bool split)
131136
public void StartNewFile(string name)
132137
{
133138
_fileManager.StartNewFile(name);
139+
_auditService.AddFile(name);
134140
}
135141
}
136142
}

Generator/Generator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="AssemblyHelper.cs" />
6363
<Compile Include="EfrpgVersion.cs" />
6464
<Compile Include="EnumerationMember.cs" />
65+
<Compile Include="FileManagement\FileAuditService.cs" />
6566
<Compile Include="FileManagement\VisualStudioFileManager.cs" />
6667
<Compile Include="FileManagement\EfCoreFileManager.cs" />
6768
<Compile Include="FileManagement\EntityFrameworkTemplateFileManager.cs" />

0 commit comments

Comments
 (0)