Skip to content

Commit 53b4f17

Browse files
silvanocerzaAlberto Iannaccone
authored and
Alberto Iannaccone
committed
Add configure and upload step when uploading to board requiring user fields
1 parent 33de26e commit 53b4f17

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

arduino-ide-extension/src/browser/contributions/upload-sketch.ts

+87-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { inject, injectable } from 'inversify';
1+
import { inject, injectable, postConstruct } from 'inversify';
22
import { Emitter } from '@theia/core/lib/common/event';
3-
import { CoreService } from '../../common/protocol';
3+
import { BoardUserField, CoreService } from '../../common/protocol';
44
import { ArduinoMenus } from '../menu/arduino-menus';
55
import { ArduinoToolbar } from '../toolbar/arduino-toolbar';
66
import { BoardsDataStore } from '../boards/boards-data-store';
@@ -14,6 +14,7 @@ import {
1414
KeybindingRegistry,
1515
TabBarToolbarRegistry,
1616
} from './contribution';
17+
import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog';
1718
import { nls } from '@theia/core/lib/browser/nls';
1819

1920
@injectable()
@@ -30,16 +31,81 @@ export class UploadSketch extends SketchContribution {
3031
@inject(BoardsServiceProvider)
3132
protected readonly boardsServiceClientImpl: BoardsServiceProvider;
3233

34+
@inject(UserFieldsDialog)
35+
protected readonly userFieldsDialog: UserFieldsDialog;
36+
37+
protected cachedUserFields: Map<string, BoardUserField[]> = new Map();
38+
3339
protected readonly onDidChangeEmitter = new Emitter<Readonly<void>>();
3440
readonly onDidChange = this.onDidChangeEmitter.event;
3541

3642
protected uploadInProgress = false;
43+
protected boardRequiresUserFields = false;
44+
45+
@postConstruct()
46+
protected init(): void {
47+
this.boardsServiceClientImpl.onBoardsConfigChanged(async () => {
48+
const userFields = await this.boardsServiceClientImpl.selectedBoardUserFields();
49+
this.boardRequiresUserFields = userFields.length > 0;
50+
})
51+
}
52+
53+
private selectedFqbnAddress(): string {
54+
const { boardsConfig } = this.boardsServiceClientImpl;
55+
const fqbn = boardsConfig.selectedBoard?.fqbn;
56+
if (!fqbn) {
57+
return "";
58+
}
59+
const address = boardsConfig.selectedBoard?.port?.address
60+
if (!address) {
61+
return "";
62+
}
63+
return fqbn + "|" + address;
64+
}
3765

3866
registerCommands(registry: CommandRegistry): void {
3967
registry.registerCommand(UploadSketch.Commands.UPLOAD_SKETCH, {
40-
execute: () => this.uploadSketch(),
68+
execute: async () => {
69+
const key = this.selectedFqbnAddress();
70+
if (!key) {
71+
return;
72+
}
73+
if (this.boardRequiresUserFields && !this.cachedUserFields.has(key)) {
74+
// Deep clone the array of board fields to avoid editing the cached ones
75+
this.userFieldsDialog.value = (await this.boardsServiceClientImpl.selectedBoardUserFields()).map(f => ({ ...f }));
76+
const result = await this.userFieldsDialog.open();
77+
if (!result) {
78+
return;
79+
}
80+
this.cachedUserFields.set(key, result);
81+
}
82+
this.uploadSketch();
83+
},
4184
isEnabled: () => !this.uploadInProgress,
4285
});
86+
registry.registerCommand(
87+
UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION,
88+
{
89+
execute: async () => {
90+
const key = this.selectedFqbnAddress();
91+
if (!key) {
92+
return;
93+
}
94+
95+
const cached = this.cachedUserFields.get(key);
96+
// Deep clone the array of board fields to avoid editing the cached ones
97+
this.userFieldsDialog.value = (cached ?? await this.boardsServiceClientImpl.selectedBoardUserFields()).map(f => ({ ...f }));
98+
99+
const result = await this.userFieldsDialog.open()
100+
if (!result) {
101+
return;
102+
}
103+
this.cachedUserFields.set(key, result);
104+
this.uploadSketch();
105+
},
106+
isEnabled: () => !this.uploadInProgress && this.boardRequiresUserFields,
107+
}
108+
);
43109
registry.registerCommand(
44110
UploadSketch.Commands.UPLOAD_SKETCH_USING_PROGRAMMER,
45111
{
@@ -63,13 +129,18 @@ export class UploadSketch extends SketchContribution {
63129
label: nls.localize('arduino/sketch/upload', 'Upload'),
64130
order: '1',
65131
});
132+
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
133+
commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id,
134+
label: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label,
135+
order: '2',
136+
});
66137
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
67138
commandId: UploadSketch.Commands.UPLOAD_SKETCH_USING_PROGRAMMER.id,
68139
label: nls.localize(
69140
'arduino/sketch/uploadUsingProgrammer',
70141
'Upload Using Programmer'
71142
),
72-
order: '2',
143+
order: '3',
73144
});
74145
}
75146

@@ -127,6 +198,11 @@ export class UploadSketch extends SketchContribution {
127198
const optimizeForDebug = this.editorMode.compileForDebug;
128199
const { selectedPort } = boardsConfig;
129200
const port = selectedPort;
201+
const userFields = this.cachedUserFields.get(this.selectedFqbnAddress());
202+
if (!userFields) {
203+
this.messageService.error(nls.localize('arduino/sketch/userFieldsNotFoundError', "Can't find user fields for connected board"));
204+
return;
205+
}
130206

131207
if (usingProgrammer) {
132208
const programmer = selectedProgrammer;
@@ -139,6 +215,7 @@ export class UploadSketch extends SketchContribution {
139215
verbose,
140216
verify,
141217
sourceOverride,
218+
userFields,
142219
};
143220
} else {
144221
options = {
@@ -149,6 +226,7 @@ export class UploadSketch extends SketchContribution {
149226
verbose,
150227
verify,
151228
sourceOverride,
229+
userFields,
152230
};
153231
}
154232
this.outputChannelManager.getChannel('Arduino').clear();
@@ -197,6 +275,11 @@ export namespace UploadSketch {
197275
export const UPLOAD_SKETCH: Command = {
198276
id: 'arduino-upload-sketch',
199277
};
278+
export const UPLOAD_WITH_CONFIGURATION: Command = {
279+
id: 'arduino-upload-with-configuration-sketch',
280+
label: nls.localize('arduino/sketch/configureAndUpload', 'Configure And Upload'),
281+
category: 'Arduino',
282+
}
200283
export const UPLOAD_SKETCH_USING_PROGRAMMER: Command = {
201284
id: 'arduino-upload-sketch-using-programmer',
202285
};

arduino-ide-extension/src/common/protocol/core-service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BoardUserField } from '.';
12
import { Port } from '../../common/protocol/boards-service';
23
import { Programmer } from './boards-service';
34

@@ -44,6 +45,7 @@ export namespace CoreService {
4445
readonly port?: Port | undefined;
4546
readonly programmer?: Programmer | undefined;
4647
readonly verify: boolean;
48+
readonly userFields: BoardUserField[];
4749
}
4850
}
4951

arduino-ide-extension/src/node/core-service-impl.ts

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
153153
}
154154
req.setVerbose(options.verbose);
155155
req.setVerify(options.verify);
156+
157+
options.userFields.forEach(e => {
158+
req.getUserFieldsMap().set(e.name, e.value);
159+
});
160+
156161
const result = responseHandler(client, req);
157162

158163
try {

0 commit comments

Comments
 (0)