Skip to content

Commit 1430eab

Browse files
committed
(cleanup) - combine TempFileTracker with InvalidFileChecker into FileHelper
1 parent 1a21fc3 commit 1430eab

File tree

5 files changed

+46
-52
lines changed

5 files changed

+46
-52
lines changed

src/GitVersionTask.Tests/InvalidFileCheckerTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void VerifyIgnoreNonAssemblyInfoFile()
3939
");
4040
}
4141

42-
InvalidFileChecker.CheckForInvalidFiles(new[] { "SomeOtherFile.cs" }, projectFile);
42+
FileHelper.CheckForInvalidFiles(new[] { "SomeOtherFile.cs" }, projectFile);
4343
}
4444

4545
[Test]
@@ -55,7 +55,7 @@ public void VerifyAttributeFoundCSharp([Values("AssemblyVersion", "AssemblyFileV
5555
", attribute);
5656
}
5757

58-
var ex = Assert.Throws<WarningException>(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile), attribute);
58+
var ex = Assert.Throws<WarningException>(() => FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile), attribute);
5959
Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.cs"));
6060
}
6161

@@ -73,7 +73,7 @@ public void VerifyUnformattedAttributeFoundCSharp([Values("AssemblyVersion", "As
7373
", attribute);
7474
}
7575

76-
var ex = Assert.Throws<WarningException>(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile), attribute);
76+
var ex = Assert.Throws<WarningException>(() => FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile), attribute);
7777
Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.cs"));
7878
}
7979

@@ -90,7 +90,7 @@ public void VerifyCommentWorksCSharp([Values("AssemblyVersion", "AssemblyFileVer
9090
", attribute);
9191
}
9292

93-
InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
93+
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
9494
}
9595

9696
[Test]
@@ -109,7 +109,7 @@ public class Temp
109109
", attribute);
110110
}
111111

112-
InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
112+
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
113113
}
114114

115115
[Test]
@@ -127,7 +127,7 @@ public class {0}
127127
", attribute);
128128
}
129129

130-
InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
130+
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
131131
}
132132

133133
[Test]
@@ -143,7 +143,7 @@ Imports System.Reflection
143143
", attribute);
144144
}
145145

146-
var ex = Assert.Throws<WarningException>(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile), attribute);
146+
var ex = Assert.Throws<WarningException>(() => FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile), attribute);
147147
Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.vb"));
148148
}
149149

@@ -161,7 +161,7 @@ Imports System.Reflection
161161
", attribute);
162162
}
163163

164-
var ex = Assert.Throws<WarningException>(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile), attribute);
164+
var ex = Assert.Throws<WarningException>(() => FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile), attribute);
165165
Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.vb"));
166166
}
167167

@@ -178,7 +178,7 @@ Imports System.Reflection
178178
", attribute);
179179
}
180180

181-
InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
181+
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
182182
}
183183

184184
[Test]
@@ -196,7 +196,7 @@ End Class
196196
", attribute);
197197
}
198198

199-
InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
199+
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
200200
}
201201

202202
[Test]
@@ -213,6 +213,6 @@ End Class
213213
", attribute);
214214
}
215215

216-
InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
216+
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
217217
}
218218
}

src/GitVersionTask/InvalidFileChecker.cs renamed to src/GitVersionTask/FileHelper.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,45 @@
55
using System.Text.RegularExpressions;
66
using GitVersion;
77

8-
public static class InvalidFileChecker
8+
public static class FileHelper
99
{
1010
private static readonly Dictionary<string, Func<string, string, bool>> versionAttributeFinders = new Dictionary<string, Func<string, string, bool>>()
1111
{
1212
{ ".cs", CSharpFileContainsVersionAttribute },
1313
{ ".vb", VisualBasicFileContainsVersionAttribute }
1414
};
1515

16+
public static string TempPath;
17+
18+
static FileHelper()
19+
{
20+
TempPath = Path.Combine(Path.GetTempPath(), "GitVersionTask");
21+
Directory.CreateDirectory(TempPath);
22+
}
23+
24+
public static void DeleteTempFiles()
25+
{
26+
if (!Directory.Exists(TempPath))
27+
{
28+
return;
29+
}
30+
31+
foreach (var file in Directory.GetFiles(TempPath))
32+
{
33+
if (File.GetLastWriteTime(file) < DateTime.Now.AddDays(-1))
34+
{
35+
try
36+
{
37+
File.Delete(file);
38+
}
39+
catch (UnauthorizedAccessException)
40+
{
41+
//ignore contention
42+
}
43+
}
44+
}
45+
}
46+
1647
public static void CheckForInvalidFiles(IEnumerable<string> compileFiles, string projectFile)
1748
{
1849
foreach (var compileFile in GetInvalidFiles(compileFiles, projectFile))

src/GitVersionTask/GitVersionTaskUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Func<string, string, string> fileNameNoIntermediatePath
7272
if (intermediateOutputPath == null)
7373
{
7474
fileName = fileNameWithIntermediatePath(projectFile, fileExtension);
75-
workingDirectory = TempFileTracker.TempPath;
75+
workingDirectory = FileHelper.TempPath;
7676
}
7777
else
7878
{

src/GitVersionTask/TempFileTracker.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/GitVersionTask/UpdateAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class UpdateAssemblyInfo
1111

1212
private static Output InnerExecute(Input input, TaskLogger logger)
1313
{
14-
TempFileTracker.DeleteTempFiles();
14+
FileHelper.DeleteTempFiles();
1515

16-
InvalidFileChecker.CheckForInvalidFiles(input.CompileFiles, input.ProjectFile);
16+
FileHelper.CheckForInvalidFiles(input.CompileFiles, input.ProjectFile);
1717

1818
if (!GitVersionTaskUtils.GetVersionVariables(input, out var versionVariables))
1919
{

0 commit comments

Comments
 (0)