|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import * as vscode from 'vscode'; |
18 | 17 | import { logger } from '../../logger'; |
19 | 18 | import { GDBTargetConfiguration, TargetConfiguration } from '../gdbtarget-configuration'; |
20 | 19 | import { BuiltinToolPath } from '../../desktop/builtin-tool-path'; |
| 20 | +import { BaseConfigurationProvider } from './base-configuration-provider'; |
21 | 21 |
|
22 | 22 | const PYOCD_BUILTIN_PATH = 'tools/pyocd/pyocd'; |
23 | | -export const PYOCD_EXECUTABLE_ONLY_REGEXP = /^\s*pyocd(|.exe)\s*$/i; |
| 23 | +const PYOCD_EXECUTABLE_ONLY_REGEXP = /^\s*pyocd(|.exe)\s*$/i; |
24 | 24 | export const PYOCD_SERVER_TYPE_REGEXP = /.*pyocd(|.exe)\s*$/i; |
25 | 25 |
|
26 | | -export class PyocdConfigurationProvider implements vscode.DebugConfigurationProvider { |
27 | | - protected builtinPyocd = new BuiltinToolPath(PYOCD_BUILTIN_PATH); |
28 | | - |
29 | | - protected async hasCommand(commandName: string): Promise<boolean> { |
30 | | - const commands = await vscode.commands.getCommands(); |
31 | | - return !!commands.find(command => command === commandName); |
32 | | - }; |
33 | | - |
34 | | - protected hasParam(name: string, params: string[]): boolean { |
35 | | - return !!params.find(param => param.trim() === name); |
36 | | - } |
| 26 | +const PYOCD_CLI_ARG_GDBSERVER = 'gdbserver'; |
| 27 | +const PYOCD_CLI_ARG_PORT = '--port'; |
| 28 | +const PYOCD_CLI_ARG_CBUILDRUN = '--cbuild-run'; |
37 | 29 |
|
38 | | - protected async shouldAppendParam(params: string[], paramName: string, commandName?: string): Promise<boolean> { |
39 | | - return !this.hasParam(paramName, params) && (!commandName || await this.hasCommand(commandName)); |
40 | | - } |
| 30 | +export class PyocdConfigurationProvider extends BaseConfigurationProvider { |
| 31 | + protected builtinPyocd = new BuiltinToolPath(PYOCD_BUILTIN_PATH); |
41 | 32 |
|
42 | 33 | protected resolveServerPath(target: TargetConfiguration): void { |
43 | 34 | const targetServer = target.server; |
44 | 35 | const useBuiltin = !targetServer || PYOCD_EXECUTABLE_ONLY_REGEXP.test(targetServer); |
45 | | - const builtinUri = useBuiltin ? this.builtinPyocd.getAbsolutePath() : undefined; |
46 | | - if (builtinUri) { |
47 | | - target.server = builtinUri.fsPath; |
| 36 | + const updateUri = useBuiltin ? this.builtinPyocd.getAbsolutePath() : undefined; |
| 37 | + if (updateUri) { |
| 38 | + target.server = updateUri.fsPath; |
48 | 39 | } |
49 | 40 | } |
50 | 41 |
|
51 | 42 | protected async resolveServerParameters(debugConfiguration: GDBTargetConfiguration): Promise<GDBTargetConfiguration> { |
| 43 | + logger.debug('Resolving pyOCD server parameters'); |
52 | 44 | if (!debugConfiguration.target) { |
53 | 45 | return debugConfiguration; |
54 | 46 | } |
55 | 47 | // server |
56 | 48 | this.resolveServerPath(debugConfiguration.target); |
57 | 49 | // serverParameters |
58 | | - const parameters = debugConfiguration.target.serverParameters ??= []; |
| 50 | + debugConfiguration.target.serverParameters ??= []; |
| 51 | + const parameters = debugConfiguration.target.serverParameters; |
59 | 52 | // gdbserver |
60 | | - if (await this.shouldAppendParam(parameters, 'gdbserver')) { |
| 53 | + if (await this.shouldAppendParameter(parameters, PYOCD_CLI_ARG_GDBSERVER)) { |
61 | 54 | // Prepend, it must be the first argument |
62 | | - parameters.unshift('gdbserver'); |
| 55 | + parameters.unshift(PYOCD_CLI_ARG_GDBSERVER); |
63 | 56 | } |
64 | 57 | // port (use value defined in 'port' outside 'serverParamters') |
65 | 58 | const port = debugConfiguration.target?.port; |
66 | | - if (port && await this.shouldAppendParam(parameters, '--port')) { |
67 | | - parameters.push('--port'); |
68 | | - parameters.push(`${port}`); |
| 59 | + if (port && await this.shouldAppendParameter(parameters, PYOCD_CLI_ARG_PORT)) { |
| 60 | + parameters.push(PYOCD_CLI_ARG_PORT, `${port}`); |
69 | 61 | } |
70 | 62 | // cbuild-run |
71 | 63 | const cbuildRunFile = debugConfiguration.cmsis?.cbuildRunFile; |
72 | | - if (cbuildRunFile && await this.shouldAppendParam(parameters, '--cbuild-run')) { |
73 | | - parameters.push('--cbuild-run'); |
74 | | - parameters.push(`${cbuildRunFile}`); |
| 64 | + if (cbuildRunFile && await this.shouldAppendParameter(parameters, PYOCD_CLI_ARG_CBUILDRUN)) { |
| 65 | + parameters.push(PYOCD_CLI_ARG_CBUILDRUN, `${cbuildRunFile}`); |
75 | 66 | } |
76 | 67 | return debugConfiguration; |
77 | 68 | } |
78 | 69 |
|
79 | | - public async resolveDebugConfigurationWithSubstitutedVariables( |
80 | | - _folder: vscode.WorkspaceFolder | undefined, |
81 | | - debugConfiguration: vscode.DebugConfiguration, |
82 | | - _token?: vscode.CancellationToken |
83 | | - ): Promise<vscode.DebugConfiguration | null | undefined> { |
84 | | - logger.debug('Resolving pyOCD configuration'); |
85 | | - const resolvedConfig = await this.resolveServerParameters(debugConfiguration); |
86 | | - return resolvedConfig; |
87 | | - } |
88 | | - |
89 | 70 | } |
0 commit comments