@@ -4,98 +4,96 @@ namespace GitVersionTask
4
4
using System . IO ;
5
5
using GitVersion ;
6
6
using GitVersion . Helpers ;
7
- using Microsoft . Build . Framework ;
8
7
9
- public class GenerateGitVersionInformation : GitVersionTaskBase
8
+ public static class GenerateGitVersionInformation
10
9
{
11
- TaskLogger logger ;
12
-
13
- public GenerateGitVersionInformation ( )
10
+ public static Output Execute (
11
+ Input input
12
+ )
14
13
{
15
- logger = new TaskLogger ( this ) ;
16
- Logger . SetLoggers ( this . LogDebug , this . LogInfo , this . LogWarning , s => this . LogError ( s ) ) ;
17
- }
18
-
19
- [ Required ]
20
- public string SolutionDirectory { get ; set ; }
21
-
22
- [ Required ]
23
- public string ProjectFile { get ; set ; }
24
-
25
- [ Required ]
26
- public string IntermediateOutputPath { get ; set ; }
27
-
28
- [ Required ]
29
- public string Language { get ; set ; }
14
+ if ( ! input . ValidateInput ( ) )
15
+ {
16
+ throw new Exception ( "Invalid input." ) ;
17
+ }
30
18
31
- [ Output ]
32
- public string GitVersionInformationFilePath { get ; set ; }
19
+ var logger = new TaskLogger ( ) ;
20
+ Logger . SetLoggers ( logger . LogInfo , logger . LogInfo , logger . LogWarning , s => logger . LogError ( s ) ) ;
33
21
34
- public bool NoFetch { get ; set ; }
35
22
36
- public override bool Execute ( )
37
- {
23
+ Output output = null ;
38
24
try
39
25
{
40
- InnerExecute ( ) ;
41
- return true ;
26
+ output = InnerExecute ( input ) ;
42
27
}
43
28
catch ( WarningException errorException )
44
29
{
45
30
logger . LogWarning ( errorException . Message ) ;
46
- return true ;
31
+ output = new Output ( ) ;
47
32
}
48
33
catch ( Exception exception )
49
34
{
50
35
logger . LogError ( "Error occurred: " + exception ) ;
51
- return false ;
36
+ throw ;
52
37
}
53
38
finally
54
39
{
55
40
Logger . Reset ( ) ;
56
41
}
42
+
43
+ return output ;
57
44
}
58
45
59
- void InnerExecute ( )
46
+ private static Output InnerExecute ( Input input )
60
47
{
61
- VersionVariables versionVariables ;
62
- if ( ! ExecuteCore . TryGetVersion ( SolutionDirectory , out versionVariables , NoFetch , new Authentication ( ) ) )
48
+ var execute = GitVersionTaskBase . CreateExecuteCore ( ) ;
49
+ if ( ! execute . TryGetVersion ( input . SolutionDirectory , out var versionVariables , input . NoFetch , new Authentication ( ) ) )
63
50
{
64
- return ;
51
+ return null ;
65
52
}
66
53
67
- var fileExtension = GetFileExtension ( ) ;
68
- var fileName = $ "GitVersionInformation.g.{ fileExtension } ";
54
+ var fileWriteInfo = input . IntermediateOutputPath . GetWorkingDirectoryAndFileNameAndExtension (
55
+ input . Language ,
56
+ input . ProjectFile ,
57
+ ( pf , ext ) => $ "GitVersionInformation.g.{ ext } ",
58
+ ( pf , ext ) => $ "GitVersionInformation_{ Path . GetFileNameWithoutExtension ( pf ) } _{ Path . GetRandomFileName ( ) } .g.{ ext } "
59
+ ) ;
69
60
70
- if ( IntermediateOutputPath == null )
61
+ var output = new Output ( )
71
62
{
72
- fileName = $ "GitVersionInformation_{ Path . GetFileNameWithoutExtension ( ProjectFile ) } _{ Path . GetRandomFileName ( ) } .g.{ fileExtension } ";
73
- }
74
-
75
- var workingDirectory = IntermediateOutputPath ?? TempFileTracker . TempPath ;
76
-
77
- GitVersionInformationFilePath = Path . Combine ( workingDirectory , fileName ) ;
78
-
79
- var generator = new GitVersionInformationGenerator ( fileName , workingDirectory , versionVariables , new FileSystem ( ) ) ;
63
+ GitVersionInformationFilePath = Path . Combine ( fileWriteInfo . WorkingDirectory , fileWriteInfo . FileName )
64
+ } ;
65
+ var generator = new GitVersionInformationGenerator ( fileWriteInfo . FileName , fileWriteInfo . WorkingDirectory , versionVariables , new FileSystem ( ) ) ;
80
66
generator . Generate ( ) ;
67
+
68
+ return output ;
81
69
}
82
70
83
- string GetFileExtension ( )
71
+ public sealed class Input
84
72
{
85
- switch ( Language )
86
- {
87
- case "C#" :
88
- return "cs" ;
73
+ public string SolutionDirectory { get ; set ; }
89
74
90
- case "F#" :
91
- return "fs" ;
75
+ public string ProjectFile { get ; set ; }
92
76
93
- case "VB" :
94
- return "vb" ;
77
+ public string IntermediateOutputPath { get ; set ; }
95
78
96
- default :
97
- throw new Exception ( $ "Unknown language detected: '{ Language } '") ;
98
- }
79
+ public string Language { get ; set ; }
80
+
81
+ public bool NoFetch { get ; set ; }
82
+ }
83
+
84
+ private static Boolean ValidateInput ( this Input input )
85
+ {
86
+ return input != null
87
+ && ! String . IsNullOrEmpty ( input . SolutionDirectory )
88
+ && ! String . IsNullOrEmpty ( input . ProjectFile )
89
+ // && !String.IsNullOrEmpty(input.IntermediateOutputPath) // This was marked as [Required] but it InnerExecute still seems to allow it to be null... ?
90
+ && ! String . IsNullOrEmpty ( input . Language )
91
+ ;
92
+ }
93
+
94
+ public sealed class Output
95
+ {
96
+ public string GitVersionInformationFilePath { get ; set ; }
99
97
}
100
98
}
101
99
}
0 commit comments