-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathDirectoryCountStatistics.cs
201 lines (171 loc) · 7.35 KB
/
DirectoryCountStatistics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Beef.CodeGen
{
/// <summary>
/// Provides <see cref="DirectoryInfo"/> count statistics.
/// </summary>
internal class DirectoryCountStatistics
{
/// <summary>
/// Initializes a new instance of the <see cref="DirectoryCountStatistics"/> class.
/// </summary>
public DirectoryCountStatistics(DirectoryInfo directory, string[] exclude)
{
Directory = directory;
if (directory.Name == "Generated")
IsGenerated = true;
Exclude = exclude ?? [];
}
/// <summary>
/// Gets the <see cref="DirectoryInfo"/>.
/// </summary>
public DirectoryInfo Directory { get; }
/// <summary>
/// Gets the directory/path names to exclude.
/// </summary>
public string[] Exclude { get; private set; }
/// <summary>
/// Gets the file count.
/// </summary>
public int FileCount { get; private set; }
/// <summary>
/// Gets the total file count including children.
/// </summary>
public int TotalFileCount => FileCount + Children.Sum(x => x.TotalFileCount);
/// <summary>
/// Gets the generated file count.
/// </summary>
public int GeneratedFileCount { get; private set; }
/// <summary>
/// Gets the total generated file count including children.
/// </summary>
public int GeneratedTotalFileCount => GeneratedFileCount + Children.Sum(x => x.GeneratedTotalFileCount);
/// <summary>
/// Gets the line count;
/// </summary>
public int LineCount { get; private set; }
/// <summary>
/// Gets the total line count including children.
/// </summary>
public int TotalLineCount => LineCount + Children.Sum(x => x.TotalLineCount);
/// <summary>
/// Gets the generated line count.
/// </summary>
public int GeneratedLineCount { get; private set; }
/// <summary>
/// Gets the total line count including children.
/// </summary>
public int GeneratedTotalLineCount => GeneratedLineCount + Children.Sum(x => x.GeneratedTotalLineCount);
/// <summary>
/// Indicates whether the contents of the directory are generated.
/// </summary>
public bool IsGenerated { get; private set; }
/// <summary>
/// Gets the child <see cref="DirectoryCountStatistics"/> instances.
/// </summary>
public List<DirectoryCountStatistics> Children { get; } = [];
/// <summary>
/// Increments the file count.
/// </summary>
public void IncrementFileCount()
{
FileCount++;
if (IsGenerated)
GeneratedFileCount++;
}
/// <summary>
/// Increments the line count.
/// </summary>
public void IncrementLineCount()
{
LineCount++;
if (IsGenerated)
GeneratedLineCount++;
}
/// <summary>
/// Adds a child <see cref="DirectoryCountStatistics"/> instance.
/// </summary>
public DirectoryCountStatistics AddChildDirectory(DirectoryInfo di)
{
var dcs = new DirectoryCountStatistics(di, Exclude);
if (IsGenerated)
dcs.IsGenerated = true;
Children.Add(dcs);
return dcs;
}
/// <summary>
/// Write the count statistics.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/>.</param>
/// <param name="columnLength">The maximum column length.</param>
/// <param name="indent">The indent size to show hierarchy.</param>
/// <param name="remove">The number of characters to remove from directory </param>
public void Write(ILogger logger, int columnLength, int indent, int remove)
{
if (indent == 0)
{
var hdrAll = string.Format("{0, " + columnLength + "}", "All");
var hdrGen = string.Format("{0, " + (columnLength + 5) + "}", "Generated");
var hdrfiles = string.Format("{0, " + columnLength + "}", "Files");
var hdrlines = string.Format("{0, " + columnLength + "}", "Lines");
logger.LogInformation("{Content}", $"{hdrAll} | {hdrAll} | {hdrGen} | {hdrGen} | Path/");
logger.LogInformation("{Content}", $"{hdrfiles} | {hdrlines} | {hdrfiles} Perc | {hdrlines} Perc | Directory");
int maxLength = 0;
DirectoryDepthAnalysis(this, 0, ref maxLength);
logger.LogInformation("{Content}", new string('-', (columnLength * 4) + 22 + maxLength - remove));
}
var totfiles = string.Format("{0, " + columnLength + "}", TotalFileCount);
var totlines = string.Format("{0, " + columnLength + "}", TotalLineCount);
var totgenFiles = string.Format("{0, " + columnLength + "}", GeneratedTotalFileCount);
var totgenFilesPerc = string.Format("{0, " + 3 + "}", GeneratedTotalFileCount == 0 ? 0 : Math.Round((double)GeneratedTotalFileCount / (double)TotalFileCount * 100.0, 0));
var totgenLines = string.Format("{0, " + columnLength + "}", GeneratedTotalLineCount);
var totgenLinesPerc = string.Format("{0, " + 3 + "}", GeneratedTotalLineCount == 0 ? 0 : Math.Round((double)GeneratedTotalLineCount / (double)TotalLineCount * 100.0, 0));
string spacer;
if (indent == 0)
spacer = string.Empty;
else
spacer = new string(' ', (indent - 1) * 2) + "- ";
logger.LogInformation("{Content}", $"{totfiles} {totlines} {totgenFiles} {totgenFilesPerc}% {totgenLines} {totgenLinesPerc}% {spacer}{Directory.FullName[remove..]}");
foreach (var dcs in Children)
{
if (dcs.TotalFileCount > 0)
dcs.Write(logger, columnLength, indent + 1, remove);
}
}
/// <summary>
/// Performs directory depth and length analysis.
/// </summary>
private static void DirectoryDepthAnalysis(DirectoryCountStatistics dcs, int depth, ref int maxLength)
{
maxLength = Math.Max(maxLength, dcs.Directory.FullName.Length + (depth * 2));
foreach (var d in dcs.Children)
{
DirectoryDepthAnalysis(d, depth + 1, ref maxLength);
}
}
/// <summary>
/// Cleans (deletes) all <see cref="IsGenerated"/> directories.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/></param>
public void Clean(ILogger logger)
{
// Where generated then delete.
if (IsGenerated)
{
logger.LogWarning(" Deleted: {Directory} [{FileCount} files]", Directory.FullName, TotalFileCount);
Directory.Delete(true);
return;
}
// Where not generated then clean children.
foreach (var dcs in Children)
{
dcs.Clean(logger);
}
}
}
}