Skip to content

Commit a2ee456

Browse files
authored
Refactoring addPyocdToPath to make it useable for adding more built-in tools to the PATH variable (#359)
* Added a function to add built-in GDB to PATH of extension host * refactoring addPyocdToPath and addGdbToPath into one function
1 parent 83cfd3e commit a2ee456

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/desktop/add-to-path.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import * as vscode from 'vscode';
18-
import { addPyocdToPath } from './add-to-path';
18+
import { addToolToPath } from './add-to-path';
1919
import { BuiltinToolPath } from './builtin-tool-path';
2020
import { extensionContextFactory } from '../__test__/vscode.factory';
2121
import { isWindows } from '../utils';
@@ -24,7 +24,7 @@ jest.mock('./builtin-tool-path');
2424
const pathPyOCD = 'tools/pyocd/pyocd';
2525
const BuiltinToolPathMock = BuiltinToolPath as jest.MockedClass<typeof BuiltinToolPath>;
2626

27-
describe('addPyocdToPath', () => {
27+
describe('addToolToPath', () => {
2828
let extensionMock: vscode.ExtensionContext;
2929
beforeEach(() => {
3030
extensionMock = extensionContextFactory();
@@ -46,7 +46,7 @@ describe('addPyocdToPath', () => {
4646
value: 'pathPyOCD'
4747
});
4848

49-
addPyocdToPath(extensionMock);
49+
addToolToPath(extensionMock, pathPyOCD);
5050

5151
expect(extensionMock.environmentVariableCollection.prepend).toHaveBeenCalledWith('PATH', expect.stringContaining(pathPyOCD));
5252
});
@@ -58,7 +58,7 @@ describe('addPyocdToPath', () => {
5858

5959
BuiltinToolPathMock.mockImplementation(() => mockInstance);
6060

61-
addPyocdToPath(extensionMock);
61+
addToolToPath(extensionMock, pathPyOCD);
6262

6363
expect(extensionMock.environmentVariableCollection.prepend).not.toHaveBeenCalled();
6464
});
@@ -75,7 +75,7 @@ describe('addPyocdToPath', () => {
7575
value: `/already/included/path${isWindows ? ';' : ':'}`
7676
});
7777

78-
addPyocdToPath(extensionMock);
78+
addToolToPath(extensionMock, pathPyOCD);
7979

8080
expect(extensionMock.environmentVariableCollection.prepend).not.toHaveBeenCalled();
8181
});

src/desktop/add-to-path.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ import { logger } from '../logger';
2020
import { isWindows } from '../utils';
2121
import { BuiltinToolPath } from './builtin-tool-path';
2222

23-
const PYOCD_BUILTIN_PATH = 'tools/pyocd/pyocd';
24-
25-
export function addPyocdToPath(context: vscode.ExtensionContext): void {
26-
//get pyOCD path from tools folder
27-
const builtinPyocd = new BuiltinToolPath(PYOCD_BUILTIN_PATH);
28-
const pathPyOCD = builtinPyocd.getAbsolutePathDir();
29-
if (!pathPyOCD) {
30-
logger.debug('pyOCD is not available');
23+
export function addToolToPath(context: vscode.ExtensionContext, toolToAdd: string): void {
24+
// get gdb path from tools folder
25+
const builtinTool = new BuiltinToolPath(toolToAdd);
26+
const pathTool = builtinTool.getAbsolutePathDir();
27+
// check if path exists
28+
if (!pathTool) {
29+
logger.debug(`${toolToAdd} is not available`);
3130
return;
3231
}
32+
// add a delimiter and prepare gdb path to be added to PATH variable
3333
const delimiter = isWindows ? ';' : ':';
34-
const updatePath = `${pathPyOCD}${delimiter}`;
35-
//get current environment variable collection
34+
const updatePath = `${pathTool}${delimiter}`;
35+
// get current environment variable collection
3636
const mutator = context.environmentVariableCollection.get('PATH');
3737
// Path included and previously used type was 'Prepend'. Change mutator
3838
// if other type (we previously used 'Replace' which caused trouble).
@@ -43,3 +43,4 @@ export function addPyocdToPath(context: vscode.ExtensionContext): void {
4343
//add updated path to PATH variable, but only for the terminal inside of vscode
4444
context.environmentVariableCollection.prepend('PATH', updatePath);
4545
}
46+

src/desktop/extension.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import * as vscode from 'vscode';
1818
import { GDBTargetDebugTracker } from '../debug-configuration/gdbtarget-debug-tracker';
1919
import { GDBTargetConfigurationProvider } from '../debug-configuration';
2020
import { logger } from '../logger';
21-
import { addPyocdToPath } from './add-to-path';
21+
import { addToolToPath } from './add-to-path';
22+
23+
const PYOCD_BUILTIN_PATH = 'tools/pyocd/pyocd';
2224

2325
export const activate = async (context: vscode.ExtensionContext): Promise<void> => {
2426
const gdbtargetDebugTracker = new GDBTargetDebugTracker();
2527
const gdbtargetConfigurationProvider = new GDBTargetConfigurationProvider();
2628

27-
addPyocdToPath(context);
29+
addToolToPath(context, PYOCD_BUILTIN_PATH);
2830
// Activate components
2931
gdbtargetDebugTracker.activate(context);
3032
gdbtargetConfigurationProvider.activate(context);

0 commit comments

Comments
 (0)