@@ -27,10 +27,9 @@ const {
27
27
join
28
28
} = require ( 'path' ) ;
29
29
const {
30
- spawnSync
30
+ spawn
31
31
} = require ( 'child_process' ) ;
32
32
33
-
34
33
const ReplaceVersion = require ( './ReplaceVersion' )
35
34
36
35
const {
@@ -57,33 +56,53 @@ if (!buildFiles.length) {
57
56
return
58
57
}
59
58
60
- console . log ( 'Checking for upgrades...\n' )
59
+ exports . debugLog = debugLog ;
61
60
62
- const gradleDependencyUpdateArgs = [ 'dependencyUpdates' , '-DoutputFormatter=json' , '-DoutputDir=build/dependencyUpdates' ]
63
- const gradleDependencyUpdateResolution = argv . resolution
64
- if ( gradleDependencyUpdateResolution ) {
65
- gradleDependencyUpdateArgs . push ( `-Drevision=${ gradleDependencyUpdateResolution } ` )
66
- }
61
+ async function executeCommandAndWaitForExitCode ( command , args ) {
62
+ let commandExitCode
67
63
68
- debugLog ( `Executing command\n${ gradleCommand } ${ gradleDependencyUpdateArgs . join ( ' ' ) } \n` )
64
+ const child = spawn ( command , args ) ;
65
+ child . stdout . setEncoding ( 'utf8' ) ;
66
+ child . stdout . on ( 'data' , function ( data ) {
67
+ debugLog ( data ) ;
68
+ } )
69
69
70
- const gradleDependencyUpdateProcess = spawnSync ( gradleCommand , gradleDependencyUpdateArgs ) ;
70
+ child . on ( 'close' , ( code ) => {
71
+ commandExitCode = code
72
+ } )
71
73
72
- if ( gradleDependencyUpdateProcess . status !== 0 ) {
73
- informUserAboutInstallingUpdatePlugin ( ) ;
74
- return
75
- }
74
+ while ( commandExitCode === undefined ) {
75
+ debugLog ( 'Waiting for command to finish' )
76
+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
77
+ }
76
78
77
- if ( ! buildFiles . length ) {
78
- console . log ( 'Unable to find build.gradle, build.gradle.kts or external build file.' . bgRed ) ;
79
- return ;
79
+ return commandExitCode
80
80
}
81
81
82
- debugLog ( 'Build files ' + buildFiles ) ;
82
+ ( async ( ) => {
83
+ console . log ( 'Checking for upgrades...\n' )
83
84
84
- exports . debugLog = debugLog ;
85
+ const gradleDependencyUpdateArgs = [ 'dependencyUpdates' , '-DoutputFormatter=json' , '-DoutputDir=build/dependencyUpdates' ]
86
+ const gradleDependencyUpdateResolution = argv . resolution
87
+ if ( gradleDependencyUpdateResolution ) {
88
+ gradleDependencyUpdateArgs . push ( `-Drevision=${ gradleDependencyUpdateResolution } ` )
89
+ }
85
90
86
- ( async ( ) => {
91
+ debugLog ( `Executing command\n${ gradleCommand } ${ gradleDependencyUpdateArgs . join ( ' ' ) } \n` )
92
+
93
+ let gradleDependencyUpdateProcessExitCode = await executeCommandAndWaitForExitCode ( gradleCommand , gradleDependencyUpdateArgs )
94
+
95
+ if ( gradleDependencyUpdateProcessExitCode !== 0 ) {
96
+ informUserAboutInstallingUpdatePlugin ( gradleDependencyUpdateProcessExitCode ) ;
97
+ return
98
+ }
99
+
100
+ if ( ! buildFiles . length ) {
101
+ console . log ( 'Unable to find build.gradle, build.gradle.kts or external build file.' . bgRed ) ;
102
+ return ;
103
+ }
104
+
105
+ debugLog ( 'Build files ' + buildFiles ) ;
87
106
88
107
debugLog ( `Reading JSON report file\n` )
89
108
@@ -117,10 +136,11 @@ exports.debugLog = debugLog;
117
136
if ( response . upgrades . some ( it => it === 'gradle' ) ) {
118
137
console . log ( 'Upgrading gradle wrapper' )
119
138
const upgradeArgs = [ 'wrapper' , '--gradle-version=' + latestGradleRelease ]
139
+
120
140
debugLog ( `Executing command\n${ gradleCommand } ${ upgradeArgs . join ( ' ' ) } \n` )
121
- const upgradeGradleWrapper = spawnSync ( gradleCommand , upgradeArgs ) ;
141
+ let upgradeGradleWrapperExitCode = await executeCommandAndWaitForExitCode ( gradleCommand , upgradeArgs )
122
142
123
- if ( upgradeGradleWrapper . status !== 0 ) {
143
+ if ( upgradeGradleWrapperExitCode !== 0 ) {
124
144
console . log ( `Error upgrading gradle wrapper (StatusCode=${ upgradeGradleWrapper . status } ).` . bgRed )
125
145
console . log ( upgradeGradleWrapper . stderr . toString ( ) . red )
126
146
return
@@ -250,11 +270,10 @@ function findUpgradeJsonReportFiles() {
250
270
return upgradeReportFiles ;
251
271
}
252
272
253
- function informUserAboutInstallingUpdatePlugin ( ) {
273
+ function informUserAboutInstallingUpdatePlugin ( exitCode ) {
254
274
const newestVersion = '0.28.0'
255
275
256
- console . log ( `Error executing gradle dependency updates (StatusCode=${ gradleDependencyUpdateProcess . status } )` . bgRed ) ;
257
- console . log ( gradleDependencyUpdateProcess . stderr . toString ( ) . red ) ;
276
+ console . log ( `Error executing gradle dependency updates (StatusCode=${ exitCode } )` . bgRed ) ;
258
277
console . log ( `\nIn case you haven't installed the gradle-versions-plugin (https://github.com/ben-manes/gradle-versions-plugin), put one of the following in your gradle build file:\n` ) ;
259
278
console . log ( `Either Plugins block` ) ;
260
279
console . log ( `
@@ -275,5 +294,4 @@ function informUserAboutInstallingUpdatePlugin() {
275
294
276
295
apply plugin: "com.github.ben-manes.versions"
277
296
` . green ) ;
278
- }
279
-
297
+ }
0 commit comments