Path: blob/main/build/azure-pipelines/common/extract-telemetry.ts
13383 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import cp from 'child_process';6import fs from 'fs';7import os from 'os';8import path from 'path';910const BUILD_STAGINGDIRECTORY = process.env.BUILD_STAGINGDIRECTORY ?? fs.mkdtempSync(path.join(os.tmpdir(), 'vscode-telemetry-'));11const BUILD_SOURCESDIRECTORY = process.env.BUILD_SOURCESDIRECTORY ?? path.resolve(import.meta.dirname, '..', '..', '..');1213const extractionDir = path.join(BUILD_STAGINGDIRECTORY, 'extraction');14fs.mkdirSync(extractionDir, { recursive: true });1516const repos = [17'https://github.com/microsoft/vscode-extension-telemetry.git',18'https://github.com/microsoft/vscode-chrome-debug-core.git',19'https://github.com/microsoft/vscode-node-debug2.git',20'https://github.com/microsoft/vscode-node-debug.git',21'https://github.com/microsoft/vscode-html-languageservice.git',22'https://github.com/microsoft/vscode-json-languageservice.git',23];2425for (const repo of repos) {26cp.execSync(`git clone --depth 1 ${repo}`, { cwd: extractionDir, stdio: 'inherit' });27}2829const extractor = path.join(BUILD_SOURCESDIRECTORY, 'node_modules', '@vscode', 'telemetry-extractor', 'out', 'extractor.js');30const telemetryConfig = path.join(BUILD_SOURCESDIRECTORY, 'build', 'azure-pipelines', 'common', 'telemetry-config.json');3132interface ITelemetryConfigEntry {33eventPrefix: string;34sourceDirs: string[];35excludedDirs: string[];36applyEndpoints: boolean;37patchDebugEvents?: boolean;38}3940const pipelineExtensionsPathPrefix = '../../s/extensions/';4142const telemetryConfigEntries = JSON.parse(fs.readFileSync(telemetryConfig, 'utf8')) as ITelemetryConfigEntry[];43let hasLocalConfigOverrides = false;4445const resolvedTelemetryConfigEntries = telemetryConfigEntries.map(entry => {46const sourceDirs = entry.sourceDirs.map(sourceDir => {47if (!sourceDir.startsWith(pipelineExtensionsPathPrefix)) {48return sourceDir;49}5051const sourceDirInExtractionDir = path.resolve(extractionDir, sourceDir);52if (fs.existsSync(sourceDirInExtractionDir)) {53return sourceDir;54}5556const extensionRelativePath = sourceDir.slice(pipelineExtensionsPathPrefix.length);57const sourceDirInWorkspace = path.join(BUILD_SOURCESDIRECTORY, 'extensions', extensionRelativePath);58if (fs.existsSync(sourceDirInWorkspace)) {59hasLocalConfigOverrides = true;60return sourceDirInWorkspace;61}6263return sourceDir;64});6566return {67...entry,68sourceDirs,69};70});7172const telemetryConfigForExtraction = hasLocalConfigOverrides73? path.join(extractionDir, 'telemetry-config.local.json')74: telemetryConfig;7576if (hasLocalConfigOverrides) {77fs.writeFileSync(telemetryConfigForExtraction, JSON.stringify(resolvedTelemetryConfigEntries, null, '\t'));78}7980try {81cp.execSync(`node "${extractor}" --sourceDir "${BUILD_SOURCESDIRECTORY}" --excludedDir "${path.join(BUILD_SOURCESDIRECTORY, 'extensions')}" --outputDir . --applyEndpoints`, { cwd: extractionDir, stdio: 'inherit' });82cp.execSync(`node "${extractor}" --config "${telemetryConfigForExtraction}" -o .`, { cwd: extractionDir, stdio: 'inherit' });83} catch (error) {84const message = error instanceof Error ? error.message : String(error);85console.error(`Telemetry extraction failed: ${message}`);86process.exit(1);87}8889const telemetryDir = path.join(BUILD_SOURCESDIRECTORY, '.build', 'telemetry');90fs.mkdirSync(telemetryDir, { recursive: true });91fs.renameSync(path.join(extractionDir, 'declarations-resolved.json'), path.join(telemetryDir, 'telemetry-core.json'));92fs.renameSync(path.join(extractionDir, 'config-resolved.json'), path.join(telemetryDir, 'telemetry-extensions.json'));9394fs.rmSync(extractionDir, { recursive: true, force: true });959697