Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/desktop/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import { EnvironmentManager } from './env-manager';
import { ExtensionApiWrapper } from '../vscode-api/extension-api-wrapper';
import { SerialMonitorApi, Version } from '@microsoft/vscode-serial-monitor-api';
import { SolutionEventHub } from '../solutions/solution-event-hub';
import { SolutionRpcData } from '../solutions/solution-rpc-data';
import { ManageSolutionCustomEditorProvider, registerManageSolutionCommand } from '../views/manage-solution/manage-solution-custom-editor';

let installDefaultToolsetProcess: Promise<void> | undefined;
Expand Down Expand Up @@ -130,9 +131,18 @@ export const activate = async (context: ExtensionContext): Promise<CsolutionExte
);

const solutionRootsWatcher = new SolutionRootsWatcher(fileWatcherProvider, workspaceFoldersProvider, workspaceFsProvider, commandsProvider);

const csolutionService = new CsolutionService(
envManager,
commandsProvider,
);

const rpcData = new SolutionRpcData(csolutionService);

const solutionManager = new SolutionManagerImpl(
activeSolutionTracker,
eventHub,
rpcData,
commandsProvider,
environmentManagerApiProvider);

Expand All @@ -146,10 +156,6 @@ export const activate = async (context: ExtensionContext): Promise<CsolutionExte
window,
workspace
);
const csolutionService = new CsolutionService(
envManager,
commandsProvider,
);
const cmsisToolboxManager = new CmsisToolboxManagerImpl(
processManager,
handleBuildEnoent,
Expand Down
27 changes: 23 additions & 4 deletions src/solutions/csolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,24 @@ export class CSolution {
return this.cmsisJsonFile.activeTargetTypeName ?? this.csolutionYml.getTargetType()?.name;
}

public getActiveTargetSet(): [TargetType['type'], Optional<ArrayElement<TargetType['target-set']>>] {
public getActiveTargetTypeWrap() {
const activeTarget = this.getActiveTargetType() ?? '';
const activeTargetSetIdx = this.cmsisJsonFile.getSelectedSet(activeTarget);
const activeTargetWrap = this.csolutionYml.getTargetType(activeTarget);
return this.csolutionYml.getTargetType(activeTarget);
}
public getActiveTargetSetWrap() {
const activeTargetWrap = this.getActiveTargetTypeWrap();
if (activeTargetWrap) {
const activeTargetSetIdx = this.cmsisJsonFile.getSelectedSet(activeTargetWrap.name);
return activeTargetWrap.getTargetSetFromIndex(activeTargetSetIdx);
}
return undefined;
}

public getActiveTargetSet(): [TargetType['type'], Optional<ArrayElement<TargetType['target-set']>>] {
const activeTarget = this.getActiveTargetType() ?? '';
const activeTargetWrap = this.getActiveTargetTypeWrap();
const activeTargetObject = activeTargetWrap?.object;
const activeTargetSetWrap = activeTargetWrap?.getTargetSetFromIndex(activeTargetSetIdx);
const activeTargetSetWrap = this.getActiveTargetSetWrap();
const activeTargetSetObject = activeTargetSetWrap?.object;
return [
activeTarget,
Expand Down Expand Up @@ -398,6 +409,14 @@ export class CSolution {
await this.cbuildPackFile.load(cbuildPackPath);
}

public getContextNames() {
const targetSet = this.getActiveTargetTypeWrap();
if (targetSet) {
return targetSet.getContexts();
}
return [];
}

public getContextDescriptors(): ContextDescriptor[] {
return this.cbuildIdxFile.activeContexts;
}
Expand Down
11 changes: 11 additions & 0 deletions src/solutions/files/csolution-wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ export class TargetTypeWrap extends TypedWrap {
}
return this.getTargetSet(this.targetSetNames.at(idx));
}

getContexts(targetSetName?: string) : string[] {
const contexts : string[] = [];
const targetSet = this.getTargetSet(targetSetName);
if (targetSet) {
for (const projectContext of targetSet.projectContexts) {
contexts.push(projectContext.name + '+' + this.name);
}
}
return contexts;
}
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/solutions/solution-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import { SolutionEventHub, ConvertResultData } from './solution-event-hub';
import { extensionApiProviderFactory } from '../vscode-api/extension-api-provider.factories';
import { EnvironmentManagerApiV1, VcpkgResults } from '@arm-software/vscode-environment-manager';
import { TestDataHandler } from '../__test__/test-data';
import { Board, Device } from '../json-rpc/csolution-rpc-client';
import { csolutionServiceFactory } from '../json-rpc/csolution-rpc-client.factory';
import { SolutionRpcData } from './solution-rpc-data';


const convertResultData: ConvertResultData = { severity: 'success', detection: false };
Expand All @@ -52,6 +55,8 @@ describe('SolutionManager', () => {
let loadBuildFilesListener: jest.Mock;
let tmpSolutionsDir: string;
let testSolutionPath: string;
let csolutionService: jest.Mocked<ReturnType<typeof csolutionServiceFactory>>;
let rpcData: SolutionRpcData;

const testDataHandler = new TestDataHandler();

Expand Down Expand Up @@ -106,10 +111,19 @@ describe('SolutionManager', () => {
};

commandsProvider = commandsProviderFactory();
csolutionService = csolutionServiceFactory();
const device: Device = { id: 'device-id' };
const board: Board = { id: 'board-id' };
csolutionService.getDeviceInfo.mockResolvedValue({ success: true, device });
csolutionService.getBoardInfo.mockResolvedValue({ success: true, board });
csolutionService.loadSolution.mockResolvedValue({ success: true });
csolutionService.getVariables.mockResolvedValue({ success: true, variables: {} });
rpcData = new SolutionRpcData(csolutionService);

solutionManager = new SolutionManagerImpl(
mockActiveSolutionTracker as unknown as ActiveSolutionTracker,
eventHub,
rpcData,
commandsProvider,
extensionApiProviderFactory(environmentManagerApi),
);
Expand Down Expand Up @@ -145,7 +159,7 @@ describe('SolutionManager', () => {
await waitTimeout(100);

const expectedLoadState: SolutionLoadState = {
solutionPath: testSolutionPath, loaded: true, converted: true,
solutionPath: testSolutionPath, loaded: true, converted: true, activated: true,
};

expect(solutionManager.loadState).toEqual(expectedLoadState);
Expand Down Expand Up @@ -189,7 +203,7 @@ describe('SolutionManager', () => {
await waitTimeout(100);

const expectedLoadState: SolutionLoadState = {
solutionPath: testSolutionPath, loaded: true, converted: true,
solutionPath: testSolutionPath, loaded: true, converted: true, activated: true,
};
expect(solutionManager.loadState).toEqual(expectedLoadState);

Expand Down
34 changes: 28 additions & 6 deletions src/solutions/solution-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ import { ExtensionApiProvider } from '../vscode-api/extension-api-provider';
import { EnvironmentManagerApiV1 } from '@arm-software/vscode-environment-manager';
import { ETextFileResult } from '../generic/text-file';
import { debounce } from 'lodash';
import { SolutionRpcData } from './solution-rpc-data';


export interface SolutionLoadState {
solutionPath?: string;
activated?: boolean; // solution is activated (loaded and converted at least once)
loaded?: boolean; // solution.yml + project.yml files loaded
converted?: boolean; // conversion executed and cbuild*.yml files are loaded.
};

export const solutionLoadStatesEqual = (a: SolutionLoadState, b: SolutionLoadState): boolean => {
return a.solutionPath === b.solutionPath
&& a.loaded === b.loaded
&& a.converted === b.converted;
&& a.converted === b.converted
&& a.activated === b.activated;
};

export interface SolutionLoadStateChangeEvent {
Expand Down Expand Up @@ -81,10 +84,12 @@ export class SolutionManagerImpl implements SolutionManager {
private readonly debouncedHandleEnvironmentChange = debounce(this.handleEnvironmentChange.bind(this), 500);
private _loadState: Readonly<SolutionLoadState> = { solutionPath: undefined };
private csolution?: CSolution;
private loadingSolution = false;

constructor(
private readonly activeSolutionTracker: ActiveSolutionTracker,
private readonly eventHub: SolutionEventHub,
private readonly rpcData: SolutionRpcData,
private readonly commandsProvider: CommandsProvider,
private readonly environmentManagerApiProvider: ExtensionApiProvider<Pick<EnvironmentManagerApiV1, 'onDidActivate' | 'getActiveTools'>>,

Expand All @@ -97,7 +102,12 @@ export class SolutionManagerImpl implements SolutionManager {
this.eventHub.onDidConvertCompleted(this.handleSolutionConvertCompleted, this),
this.commandsProvider.registerCommand(SolutionManagerImpl.refreshCommandId, this.refresh, this),
this.environmentManagerApiProvider.onActivate(environmentManagerApi => {
environmentManagerApi.onDidActivate(this.debouncedHandleEnvironmentChange, this, context.subscriptions);
environmentManagerApi.onDidActivate(() => {
if (!this.isSolutionActivated()) {
return;
}
this.debouncedHandleEnvironmentChange();
}, undefined, context.subscriptions);
}),
this.loadStateChangeEmitter,
this.loadBuildFilesEmitter,
Expand All @@ -118,8 +128,12 @@ export class SolutionManagerImpl implements SolutionManager {
return vscode.workspace.getWorkspaceFolder(vscode.Uri.file(solutionPath))?.uri;
}

private isSolutionActivated(): boolean {
return !!this.loadState.solutionPath && this.loadState.activated === true;
}

private async handleEnvironmentChange(): Promise<void> {
if (!this.loadState.solutionPath) {
if (!this.isSolutionActivated()) {
return;
}
await this.loadSolution();
Expand Down Expand Up @@ -183,11 +197,11 @@ export class SolutionManagerImpl implements SolutionManager {
}

private async loadSolution(): Promise<void> {
if (!this.loadState.solutionPath) {
if (this.loadingSolution || !this.loadState.solutionPath) {
return;
}

try {
this.loadingSolution = true;
this.csolution = new CSolution();
await this.csolution.load(this.loadState.solutionPath);

Expand All @@ -199,11 +213,18 @@ export class SolutionManagerImpl implements SolutionManager {
this.setLoadState(newState, true);
} catch (error) {
console.error(`Failed to load ${this.loadState.solutionPath}`, error);
} finally {
this.loadingSolution = false;
}
}

private async handleSolutionConvertCompleted(data: ConvertResultData) {
if (!this.csolution) {
return;
}
await this.rpcData.update(this.csolution);
await this.loadSolutionBuildFiles();

if (data.severity != 'error') {
await this.commandsProvider.executeCommandIfRegistered(UPDATE_DEBUG_TASKS_COMMAND_ID);
}
Expand All @@ -216,7 +237,8 @@ export class SolutionManagerImpl implements SolutionManager {
const result = await this.csolution.loadBuildFiles();
const newState: SolutionLoadState = {
...this.loadState,
converted: true
activated: true,
converted: true,
};
this.setLoadState(newState, result !== ETextFileResult.Unchanged);
}
Expand Down
73 changes: 73 additions & 0 deletions src/solutions/solution-rpc-data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2026 Arm Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { beforeEach, describe, expect, it } from '@jest/globals';
import { csolutionServiceFactory } from '../json-rpc/csolution-rpc-client.factory';
import { csolutionFactory, CSolutionMock } from './csolution.factory';
import { SolutionRpcData } from './solution-rpc-data';

describe('SolutionRpcData', () => {
let csolutionService: jest.Mocked<ReturnType<typeof csolutionServiceFactory>>;
let rpcData: SolutionRpcData;
let solution: CSolutionMock;

beforeEach(() => {
csolutionService = csolutionServiceFactory();
solution = csolutionFactory({
solutionPath: 'path/to/solution.csolution.yml',
getActiveTargetTypeWrap: jest.fn().mockReturnValue({ name: 'ActiveTarget' }),
getContextNames: jest.fn().mockReturnValue(['ctx']),
});
rpcData = new SolutionRpcData(csolutionService);
});

it('loads context data when loadSolution succeeds', async () => {
csolutionService.loadSolution.mockResolvedValue({ success: true });
csolutionService.getVariables.mockResolvedValue({ success: true, variables: { FOO: 'bar' } });

await rpcData.update(solution);

expect(csolutionService.loadSolution).toHaveBeenCalledWith({
solution: solution.solutionPath,
activeTarget: 'ActiveTarget',
});
expect(csolutionService.getVariables).toHaveBeenCalledWith({ context: 'ctx' });
expect(rpcData.resolveVariable('ctx', '$FOO$')).toBe('bar');
expect(rpcData.resolveVariable('ctx', '$MISSING$')).toBeUndefined();
});

it('does not fetch context data when loadSolution fails', async () => {
csolutionService.loadSolution.mockResolvedValue({ success: false });

await rpcData.update(solution);

expect(csolutionService.getVariables).not.toHaveBeenCalled();
expect(rpcData.resolveVariable('ctx', 'FOO')).toBeUndefined();
});

it('expands $VAR$ placeholders for a context', async () => {
csolutionService.loadSolution.mockResolvedValue({ success: true });
csolutionService.getVariables.mockResolvedValue({ success: true, variables: { FOO: 'bar', HELLO: 'world' } });

await rpcData.update(solution);

expect(rpcData.expandString('Value: $FOO$ and $HELLO$', 'ctx')).toBe('Value: bar and world');
});

it('returns original string when no variables are available', () => {
expect(rpcData.expandString('plain string', 'ctx')).toBe('plain string');
});
});
Loading
Loading