-
Notifications
You must be signed in to change notification settings - Fork 440
/
debug.ts
351 lines (309 loc) · 9.7 KB
/
debug.ts
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import * as path from "path";
import * as os from "os";
import { logger } from "./logging";
import { exec } from "./util";
import { terraformVersion } from "./terraform";
import { DISPLAY_VERSION } from "./version";
export function getLanguage(projectPath = process.cwd()): string | undefined {
try {
const cdktfJson = require(path.resolve(projectPath, "cdktf.json"));
return cdktfJson.language;
} catch {
// We can not detect the language
logger.debug(`Unable to detect language in ${projectPath}`);
return undefined;
}
}
async function getBinaryVersion(
binary: string,
versionCommand: string
): Promise<string | undefined> {
const noOp = () => {}; // eslint-disable-line @typescript-eslint/no-empty-function
try {
const result = await exec(
binary,
[versionCommand],
{ env: process.env, stdio: [undefined, undefined, "ignore"] },
noOp,
noOp
);
return result.trim().replace(/\r?\n|\r/g, "");
} catch (e) {
logger.debug(`Failed to run ${binary} ${versionCommand}: ${e}`);
return undefined;
}
}
export function getDotnetVersion() {
return getBinaryVersion("dotnet", "--version");
}
export function getJavaVersion() {
return getBinaryVersion("java", "-version");
}
export function getMavenVersion() {
return getBinaryVersion("mvn", "--version");
}
export async function getPythonVersion() {
return (
(await getBinaryVersion("python3", "--version")) ||
(await getBinaryVersion("python", "--version"))
);
}
export function getPipVersion() {
return getBinaryVersion("pip", "--version");
}
export function getPipenvVersion() {
return getBinaryVersion("pipenv", "--version");
}
export function getGoVersion() {
return getBinaryVersion("go", "version");
}
export function getNodeVersion() {
return getBinaryVersion("node", "--version");
}
async function getNodeModuleVersion(
packageName: string
): Promise<string | undefined> {
let output;
try {
output = await exec("npm", ["list", packageName, "--json"], {
env: { ...process.env },
});
} catch (e) {
logger.debug(`Unable to run 'npm list ${packageName} --json': ${e}`);
return undefined;
}
let json;
try {
json = JSON.parse(output);
} catch (e) {
logger.debug(
`Unable to parse output of 'npm list ${packageName} --json': ${e}`
);
return undefined;
}
if (
!json.dependencies ||
!json.dependencies[packageName] ||
!json.dependencies[packageName].version
) {
logger.debug(
`Unable to find '${packageName}' in 'npm list ${packageName} --json': ${output}`
);
return undefined;
}
return json.dependencies[packageName].version;
}
async function getPythonPackageVersion(
packageName: string
): Promise<string | undefined> {
let output;
try {
output = await exec("pipenv", ["run", "pip", "show", packageName], {
env: process.env,
});
} catch (e) {
logger.debug(`Unable to run 'pipenv run pip show ${packageName}': ${e}`);
}
// If we couldn't get the output using pipenv, try to get it using pip directly
if (!output) {
try {
output = await exec("pip", ["show", packageName], {
env: process.env,
});
} catch (e) {
logger.debug(`Unable to run 'pip show ${packageName}': ${e}`);
}
}
if (!output) {
return undefined;
}
const versionInfo = output
.split(/\r\n|\r|\n/)
.find((line) => line.startsWith("Version:"));
if (!versionInfo) {
logger.debug(
`Unable to find version in output of 'pipenv run pip show ${packageName}' / 'pip show ${packageName}': ${output}`
);
return undefined;
}
const version = versionInfo.split(":")[1].trim();
return version;
}
// Once https://github.com/NuGet/Home/issues/7752 gets resolved we can also try with --json
async function getCSharpPackageVersion(packageName: string) {
const translationMap: Record<string, string> = {
jsii: "Amazon.JSII.Runtime",
constructs: "Constructs",
cdktf: "HashiCorp.Cdktf",
};
const cSharpPackageName = translationMap[packageName] || packageName;
let output;
try {
output = await exec("dotnet", ["list", "package", "--include-transitive"], {
env: process.env,
});
} catch (e) {
logger.debug(
`Unable to run 'dotnet list package --include-transitive': ${e}`
);
return undefined;
}
const versionLine = output
.split(/\r\n|\r|\n/)
.find((line) => line.includes(`> ${cSharpPackageName}`));
if (!versionLine) {
logger.debug(
`Unable to find version for '${cSharpPackageName}' in output of 'dotnet list package --include-transitive': ${output}`
);
return undefined;
}
// A line might look like this:
// Transitive Package Resolved
// > Amazon.JSII.Runtime 1.17.1
// Therefore we reverse split by whitespace and reverse
// so that the first thing we find is the version number
return versionLine
.split(" ")
.reverse()
.find((part) => part !== "");
}
async function getGoPackageVersion(packageName: string) {
const translationMap: Record<string, string> = {
jsii: "jsii-runtime-go",
cdktf: "github.com/hashicorp/terraform-cdk-go",
};
const goPackageName = translationMap[packageName] || packageName;
let output;
try {
output = await exec("go", ["list", "-m", "all"], {
env: process.env,
});
} catch (e) {
logger.debug(`Unable to run 'go list -m all': ${e}`);
return undefined;
}
let versionLine = output
.split(/\r\n|\r|\n/)
.find((line) => line.includes(goPackageName));
if (!versionLine) {
logger.debug(
`Unable to find version for '${goPackageName}' in output of 'go list -m all': ${output}`
);
return undefined;
}
// We are dealing with a local version
if (versionLine.includes("=>")) {
logger.debug(`Found local version for '${goPackageName}': ${versionLine}`);
versionLine = versionLine.split("=>")[0].trim();
}
return versionLine.split(" ").pop();
}
async function getJavaPackageVersion(packageName: string) {
const translationMap: Record<string, string> = {
jsii: "jsii-runtime",
};
const javaPackageName = translationMap[packageName] || packageName;
let output;
try {
output = await exec("mvn", ["dependency:list"], {
env: process.env,
});
} catch (e) {
logger.debug(`Unable to run 'mvn dependency:list': ${e}`);
return undefined;
}
const resolutionPart = output.split(
"The following files have been resolved"
)[1];
if (!resolutionPart) {
logger.debug(
`Unable to find resolution passage in output of 'mvn dependency:list': ${output}`
);
return undefined;
}
// We need to search for the package name AND a colon to not match a line like
// [INFO] com.hashicorp:cdktf-provider-aws:jar:9.0.9:compile
// when looking for the cdktf package (see Github issue #1994)
const versionLine = resolutionPart
.split(/\r\n|\r|\n/)
.find((line) => line.includes(`${javaPackageName}:`));
if (!versionLine) {
logger.debug(
`Unable to find version for '${javaPackageName}' in output of 'mvn dependency:list': ${output}`
);
return undefined;
}
// Example line: [INFO] com.hashicorp:cdktf:jar:0.0.0:compile
const versionStartDelimiter = `${javaPackageName}:jar:`;
const versionStart =
versionLine.indexOf(versionStartDelimiter) + versionStartDelimiter.length;
const versionEndDelemiter = ":compile";
const versionEnd = versionLine.indexOf(versionEndDelemiter);
return versionLine.substring(versionStart, versionEnd);
}
export async function getPackageVersion(
language: string,
packageName: string
): Promise<string | undefined> {
const noOp = async () => undefined; // eslint-disable-line @typescript-eslint/no-empty-function
const getLibraryVersionMap: Record<
string,
(name: string) => Promise<string | undefined>
> = {
typescript: getNodeModuleVersion,
python: getPythonPackageVersion,
go: getGoPackageVersion,
csharp: getCSharpPackageVersion,
java: getJavaPackageVersion,
};
const libVersion = await (getLibraryVersionMap[language] || noOp)(
packageName
);
return libVersion ?? undefined;
}
export async function collectDebugInformation() {
const debugOutput: Record<string, string | null> = {};
const language = getLanguage();
debugOutput["language"] = language ?? null;
debugOutput["cdktf-cli"] = DISPLAY_VERSION;
const node = getNodeVersion();
debugOutput["node"] = (await node) ?? null;
if (language) {
const cdktf = getPackageVersion(language, "cdktf");
const constructs = getPackageVersion(language, "constructs");
const jsii = getPackageVersion(language, "jsii");
debugOutput["cdktf"] = (await cdktf) ?? null;
debugOutput["constructs"] = (await constructs) ?? null;
debugOutput["jsii"] = (await jsii) ?? null;
}
debugOutput["terraform"] = await terraformVersion;
debugOutput["arch"] = os.arch();
debugOutput["os"] = `${os.platform()} ${os.release()}`;
switch (language) {
case "python":
{
const python = getPythonVersion();
const pip = getPipVersion();
const pipenv = getPipenvVersion();
debugOutput["python"] = (await python) ?? null;
debugOutput["pip"] = (await pip) ?? null;
debugOutput["pipenv"] = (await pipenv) ?? null;
}
break;
case "java":
{
const java = getJavaVersion();
const maven = getMavenVersion();
debugOutput["java"] = (await java) ?? null;
debugOutput["maven"] = (await maven) ?? null;
}
break;
case "csharp":
debugOutput["dotnet"] = (await getDotnetVersion()) ?? null;
break;
case "go":
debugOutput["go"] = (await getGoVersion()) ?? null;
break;
}
return debugOutput;
}