Skip to content

Commit fe649fe

Browse files
committed
added back the msbuild tasks (cleanup)
1 parent 047b34d commit fe649fe

File tree

6 files changed

+178
-209
lines changed

6 files changed

+178
-209
lines changed

src/GitVersionTask.Tests/GetVersionTaskTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using GitVersion;
33
using GitVersionTask;
4+
using Microsoft.Build.Framework;
45
using NUnit.Framework;
56
using Shouldly;
67

@@ -10,8 +11,9 @@ public class GetVersionTaskTests : TestBase
1011
[Test]
1112
public void OutputsShouldMatchVariableProvider()
1213
{
13-
var taskProperties = typeof(GetVersion.Output)
14+
var taskProperties = typeof(GetVersion)
1415
.GetProperties()
16+
.Where(p => p.GetCustomAttributes(typeof(OutputAttribute), false).Any())
1517
.Select(p => p.Name);
1618

1719
var variablesProperties = VersionVariables.AvailableVariables;

src/GitVersionTask.Tests/InvalidFileCheckerTests.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.IO;
33
using GitVersion;
4+
using GitVersionTask;
5+
using GitVersionTask.Tests.Mocks;
6+
using Microsoft.Build.Framework;
47
using NUnit.Framework;
58

69
[TestFixture]
@@ -39,7 +42,7 @@ public void VerifyIgnoreNonAssemblyInfoFile()
3942
");
4043
}
4144

42-
FileHelper.CheckForInvalidFiles(new[] { "SomeOtherFile.cs" }, projectFile);
45+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "SomeOtherFile.cs" } }, projectFile);
4346
}
4447

4548
[Test]
@@ -55,7 +58,7 @@ public void VerifyAttributeFoundCSharp([Values("AssemblyVersion", "AssemblyFileV
5558
", attribute);
5659
}
5760

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

@@ -73,7 +76,7 @@ public void VerifyUnformattedAttributeFoundCSharp([Values("AssemblyVersion", "As
7376
", attribute);
7477
}
7578

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

@@ -90,7 +93,7 @@ public void VerifyCommentWorksCSharp([Values("AssemblyVersion", "AssemblyFileVer
9093
", attribute);
9194
}
9295

93-
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
96+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
9497
}
9598

9699
[Test]
@@ -109,7 +112,7 @@ public class Temp
109112
", attribute);
110113
}
111114

112-
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
115+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
113116
}
114117

115118
[Test]
@@ -127,7 +130,7 @@ public class {0}
127130
", attribute);
128131
}
129132

130-
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile);
133+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile);
131134
}
132135

133136
[Test]
@@ -143,7 +146,7 @@ Imports System.Reflection
143146
", attribute);
144147
}
145148

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

@@ -161,7 +164,7 @@ Imports System.Reflection
161164
", attribute);
162165
}
163166

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

@@ -178,7 +181,7 @@ Imports System.Reflection
178181
", attribute);
179182
}
180183

181-
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
184+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile);
182185
}
183186

184187
[Test]
@@ -196,7 +199,7 @@ End Class
196199
", attribute);
197200
}
198201

199-
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
202+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile);
200203
}
201204

202205
[Test]
@@ -213,6 +216,6 @@ End Class
213216
", attribute);
214217
}
215218

216-
FileHelper.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile);
219+
FileHelper.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile);
217220
}
218221
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections;
3+
using Microsoft.Build.Framework;
4+
5+
namespace GitVersionTask.Tests.Mocks
6+
{
7+
class MockTaskItem : ITaskItem
8+
{
9+
public string ItemSpec { get; set; }
10+
11+
public int MetadataCount { get; private set; }
12+
13+
public ICollection MetadataNames { get; private set; }
14+
15+
public IDictionary CloneCustomMetadata()
16+
{
17+
throw new NotImplementedException();
18+
}
19+
20+
public void CopyMetadataTo(ITaskItem destinationItem)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
25+
public string GetMetadata(string metadataName)
26+
{
27+
throw new NotImplementedException();
28+
}
29+
30+
public void RemoveMetadata(string metadataName)
31+
{
32+
throw new NotImplementedException();
33+
}
34+
35+
public void SetMetadata(string metadataName, string metadataValue)
36+
{
37+
throw new NotImplementedException();
38+
}
39+
}
40+
}

src/GitVersionTask/FileHelper.cs

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,96 +6,98 @@
66
using GitVersion;
77
using Microsoft.Build.Framework;
88

9-
public static class FileHelper
9+
namespace GitVersionTask
1010
{
11-
private static readonly Dictionary<string, Func<string, string, bool>> versionAttributeFinders = new Dictionary<string, Func<string, string, bool>>()
11+
public static class FileHelper
1212
{
13-
{ ".cs", CSharpFileContainsVersionAttribute },
14-
{ ".vb", VisualBasicFileContainsVersionAttribute }
15-
};
13+
private static readonly Dictionary<string, Func<string, string, bool>> versionAttributeFinders = new Dictionary<string, Func<string, string, bool>>()
14+
{
15+
{ ".cs", CSharpFileContainsVersionAttribute },
16+
{ ".vb", VisualBasicFileContainsVersionAttribute }
17+
};
1618

17-
public static string TempPath;
19+
public static string TempPath;
1820

19-
static FileHelper()
20-
{
21-
TempPath = Path.Combine(Path.GetTempPath(), "GitVersionTask");
22-
Directory.CreateDirectory(TempPath);
23-
}
24-
25-
public static void DeleteTempFiles()
26-
{
27-
if (!Directory.Exists(TempPath))
21+
static FileHelper()
2822
{
29-
return;
23+
TempPath = Path.Combine(Path.GetTempPath(), "GitVersionTask");
24+
Directory.CreateDirectory(TempPath);
3025
}
3126

32-
foreach (var file in Directory.GetFiles(TempPath))
27+
public static void DeleteTempFiles()
3328
{
34-
if (File.GetLastWriteTime(file) < DateTime.Now.AddDays(-1))
29+
if (!Directory.Exists(TempPath))
3530
{
36-
try
37-
{
38-
File.Delete(file);
39-
}
40-
catch (UnauthorizedAccessException)
31+
return;
32+
}
33+
34+
foreach (var file in Directory.GetFiles(TempPath))
35+
{
36+
if (File.GetLastWriteTime(file) < DateTime.Now.AddDays(-1))
4137
{
42-
//ignore contention
38+
try
39+
{
40+
File.Delete(file);
41+
}
42+
catch (UnauthorizedAccessException)
43+
{
44+
//ignore contention
45+
}
4346
}
4447
}
4548
}
46-
}
4749

48-
public static string GetFileExtension(string language)
49-
{
50-
switch (language)
50+
public static string GetFileExtension(string language)
5151
{
52-
case "C#":
53-
return "cs";
54-
case "F#":
55-
return "fs";
56-
case "VB":
57-
return "vb";
58-
default:
59-
throw new ArgumentException($"Unknown language detected: '{language}'");
52+
switch (language)
53+
{
54+
case "C#":
55+
return "cs";
56+
case "F#":
57+
return "fs";
58+
case "VB":
59+
return "vb";
60+
default:
61+
throw new ArgumentException($"Unknown language detected: '{language}'");
62+
}
6063
}
61-
}
6264

63-
public static void CheckForInvalidFiles(IEnumerable<ITaskItem> compileFiles, string projectFile)
64-
{
65-
foreach (var compileFile in GetInvalidFiles(compileFiles, projectFile))
65+
public static void CheckForInvalidFiles(IEnumerable<ITaskItem> compileFiles, string projectFile)
6666
{
67-
throw new WarningException("File contains assembly version attributes which conflict with the attributes generated by GitVersion " + compileFile);
67+
foreach (var compileFile in GetInvalidFiles(compileFiles, projectFile))
68+
{
69+
throw new WarningException("File contains assembly version attributes which conflict with the attributes generated by GitVersion " + compileFile);
70+
}
6871
}
69-
}
7072

71-
private static bool FileContainsVersionAttribute(string compileFile, string projectFile)
72-
{
73-
var compileFileExtension = Path.GetExtension(compileFile);
74-
75-
if (versionAttributeFinders.TryGetValue(compileFileExtension, out var languageSpecificFileContainsVersionAttribute))
73+
private static bool FileContainsVersionAttribute(string compileFile, string projectFile)
7674
{
77-
return languageSpecificFileContainsVersionAttribute(compileFile, projectFile);
78-
}
75+
var compileFileExtension = Path.GetExtension(compileFile);
7976

80-
throw new WarningException("File with name containing AssemblyInfo could not be checked for assembly version attributes which conflict with the attributes generated by GitVersion " + compileFile);
81-
}
77+
if (versionAttributeFinders.TryGetValue(compileFileExtension, out var languageSpecificFileContainsVersionAttribute))
78+
{
79+
return languageSpecificFileContainsVersionAttribute(compileFile, projectFile);
80+
}
8281

83-
private static bool CSharpFileContainsVersionAttribute(string compileFile, string projectFile)
84-
{
85-
var combine = Path.Combine(Path.GetDirectoryName(projectFile), compileFile);
86-
var allText = File.ReadAllText(combine);
82+
throw new WarningException("File with name containing AssemblyInfo could not be checked for assembly version attributes which conflict with the attributes generated by GitVersion " + compileFile);
83+
}
84+
85+
private static bool CSharpFileContainsVersionAttribute(string compileFile, string projectFile)
86+
{
87+
var combine = Path.Combine(Path.GetDirectoryName(projectFile), compileFile);
88+
var allText = File.ReadAllText(combine);
8789

88-
var blockComments = @"/\*(.*?)\*/";
89-
var lineComments = @"//(.*?)\r?\n";
90-
var strings = @"""((\\[^\n]|[^""\n])*)""";
91-
var verbatimStrings = @"@(""[^""]*"")+";
90+
var blockComments = @"/\*(.*?)\*/";
91+
var lineComments = @"//(.*?)\r?\n";
92+
var strings = @"""((\\[^\n]|[^""\n])*)""";
93+
var verbatimStrings = @"@(""[^""]*"")+";
9294

93-
var noCommentsOrStrings = Regex.Replace(allText,
94-
blockComments + "|" + lineComments + "|" + strings + "|" + verbatimStrings,
95-
me => me.Value.StartsWith("//") ? Environment.NewLine : "",
96-
RegexOptions.Singleline);
95+
var noCommentsOrStrings = Regex.Replace(allText,
96+
blockComments + "|" + lineComments + "|" + strings + "|" + verbatimStrings,
97+
me => me.Value.StartsWith("//") ? Environment.NewLine : string.Empty,
98+
RegexOptions.Singleline);
9799

98-
return Regex.IsMatch(noCommentsOrStrings, @"(?x) # IgnorePatternWhitespace
100+
return Regex.IsMatch(noCommentsOrStrings, @"(?x) # IgnorePatternWhitespace
99101
100102
\[\s*assembly\s*:\s* # The [assembly: part
101103
@@ -104,22 +106,22 @@ private static bool CSharpFileContainsVersionAttribute(string compileFile, strin
104106
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
105107
106108
\s*\(\s*\)\s*\] # End brackets ()]");
107-
}
109+
}
108110

109-
private static bool VisualBasicFileContainsVersionAttribute(string compileFile, string projectFile)
110-
{
111-
var combine = Path.Combine(Path.GetDirectoryName(projectFile), compileFile);
112-
var allText = File.ReadAllText(combine);
111+
private static bool VisualBasicFileContainsVersionAttribute(string compileFile, string projectFile)
112+
{
113+
var combine = Path.Combine(Path.GetDirectoryName(projectFile), compileFile);
114+
var allText = File.ReadAllText(combine);
113115

114-
var lineComments = @"'(.*?)\r?\n";
115-
var strings = @"""((\\[^\n]|[^""\n])*)""";
116+
var lineComments = @"'(.*?)\r?\n";
117+
var strings = @"""((\\[^\n]|[^""\n])*)""";
116118

117-
var noCommentsOrStrings = Regex.Replace(allText,
118-
lineComments + "|" + strings,
119-
me => me.Value.StartsWith("'") ? Environment.NewLine : "",
120-
RegexOptions.Singleline);
119+
var noCommentsOrStrings = Regex.Replace(allText,
120+
lineComments + "|" + strings,
121+
me => me.Value.StartsWith("'") ? Environment.NewLine : string.Empty,
122+
RegexOptions.Singleline);
121123

122-
return Regex.IsMatch(noCommentsOrStrings, @"(?x) # IgnorePatternWhitespace
124+
return Regex.IsMatch(noCommentsOrStrings, @"(?x) # IgnorePatternWhitespace
123125
124126
\<\s*Assembly\s*:\s* # The <Assembly: part
125127
@@ -128,12 +130,13 @@ private static bool VisualBasicFileContainsVersionAttribute(string compileFile,
128130
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
129131
130132
\s*\(\s*\)\s*\> # End brackets ()>");
131-
}
133+
}
132134

133-
private static IEnumerable<string> GetInvalidFiles(IEnumerable<ITaskItem> compileFiles, string projectFile)
134-
{
135-
return compileFiles.Select(x => x.ItemSpec)
136-
.Where(compileFile => compileFile.Contains("AssemblyInfo"))
137-
.Where(s => FileContainsVersionAttribute(s, projectFile));
135+
private static IEnumerable<string> GetInvalidFiles(IEnumerable<ITaskItem> compileFiles, string projectFile)
136+
{
137+
return compileFiles.Select(x => x.ItemSpec)
138+
.Where(compileFile => compileFile.Contains("AssemblyInfo"))
139+
.Where(s => FileContainsVersionAttribute(s, projectFile));
140+
}
138141
}
139142
}

0 commit comments

Comments
 (0)