Skip to content

[ATL-1531] Integrate arduino-cli 0.19.0 #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Made CLI initialization partly asynchronous
  • Loading branch information
silvanocerza committed Jun 16, 2021
commit 8f659880777ce738cd7001da03df1f1ce671bc42
47 changes: 27 additions & 20 deletions arduino-ide-extension/src/node/core-client-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as grpc from '@grpc/grpc-js';
import { inject, injectable } from 'inversify';
import { inject, injectable, postConstruct } from 'inversify';
import { Event, Emitter } from '@theia/core/lib/common/event';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
import { GrpcClientProvider } from './grpc-client-provider';
Expand All @@ -25,27 +25,39 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
client.client.close();
}

protected async reconcileClient(port: string | number | undefined): Promise<void> {
if (port && port === this._port) {
// No need to create a new gRPC client, but we have to update the indexes.
@postConstruct()
protected async init(): Promise<void> {
this.daemon.ready.then(async () => {
const cliConfig = this.configService.cliConfiguration;
// First create the client and the instance synchronously
// and notify client is ready.
// TODO: Creation failure should probably be handled here
await this.reconcileClient(cliConfig ? cliConfig.daemon.port : undefined)
.then(() => { this.onClientReadyEmitter.fire(); });

// If client has been created correctly update indexes and initialize
// its instance by loading platforms and cores.
if (this._client && !(this._client instanceof Error)) {
await this.updateIndexes(this._client);
// We must initialize the client again to get the updated indexes.
await this.initInstance(this._client);
this.onClientReadyEmitter.fire();
await this.updateIndexes(this._client)
.then(this.initInstance);
}
} else {
await super.reconcileClient(port);
this.onClientReadyEmitter.fire();
}
});

this.daemon.onDaemonStopped(() => {
if (this._client && !(this._client instanceof Error)) {
this.close(this._client);
}
this._client = undefined;
this._port = undefined;
})
}

protected async createClient(port: string | number): Promise<CoreClientProvider.Client> {
// https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/doc/grpcjs_support.md#usage
// @ts-ignore
const ArduinoCoreServiceClient = grpc.makeClientConstructor(commandsGrpcPb['cc.arduino.cli.commands.v1.ArduinoCoreService'], 'ArduinoCoreServiceService') as any;
const client = new ArduinoCoreServiceClient(`localhost:${port}`, grpc.credentials.createInsecure(), this.channelOptions) as ArduinoCoreServiceClient;

const createRes = await new Promise<CreateResponse>((resolve, reject) => {
client.create(new CreateRequest(), (err, res: CreateResponse) => {
if (err) {
Expand All @@ -61,12 +73,6 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
throw new Error('Could not retrieve instance from the initialize response.');
}

// Update indexes before init
await this.updateIndexes({ instance, client });

// Initialize instance by loading indexes, platforms and libraries
await this.initInstance({instance, client})

return { instance, client };
}

Expand Down Expand Up @@ -100,7 +106,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
})
}

protected async updateIndexes({ client, instance }: CoreClientProvider.Client): Promise<void> {
protected async updateIndexes({ client, instance }: CoreClientProvider.Client): Promise<CoreClientProvider.Client> {
// in a separate promise, try and update the index
let indexUpdateSucceeded = true;
for (let i = 0; i < 10; i++) {
Expand Down Expand Up @@ -133,6 +139,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
if (indexUpdateSucceeded && libIndexUpdateSucceeded) {
this.notificationService.notifyIndexUpdated();
}
return { client, instance }
}

protected async updateLibraryIndex({ client, instance }: CoreClientProvider.Client): Promise<void> {
Expand Down