Skip to content

Commit 86dcf63

Browse files
committed
Add configure and upload step when uploading to board requiring user fields
1 parent dc2ce59 commit 86dcf63

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

Diff for: 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

@@ -135,6 +206,11 @@ export class UploadSketch extends SketchContribution {
135206
const optimizeForDebug = this.editorMode.compileForDebug;
136207
const { selectedPort } = boardsConfig;
137208
const port = selectedPort;
209+
const userFields = this.cachedUserFields.get(this.selectedFqbnAddress());
210+
if (!userFields) {
211+
this.messageService.error(nls.localize('arduino/sketch/userFieldsNotFoundError', "Can't find user fields for connected board"));
212+
return;
213+
}
138214

139215
if (usingProgrammer) {
140216
const programmer = selectedProgrammer;
@@ -147,6 +223,7 @@ export class UploadSketch extends SketchContribution {
147223
verbose,
148224
verify,
149225
sourceOverride,
226+
userFields,
150227
};
151228
} else {
152229
options = {
@@ -157,6 +234,7 @@ export class UploadSketch extends SketchContribution {
157234
verbose,
158235
verify,
159236
sourceOverride,
237+
userFields,
160238
};
161239
}
162240
this.outputChannelManager.getChannel('Arduino').clear();
@@ -207,6 +285,11 @@ export namespace UploadSketch {
207285
export const UPLOAD_SKETCH: Command = {
208286
id: 'arduino-upload-sketch',
209287
};
288+
export const UPLOAD_WITH_CONFIGURATION: Command = {
289+
id: 'arduino-upload-with-configuration-sketch',
290+
label: nls.localize('arduino/sketch/configureAndUpload', 'Configure And Upload'),
291+
category: 'Arduino',
292+
}
210293
export const UPLOAD_SKETCH_USING_PROGRAMMER: Command = {
211294
id: 'arduino-upload-sketch-using-programmer',
212295
};

Diff for: 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

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

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

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
146146
}
147147
req.setVerbose(options.verbose);
148148
req.setVerify(options.verify);
149+
150+
options.userFields.forEach(e => {
151+
req.getUserFieldsMap().set(e.name, e.value);
152+
});
153+
149154
const result = responseHandler(client, req);
150155

151156
try {

0 commit comments

Comments
 (0)