1
1
namespace GitVersionTask
2
2
{
3
3
using System ;
4
+ using System . Collections . Generic ;
4
5
using System . ComponentModel ;
5
6
using System . IO ;
6
- using System . Text ;
7
-
8
7
using GitVersion ;
9
8
using GitVersion . Helpers ;
10
-
11
9
using Microsoft . Build . Framework ;
12
10
13
- // TODO: Consolidate this with GitVersion.AssemblyInfoFileUpdate in GitVersionExe. @asbjornu
14
11
public class UpdateAssemblyInfo : GitVersionTaskBase
15
12
{
16
13
TaskLogger logger ;
@@ -37,7 +34,7 @@ public UpdateAssemblyInfo()
37
34
public ITaskItem [ ] CompileFiles { get ; set ; }
38
35
39
36
[ Required ]
40
- public string RootNamespace { get ; set ; }
37
+ public string Language { get ; set ; }
41
38
42
39
[ Output ]
43
40
public string AssemblyInfoTempFilePath { get ; set ; }
@@ -78,45 +75,47 @@ void InnerExecute()
78
75
{
79
76
return ;
80
77
}
78
+
81
79
CreateTempAssemblyInfo ( versionVariables ) ;
82
80
}
83
81
84
82
void CreateTempAssemblyInfo ( VersionVariables versionVariables )
85
83
{
86
- var assemblyInfoBuilder = AssemblyInfoBuilder . GetAssemblyInfoBuilder ( CompileFiles ) ;
84
+ var fileExtension = GetFileExtension ( ) ;
85
+ var assemblyInfoFileName = $ "GitVersionTaskAssemblyInfo.g.{ fileExtension } ";
87
86
88
87
if ( IntermediateOutputPath == null )
89
88
{
90
- var tempFileName = string . Format ( "AssemblyInfo_{0}_{1}.g.{2}" , Path . GetFileNameWithoutExtension ( ProjectFile ) , Path . GetRandomFileName ( ) , assemblyInfoBuilder . AssemblyInfoExtension ) ;
91
- AssemblyInfoTempFilePath = Path . Combine ( TempFileTracker . TempPath , tempFileName ) ;
92
- }
93
- else
94
- {
95
- AssemblyInfoTempFilePath = Path . Combine ( IntermediateOutputPath , string . Format ( "GitVersionTaskAssemblyInfo.g.{0}" , assemblyInfoBuilder . AssemblyInfoExtension ) ) ;
89
+ assemblyInfoFileName = $ "AssemblyInfo_{ Path . GetFileNameWithoutExtension ( ProjectFile ) } _{ Path . GetRandomFileName ( ) } .g.{ fileExtension } ";
96
90
}
97
91
98
- var assemblyInfo = assemblyInfoBuilder . GetAssemblyInfoText ( versionVariables , RootNamespace ) . Trim ( ) ;
99
- var encoding = EncodingHelper . DetectEncoding ( AssemblyInfoTempFilePath ) ?? Encoding . UTF8 ;
92
+ var workingDirectory = IntermediateOutputPath ?? TempFileTracker . TempPath ;
100
93
101
- // We need to try to read the existing text first if the file exists and see if it's the same
102
- // This is to avoid writing when there's no differences and causing a rebuild
103
- try
94
+ AssemblyInfoTempFilePath = Path . Combine ( workingDirectory , assemblyInfoFileName ) ;
95
+
96
+ using ( var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater ( assemblyInfoFileName , workingDirectory , versionVariables , new FileSystem ( ) , true ) )
104
97
{
105
- if ( File . Exists ( AssemblyInfoTempFilePath ) )
106
- {
107
- var content = File . ReadAllText ( AssemblyInfoTempFilePath , encoding ) . Trim ( ) ;
108
- if ( string . Equals ( assemblyInfo , content , StringComparison . Ordinal ) )
109
- {
110
- return ; // nothign to do as the file matches what we'd create
111
- }
112
- }
98
+ assemblyInfoFileUpdater . Update ( ) ;
99
+ assemblyInfoFileUpdater . DoNotRestoreAssemblyInfo ( ) ;
113
100
}
114
- catch ( Exception )
101
+ }
102
+
103
+ string GetFileExtension ( )
104
+ {
105
+ switch ( Language )
115
106
{
116
- // Something happened reading the file, try to overwrite anyway
117
- }
107
+ case "C#" :
108
+ return "cs" ;
118
109
119
- File . WriteAllText ( AssemblyInfoTempFilePath , assemblyInfo , encoding ) ;
110
+ case "F#" :
111
+ return "fs" ;
112
+
113
+ case "VB" :
114
+ return "vb" ;
115
+
116
+ default :
117
+ throw new Exception ( $ "Unknown language detected: '{ Language } '") ;
118
+ }
120
119
}
121
120
}
122
121
}
0 commit comments