Skip to content

Commit 41be600

Browse files
committed
Fix for warnings.
1 parent 2743372 commit 41be600

17 files changed

+147
-123
lines changed

.github/workflows/dotnet.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
run: dotnet pack src/DtronixPackage -c Release -o ./artifacts
3939

4040
- name: Export artifacts
41-
uses: actions/upload-artifact@v2
41+
uses: actions/upload-artifact@v4
4242
with:
4343
path: artifacts/*
4444

src/DtronixPackage/ChangelogEntry.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ public class ChangelogEntry
1212
/// <summary>
1313
/// Username active while saving.
1414
/// </summary>
15-
public string Username { get; set; }
15+
public string? Username { get; set; }
1616

1717
/// <summary>
1818
/// Active computer name which saved the file.
1919
/// </summary>
20-
public string ComputerName { get; set; }
20+
public string? ComputerName { get; set; }
2121

2222
/// <summary>
2323
/// Time of the save.
@@ -27,7 +27,7 @@ public class ChangelogEntry
2727
/// <summary>
2828
/// Contains additional information about this save.
2929
/// </summary>
30-
public string Note { get; set; }
30+
public string? Note { get; set; }
3131

3232
public ChangelogEntry(ChangelogEntryType type, string username, string computerName, DateTimeOffset time)
3333
{

src/DtronixPackage/IPackage.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public interface IPackage : IDisposable
2525
/// <summary>
2626
/// Opened package application version.
2727
/// </summary>
28-
Version PackageAppVersion { get; }
28+
Version? PackageAppVersion { get; }
2929

3030
/// <summary>
3131
/// Current version of the application.
3232
/// </summary>
33-
Version CurrentAppVersion { get; }
33+
Version? CurrentAppVersion { get; }
3434

3535
/// <summary>
3636
/// If set to true, a ".BAK" package will be created with the previously saved package.
@@ -40,7 +40,7 @@ public interface IPackage : IDisposable
4040
/// <summary>
4141
/// Path to save the current package.
4242
/// </summary>
43-
string SavePath { get; }
43+
string? SavePath { get; }
4444

4545
/// <summary>
4646
/// True if this package is in a read only state and can not be saved to the same package.

src/DtronixPackage/Logging/LogEntry.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace DtronixPackage.Logging;
55
public class LogEntry
66
{
77
public readonly LogEntryEventType Severity;
8-
public readonly string Message;
9-
public readonly Exception Exception;
8+
public readonly string? Message;
9+
public readonly Exception? Exception;
1010

11-
public LogEntry(LogEntryEventType severity, string message, Exception exception = null)
11+
public LogEntry(LogEntryEventType severity, string message, Exception? exception = null)
1212
{
1313
if (message == null)
1414
throw new ArgumentNullException(nameof(message));

src/DtronixPackage/Logging/LoggerExtensions.cs

+25-24
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,88 @@ namespace DtronixPackage.Logging;
55

66
public static class LoggerExtensions
77
{
8+
private const string NoMessage = "No message from exception";
89
// Trace
9-
public static void Trace(this ILogger logger, Exception exception) {
10-
logger.Log(new LogEntry(LogEntryEventType.Trace, exception.Message, exception));
10+
public static void Trace(this ILogger logger, Exception? exception) {
11+
logger.Log(new LogEntry(LogEntryEventType.Trace, exception?.Message ?? NoMessage, exception));
1112
}
1213

1314
public static void Trace(this ILogger logger, string message) {
1415
logger.Log(new LogEntry(LogEntryEventType.Trace, message));
1516
}
1617

17-
public static void Trace(this ILogger logger, Exception exception, string message) {
18+
public static void Trace(this ILogger logger, Exception? exception, string message) {
1819
logger.Log(new LogEntry(LogEntryEventType.Trace, message, exception));
1920
}
2021

2122
// Debug
22-
public static void Debug(this ILogger logger, Exception exception) {
23-
logger.Log(new LogEntry(LogEntryEventType.Debug, exception.Message, exception));
23+
public static void Debug(this ILogger logger, Exception? exception) {
24+
logger.Log(new LogEntry(LogEntryEventType.Debug, exception?.Message ?? NoMessage, exception));
2425
}
2526

2627
public static void Debug(this ILogger logger, string message) {
2728
logger.Log(new LogEntry(LogEntryEventType.Debug, message));
2829
}
2930

30-
public static void Debug(this ILogger logger, Exception exception, string message) {
31+
public static void Debug(this ILogger logger, Exception? exception, string message) {
3132
logger.Log(new LogEntry(LogEntryEventType.Debug, message, exception));
3233
}
3334

3435
// Info
35-
public static void Info(this ILogger logger, Exception exception) {
36-
logger.Log(new LogEntry(LogEntryEventType.Info, exception.Message, exception));
36+
public static void Info(this ILogger logger, Exception? exception) {
37+
logger.Log(new LogEntry(LogEntryEventType.Info, exception?.Message ?? NoMessage, exception));
3738
}
3839

3940
public static void Info(this ILogger logger, string message) {
4041
logger.Log(new LogEntry(LogEntryEventType.Info, message));
4142
}
4243

43-
public static void Info(this ILogger logger, Exception exception, string message) {
44+
public static void Info(this ILogger logger, Exception? exception, string message) {
4445
logger.Log(new LogEntry(LogEntryEventType.Info, message, exception));
4546
}
4647

4748
// Warn
48-
public static void Warn(this ILogger logger, Exception exception) {
49-
logger.Log(new LogEntry(LogEntryEventType.Warn, exception.Message, exception));
49+
public static void Warn(this ILogger logger, Exception? exception) {
50+
logger.Log(new LogEntry(LogEntryEventType.Warn, exception?.Message ?? NoMessage, exception));
5051
}
5152

5253
public static void Warn(this ILogger logger, string message) {
5354
logger.Log(new LogEntry(LogEntryEventType.Warn, message));
5455
}
5556

56-
public static void Warn(this ILogger logger, Exception exception, string message) {
57+
public static void Warn(this ILogger logger, Exception? exception, string message) {
5758
logger.Log(new LogEntry(LogEntryEventType.Warn, message, exception));
5859
}
5960

6061
// Error
61-
public static void Error(this ILogger logger, Exception exception) {
62-
logger.Log(new LogEntry(LogEntryEventType.Error, exception.Message, exception));
62+
public static void Error(this ILogger logger, Exception? exception) {
63+
logger.Log(new LogEntry(LogEntryEventType.Error, exception?.Message ?? NoMessage, exception));
6364
}
6465

6566
public static void Error(this ILogger logger, string message) {
6667
logger.Log(new LogEntry(LogEntryEventType.Error, message));
6768
}
6869

69-
public static void Error(this ILogger logger, Exception exception, string message) {
70+
public static void Error(this ILogger logger, Exception? exception, string message) {
7071
logger.Log(new LogEntry(LogEntryEventType.Error, message, exception));
7172
}
7273

7374
// Fatal
74-
public static void Fatal(this ILogger logger, Exception exception) {
75-
logger.Log(new LogEntry(LogEntryEventType.Fatal, exception.Message, exception));
75+
public static void Fatal(this ILogger logger, Exception? exception) {
76+
logger.Log(new LogEntry(LogEntryEventType.Fatal, exception?.Message ?? NoMessage, exception));
7677
}
7778

7879
public static void Fatal(this ILogger logger, string message) {
7980
logger.Log(new LogEntry(LogEntryEventType.Fatal, message));
8081
}
8182

82-
public static void Fatal(this ILogger logger, Exception exception, string message) {
83+
public static void Fatal(this ILogger logger, Exception? exception, string message) {
8384
logger.Log(new LogEntry(LogEntryEventType.Fatal, message, exception));
8485
}
8586

8687
[Conditional("DEBUG")]
87-
public static void ConditionalDebug(this ILogger logger, Exception exception) {
88-
logger.Log(new LogEntry(LogEntryEventType.Debug, exception.Message, exception));
88+
public static void ConditionalDebug(this ILogger logger, Exception? exception) {
89+
logger.Log(new LogEntry(LogEntryEventType.Debug, exception?.Message ?? NoMessage, exception));
8990
}
9091

9192
[Conditional("DEBUG")]
@@ -94,7 +95,7 @@ public static void ConditionalDebug(this ILogger logger, string message) {
9495
}
9596

9697
[Conditional("DEBUG")]
97-
public static void ConditionalDebug(this ILogger logger, string message, Exception exception) {
98+
public static void ConditionalDebug(this ILogger logger, string message, Exception? exception) {
9899
logger.Log(new LogEntry(LogEntryEventType.Debug, message, exception));
99100
}
100101

@@ -104,12 +105,12 @@ public static void ConditionalTrace(this ILogger logger, string message) {
104105
}
105106

106107
[Conditional("DEBUG")]
107-
public static void ConditionalTrace(this ILogger logger, Exception exception) {
108-
logger.Log(new LogEntry(LogEntryEventType.Trace, exception.Message, exception));
108+
public static void ConditionalTrace(this ILogger logger, Exception? exception) {
109+
logger.Log(new LogEntry(LogEntryEventType.Trace, exception?.Message ?? NoMessage, exception));
109110
}
110111

111112
[Conditional("DEBUG")]
112-
public static void ConditionalTrace(this ILogger logger, string message, Exception exception) {
113+
public static void ConditionalTrace(this ILogger logger, string message, Exception? exception) {
113114
logger.Log(new LogEntry(LogEntryEventType.Trace, message, exception));
114115
}
115116
}

src/DtronixPackage/Package.Open.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.IO;
44
using System.IO.Compression;
55
using System.Linq;
6-
using System.Text;
76
using System.Text.Json;
87
using System.Threading;
98
using System.Threading.Tasks;
@@ -12,7 +11,7 @@
1211

1312
namespace DtronixPackage;
1413

15-
public abstract partial class Package<TContent> : IPackage
14+
public abstract partial class Package<TContent>
1615
where TContent : PackageContent, new()
1716
{
1817

@@ -44,7 +43,7 @@ public async Task<PackageOpenResult> Open(
4443

4544
await _packageOperationSemaphore.WaitAsync(cancellationToken);
4645
var isMonitorEnabledOriginalValue = IsMonitorEnabled;
47-
PackageOpenResult returnValue = null;
46+
PackageOpenResult? returnValue = null;
4847

4948
try
5049
{
@@ -158,7 +157,7 @@ public async Task<PackageOpenResult> Open(
158157
}
159158
catch (Exception e)
160159
{
161-
return returnValue = new PackageOpenResult(PackageOpenResultType.Corrupted, e, PackageAppVersion);
160+
return returnValue = new PackageOpenResult(PackageOpenResultType.Corrupted, e, PackageAppVersion ?? new Version(0,0,0,0));
162161
}
163162

164163
var upgradeManager = new UpgradeManager(_openPkgVersion, PackageAppVersion);
@@ -188,9 +187,11 @@ public async Task<PackageOpenResult> Open(
188187
options: null,
189188
cancellationToken);
190189

191-
foreach (var changelogItem in logItems)
192-
_changelog.Add(changelogItem);
193-
190+
if (logItems != null)
191+
foreach (var changelogItem in logItems)
192+
_changelog.Add(changelogItem);
193+
else
194+
throw new Exception("Could not parse Changelogs");
194195
}
195196
catch (Exception e)
196197
{
@@ -268,7 +269,7 @@ public async Task<PackageOpenResult> Open(
268269
}
269270
catch (Exception e)
270271
{
271-
return returnValue = new PackageOpenResult(PackageOpenResultType.UnknownFailure, e, PackageAppVersion);
272+
return returnValue = new PackageOpenResult(PackageOpenResultType.UnknownFailure, e, PackageAppVersion ?? new Version(0,0,0,0));
272273
}
273274
finally
274275
{
@@ -285,8 +286,11 @@ public async Task<PackageOpenResult> Open(
285286
}
286287

287288

288-
private async Task<PackageOpenResult> ApplyUpgrades(IEnumerable<PackageUpgrade> upgrades)
289+
private async Task<PackageOpenResult?> ApplyUpgrades(IEnumerable<PackageUpgrade> upgrades)
289290
{
291+
if(_openArchive == null)
292+
return new PackageOpenResult(PackageOpenResultType.FileNotFound);
293+
290294
foreach (var upgrade in upgrades)
291295
{
292296
var isPackageUpgrade = upgrade is InternalPackageUpgrade;
@@ -300,7 +304,7 @@ private async Task<PackageOpenResult> ApplyUpgrades(IEnumerable<PackageUpgrade>
300304
{
301305
// Upgrade soft failed, log it and notify the opener.
302306
Logger?.Error($"Unable to perform{(isPackageUpgrade ? " package" : " application")} upgrade of package to version {upgrade.DependentPackageVersion}.");
303-
return new PackageOpenResult(PackageOpenResultType.UpgradeFailure, PackageAppVersion);
307+
return new PackageOpenResult(PackageOpenResultType.UpgradeFailure, PackageAppVersion ?? new Version(0,0,0,0));
304308
}
305309

306310
_changelog.Add(new ChangelogEntry(isPackageUpgrade
@@ -319,7 +323,7 @@ private async Task<PackageOpenResult> ApplyUpgrades(IEnumerable<PackageUpgrade>
319323
{
320324
// Upgrade hard failed.
321325
Logger?.Error(e, $"Unable to perform{(isPackageUpgrade ? " package" : " application")} upgrade of package to version {upgrade.DependentPackageVersion}.");
322-
return new PackageOpenResult(PackageOpenResultType.UpgradeFailure, e, PackageAppVersion);
326+
return new PackageOpenResult(PackageOpenResultType.UpgradeFailure, e, PackageAppVersion ?? new Version(0,0,0,0));
323327
}
324328

325329
// Since we did perform an upgrade, set set that the package has been changed.

src/DtronixPackage/Package.Save.cs

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.IO.Compression;
5-
using System.Linq;
6-
using System.Text;
74
using System.Threading.Tasks;
85
using DtronixPackage.Logging;
96

@@ -58,7 +55,7 @@ public async Task<PackageSaveResult> Save(string path)
5855
{
5956
_lockFile.Close();
6057
_lockFile = null;
61-
File.Delete(_lockFilePath);
58+
File.Delete(_lockFilePath!);
6259
}
6360
catch (Exception e)
6461
{
@@ -101,7 +98,7 @@ public async Task<PackageSaveResult> Save(string path)
10198
private async Task<PackageSaveResult> SaveInternal(bool autoSave)
10299
{
103100
Logger?.ConditionalTrace($"SaveInternal(autoSave:{autoSave})");
104-
PackageSaveResult returnValue = null;
101+
PackageSaveResult? returnValue = null;
105102
try
106103
{
107104
// TODO: Reuse existing archive when saving from a read-only package. Prevents duplication of the MS.
@@ -115,13 +112,13 @@ private async Task<PackageSaveResult> SaveInternal(bool autoSave)
115112
await using (var packageVersionStream = writer.CreateEntityStream("version", false))
116113
{
117114
await using var packageVersionStreamWriter = new StreamWriter(packageVersionStream);
118-
await packageVersionStreamWriter.WriteAsync(CurrentPkgVersion.ToString());
115+
await packageVersionStreamWriter.WriteAsync(CurrentPkgVersion?.ToString());
119116
}
120117

121118
await OnWrite(writer);
122119

123120
// Write package version file
124-
await writer.Write("version", CurrentAppVersion.ToString());
121+
await writer.Write("version", CurrentAppVersion?.ToString() ?? "0.0.0.0");
125122

126123
var log = new ChangelogEntry
127124
(autoSave ? ChangelogEntryType.AutoSave : ChangelogEntryType.Save,
@@ -197,10 +194,15 @@ private async Task<PackageSaveResult> SaveInternal(bool autoSave)
197194

198195
if (!autoSave)
199196
{
197+
198+
200199
// Create a new package/overwrite existing if a backup was not created.
201200
// Retain the lock on the package.
202201
if (_openPackageStream == null)
203202
{
203+
if (SavePath == null)
204+
throw new Exception("Save path can't be null when saving");
205+
204206
// Create a new source package.
205207
_openPackageStream = new FileStream(SavePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
206208
}
@@ -217,7 +219,17 @@ private async Task<PackageSaveResult> SaveInternal(bool autoSave)
217219
Logger?.ConditionalTrace($"Creating AutoSave package {AutoSavePath}.");
218220
}
219221

220-
var destinationStream = autoSave ? File.Create(AutoSavePath) : _openPackageStream;
222+
223+
FileStream? destinationStream;
224+
if (autoSave)
225+
{
226+
if (AutoSavePath == null)
227+
throw new Exception($"{nameof(AutoSavePath)} Can't be null when auto-saving.");
228+
229+
destinationStream = File.Create(AutoSavePath);
230+
}
231+
else
232+
destinationStream = _openPackageStream;
221233

222234
if (autoSave)
223235
{
@@ -275,7 +287,7 @@ private async Task<PackageSaveResult> SaveInternal(bool autoSave)
275287
IsReadOnly = false;
276288
}
277289

278-
Logger?.ConditionalTrace($"InternalSave return: {returnValue}");
290+
Logger?.ConditionalTrace($"InternalSave return: {returnValue ?? PackageSaveResult.Failure}");
279291
Logger?.ConditionalTrace("Released _packageOperationSemaphore");
280292
_packageOperationSemaphore.Release();
281293
}
@@ -361,7 +373,7 @@ private async Task AutoSave(bool synchronous)
361373
}
362374
}
363375

364-
private void AutoSaveElapsed(object state)
376+
private void AutoSaveElapsed(object? state)
365377
{
366378
Logger?.ConditionalTrace("AutoSaveElapsed()");
367379
_ = AutoSave(false);

0 commit comments

Comments
 (0)