|
| 1 | + |
| 2 | +import {Displayable, ProgressNotification, UxDisplay} from "./display"; |
| 3 | +import {Logger} from "./logger"; |
| 4 | +import {DiagnosticConvertible, DiagnosticManager} from "./diagnostics"; |
| 5 | +import vscode from "vscode"; |
| 6 | +import {messages} from "./messages"; |
| 7 | +import {TelemetryService} from "./external-services/telemetry-service"; |
| 8 | +import {SettingsManager} from "./settings"; |
| 9 | +import {CliScannerV5Strategy} from "./scanner-strategies/v5-scanner"; |
| 10 | +import {CliScannerV4Strategy} from "./scanner-strategies/v4-scanner"; |
| 11 | +import {ScannerAction, ScannerDependencies} from "./actions/scanner-action"; |
| 12 | +import * as Constants from './constants'; |
| 13 | + |
| 14 | +export class CodeAnalyzerRunner { |
| 15 | + private readonly diagnosticManager: DiagnosticManager; |
| 16 | + private readonly settingsManager: SettingsManager; |
| 17 | + private readonly telemetryService: TelemetryService; |
| 18 | + private readonly logger: Logger; |
| 19 | + |
| 20 | + constructor(diagnosticManager: DiagnosticManager, settingsManager: SettingsManager, telemetryService: TelemetryService, logger: Logger) { |
| 21 | + this.diagnosticManager = diagnosticManager; |
| 22 | + this.settingsManager = settingsManager; |
| 23 | + this.telemetryService = telemetryService; |
| 24 | + this.logger = logger; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Runs the scanner against the specified file and displays the results. |
| 29 | + * @param commandName The command being run |
| 30 | + * @param targets The files/folders to run against |
| 31 | + */ |
| 32 | + async runAndDisplay(commandName: string, targets: string[]): Promise<void> { |
| 33 | + const startTime = Date.now(); |
| 34 | + try { |
| 35 | + return await vscode.window.withProgress({ |
| 36 | + location: vscode.ProgressLocation.Notification |
| 37 | + }, async (progress) => { |
| 38 | + const display: UxDisplay = new UxDisplay(new VSCodeDisplayable((notif: ProgressNotification) => progress.report(notif), this.logger)); |
| 39 | + const scannerStrategy = this.settingsManager.getCodeAnalyzerV5Enabled() |
| 40 | + ? new CliScannerV5Strategy({ |
| 41 | + tags: this.settingsManager.getCodeAnalyzerTags() |
| 42 | + }) |
| 43 | + : new CliScannerV4Strategy({ |
| 44 | + engines: this.settingsManager.getEnginesToRun(), |
| 45 | + pmdCustomConfigFile: this.settingsManager.getPmdCustomConfigFile(), |
| 46 | + rulesCategory: this.settingsManager.getRulesCategory(), |
| 47 | + normalizeSeverity: this.settingsManager.getNormalizeSeverityEnabled() |
| 48 | + }); |
| 49 | + const actionDependencies: ScannerDependencies = { |
| 50 | + scannerStrategy: scannerStrategy, |
| 51 | + display: display, |
| 52 | + diagnosticManager: this.diagnosticManager, |
| 53 | + telemetryService: this.telemetryService |
| 54 | + }; |
| 55 | + const scannerAction = new ScannerAction(commandName, actionDependencies); |
| 56 | + await scannerAction.runScanner(targets); |
| 57 | + }); |
| 58 | + } catch (e) { |
| 59 | + const errMsg = e instanceof Error ? e.message : e as string; |
| 60 | + this.telemetryService.sendException(Constants.TELEM_FAILED_STATIC_ANALYSIS, errMsg, { |
| 61 | + executedCommand: commandName, |
| 62 | + duration: (Date.now() - startTime).toString() |
| 63 | + }); |
| 64 | + // This has to be a floating promise, since the command won't complete until |
| 65 | + // the error is dismissed. |
| 66 | + vscode.window.showErrorMessage(messages.error.analysisFailedGenerator(errMsg)); |
| 67 | + this.logger.error(errMsg); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class VSCodeDisplayable implements Displayable { |
| 73 | + private readonly progressCallback: (notif: ProgressNotification) => void; |
| 74 | + private readonly logger: Logger; |
| 75 | + |
| 76 | + public constructor(progressCallback: (notif: ProgressNotification) => void, logger: Logger) { |
| 77 | + this.progressCallback = progressCallback; |
| 78 | + this.logger = logger; |
| 79 | + } |
| 80 | + |
| 81 | + public progress(notification: ProgressNotification): void { |
| 82 | + this.progressCallback(notification); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Display a Toast summarizing the results of a non-DFA scan, i.e. how many files were scanned, how many had violations, and how many violations were found. |
| 87 | + * @param allTargets The files that were scanned. This may be a superset of the files that actually had violations. |
| 88 | + * @param results The results of a scan. |
| 89 | + */ |
| 90 | + public async results(allTargets: string[], results: DiagnosticConvertible[]): Promise<void> { |
| 91 | + const uniqueFiles: Set<string> = new Set(); |
| 92 | + for (const result of results) { |
| 93 | + uniqueFiles.add(result.locations[result.primaryLocationIndex].file); |
| 94 | + } |
| 95 | + await vscode.window.showInformationMessage(messages.info.finishedScan(allTargets.length, uniqueFiles.size, results.length)); |
| 96 | + } |
| 97 | + |
| 98 | + public log(msg: string): void { |
| 99 | + this.logger.log(msg); |
| 100 | + } |
| 101 | +} |
0 commit comments