-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathexec-gradle.ts
More file actions
204 lines (186 loc) · 6.37 KB
/
exec-gradle.ts
File metadata and controls
204 lines (186 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
AggregateCreateNodesError,
NxJsonConfiguration,
workspaceRoot,
} from '@nx/devkit';
import { ExecFileOptions, execFile } from 'node:child_process';
import { existsSync } from 'node:fs';
import { dirname, join, isAbsolute } from 'node:path';
import { LARGE_BUFFER } from 'nx/src/executors/run-commands/run-commands.impl';
import { GradlePluginOptions } from '../plugin/utils/gradle-plugin-options';
import { signalToCode } from 'nx/src/utils/exit-codes';
export const fileSeparator = process.platform.startsWith('win')
? 'file:///'
: 'file://';
export const newLineSeparator = process.platform.startsWith('win')
? '\r\n'
: '\n';
/**
* For gradle command, it needs to be run from the directory of the gradle binary
* @returns gradle binary file name
*/
export function getGradleExecFile(): string {
return process.platform.startsWith('win') ? '.\\gradlew.bat' : './gradlew';
}
/**
* This function executes gradle with the given arguments
* @param gradleBinaryPath absolute path to gradle binary
* @param args args passed to gradle
* @param execOptions exec options
* @returns promise with the stdout buffer
*/
export function execGradleAsync(
gradleBinaryPath: string,
args: ReadonlyArray<string>,
execOptions: ExecFileOptions = {}
): Promise<Buffer> {
return new Promise<Buffer>((res, rej: (stdout: Buffer) => void) => {
const cp = execFile(
gradleBinaryPath,
args,
{
cwd: dirname(gradleBinaryPath),
shell: true,
windowsHide: true,
env: process.env,
maxBuffer: LARGE_BUFFER,
...execOptions,
},
undefined
);
let stdout = Buffer.from('');
cp.stdout?.on('data', (data) => {
stdout += data;
});
cp.stderr?.on('data', (data) => {
stdout += data;
});
cp.on('exit', (code, signal) => {
if (code === null) code = signalToCode(signal);
// Forcibly destroy streams to prevent Gradle daemon pipe hang.
// When shell: true is used, the Gradle daemon inherits the shell's
// stdout/stderr pipes and holds them open after the task completes.
// This prevents the shell wrapper from exiting, which in turn
// prevents the 'exit' event from firing on the ChildProcess.
// By this point all output has already been buffered, so destroying
// the streams is safe and releases the pipe FDs from Node's
// perspective.
// See: nodejs/node#5637, gradle/gradle#3987
cp.stdout?.destroy();
cp.stderr?.destroy();
if (code === 0) {
res(stdout);
} else {
rej(stdout);
}
});
});
}
export function getCustomGradleExecutableDirectoryFromPlugin(
nxJson: NxJsonConfiguration
): string | undefined {
const gradlePlugin = nxJson.plugins?.find((plugin) => {
if (typeof plugin === 'string') {
return plugin === '@nx/gradle';
}
return plugin.plugin === '@nx/gradle';
});
return gradlePlugin && typeof gradlePlugin !== 'string'
? (gradlePlugin.options as GradlePluginOptions)?.gradleExecutableDirectory
: undefined;
}
/**
* This function recursively finds the nearest gradlew file in the workspace
* @param filePathToSearch the original file to search for, relative to workspace root, file path not directory path
* @param workspaceRoot workspace root
* @param customExecutableDirectory a custom directory to search for the gradle wrapper file
* @returns the relative path of the gradlew file to workspace root, throws an error if gradlew file is not found
* It will return relative path to workspace root of gradlew.bat file on windows and gradlew file on other platforms
*/
export function findGradlewFile(
filePathToSearch: string,
workspaceRoot: string,
customExecutableDirectory?: string
): string {
if (customExecutableDirectory) {
return findGradlewUsingCustomExecutableDirectory(
customExecutableDirectory,
workspaceRoot
);
}
return findGradlewUsingFilePathTraversal(filePathToSearch, workspaceRoot);
}
export function findGradlewUsingFilePathTraversal(
filePathToSearch: string,
workspaceRoot: string,
currentSearchPath?: string
) {
currentSearchPath ??= filePathToSearch;
const parent = dirname(currentSearchPath);
if (currentSearchPath === parent) {
throw new AggregateCreateNodesError(
[
[
filePathToSearch,
new Error(
`No Gradlew file found at ${filePathToSearch} or any of its parent directories. Run "gradle init"`
),
],
],
[]
);
}
const gradlewPath = join(parent, 'gradlew');
const gradlewBatPath = join(parent, 'gradlew.bat');
if (process.platform.startsWith('win')) {
if (existsSync(join(workspaceRoot, gradlewBatPath))) {
return gradlewBatPath;
}
} else {
if (existsSync(join(workspaceRoot, gradlewPath))) {
return gradlewPath;
}
}
return findGradlewUsingFilePathTraversal(
filePathToSearch,
workspaceRoot,
parent
);
}
export function findGradlewUsingCustomExecutableDirectory(
customGradleExecutableDirectory: string,
workspaceRoot: string
) {
// Resolve the custom installation path - if relative, resolve against workspace root
const resolvedInstallationPath = isAbsolute(customGradleExecutableDirectory)
? customGradleExecutableDirectory
: join(workspaceRoot, customGradleExecutableDirectory);
const customGradlewPath = join(resolvedInstallationPath, 'gradlew');
const customGradlewBatPath = join(resolvedInstallationPath, 'gradlew.bat');
if (process.platform.startsWith('win')) {
if (existsSync(customGradlewBatPath)) {
// Return path relative to workspace root if it was relative, otherwise return absolute
return isAbsolute(customGradleExecutableDirectory)
? customGradlewBatPath
: join(customGradleExecutableDirectory, 'gradlew.bat');
}
} else {
if (existsSync(customGradlewPath)) {
// Return path relative to workspace root if it was relative, otherwise return absolute
return isAbsolute(customGradleExecutableDirectory)
? customGradlewPath
: join(customGradleExecutableDirectory, 'gradlew');
}
}
throw new AggregateCreateNodesError(
[
[
customGradleExecutableDirectory,
new Error(
`No Gradlew file found at custom gradle executable directory. Please ensure that there is a gradle wrapper file located at ${customGradleExecutableDirectory}`
),
],
],
[]
);
}