@@ -12,6 +12,13 @@ public class AssemblyInfoFileUpdater : IDisposable
12
12
readonly List < Action > restoreBackupTasks = new List < Action > ( ) ;
13
13
readonly List < Action > cleanupBackupTasks = new List < Action > ( ) ;
14
14
15
+ readonly IDictionary < string , Regex > assemblyAttributeRegexes = new Dictionary < string , Regex >
16
+ {
17
+ { ".cs" , new Regex ( @"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)" , RegexOptions . Multiline ) } ,
18
+ { ".fs" , new Regex ( @"(\s*\[\s*\<assembly:\s*(?:.*)\>\s*\]\s*$(\r?\n)?)" , RegexOptions . Multiline ) } ,
19
+ { ".vb" , new Regex ( @"(\s*\<Assembly:\s*(?:.*)\>\s*$(\r?\n)?)" , RegexOptions . Multiline ) } ,
20
+ } ;
21
+
15
22
ISet < string > assemblyInfoFileNames ;
16
23
string workingDirectory ;
17
24
VersionVariables variables ;
@@ -20,7 +27,7 @@ public class AssemblyInfoFileUpdater : IDisposable
20
27
TemplateManager templateManager ;
21
28
22
29
public AssemblyInfoFileUpdater ( string assemblyInfoFileName , string workingDirectory , VersionVariables variables , IFileSystem fileSystem , bool ensureAssemblyInfo ) :
23
- this ( new HashSet < string > { assemblyInfoFileName } , workingDirectory , variables , fileSystem , ensureAssemblyInfo )
30
+ this ( new HashSet < string > { assemblyInfoFileName } , workingDirectory , variables , fileSystem , ensureAssemblyInfo )
24
31
{ }
25
32
26
33
public AssemblyInfoFileUpdater ( ISet < string > assemblyInfoFileNames , string workingDirectory , VersionVariables variables , IFileSystem fileSystem , bool ensureAssemblyInfo )
@@ -77,15 +84,15 @@ public void Update()
77
84
78
85
if ( ! string . IsNullOrWhiteSpace ( assemblyVersion ) )
79
86
{
80
- fileContents = ReplaceOrAppend ( assemblyVersionRegex , fileContents , assemblyVersionString , assemblyInfoFile . Extension , ref appendedAttributes ) ;
87
+ fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend ( assemblyVersionRegex , fileContents , assemblyVersionString , assemblyInfoFile . Extension , ref appendedAttributes ) ;
81
88
}
82
89
83
90
if ( ! string . IsNullOrWhiteSpace ( assemblyFileVersion ) )
84
91
{
85
- fileContents = ReplaceOrAppend ( assemblyFileVersionRegex , fileContents , assemblyFileVersionString , assemblyInfoFile . Extension , ref appendedAttributes ) ;
92
+ fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend ( assemblyFileVersionRegex , fileContents , assemblyFileVersionString , assemblyInfoFile . Extension , ref appendedAttributes ) ;
86
93
}
87
94
88
- fileContents = ReplaceOrAppend ( assemblyInfoVersionRegex , fileContents , assemblyInfoVersionString , assemblyInfoFile . Extension , ref appendedAttributes ) ;
95
+ fileContents = ReplaceOrInsertAfterLastAssemblyAttributeOrAppend ( assemblyInfoVersionRegex , fileContents , assemblyInfoVersionString , assemblyInfoFile . Extension , ref appendedAttributes ) ;
89
96
90
97
if ( appendedAttributes )
91
98
{
@@ -100,20 +107,32 @@ public void Update()
100
107
}
101
108
}
102
109
103
- string ReplaceOrAppend ( Regex replaceRegex , string inputString , string replaceString , string fileExtension , ref bool appendedAttributes )
110
+ string ReplaceOrInsertAfterLastAssemblyAttributeOrAppend ( Regex replaceRegex , string inputString , string replaceString , string fileExtension , ref bool appendedAttributes )
104
111
{
105
112
var assemblyAddFormat = templateManager . GetAddFormatFor ( fileExtension ) ;
106
113
107
114
if ( replaceRegex . IsMatch ( inputString ) )
108
115
{
109
- inputString = replaceRegex . Replace ( inputString , replaceString ) ;
116
+ return replaceRegex . Replace ( inputString , replaceString ) ;
110
117
}
111
- else
118
+
119
+ Regex assemblyRegex ;
120
+ if ( assemblyAttributeRegexes . TryGetValue ( fileExtension , out assemblyRegex ) )
112
121
{
113
- inputString += Environment . NewLine + string . Format ( assemblyAddFormat , replaceString ) ;
114
- appendedAttributes = true ;
122
+ var assemblyMatches = assemblyRegex . Matches ( inputString ) ;
123
+ if ( assemblyMatches . Count > 0 )
124
+ {
125
+ var lastMatch = assemblyMatches [ assemblyMatches . Count - 1 ] ;
126
+ var replacementString = lastMatch . Value ;
127
+ if ( ! lastMatch . Value . EndsWith ( Environment . NewLine ) ) replacementString += Environment . NewLine ;
128
+ replacementString += string . Format ( assemblyAddFormat , replaceString ) ;
129
+ replacementString += Environment . NewLine ;
130
+ return inputString . Replace ( lastMatch . Value , replacementString ) ;
131
+ }
115
132
}
116
-
133
+
134
+ inputString += Environment . NewLine + string . Format ( assemblyAddFormat , replaceString ) ;
135
+ appendedAttributes = true ;
117
136
return inputString ;
118
137
}
119
138
0 commit comments