1
1
import tl = require( 'azure-pipelines-task-lib/task' ) ;
2
- import { IExecOptions , ToolRunner } from 'azure-pipelines-task-lib/toolrunner' ;
2
+ import tr = require ( 'azure-pipelines-task-lib/toolrunner' ) ;
3
3
import path = require( 'path' ) ;
4
4
import os = require( 'os' ) ;
5
- import { ArgumentParser } from 'argparse' ;
6
5
7
6
export class GitVersionTask {
7
+ execOptions : tr . IExecOptions ;
8
+
9
+ preferBundledVersion : boolean ;
10
+ updateAssemblyInfo : boolean ;
11
+
12
+ updateAssemblyInfoFilename : string ;
13
+ additionalArguments : string ;
14
+ targetPath : string ;
15
+ sourcesDirectory : string ;
16
+ currentDirectory : string ;
17
+ workingDirectory : string ;
18
+ gitVersionPath : string ;
19
+ runtime : string ;
20
+
21
+ constructor ( ) {
22
+ this . preferBundledVersion = tl . getBoolInput ( 'preferBundledVersion' ) || true ;
23
+ this . updateAssemblyInfo = tl . getBoolInput ( 'updateAssemblyInfo' ) ;
24
+
25
+ this . updateAssemblyInfoFilename = tl . getInput ( 'updateAssemblyInfoFilename' ) ;
26
+ this . additionalArguments = tl . getInput ( 'additionalArguments' ) ;
27
+ this . targetPath = tl . getInput ( 'targetPath' ) ;
28
+ this . runtime = tl . getInput ( 'runtime' ) || "core" ;
29
+ this . gitVersionPath = tl . getInput ( 'gitVersionPath' ) ;
30
+
31
+ this . sourcesDirectory = tl . getVariable ( "Build.SourcesDirectory" ) ;
32
+
33
+ this . currentDirectory = __dirname ;
34
+ this . workingDirectory = ! this . targetPath
35
+ ? this . sourcesDirectory
36
+ : path . join ( this . sourcesDirectory , this . targetPath ) ;
37
+
38
+ this . execOptions = {
39
+ cwd : undefined ,
40
+ env : undefined ,
41
+ silent : undefined ,
42
+ failOnStdErr : undefined ,
43
+ ignoreReturnCode : undefined ,
44
+ errStream : undefined ,
45
+ outStream : undefined ,
46
+ windowsVerbatimArguments : undefined
47
+ } ;
48
+ }
8
49
9
- public static async execute ( ) {
50
+ public async execute ( ) {
10
51
try {
11
-
12
- const updateAssemblyInfo = tl . getBoolInput ( 'updateAssemblyInfo' ) ;
13
- const updateAssemblyInfoFilename = tl . getInput ( 'updateAssemblyInfoFilename' ) ;
14
- const additionalArguments = tl . getInput ( 'additionalArguments' ) ;
15
- const targetPath = tl . getInput ( 'targetPath' ) ;
16
- const preferBundledVersion = tl . getBoolInput ( 'preferBundledVersion' ) ;
17
-
18
- const currentDirectory = __dirname ;
19
- const workingDirectory = ! targetPath
20
- ? tl . getVariable ( "Build.SourcesDirectory" )
21
- : path . join ( tl . getVariable ( "Build.SourcesDirectory" ) , targetPath ) ;
22
-
23
- let gitVersionPath = tl . getInput ( 'gitVersionPath' ) ;
24
- if ( ! gitVersionPath ) {
25
- gitVersionPath = tl . which ( "GitVersion.exe" ) ;
26
- if ( preferBundledVersion || ! gitVersionPath ) {
27
- gitVersionPath = path . join ( currentDirectory , "GitVersion.exe" ) ;
28
- }
29
- }
30
-
31
- const execOptions : IExecOptions = {
32
- cwd : undefined ,
33
- env : undefined ,
34
- silent : undefined ,
35
- failOnStdErr : undefined ,
36
- ignoreReturnCode : undefined ,
37
- errStream : undefined ,
38
- outStream : undefined ,
39
- windowsVerbatimArguments : undefined
40
- } ;
41
-
42
- let toolRunner : ToolRunner ;
43
-
44
- const parser = new ArgumentParser ( ) ;
45
- parser . addArgument (
46
- [ '-r' , '--runtime' ] ,
47
- {
48
- help : '[full|netcore]' ,
49
- defaultValue : 'full'
50
- }
51
- ) ;
52
-
53
- const args = parser . parseArgs ( ) ;
54
- switch ( args . runtime ) {
55
- case 'netcore' :
56
- gitVersionPath = path . join ( currentDirectory , "netcore" , "GitVersion.dll" ) ;
57
- toolRunner = tl . tool ( "dotnet" ) ;
58
- toolRunner . arg ( gitVersionPath ) ;
59
- break ;
60
-
61
- case 'full' :
62
- default :
63
- const isWin32 = os . platform ( ) == "win32" ;
64
- if ( isWin32 ) {
65
- toolRunner = tl . tool ( gitVersionPath ) ;
66
- } else {
67
- toolRunner = tl . tool ( "mono" ) ;
68
- toolRunner . arg ( gitVersionPath ) ;
69
- }
70
-
71
- break ;
72
- }
73
-
74
- toolRunner . arg ( [
75
- workingDirectory ,
52
+ let exe = this . getExecutable ( ) ;
53
+ exe . arg ( [
54
+ this . workingDirectory ,
76
55
"/output" ,
77
56
"buildserver" ,
78
57
"/nofetch" ] ) ;
79
58
80
- if ( updateAssemblyInfo ) {
81
- toolRunner . arg ( "/updateassemblyinfo" ) ;
82
- if ( updateAssemblyInfoFilename ) {
83
- toolRunner . arg ( updateAssemblyInfoFilename ) ;
59
+ if ( this . updateAssemblyInfo ) {
60
+ exe . arg ( "/updateassemblyinfo" ) ;
61
+ if ( this . updateAssemblyInfoFilename ) {
62
+ exe . arg ( this . updateAssemblyInfoFilename ) ;
84
63
} else {
85
- toolRunner . arg ( "true" ) ;
64
+ exe . arg ( "true" ) ;
86
65
}
87
66
}
88
67
89
- if ( additionalArguments ) {
90
- toolRunner . line ( additionalArguments ) ;
68
+ if ( this . additionalArguments ) {
69
+ exe . line ( this . additionalArguments ) ;
91
70
}
92
71
93
- const result = await toolRunner . exec ( execOptions ) ;
72
+ const result = await exe . exec ( this . execOptions ) ;
94
73
if ( result ) {
95
74
tl . setResult ( tl . TaskResult . Failed , "An error occured during GitVersion execution" )
96
75
} else {
@@ -102,6 +81,40 @@ export class GitVersionTask {
102
81
tl . setResult ( tl . TaskResult . Failed , err ) ;
103
82
}
104
83
}
84
+
85
+ public getExecutable ( ) {
86
+ let exe : tr . ToolRunner ;
87
+
88
+ switch ( this . runtime ) {
89
+ case "full" :
90
+ const isWin32 = os . platform ( ) == "win32" ;
91
+ let exePath = this . getExecutablePath ( "GitVersion.exe" ) || tl . which ( "GitVersion.exe" , true ) ;
92
+ if ( isWin32 ) {
93
+ exe = tl . tool ( exePath ) ;
94
+ } else {
95
+ exe = tl . tool ( "mono" ) ;
96
+ exe . arg ( exePath ) ;
97
+ }
98
+ break ;
99
+ case "core" :
100
+ let assemblyPath = this . getExecutablePath ( "GitVersion.dll" ) ;
101
+ let dotnetPath = tl . which ( "dotnet" , true ) ;
102
+ exe = tl . tool ( dotnetPath ) ;
103
+ exe . arg ( assemblyPath ) ;
104
+ break ;
105
+ }
106
+
107
+ return exe ;
108
+ }
109
+
110
+ public getExecutablePath ( exeName :string ) {
111
+ if ( this . gitVersionPath ) {
112
+ return this . gitVersionPath ;
113
+ } else if ( this . preferBundledVersion ) {
114
+ return path . join ( this . currentDirectory , this . runtime , exeName ) ;
115
+ }
116
+ }
105
117
}
106
118
107
- GitVersionTask . execute ( ) ;
119
+ var exe = new GitVersionTask ( ) ;
120
+ exe . execute ( ) . catch ( ( reason ) => tl . setResult ( tl . TaskResult . Failed , reason ) ) ;
0 commit comments