Skip to content

Commit b27e341

Browse files
committed
Improvements to comments and formatting. Minor refactor to reduce code duplication.
1 parent d412541 commit b27e341

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

SIL.Core/IO/TempFile.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static string MakeFileAtRandomPath(string extension = null)
5151
string result = "";
5252
while (needMoreTries)
5353
{
54-
result = Combine(GetTempPath(), (NamePrefix??"") + GetRandomFileName());
54+
result = Combine(GetTempPath(), (NamePrefix ?? "") + GetRandomFileName());
5555
if (extension != null)
5656
{
5757
result = ChangeExtension(result, extension);
@@ -73,12 +73,21 @@ private static string MakeFileAtRandomPath(string extension = null)
7373
return result;
7474
}
7575

76+
/// <summary>
77+
/// Use this constructor to set the path manually later. If
78+
/// <paramref name="dontMakeMeAFileAndDontSetPath"/> is <c>false</c>, a temporary file will
79+
/// be created and assigned to <see cref="Path"/> (similar to the default constructor,
80+
/// except <see cref="NamePrefix"/> is ignored). If <c>true</c>, no file is created and
81+
/// <see cref="Path"/> remains unset.
82+
/// </summary>
83+
/// <param name="dontMakeMeAFileAndDontSetPath">
84+
/// Pass <c>true</c> to skip creating a temp file and leave <see cref="Path"/> unset.
85+
/// Caller is responsible for setting it later.
86+
/// </param>
7687
public TempFile(bool dontMakeMeAFileAndDontSetPath)
7788
{
78-
if(!dontMakeMeAFileAndDontSetPath)
79-
{
89+
if (!dontMakeMeAFileAndDontSetPath)
8090
Path = GetTempFileName();
81-
}
8291
}
8392

8493
/// <summary>

SIL.TestUtilities/TempLiftFile.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using System.IO;
33
using JetBrains.Annotations;
44
using SIL.IO;
5+
using static System.IO.Path;
56

67
namespace SIL.TestUtilities
78
{
89
public class TempLiftFile : TempFile
910
{
10-
private const string LiftFileExt = ".lift";
11+
private const string kLiftFileExt = ".lift";
1112

1213
public TempLiftFile(string xmlOfEntries)
1314
: this(xmlOfEntries, /*LiftIO.Validation.Validator.LiftVersion*/ "0.12")
@@ -19,34 +20,25 @@ public TempLiftFile(string xmlOfEntries, string claimedLiftVersion)
1920
}
2021

2122
public TempLiftFile(TemporaryFolder parentFolder, string xmlOfEntries, string claimedLiftVersion)
22-
: base(true) // True means "I'll set the pathname, thank you very much." Otherwise, the temp one 'false' creates will stay forever, and fill the hard drive up.
23+
: base(dontMakeMeAFileAndDontSetPath: true)
2324
{
24-
if (parentFolder != null)
25-
{
26-
Path = parentFolder.GetPathForNewTempFile(false) + LiftFileExt;
27-
}
28-
else
29-
{
30-
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName() + LiftFileExt);
31-
}
25+
Path = parentFolder != null
26+
? parentFolder.GetPathForNewTempFile(false) + kLiftFileExt
27+
: Combine(GetTempPath(), GetRandomFileName() + kLiftFileExt);
3228

33-
string liftContents =
34-
$"<?xml version='1.0' encoding='utf-8'?><lift version='{claimedLiftVersion}'>{xmlOfEntries}</lift>";
35-
RobustFile.WriteAllText(Path, liftContents);
29+
WriteContents(xmlOfEntries, claimedLiftVersion);
3630
}
3731

3832
public TempLiftFile(string fileName, TemporaryFolder parentFolder, string xmlOfEntries, string claimedLiftVersion)
39-
: base(true) // True means "I'll set the pathname, thank you very much." Otherwise, the temp one 'false' creates will stay forever, and fill the hard drive up.
33+
: base(dontMakeMeAFileAndDontSetPath: true)
4034
{
4135
Path = parentFolder.Combine(fileName);
4236

43-
string liftContents =
44-
$"<?xml version='1.0' encoding='utf-8'?><lift version='{claimedLiftVersion}'>{xmlOfEntries}</lift>";
45-
RobustFile.WriteAllText(Path, liftContents);
37+
WriteContents(xmlOfEntries, claimedLiftVersion);
4638
}
4739

4840
private TempLiftFile()
49-
: base(true) // True means "I'll set the pathname, thank you very much." Otherwise, the temp one 'false' creates will stay forever, and fill the hard drive up.
41+
: base(dontMakeMeAFileAndDontSetPath: true)
5042
{
5143
}
5244

@@ -61,5 +53,12 @@ private TempLiftFile()
6153
t.Path = path;
6254
return t;
6355
}
56+
57+
private void WriteContents(string xmlOfEntries, string claimedLiftVersion)
58+
{
59+
var liftContents = "<?xml version='1.0' encoding='utf-8'?>" +
60+
$"<lift version='{claimedLiftVersion}'>{xmlOfEntries}</lift>";
61+
RobustFile.WriteAllText(Path, liftContents);
62+
}
6463
}
6564
}

0 commit comments

Comments
 (0)