Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/azure-pipelines/common/extract-telemetry.ts
13383 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import cp from 'child_process';
7
import fs from 'fs';
8
import os from 'os';
9
import path from 'path';
10
11
const BUILD_STAGINGDIRECTORY = process.env.BUILD_STAGINGDIRECTORY ?? fs.mkdtempSync(path.join(os.tmpdir(), 'vscode-telemetry-'));
12
const BUILD_SOURCESDIRECTORY = process.env.BUILD_SOURCESDIRECTORY ?? path.resolve(import.meta.dirname, '..', '..', '..');
13
14
const extractionDir = path.join(BUILD_STAGINGDIRECTORY, 'extraction');
15
fs.mkdirSync(extractionDir, { recursive: true });
16
17
const repos = [
18
'https://github.com/microsoft/vscode-extension-telemetry.git',
19
'https://github.com/microsoft/vscode-chrome-debug-core.git',
20
'https://github.com/microsoft/vscode-node-debug2.git',
21
'https://github.com/microsoft/vscode-node-debug.git',
22
'https://github.com/microsoft/vscode-html-languageservice.git',
23
'https://github.com/microsoft/vscode-json-languageservice.git',
24
];
25
26
for (const repo of repos) {
27
cp.execSync(`git clone --depth 1 ${repo}`, { cwd: extractionDir, stdio: 'inherit' });
28
}
29
30
const extractor = path.join(BUILD_SOURCESDIRECTORY, 'node_modules', '@vscode', 'telemetry-extractor', 'out', 'extractor.js');
31
const telemetryConfig = path.join(BUILD_SOURCESDIRECTORY, 'build', 'azure-pipelines', 'common', 'telemetry-config.json');
32
33
interface ITelemetryConfigEntry {
34
eventPrefix: string;
35
sourceDirs: string[];
36
excludedDirs: string[];
37
applyEndpoints: boolean;
38
patchDebugEvents?: boolean;
39
}
40
41
const pipelineExtensionsPathPrefix = '../../s/extensions/';
42
43
const telemetryConfigEntries = JSON.parse(fs.readFileSync(telemetryConfig, 'utf8')) as ITelemetryConfigEntry[];
44
let hasLocalConfigOverrides = false;
45
46
const resolvedTelemetryConfigEntries = telemetryConfigEntries.map(entry => {
47
const sourceDirs = entry.sourceDirs.map(sourceDir => {
48
if (!sourceDir.startsWith(pipelineExtensionsPathPrefix)) {
49
return sourceDir;
50
}
51
52
const sourceDirInExtractionDir = path.resolve(extractionDir, sourceDir);
53
if (fs.existsSync(sourceDirInExtractionDir)) {
54
return sourceDir;
55
}
56
57
const extensionRelativePath = sourceDir.slice(pipelineExtensionsPathPrefix.length);
58
const sourceDirInWorkspace = path.join(BUILD_SOURCESDIRECTORY, 'extensions', extensionRelativePath);
59
if (fs.existsSync(sourceDirInWorkspace)) {
60
hasLocalConfigOverrides = true;
61
return sourceDirInWorkspace;
62
}
63
64
return sourceDir;
65
});
66
67
return {
68
...entry,
69
sourceDirs,
70
};
71
});
72
73
const telemetryConfigForExtraction = hasLocalConfigOverrides
74
? path.join(extractionDir, 'telemetry-config.local.json')
75
: telemetryConfig;
76
77
if (hasLocalConfigOverrides) {
78
fs.writeFileSync(telemetryConfigForExtraction, JSON.stringify(resolvedTelemetryConfigEntries, null, '\t'));
79
}
80
81
try {
82
cp.execSync(`node "${extractor}" --sourceDir "${BUILD_SOURCESDIRECTORY}" --excludedDir "${path.join(BUILD_SOURCESDIRECTORY, 'extensions')}" --outputDir . --applyEndpoints`, { cwd: extractionDir, stdio: 'inherit' });
83
cp.execSync(`node "${extractor}" --config "${telemetryConfigForExtraction}" -o .`, { cwd: extractionDir, stdio: 'inherit' });
84
} catch (error) {
85
const message = error instanceof Error ? error.message : String(error);
86
console.error(`Telemetry extraction failed: ${message}`);
87
process.exit(1);
88
}
89
90
const telemetryDir = path.join(BUILD_SOURCESDIRECTORY, '.build', 'telemetry');
91
fs.mkdirSync(telemetryDir, { recursive: true });
92
fs.renameSync(path.join(extractionDir, 'declarations-resolved.json'), path.join(telemetryDir, 'telemetry-core.json'));
93
fs.renameSync(path.join(extractionDir, 'config-resolved.json'), path.join(telemetryDir, 'telemetry-extensions.json'));
94
95
fs.rmSync(extractionDir, { recursive: true, force: true });
96
97