6
6
using GitVersion ;
7
7
using Microsoft . Build . Framework ;
8
8
9
- public static class FileHelper
9
+ namespace GitVersionTask
10
10
{
11
- private static readonly Dictionary < string , Func < string , string , bool > > versionAttributeFinders = new Dictionary < string , Func < string , string , bool > > ( )
11
+ public static class FileHelper
12
12
{
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
+ } ;
16
18
17
- public static string TempPath ;
19
+ public static string TempPath ;
18
20
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 ( )
28
22
{
29
- return ;
23
+ TempPath = Path . Combine ( Path . GetTempPath ( ) , "GitVersionTask" ) ;
24
+ Directory . CreateDirectory ( TempPath ) ;
30
25
}
31
26
32
- foreach ( var file in Directory . GetFiles ( TempPath ) )
27
+ public static void DeleteTempFiles ( )
33
28
{
34
- if ( File . GetLastWriteTime ( file ) < DateTime . Now . AddDays ( - 1 ) )
29
+ if ( ! Directory . Exists ( TempPath ) )
35
30
{
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 ) )
41
37
{
42
- //ignore contention
38
+ try
39
+ {
40
+ File . Delete ( file ) ;
41
+ }
42
+ catch ( UnauthorizedAccessException )
43
+ {
44
+ //ignore contention
45
+ }
43
46
}
44
47
}
45
48
}
46
- }
47
49
48
- public static string GetFileExtension ( string language )
49
- {
50
- switch ( language )
50
+ public static string GetFileExtension ( string language )
51
51
{
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
+ }
60
63
}
61
- }
62
64
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 )
66
66
{
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
+ }
68
71
}
69
- }
70
72
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 )
76
74
{
77
- return languageSpecificFileContainsVersionAttribute ( compileFile , projectFile ) ;
78
- }
75
+ var compileFileExtension = Path . GetExtension ( compileFile ) ;
79
76
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
+ }
82
81
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 ) ;
87
89
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 = @"@(""[^""]*"")+" ;
92
94
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 ) ;
97
99
98
- return Regex . IsMatch ( noCommentsOrStrings , @"(?x) # IgnorePatternWhitespace
100
+ return Regex . IsMatch ( noCommentsOrStrings , @"(?x) # IgnorePatternWhitespace
99
101
100
102
\[\s*assembly\s*:\s* # The [assembly: part
101
103
@@ -104,22 +106,22 @@ private static bool CSharpFileContainsVersionAttribute(string compileFile, strin
104
106
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
105
107
106
108
\s*\(\s*\)\s*\] # End brackets ()]" ) ;
107
- }
109
+ }
108
110
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 ) ;
113
115
114
- var lineComments = @"'(.*?)\r?\n" ;
115
- var strings = @"""((\\[^\n]|[^""\n])*)""" ;
116
+ var lineComments = @"'(.*?)\r?\n" ;
117
+ var strings = @"""((\\[^\n]|[^""\n])*)""" ;
116
118
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 ) ;
121
123
122
- return Regex . IsMatch ( noCommentsOrStrings , @"(?x) # IgnorePatternWhitespace
124
+ return Regex . IsMatch ( noCommentsOrStrings , @"(?x) # IgnorePatternWhitespace
123
125
124
126
\<\s*Assembly\s*:\s* # The <Assembly: part
125
127
@@ -128,12 +130,13 @@ private static bool VisualBasicFileContainsVersionAttribute(string compileFile,
128
130
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
129
131
130
132
\s*\(\s*\)\s*\> # End brackets ()>" ) ;
131
- }
133
+ }
132
134
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
+ }
138
141
}
139
142
}
0 commit comments