Skip to content

Commit b65a4dd

Browse files
committed
.NET 4.8 & 6.0. Nullable reference types.
1 parent 317113f commit b65a4dd

File tree

8 files changed

+131
-91
lines changed

8 files changed

+131
-91
lines changed

src/Directory.Build.props

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,57 @@
99
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
1010
<SignAssembly>true</SignAssembly>
1111
<LangVersion>latest</LangVersion>
12-
<Copyright>Copyright © 2012-2021 Bill Menees</Copyright>
12+
<Copyright>Copyright © 2003-$([System.DateTime]::UtcNow.ToString(`yyyy`)) Bill Menees</Copyright>
1313

14-
<!-- These paths must be relative to the projects that include the property (not to this .props file). -->
15-
<CodeAnalysisRuleSet>../DiskUsage.ruleset</CodeAnalysisRuleSet>
16-
<AssemblyOriginatorKeyFile>../DiskUsage.snk</AssemblyOriginatorKeyFile>
14+
<!-- When TargetFrameworks changes, also update the app's Properties\PublishProfiles\*.pubxml files. -->
15+
<TargetFrameworks>net48;net6.0-windows</TargetFrameworks>
16+
17+
<RepoSrcFolder>$(MSBuildThisFileDirectory)</RepoSrcFolder>
18+
<CodeAnalysisRuleSet>$(RepoSrcFolder)DiskUsage.ruleset</CodeAnalysisRuleSet>
19+
<AssemblyOriginatorKeyFile>$(RepoSrcFolder)DiskUsage.snk</AssemblyOriginatorKeyFile>
20+
<UseCustomCodeAnalyzers>true</UseCustomCodeAnalyzers>
21+
<Nullable>enable</Nullable>
22+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
23+
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
1724

1825
<!-- Make the assembly, file, and NuGet package versions the same. -->
19-
<Version>3.3.2</Version>
26+
<Version>3.4.0</Version>
2027
</PropertyGroup>
2128

2229
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
2330
<DefineConstants>DEBUG;TRACE</DefineConstants>
2431
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
32+
33+
<!-- Only set the BuildTime's date in Debug builds, so it doesn't force a rebuild every time.-->
34+
<!-- https://stackoverflow.com/a/6656553/1882616 -->
35+
<BuildTime Condition="'$(BuildTime)' == ''">$([System.DateTime]::UtcNow.ToString(`yyyy-MM-dd 00:00:00Z`))</BuildTime>
2536
</PropertyGroup>
2637

2738
<PropertyGroup Condition="'$(Configuration)'=='Release'">
2839
<DefineConstants>TRACE</DefineConstants>
40+
41+
<!-- Set the full BuildTime in Release builds. It will cause a rebuild, but the AssemblyMetadata will be correct. -->
42+
<BuildTime Condition="'$(BuildTime)' == ''">$([System.DateTime]::UtcNow.ToString(`yyyy-MM-dd HH:mm:ssZ`))</BuildTime>
2943
</PropertyGroup>
3044

3145
<ItemGroup>
46+
<AssemblyMetadata Condition="'$(BuildTime)' != ''" Include="BuildTime" Value="$(BuildTime)" />
47+
</ItemGroup>
48+
49+
<ItemGroup Condition="$(UseCustomCodeAnalyzers)">
3250
<PackageReference Include="codecracker.CSharp" Version="1.1.0">
3351
<PrivateAssets>all</PrivateAssets>
3452
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3553
</PackageReference>
36-
<PackageReference Include="Menees.Analyzers" Version="2.0.4">
54+
<PackageReference Include="Menees.Analyzers" Version="3.0.4">
3755
<PrivateAssets>all</PrivateAssets>
3856
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3957
</PackageReference>
40-
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
58+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
4159
<PrivateAssets>all</PrivateAssets>
4260
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4361
</PackageReference>
44-
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
62+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.376">
4563
<PrivateAssets>all</PrivateAssets>
4664
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4765
</PackageReference>

src/DiskUsage.ruleset

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="16.0">
2+
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="17.0">
33
<Rules AnalyzerId="CodeCracker.CSharp" RuleNamespace="CodeCracker.CSharp">
44
<Rule Id="CC0001" Action="None" />
55
<Rule Id="CC0014" Action="None" />
@@ -17,6 +17,10 @@
1717
<Rule Id="MEN007" Action="Warning" />
1818
<Rule Id="MEN010" Action="Warning" />
1919
</Rules>
20+
<Rules AnalyzerId="Microsoft.CodeAnalysis.NetAnalyzers" RuleNamespace="Microsoft.CodeAnalysis.NetAnalyzers">
21+
<Rule Id="CA1031" Action="Warning" />
22+
<Rule Id="CA2002" Action="Warning" />
23+
</Rules>
2024
<Rules AnalyzerId="Microsoft.CodeQuality.Analyzers" RuleNamespace="Microsoft.CodeQuality.Analyzers">
2125
<Rule Id="CA1062" Action="None" />
2226
</Rules>

src/DiskUsage/DirectoryData.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public class DirectoryData
2424

2525
private const double BytesPerMegabyte = 1048576.0;
2626

27-
private readonly DirectoryInfo directoryInfo;
27+
private readonly DirectoryInfo? directoryInfo;
28+
private readonly object resourceLock = new();
2829
private long size;
2930
private long fileCount;
3031
private long folderCount;
31-
private string name;
32-
private string fullName;
32+
private string? name;
33+
private string? fullName;
3334
private DirectoryData[] subData;
34-
private Node treeMapNode = new Node(string.Empty, 0, 0);
35+
private Node treeMapNode = new(string.Empty, 0, 0);
3536

3637
#endregion
3738

@@ -65,13 +66,14 @@ public DirectoryData(string error)
6566
this.PullNodes();
6667
}
6768

68-
private DirectoryData(DirectoryInfo directoryInfo, BackgroundWorker worker, bool reportProgress)
69+
private DirectoryData(DirectoryInfo directoryInfo, BackgroundWorker? worker, bool reportProgress)
6970
{
7071
this.DataType = DirectoryDataType.Directory;
7172
this.treeMapNode.Tag = this;
7273

7374
this.directoryInfo = directoryInfo;
7475
this.SetName(this.directoryInfo.Name, this.directoryInfo.FullName);
76+
this.subData = CollectionUtility.EmptyArray<DirectoryData>();
7577
this.Refresh(worker, reportProgress);
7678
}
7779

@@ -87,9 +89,9 @@ private DirectoryData(DirectoryInfo directoryInfo, BackgroundWorker worker, bool
8789

8890
public long FolderCount => this.folderCount;
8991

90-
public string Name => this.name;
92+
public string Name => this.name!;
9193

92-
public string FullName => this.fullName;
94+
public string FullName => this.fullName!;
9395

9496
public DirectoryDataType DataType { get; private set; } = DirectoryDataType.Directory;
9597

@@ -130,7 +132,7 @@ private static void AddErrorData(List<DirectoryData> subDataList, string prefix,
130132
subDataList.Add(new DirectoryData(prefix + message));
131133
}
132134

133-
private static bool CheckCancelled(BackgroundWorker worker)
135+
private static bool CheckCanceled(BackgroundWorker? worker)
134136
{
135137
bool result = false;
136138

@@ -142,7 +144,7 @@ private static bool CheckCancelled(BackgroundWorker worker)
142144
return result;
143145
}
144146

145-
private void Refresh(BackgroundWorker worker, bool reportProgress)
147+
private void Refresh(BackgroundWorker? worker, bool reportProgress)
146148
{
147149
if (this.directoryInfo != null)
148150
{
@@ -156,10 +158,10 @@ private void Refresh(BackgroundWorker worker, bool reportProgress)
156158
this.fileCount = 0;
157159
this.folderCount = 0;
158160

159-
List<DirectoryData> subDataList = new List<DirectoryData>();
161+
List<DirectoryData> subDataList = new();
160162

161163
this.CalculateFileSizes(subDataList);
162-
if (!CheckCancelled(worker))
164+
if (!CheckCanceled(worker))
163165
{
164166
if (reportProgress)
165167
{
@@ -170,7 +172,7 @@ private void Refresh(BackgroundWorker worker, bool reportProgress)
170172
this.CalculateDirectorySizesParallel(subDataList, worker);
171173
}
172174

173-
if (!CheckCancelled(worker))
175+
if (!CheckCanceled(worker))
174176
{
175177
this.subData = new DirectoryData[subDataList.Count];
176178
subDataList.CopyTo(this.subData);
@@ -238,7 +240,7 @@ private void CalculateFileSizes(List<DirectoryData> subDataList)
238240
int fileCount = 0;
239241
long fileSize = 0;
240242
Parallel.ForEach(
241-
this.directoryInfo.EnumerateFiles(),
243+
this.directoryInfo!.EnumerateFiles(),
242244
info =>
243245
{
244246
Interlocked.Add(ref fileSize, info.Length);
@@ -252,7 +254,7 @@ private void CalculateFileSizes(List<DirectoryData> subDataList)
252254
{
253255
// Add a faux node for files
254256
const string FilesNodeName = "[Files]";
255-
DirectoryData data = new DirectoryData(FilesNodeName, Path.Combine(this.directoryInfo.FullName, FilesNodeName), fileSize, fileCount);
257+
DirectoryData data = new(FilesNodeName, Path.Combine(this.directoryInfo.FullName, FilesNodeName), fileSize, fileCount);
256258
subDataList.Add(data);
257259
}
258260
}
@@ -266,11 +268,11 @@ private void CalculateFileSizes(List<DirectoryData> subDataList)
266268
"Microsoft.Design",
267269
"CA1031:DoNotCatchGeneralExceptionTypes",
268270
Justification = "I don't want an error in one node to stop the whole process.")]
269-
private void CalculateDirectorySizesWithProgress(List<DirectoryData> subDataList, BackgroundWorker worker)
271+
private void CalculateDirectorySizesWithProgress(List<DirectoryData> subDataList, BackgroundWorker? worker)
270272
{
271273
try
272274
{
273-
DirectoryInfo[] directories = this.directoryInfo.GetDirectories();
275+
DirectoryInfo[] directories = this.directoryInfo!.GetDirectories();
274276

275277
// Since we're reporting the directory names as our progress, let's do them in alphabetical order.
276278
Array.Sort(directories, (x, y) => string.Compare(x.Name, y.Name, true));
@@ -280,7 +282,7 @@ private void CalculateDirectorySizesWithProgress(List<DirectoryData> subDataList
280282
{
281283
DirectoryInfo info = directories[i];
282284

283-
if (CheckCancelled(worker))
285+
if (CheckCanceled(worker))
284286
{
285287
worker = null;
286288
break;
@@ -311,15 +313,15 @@ private void CalculateDirectorySizesWithProgress(List<DirectoryData> subDataList
311313
"Microsoft.Design",
312314
"CA1031:DoNotCatchGeneralExceptionTypes",
313315
Justification = "I don't want an error in one node to stop the whole process.")]
314-
private void CalculateDirectorySizesParallel(List<DirectoryData> subDataList, BackgroundWorker worker)
316+
private void CalculateDirectorySizesParallel(List<DirectoryData> subDataList, BackgroundWorker? worker)
315317
{
316318
try
317319
{
318320
Parallel.ForEach(
319-
this.directoryInfo.EnumerateDirectories(),
321+
this.directoryInfo!.EnumerateDirectories(),
320322
info =>
321323
{
322-
if (!CheckCancelled(worker))
324+
if (!CheckCanceled(worker))
323325
{
324326
this.CalculateDirectorySize(subDataList, worker, info);
325327
}
@@ -331,11 +333,11 @@ private void CalculateDirectorySizesParallel(List<DirectoryData> subDataList, Ba
331333
}
332334
}
333335

334-
private void CalculateDirectorySize(List<DirectoryData> subDataList, BackgroundWorker worker, DirectoryInfo info)
336+
private void CalculateDirectorySize(List<DirectoryData> subDataList, BackgroundWorker? worker, DirectoryInfo info)
335337
{
336338
// Never report progress for sub-directories. It adds too much blocking, which makes things take forever.
337339
// Add 1 to the folder count to account for the current directory.
338-
DirectoryData data = new DirectoryData(info, worker, false);
340+
DirectoryData data = new(info, worker, false);
339341
this.AdjustStats(data.Size, data.FileCount, 1 + data.FolderCount, false);
340342
lock (subDataList)
341343
{
@@ -407,9 +409,7 @@ private void AdjustStats(long sizeAdjustment, long fileCountAdjustment, long fol
407409
// If multiple threads are calculating sub-directory sizes, then the parent directory size
408410
// can be adjusted simultaneously. We need to lock on some member to ensure the size
409411
// is safely adjusted along with all dependent info (e.g., tree map size).
410-
#pragma warning disable CA2002 // Do not lock on objects with weak identity. There is only one AppDomain in use in this process.
411-
lock (this.directoryInfo)
412-
#pragma warning restore CA2002 // Do not lock on objects with weak identity
412+
lock (this.resourceLock)
413413
{
414414
this.SetSize(this.size + sizeAdjustment);
415415
}

src/DiskUsage/DiskUsage.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net45</TargetFrameworks>
54
<OutputType>WinExe</OutputType>
65
<UseWindowsForms>true</UseWindowsForms>
76
<ApplicationIcon>Images\App.ico</ApplicationIcon>
87
</PropertyGroup>
98

109
<ItemGroup>
11-
<PackageReference Include="Menees.Windows.Forms" Version="4.9.10" />
10+
<PackageReference Include="Menees.Windows.Forms" Version="5.0.0" />
1211
<PackageReference Include="TreemapControl" Version="1.0.2" />
1312
</ItemGroup>
1413

0 commit comments

Comments
 (0)