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' ;
56
67export class GitVersionTask {
78 execOptions : tr . IExecOptions ;
@@ -15,31 +16,26 @@ export class GitVersionTask {
1516 additionalArguments : string ;
1617 targetPath : string ;
1718 sourcesDirectory : string ;
18- currentDirectory : string ;
19- workingDirectory : string ;
2019 gitVersionPath : string ;
2120 runtime : string ;
2221
2322 constructor ( ) {
24- this . preferBundledVersion = tl . getBoolInput ( 'preferBundledVersion' ) || true ;
23+
24+ this . targetPath = tl . getInput ( 'targetPath' ) ;
2525
2626 this . useConfigFile = tl . getBoolInput ( 'useConfigFile' ) ;
2727 this . configFilePath = tl . getInput ( 'configFilePath' ) ;
2828
2929 this . updateAssemblyInfo = tl . getBoolInput ( 'updateAssemblyInfo' ) ;
3030 this . updateAssemblyInfoFilename = tl . getInput ( 'updateAssemblyInfoFilename' ) ;
3131
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' ;
3534 this . gitVersionPath = tl . getInput ( 'gitVersionPath' ) ;
3635
37- this . sourcesDirectory = tl . getVariable ( "Build.SourcesDirectory" ) ;
36+ this . additionalArguments = tl . getInput ( 'additionalArguments' ) ;
3837
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, '/' ) ;
4339
4440 this . execOptions = {
4541 cwd : undefined ,
@@ -55,9 +51,10 @@ export class GitVersionTask {
5551
5652 public async execute ( ) {
5753 try {
54+ let workingDirectory = this . getWorkingDirectory ( this . targetPath ) ;
5855 let exe = this . getExecutable ( ) ;
5956 exe . arg ( [
60- this . workingDirectory ,
57+ workingDirectory ,
6158 "/output" ,
6259 "buildserver" ,
6360 "/nofetch" ] ) ;
@@ -67,7 +64,7 @@ export class GitVersionTask {
6764 exe . arg ( [ "/config" , this . configFilePath ] ) ;
6865 }
6966 else {
70- throw 'GitVersion configuration file not found at ' + this . configFilePath ;
67+ throw new Error ( 'GitVersion configuration file not found at ' + this . configFilePath ) ;
7168 }
7269 }
7370
@@ -77,7 +74,7 @@ export class GitVersionTask {
7774 exe . arg ( this . updateAssemblyInfoFilename ) ;
7875 }
7976 else {
80- throw 'AssemblyInfoFilename file not found at ' + this . updateAssemblyInfoFilename ;
77+ throw new Error ( 'AssemblyInfoFilename file not found at ' + this . updateAssemblyInfoFilename ) ;
8178 }
8279 }
8380
@@ -87,14 +84,14 @@ export class GitVersionTask {
8784
8885 const result = await exe . exec ( this . execOptions ) ;
8986 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" ) ;
9188 } else {
92- tl . setResult ( tl . TaskResult . Succeeded , "GitVersion executed successfully" )
89+ tl . setResult ( tl . TaskResult . Succeeded , "GitVersion executed successfully" ) ;
9390 }
9491 }
9592 catch ( err ) {
9693 tl . debug ( err . stack ) ;
97- tl . setResult ( tl . TaskResult . Failed , err ) ;
94+ tl . setResult ( tl . TaskResult . Failed , err , true ) ;
9895 }
9996 }
10097
@@ -124,13 +121,37 @@ export class GitVersionTask {
124121 }
125122
126123 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+ }
131151 }
152+ return workDir . replace ( / \\ / g, '/' ) ;
132153 }
133154}
134155
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