Skip to content

Commit 72b89d8

Browse files
author
Akos Kitta
committed
unknown progress for upload kinds
Signed-off-by: Akos Kitta <[email protected]>
1 parent f8331d9 commit 72b89d8

File tree

4 files changed

+143
-80
lines changed

4 files changed

+143
-80
lines changed

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

+15-23
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { Emitter } from '@theia/core/lib/common/event';
33
import { BoardUserField, CoreService } from '../../common/protocol';
44
import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus';
55
import { ArduinoToolbar } from '../toolbar/arduino-toolbar';
6-
import { BoardsDataStore } from '../boards/boards-data-store';
76
import { BoardsServiceProvider } from '../boards/boards-service-provider';
87
import {
9-
CoreServiceContribution,
108
Command,
119
CommandRegistry,
1210
MenuModelRegistry,
@@ -16,16 +14,13 @@ import {
1614
import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog';
1715
import { DisposableCollection, nls } from '@theia/core/lib/common';
1816
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
19-
import { VerifySketch } from './verify-sketch';
17+
import { VerifyParams, VerifySketch } from './verify-sketch';
2018

2119
@injectable()
22-
export class UploadSketch extends CoreServiceContribution {
20+
export class UploadSketch extends VerifySketch {
2321
@inject(MenuModelRegistry)
2422
private readonly menuRegistry: MenuModelRegistry;
2523

26-
@inject(BoardsDataStore)
27-
private readonly boardsDataStore: BoardsDataStore;
28-
2924
@inject(BoardsServiceProvider)
3025
private readonly boardsServiceClientImpl: BoardsServiceProvider;
3126

@@ -36,8 +31,9 @@ export class UploadSketch extends CoreServiceContribution {
3631
private readonly cachedUserFields: Map<string, BoardUserField[]> = new Map();
3732
private readonly menuActionsDisposables = new DisposableCollection();
3833

39-
private readonly onDidChangeEmitter = new Emitter<void>();
40-
private readonly onDidChange = this.onDidChangeEmitter.event;
34+
private readonly onUploadInProgressDidChangeEmitter = new Emitter<void>();
35+
private readonly onUploadInProgressDidChange =
36+
this.onUploadInProgressDidChangeEmitter.event;
4137
private uploadInProgress = false;
4238

4339
protected override init(): void {
@@ -186,7 +182,7 @@ export class UploadSketch extends CoreServiceContribution {
186182
command: UploadSketch.Commands.UPLOAD_SKETCH_TOOLBAR.id,
187183
tooltip: nls.localize('arduino/sketch/upload', 'Upload'),
188184
priority: 1,
189-
onDidChange: this.onDidChange,
185+
onDidChange: this.onUploadInProgressDidChange,
190186
});
191187
}
192188

@@ -195,16 +191,12 @@ export class UploadSketch extends CoreServiceContribution {
195191
return;
196192
}
197193

198-
const ref = await this.commandService.executeCommand<VerifySketch.Ref>(
199-
VerifySketch.Commands.VERIFY_SKETCH.id,
200-
false
201-
);
202-
203-
if (!ref) {
194+
const params = await this.verifySketch(false);
195+
if (!params) {
204196
return;
205197
}
206198

207-
const options = await this.options(usingProgrammer, ref);
199+
const options = await this.uploadOptions(usingProgrammer, params);
208200
if (!options) {
209201
return;
210202
}
@@ -224,7 +216,7 @@ export class UploadSketch extends CoreServiceContribution {
224216
// uploadInProgress will be set to false whether the upload fails or not
225217
this.uploadInProgress = true;
226218
this.coreErrorHandler.reset();
227-
this.onDidChangeEmitter.fire();
219+
this.onUploadInProgressDidChangeEmitter.fire();
228220
await this.doWithProgress({
229221
progressText: nls.localize('arduino/sketch/uploading', 'Uploading...'),
230222
task: (progressId, coreService) => {
@@ -241,13 +233,13 @@ export class UploadSketch extends CoreServiceContribution {
241233
this.handleError(e);
242234
} finally {
243235
this.uploadInProgress = false;
244-
this.onDidChangeEmitter.fire();
236+
this.onUploadInProgressDidChangeEmitter.fire();
245237
}
246238
}
247239

248-
private async options(
240+
private async uploadOptions(
249241
usingProgrammer: boolean,
250-
verifyRef: VerifySketch.Ref
242+
params: VerifyParams
251243
): Promise<CoreService.Options.Upload | undefined> {
252244
const sketch = await this.sketchServiceClient.currentSketch();
253245
if (!CurrentSketch.isValid(sketch)) {
@@ -257,8 +249,8 @@ export class UploadSketch extends CoreServiceContribution {
257249
const { boardsConfig } = this.boardsServiceClientImpl;
258250
const [fqbn, { selectedProgrammer: programmer }, verify, verbose] =
259251
await Promise.all([
260-
verifyRef.fqbn.decorated,
261-
this.boardsDataStore.getData(verifyRef.fqbn.original),
252+
params.fqbn.decorated,
253+
this.boardsDataStore.getData(params.fqbn.original),
262254
this.preferences.get('arduino.upload.verify'),
263255
this.preferences.get('arduino.upload.verbose'),
264256
]);

Diff for: arduino-ide-extension/src/browser/contributions/verify-sketch.ts

+35-36
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,47 @@ import { CoreService } from '../../common/protocol';
1919
@injectable()
2020
export class VerifySketch extends CoreServiceContribution {
2121
@inject(BoardsDataStore)
22-
private readonly boardsDataStore: BoardsDataStore;
22+
protected readonly boardsDataStore: BoardsDataStore;
2323

2424
@inject(BoardsServiceProvider)
2525
private readonly boardsServiceProvider: BoardsServiceProvider;
2626

27-
private readonly onDidChangeEmitter = new Emitter<void>();
28-
private readonly onDidChange = this.onDidChangeEmitter.event;
27+
private readonly onVerifyInProgressDidChangeEmitter = new Emitter<void>();
28+
private readonly onVerifyInProgressDidChange =
29+
this.onVerifyInProgressDidChangeEmitter.event;
2930
private verifyInProgress = false;
3031

3132
override registerCommands(registry: CommandRegistry): void {
32-
registry.registerCommand(VerifySketch.Commands.VERIFY_SKETCH, {
33+
registry.registerCommand(VerifySketchCommands.VERIFY_SKETCH, {
3334
execute: async (exportBinaries?: boolean) => {
3435
return this.verifySketch(exportBinaries);
3536
},
3637
isEnabled: () => !this.verifyInProgress,
3738
});
38-
registry.registerCommand(VerifySketch.Commands.EXPORT_BINARIES, {
39+
registry.registerCommand(VerifySketchCommands.EXPORT_BINARIES, {
3940
execute: async () => {
4041
return this.verifySketch(true);
4142
},
4243
isEnabled: () => !this.verifyInProgress,
4344
});
44-
registry.registerCommand(VerifySketch.Commands.VERIFY_SKETCH_TOOLBAR, {
45+
registry.registerCommand(VerifySketchCommands.VERIFY_SKETCH_TOOLBAR, {
4546
isVisible: (widget) =>
4647
ArduinoToolbar.is(widget) && widget.side === 'left',
4748
isEnabled: () => !this.verifyInProgress,
4849
isToggled: () => this.verifyInProgress,
4950
execute: () =>
50-
registry.executeCommand(VerifySketch.Commands.VERIFY_SKETCH.id),
51+
registry.executeCommand(VerifySketchCommands.VERIFY_SKETCH.id),
5152
});
5253
}
5354

5455
override registerMenus(registry: MenuModelRegistry): void {
5556
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
56-
commandId: VerifySketch.Commands.VERIFY_SKETCH.id,
57+
commandId: VerifySketchCommands.VERIFY_SKETCH.id,
5758
label: nls.localize('arduino/sketch/verifyOrCompile', 'Verify/Compile'),
5859
order: '0',
5960
});
6061
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
61-
commandId: VerifySketch.Commands.EXPORT_BINARIES.id,
62+
commandId: VerifySketchCommands.EXPORT_BINARIES.id,
6263
label: nls.localize(
6364
'arduino/sketch/exportBinary',
6465
'Export Compiled Binary'
@@ -69,43 +70,43 @@ export class VerifySketch extends CoreServiceContribution {
6970

7071
override registerKeybindings(registry: KeybindingRegistry): void {
7172
registry.registerKeybinding({
72-
command: VerifySketch.Commands.VERIFY_SKETCH.id,
73+
command: VerifySketchCommands.VERIFY_SKETCH.id,
7374
keybinding: 'CtrlCmd+R',
7475
});
7576
registry.registerKeybinding({
76-
command: VerifySketch.Commands.EXPORT_BINARIES.id,
77+
command: VerifySketchCommands.EXPORT_BINARIES.id,
7778
keybinding: 'CtrlCmd+Alt+S',
7879
});
7980
}
8081

8182
override registerToolbarItems(registry: TabBarToolbarRegistry): void {
8283
registry.registerItem({
83-
id: VerifySketch.Commands.VERIFY_SKETCH_TOOLBAR.id,
84-
command: VerifySketch.Commands.VERIFY_SKETCH_TOOLBAR.id,
84+
id: VerifySketchCommands.VERIFY_SKETCH_TOOLBAR.id,
85+
command: VerifySketchCommands.VERIFY_SKETCH_TOOLBAR.id,
8586
tooltip: nls.localize('arduino/sketch/verify', 'Verify'),
8687
priority: 0,
87-
onDidChange: this.onDidChange,
88+
onDidChange: this.onVerifyInProgressDidChange,
8889
});
8990
}
9091

91-
private async verifySketch(
92+
protected async verifySketch(
9293
exportBinaries?: boolean
93-
): Promise<undefined | VerifySketch.Ref> {
94+
): Promise<undefined | VerifyParams> {
9495
if (this.verifyInProgress) {
9596
return undefined;
9697
}
9798

9899
const originalFqbn =
99100
this.boardsServiceProvider.boardsConfig.selectedBoard?.fqbn;
100-
const options = await this.options(exportBinaries);
101+
const options = await this.verifyOptions(exportBinaries);
101102
if (!options) {
102103
return undefined;
103104
}
104105

105106
try {
106107
this.verifyInProgress = true;
107108
this.coreErrorHandler.reset();
108-
this.onDidChangeEmitter.fire();
109+
this.onVerifyInProgressDidChangeEmitter.fire();
109110
await this.doWithProgress({
110111
progressText: nls.localize(
111112
'arduino/sketch/compile',
@@ -132,11 +133,11 @@ export class VerifySketch extends CoreServiceContribution {
132133
return undefined;
133134
} finally {
134135
this.verifyInProgress = false;
135-
this.onDidChangeEmitter.fire();
136+
this.onVerifyInProgressDidChangeEmitter.fire();
136137
}
137138
}
138139

139-
private async options(
140+
private async verifyOptions(
140141
exportBinaries?: boolean
141142
): Promise<CoreService.Options.Compile | undefined> {
142143
const sketch = await this.sketchServiceClient.currentSketch();
@@ -165,20 +166,18 @@ export class VerifySketch extends CoreServiceContribution {
165166
}
166167
}
167168

168-
export namespace VerifySketch {
169-
export namespace Commands {
170-
export const VERIFY_SKETCH: Command = {
171-
id: 'arduino-verify-sketch',
172-
};
173-
export const EXPORT_BINARIES: Command = {
174-
id: 'arduino-export-binaries',
175-
};
176-
export const VERIFY_SKETCH_TOOLBAR: Command = {
177-
id: 'arduino-verify-sketch--toolbar',
178-
};
179-
}
180-
export type Ref = CoreService.Options.SketchBased &
181-
Readonly<{
182-
fqbn: { original?: string | undefined; decorated?: string | undefined };
183-
}>;
169+
export namespace VerifySketchCommands {
170+
export const VERIFY_SKETCH: Command = {
171+
id: 'arduino-verify-sketch',
172+
};
173+
export const EXPORT_BINARIES: Command = {
174+
id: 'arduino-export-binaries',
175+
};
176+
export const VERIFY_SKETCH_TOOLBAR: Command = {
177+
id: 'arduino-verify-sketch--toolbar',
178+
};
184179
}
180+
export type VerifyParams = CoreService.Options.SketchBased &
181+
Readonly<{
182+
fqbn: { original?: string | undefined; decorated?: string | undefined };
183+
}>;

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

+36-17
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { tryParseError } from './cli-error-parser';
3333
import { Instance } from './cli-protocol/cc/arduino/cli/commands/v1/common_pb';
3434
import { firstToUpperCase, notEmpty } from '../common/utils';
3535
import { ServiceError } from './service-error';
36-
import { ExecuteWithProgress } from './grpc-progressible';
36+
import { ExecuteWithProgress, ProgressResponse } from './grpc-progressible';
3737

3838
namespace Uploadable {
3939
export type Request = UploadRequest | UploadUsingProgrammerRequest;
@@ -55,13 +55,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
5555
const coreClient = await this.coreClient;
5656
const { client, instance } = coreClient;
5757
let buildPath: string | undefined = undefined;
58-
const progressHandler =
59-
!!options.progressId &&
60-
ExecuteWithProgress.createDataCallback({
61-
progressId: options.progressId,
62-
responseService: this.responseService,
63-
});
64-
const handler = this.createOnDataHandler<CompileResponse>((response) => {
58+
const progressHandler = this.createProgressHandler(options);
59+
const buildPathHandler = (response: CompileResponse) => {
6560
if (progressHandler) {
6661
progressHandler(response);
6762
}
@@ -75,7 +70,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
7570
);
7671
}
7772
}
78-
});
73+
};
74+
const handler = this.createOnDataHandler<CompileResponse>(
75+
progressHandler,
76+
buildPathHandler
77+
);
7978
const request = this.compileRequest(options, instance);
8079
return new Promise<void>((resolve, reject) => {
8180
client
@@ -206,7 +205,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
206205
): Promise<void> {
207206
const coreClient = await this.coreClient;
208207
const { client, instance } = coreClient;
209-
const handler = this.createOnDataHandler();
208+
const progressHandler = this.createProgressHandler(options);
209+
const handler = this.createOnDataHandler(progressHandler);
210210
return this.notifyUploadWillStart(options).then(() =>
211211
new Promise<void>((resolve, reject) => {
212212
responseHandler(
@@ -273,7 +273,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
273273
async burnBootloader(options: CoreService.Options.Bootloader): Promise<void> {
274274
const coreClient = await this.coreClient;
275275
const { client, instance } = coreClient;
276-
const handler = this.createOnDataHandler();
276+
const progressHandler = this.createProgressHandler(options);
277+
const handler = this.createOnDataHandler(progressHandler);
277278
const request = this.burnBootloaderRequest(options, instance);
278279
return this.notifyUploadWillStart(options).then(() =>
279280
new Promise<void>((resolve, reject) => {
@@ -328,8 +329,23 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
328329
return request;
329330
}
330331

332+
private createProgressHandler<R extends ProgressResponse>(
333+
options: CoreService.Options.Base
334+
) {
335+
// If client did not provide the progress ID, do nothing.
336+
if (!options.progressId) {
337+
return () => {
338+
/* NOOP */
339+
};
340+
}
341+
return ExecuteWithProgress.createDataCallback<R>({
342+
progressId: options.progressId,
343+
responseService: this.responseService,
344+
});
345+
}
346+
331347
private createOnDataHandler<R extends StreamingResponse>(
332-
onResponse?: (response: R) => void
348+
...handlers: ((response: R) => void)[] // TODO: clean up this API.
333349
): Disposable & {
334350
stderr: Buffer[];
335351
onData: (response: R) => void;
@@ -348,7 +364,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
348364
buffer.addChunk(out);
349365
buffer.addChunk(err, OutputMessage.Severity.Error);
350366
},
351-
onResponse,
367+
handlers,
352368
});
353369
return {
354370
dispose: () => buffer.dispose(),
@@ -424,14 +440,17 @@ namespace StreamingResponse {
424440
const err = response.getErrStream_asU8();
425441
options.stderr.push(err);
426442
options.onData(out, err);
427-
if (options.onResponse) {
428-
options.onResponse(response);
429-
}
443+
options.handlers?.forEach((handler) => handler(response));
430444
};
431445
}
432446
export interface Options<R extends StreamingResponse> {
433447
readonly stderr: Uint8Array[];
434448
readonly onData: (out: Uint8Array, err: Uint8Array) => void;
435-
readonly onResponse?: (response: R) => void;
449+
/**
450+
* Additional request handlers.
451+
* For example, when tracing the progress of a task and
452+
* collecting the output (out, err) and the `build_path` from the CLI.
453+
*/
454+
readonly handlers?: ((response: R) => void)[];
436455
}
437456
}

0 commit comments

Comments
 (0)