Skip to content

Commit

Permalink
feat: update port selection when it changes during upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Akos Kitta committed Jul 27, 2023
1 parent 8e76373 commit 9666f89
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
37 changes: 26 additions & 11 deletions arduino-ide-extension/src/browser/contributions/upload-sketch.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { inject, injectable } from '@theia/core/shared/inversify';
import { Emitter } from '@theia/core/lib/common/event';
import { CoreService, sanitizeFqbn } from '../../common/protocol';
import { nls } from '@theia/core/lib/common/nls';
import { inject, injectable } from '@theia/core/shared/inversify';
import {
CoreService,
portIdentifierEquals,
sanitizeFqbn,
} from '../../common/protocol';
import { ArduinoMenus } from '../menu/arduino-menus';
import { CurrentSketch } from '../sketches-service-client-impl';
import { ArduinoToolbar } from '../toolbar/arduino-toolbar';
import {
Command,
CommandRegistry,
MenuModelRegistry,
CoreServiceContribution,
KeybindingRegistry,
MenuModelRegistry,
TabBarToolbarRegistry,
CoreServiceContribution,
} from './contribution';
import { nls } from '@theia/core/lib/common';
import { CurrentSketch } from '../sketches-service-client-impl';
import type { VerifySketchParams } from './verify-sketch';
import { UserFields } from './user-fields';
import type { VerifySketchParams } from './verify-sketch';

@injectable()
export class UploadSketch extends CoreServiceContribution {
@inject(UserFields)
private readonly userFields: UserFields;

private readonly onDidChangeEmitter = new Emitter<void>();
private readonly onDidChange = this.onDidChangeEmitter.event;
private uploadInProgress = false;

@inject(UserFields)
private readonly userFields: UserFields;

override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(UploadSketch.Commands.UPLOAD_SKETCH, {
execute: async () => {
Expand Down Expand Up @@ -134,13 +138,24 @@ export class UploadSketch extends CoreServiceContribution {
return;
}

await this.doWithProgress({
const uploadResponse = await this.doWithProgress({
progressText: nls.localize('arduino/sketch/uploading', 'Uploading...'),
task: (progressId, coreService) =>
coreService.upload({ ...uploadOptions, progressId }),
keepOutput: true,
});

if (
uploadResponse.portBeforeUpload &&
!portIdentifierEquals(
uploadResponse.portBeforeUpload,
uploadResponse.portAfterUpload
)
) {
// https://github.com/arduino/arduino-cli/issues/2245
this.boardsServiceProvider.updatePort(uploadResponse.portAfterUpload);
}

this.messageService.info(
nls.localize('arduino/sketch/doneUploading', 'Done uploading.'),
{ timeout: 3000 }
Expand Down
26 changes: 20 additions & 6 deletions arduino-ide-extension/src/common/protocol/core-service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { nls } from '@theia/core/lib/common/nls';
import { ApplicationError } from '@theia/core/lib/common/application-error';
import { nls } from '@theia/core/lib/common/nls';
import type {
Location,
Range,
Position,
Range,
} from '@theia/core/shared/vscode-languageserver-protocol';
import type { CompileSummary as ApiCompileSummary } from 'vscode-arduino-api';
import type { BoardUserField, Installable } from '../../common/protocol/';
import type { PortIdentifier, Programmer } from './boards-service';
import { isPortIdentifier, PortIdentifier, Programmer } from './boards-service';
import type { IndexUpdateSummary } from './notification-service';
import type { Sketch } from './sketches-service';
import { IndexUpdateSummary } from './notification-service';
import type { CompileSummary as ApiCompileSummary } from 'vscode-arduino-api';

export const CompilerWarningLiterals = [
'None',
Expand Down Expand Up @@ -148,11 +148,25 @@ export function isCompileSummary(arg: unknown): arg is CompileSummary {
);
}

export interface UploadResponse {
readonly portBeforeUpload?: PortIdentifier | undefined;
readonly portAfterUpload: PortIdentifier;
}
export function isUploadResponse(arg: unknown): arg is UploadResponse {
return (
Boolean(arg) &&
typeof arg === 'object' &&
((<UploadResponse>arg).portBeforeUpload === undefined ||
isPortIdentifier((<UploadResponse>arg).portBeforeUpload)) &&
isPortIdentifier((<UploadResponse>arg).portAfterUpload)
);
}

export const CoreServicePath = '/services/core-service';
export const CoreService = Symbol('CoreService');
export interface CoreService {
compile(options: CoreService.Options.Compile): Promise<void>;
upload(options: CoreService.Options.Upload): Promise<void>;
upload(options: CoreService.Options.Upload): Promise<UploadResponse>;
burnBootloader(options: CoreService.Options.Bootloader): Promise<void>;
/**
* Refreshes the underling core gRPC client for the Arduino CLI.
Expand Down
40 changes: 34 additions & 6 deletions arduino-ide-extension/src/node/core-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { inject, injectable } from '@theia/core/shared/inversify';
import { relative } from 'node:path';
import * as jspb from 'google-protobuf';
import { BoolValue } from 'google-protobuf/google/protobuf/wrappers_pb';
import { ClientReadableStream } from '@grpc/grpc-js';
import type { ClientReadableStream } from '@grpc/grpc-js';
import {
CompilerWarnings,
CoreService,
CoreError,
CompileSummary,
isCompileSummary,
isUploadResponse,
} from '../common/protocol/core-service';
import {
CompileRequest,
Expand All @@ -30,6 +31,7 @@ import {
OutputMessage,
PortIdentifier,
Port,
UploadResponse as ApiUploadResponse,
} from '../common/protocol';
import { ArduinoCoreServiceClient } from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb';
import { Port as RpcPort } from './cli-protocol/cc/arduino/cli/commands/v1/port_pb';
Expand Down Expand Up @@ -174,7 +176,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
return request;
}

upload(options: CoreService.Options.Upload): Promise<void> {
upload(options: CoreService.Options.Upload): Promise<ApiUploadResponse> {
const { usingProgrammer } = options;
return this.doUpload(
options,
Expand Down Expand Up @@ -203,14 +205,25 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
) => (request: REQ) => ClientReadableStream<RESP>,
errorCtor: ApplicationError.Constructor<number, CoreError.ErrorLocation[]>,
task: string
): Promise<void> {
): Promise<ApiUploadResponse> {
const uploadResponseFragment: Mutable<Partial<ApiUploadResponse>> = {
portBeforeUpload: options.port,
};
const coreClient = await this.coreClient;
const { client, instance } = coreClient;
const progressHandler = this.createProgressHandler(options);
const handler = this.createOnDataHandler(progressHandler);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const updateUploadResponseFragmentHandler = (_: RESP) => {
// TODO: update the upload response fragment with the new port.
// https://github.com/arduino/arduino-cli/issues/2245
};
const handler = this.createOnDataHandler(
progressHandler,
updateUploadResponseFragmentHandler
);
const grpcCall = responseFactory(client);
return this.notifyUploadWillStart(options).then(() =>
new Promise<void>((resolve, reject) => {
new Promise<ApiUploadResponse>((resolve, reject) => {
grpcCall(this.initUploadRequest(request, options, instance))
.on('data', handler.onData)
.on('error', (error) => {
Expand All @@ -236,7 +249,22 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
);
}
})
.on('end', resolve);
.on('end', () => {
uploadResponseFragment.portAfterUpload = options.port; // https://github.com/arduino/arduino-cli/issues/2245
if (isUploadResponse(uploadResponseFragment)) {
resolve(uploadResponseFragment);
} else {
reject(
new Error(
`Could not detect the port after the upload. Upload options were: ${JSON.stringify(
options
)}, upload response was: ${JSON.stringify(
uploadResponseFragment
)}`
)
);
}
});
}).finally(async () => {
handler.dispose();
await this.notifyUploadDidFinish(options);
Expand Down

0 comments on commit 9666f89

Please sign in to comment.