1
- import tl = require( 'azure-pipelines-task-lib/task' ) ;
2
- import tr = require( 'azure-pipelines-task-lib/toolrunner' ) ;
3
- import path = require( 'path' ) ;
4
- import os = require( 'os' ) ;
1
+ import * as path from 'path' ;
2
+ import * as os from 'os' ;
3
+
4
+ import * as tl from 'azure-pipelines-task-lib/task' ;
5
+ import * as tr from 'azure-pipelines-task-lib/toolrunner' ;
5
6
6
7
export class GitVersionTask {
7
8
execOptions : tr . IExecOptions ;
@@ -15,31 +16,26 @@ export class GitVersionTask {
15
16
additionalArguments : string ;
16
17
targetPath : string ;
17
18
sourcesDirectory : string ;
18
- currentDirectory : string ;
19
- workingDirectory : string ;
20
19
gitVersionPath : string ;
21
20
runtime : string ;
22
21
23
22
constructor ( ) {
24
- this . preferBundledVersion = tl . getBoolInput ( 'preferBundledVersion' ) || true ;
23
+
24
+ this . targetPath = tl . getInput ( 'targetPath' ) ;
25
25
26
26
this . useConfigFile = tl . getBoolInput ( 'useConfigFile' ) ;
27
27
this . configFilePath = tl . getInput ( 'configFilePath' ) ;
28
28
29
29
this . updateAssemblyInfo = tl . getBoolInput ( 'updateAssemblyInfo' ) ;
30
30
this . updateAssemblyInfoFilename = tl . getInput ( 'updateAssemblyInfoFilename' ) ;
31
31
32
- this . additionalArguments = tl . getInput ( 'additionalArguments' ) ;
33
- this . targetPath = tl . getInput ( 'targetPath' ) ;
34
- this . runtime = tl . getInput ( 'runtime' ) || "core" ;
32
+ this . preferBundledVersion = tl . getBoolInput ( 'preferBundledVersion' ) ;
33
+ this . runtime = tl . getInput ( 'runtime' ) || 'core' ;
35
34
this . gitVersionPath = tl . getInput ( 'gitVersionPath' ) ;
36
35
37
- this . sourcesDirectory = tl . getVariable ( "Build.SourcesDirectory" ) ;
36
+ this . additionalArguments = tl . getInput ( 'additionalArguments' ) ;
38
37
39
- this . currentDirectory = __dirname ;
40
- this . workingDirectory = ! this . targetPath
41
- ? this . sourcesDirectory
42
- : path . join ( this . sourcesDirectory , this . targetPath ) ;
38
+ this . sourcesDirectory = tl . getVariable ( 'Build.SourcesDirectory' ) . replace ( / \\ / g, '/' ) ;
43
39
44
40
this . execOptions = {
45
41
cwd : undefined ,
@@ -55,9 +51,10 @@ export class GitVersionTask {
55
51
56
52
public async execute ( ) {
57
53
try {
54
+ let workingDirectory = this . getWorkingDirectory ( this . targetPath ) ;
58
55
let exe = this . getExecutable ( ) ;
59
56
exe . arg ( [
60
- this . workingDirectory ,
57
+ workingDirectory ,
61
58
"/output" ,
62
59
"buildserver" ,
63
60
"/nofetch" ] ) ;
@@ -67,7 +64,7 @@ export class GitVersionTask {
67
64
exe . arg ( [ "/config" , this . configFilePath ] ) ;
68
65
}
69
66
else {
70
- throw 'GitVersion configuration file not found at ' + this . configFilePath ;
67
+ throw new Error ( 'GitVersion configuration file not found at ' + this . configFilePath ) ;
71
68
}
72
69
}
73
70
@@ -77,7 +74,7 @@ export class GitVersionTask {
77
74
exe . arg ( this . updateAssemblyInfoFilename ) ;
78
75
}
79
76
else {
80
- throw 'AssemblyInfoFilename file not found at ' + this . updateAssemblyInfoFilename ;
77
+ throw new Error ( 'AssemblyInfoFilename file not found at ' + this . updateAssemblyInfoFilename ) ;
81
78
}
82
79
}
83
80
@@ -87,14 +84,14 @@ export class GitVersionTask {
87
84
88
85
const result = await exe . exec ( this . execOptions ) ;
89
86
if ( result ) {
90
- tl . setResult ( tl . TaskResult . Failed , "An error occured during GitVersion execution" )
87
+ tl . setResult ( tl . TaskResult . Failed , "An error occured during GitVersion execution" ) ;
91
88
} else {
92
- tl . setResult ( tl . TaskResult . Succeeded , "GitVersion executed successfully" )
89
+ tl . setResult ( tl . TaskResult . Succeeded , "GitVersion executed successfully" ) ;
93
90
}
94
91
}
95
92
catch ( err ) {
96
93
tl . debug ( err . stack ) ;
97
- tl . setResult ( tl . TaskResult . Failed , err ) ;
94
+ tl . setResult ( tl . TaskResult . Failed , err , true ) ;
98
95
}
99
96
}
100
97
@@ -124,13 +121,37 @@ export class GitVersionTask {
124
121
}
125
122
126
123
public getExecutablePath ( exeName :string ) {
127
- if ( this . gitVersionPath ) {
128
- return this . gitVersionPath ;
129
- } else if ( this . preferBundledVersion ) {
130
- return path . join ( this . currentDirectory , this . runtime , exeName ) ;
124
+ let exePath ;
125
+ if ( this . preferBundledVersion ) {
126
+ let currentDirectory = __dirname ;
127
+ exePath = path . join ( currentDirectory , this . runtime , exeName ) ;
128
+ } else {
129
+ if ( tl . filePathSupplied ( 'gitVersionPath' ) && tl . exist ( this . gitVersionPath ) && tl . stats ( this . gitVersionPath ) . isFile ( ) ) {
130
+ exePath = this . gitVersionPath ;
131
+ } else {
132
+ throw new Error ( 'GitVersion executable not found at ' + this . gitVersionPath ) ;
133
+ }
134
+ }
135
+
136
+ return exePath . replace ( / \\ / g, '/' ) ;
137
+ }
138
+
139
+ public getWorkingDirectory ( targetPath : string ) {
140
+ let workDir ;
141
+
142
+ if ( ! targetPath ) {
143
+ workDir = this . sourcesDirectory ;
144
+ } else {
145
+ if ( tl . exist ( targetPath ) && tl . stats ( targetPath ) . isDirectory ( ) ) {
146
+ workDir = path . join ( this . sourcesDirectory , targetPath ) ;
147
+ }
148
+ else {
149
+ throw new Error ( 'Directory not found at ' + targetPath ) ;
150
+ }
131
151
}
152
+ return workDir . replace ( / \\ / g, '/' ) ;
132
153
}
133
154
}
134
155
135
- var exe = new GitVersionTask ( ) ;
136
- exe . execute ( ) . catch ( ( reason ) => tl . setResult ( tl . TaskResult . Failed , reason ) ) ;
156
+ var task = new GitVersionTask ( ) ;
157
+ task . execute ( ) . catch ( ( reason ) => tl . setResult ( tl . TaskResult . Failed , reason ) ) ;
0 commit comments