|
| 1 | +import { OutputChannel, Uri, window, WorkspaceConfiguration, WorkspaceFolder } from 'vscode'; |
| 2 | +import { expandHomeDir, IEnvVars } from './utils'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { Logger } from 'vscode-languageclient'; |
| 5 | +import { ExtensionLogger } from './logger'; |
| 6 | +import { GHCupConfig } from './ghcup'; |
| 7 | + |
| 8 | +export type LogLevel = 'off' | 'messages' | 'verbose'; |
| 9 | +export type ClientLogLevel = 'off' | 'error' | 'info' | 'debug'; |
| 10 | + |
| 11 | +export type Config = { |
| 12 | + /** |
| 13 | + * Unique name per workspace folder (useful for multi-root workspaces). |
| 14 | + */ |
| 15 | + langName: string; |
| 16 | + logLevel: LogLevel; |
| 17 | + clientLogLevel: ClientLogLevel; |
| 18 | + logFilePath?: string; |
| 19 | + workingDir: string; |
| 20 | + outputChannel: OutputChannel; |
| 21 | + serverArgs: string[]; |
| 22 | + serverEnvironment: IEnvVars; |
| 23 | + ghcupConfig: GHCupConfig; |
| 24 | +}; |
| 25 | + |
| 26 | +export function initConfig(workspaceConfig: WorkspaceConfiguration, uri: Uri, folder?: WorkspaceFolder): Config { |
| 27 | + // Set a unique name per workspace folder (useful for multi-root workspaces). |
| 28 | + const langName = 'Haskell' + (folder ? ` (${folder.name})` : ''); |
| 29 | + const currentWorkingDir = folder ? folder.uri.fsPath : path.dirname(uri.fsPath); |
| 30 | + |
| 31 | + const logLevel = getLogLevel(workspaceConfig); |
| 32 | + const clientLogLevel = getClientLogLevel(workspaceConfig); |
| 33 | + |
| 34 | + const logFile = getLogFile(workspaceConfig); |
| 35 | + const logFilePath = resolveLogFilePath(logFile, currentWorkingDir); |
| 36 | + |
| 37 | + const outputChannel: OutputChannel = window.createOutputChannel(langName); |
| 38 | + const serverArgs = getServerArgs(workspaceConfig, logLevel, logFilePath); |
| 39 | + |
| 40 | + return { |
| 41 | + langName: langName, |
| 42 | + logLevel: logLevel, |
| 43 | + clientLogLevel: clientLogLevel, |
| 44 | + logFilePath: logFilePath, |
| 45 | + workingDir: currentWorkingDir, |
| 46 | + outputChannel: outputChannel, |
| 47 | + serverArgs: serverArgs, |
| 48 | + serverEnvironment: workspaceConfig.serverEnvironment, |
| 49 | + ghcupConfig: { |
| 50 | + metadataUrl: workspaceConfig.metadataURL as string, |
| 51 | + upgradeGHCup: workspaceConfig.get('upgradeGHCup') as boolean, |
| 52 | + executablePath: workspaceConfig.get('ghcupExecutablePath') as string, |
| 53 | + }, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +export function initLoggerFromConfig(config: Config): ExtensionLogger { |
| 58 | + return new ExtensionLogger('client', config.clientLogLevel, config.outputChannel, config.logFilePath); |
| 59 | +} |
| 60 | + |
| 61 | +export function logConfig(logger: Logger, config: Config) { |
| 62 | + if (config.logFilePath) { |
| 63 | + logger.info(`Writing client log to file ${config.logFilePath}`); |
| 64 | + } |
| 65 | + logger.log('Environment variables:'); |
| 66 | + Object.entries(process.env).forEach(([key, value]: [string, string | undefined]) => { |
| 67 | + // only list environment variables that we actually care about. |
| 68 | + // this makes it safe for users to just paste the logs to whoever, |
| 69 | + // and avoids leaking secrets. |
| 70 | + if (['PATH'].includes(key)) { |
| 71 | + logger.log(` ${key}: ${value}`); |
| 72 | + } |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +function getLogFile(workspaceConfig: WorkspaceConfiguration) { |
| 77 | + const logFile_: unknown = workspaceConfig.logFile; |
| 78 | + let logFile: string | undefined; |
| 79 | + if (typeof logFile_ === 'string') { |
| 80 | + logFile = logFile_ !== '' ? logFile_ : undefined; |
| 81 | + } |
| 82 | + return logFile; |
| 83 | +} |
| 84 | + |
| 85 | +function getClientLogLevel(workspaceConfig: WorkspaceConfiguration): ClientLogLevel { |
| 86 | + const clientLogLevel_: unknown = workspaceConfig.trace.client; |
| 87 | + let clientLogLevel; |
| 88 | + if (typeof clientLogLevel_ === 'string') { |
| 89 | + switch (clientLogLevel_) { |
| 90 | + case 'off': |
| 91 | + case 'error': |
| 92 | + case 'info': |
| 93 | + case 'debug': |
| 94 | + clientLogLevel = clientLogLevel_; |
| 95 | + break; |
| 96 | + default: |
| 97 | + throw new Error("Option \"haskell.trace.client\" is expected to be one of 'off', 'error', 'info', 'debug'."); |
| 98 | + } |
| 99 | + } else { |
| 100 | + throw new Error('Option "haskell.trace.client" is expected to be a string'); |
| 101 | + } |
| 102 | + return clientLogLevel; |
| 103 | +} |
| 104 | + |
| 105 | +function getLogLevel(workspaceConfig: WorkspaceConfiguration): LogLevel { |
| 106 | + const logLevel_: unknown = workspaceConfig.trace.server; |
| 107 | + let logLevel; |
| 108 | + if (typeof logLevel_ === 'string') { |
| 109 | + switch (logLevel_) { |
| 110 | + case 'off': |
| 111 | + case 'messages': |
| 112 | + case 'verbose': |
| 113 | + logLevel = logLevel_; |
| 114 | + break; |
| 115 | + default: |
| 116 | + throw new Error("Option \"haskell.trace.server\" is expected to be one of 'off', 'messages', 'verbose'."); |
| 117 | + } |
| 118 | + } else { |
| 119 | + throw new Error('Option "haskell.trace.server" is expected to be a string'); |
| 120 | + } |
| 121 | + return logLevel; |
| 122 | +} |
| 123 | + |
| 124 | +function resolveLogFilePath(logFile: string | undefined, currentWorkingDir: string): string | undefined { |
| 125 | + return logFile !== undefined ? path.resolve(currentWorkingDir, expandHomeDir(logFile)) : undefined; |
| 126 | +} |
| 127 | + |
| 128 | +function getServerArgs(workspaceConfig: WorkspaceConfiguration, logLevel: LogLevel, logFilePath?: string): string[] { |
| 129 | + const serverArgs = ['--lsp'] |
| 130 | + .concat(logLevel === 'messages' ? ['-d'] : []) |
| 131 | + .concat(logFilePath !== undefined ? ['-l', logFilePath] : []); |
| 132 | + |
| 133 | + const rawExtraArgs: unknown = workspaceConfig.serverExtraArgs; |
| 134 | + if (typeof rawExtraArgs === 'string' && rawExtraArgs !== '') { |
| 135 | + const e = rawExtraArgs.split(' '); |
| 136 | + serverArgs.push(...e); |
| 137 | + } |
| 138 | + |
| 139 | + // We don't want empty strings in our args |
| 140 | + return serverArgs.map((x) => x.trim()).filter((x) => x !== ''); |
| 141 | +} |
0 commit comments