Skip to content

Commit f1c8004

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
GH-432: Made compile/verify work on dirty editors
Signed-off-by: Akos Kitta <[email protected]>
1 parent ec1abcc commit f1c8004

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { inject, injectable, interfaces } from 'inversify';
22
import URI from '@theia/core/lib/common/uri';
33
import { ILogger } from '@theia/core/lib/common/logger';
4+
import { Saveable } from '@theia/core/lib/browser/saveable';
45
import { FileService } from '@theia/filesystem/lib/browser/file-service';
56
import { MaybePromise } from '@theia/core/lib/common/types';
67
import { LabelProvider } from '@theia/core/lib/browser/label-provider';
8+
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
79
import { MessageService } from '@theia/core/lib/common/message-service';
810
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
911
import { open, OpenerService } from '@theia/core/lib/browser/opener-service';
@@ -85,6 +87,23 @@ export abstract class SketchContribution extends Contribution {
8587
@inject(ArduinoPreferences)
8688
protected readonly preferences: ArduinoPreferences;
8789

90+
@inject(EditorManager)
91+
protected readonly editorManager: EditorManager;
92+
93+
protected async sourceOverride(): Promise<Record<string, string>> {
94+
const override: Record<string, string> = {};
95+
const sketch = await this.sketchServiceClient.currentSketch();
96+
if (sketch) {
97+
for (const editor of this.editorManager.all) {
98+
const uri = editor.editor.uri;
99+
if (Saveable.isDirty(editor) && Sketch.isInSketch(uri, sketch)) {
100+
override[uri.toString()] = editor.editor.document.getText();
101+
}
102+
}
103+
}
104+
return override;
105+
}
106+
88107
}
89108

90109
export namespace Contribution {

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ export class UploadSketch extends SketchContribution {
7373
}
7474

7575
async uploadSketch(usingProgrammer: boolean = false): Promise<void> {
76-
const uri = await this.sketchServiceClient.currentSketchFile();
77-
if (!uri) {
76+
const sketch = await this.sketchServiceClient.currentSketch();
77+
if (!sketch) {
7878
return;
7979
}
8080
let shouldAutoConnect = false;
@@ -88,15 +88,16 @@ export class UploadSketch extends SketchContribution {
8888
}
8989
try {
9090
const { boardsConfig } = this.boardsServiceClientImpl;
91-
const [fqbn, { selectedProgrammer }, verify, verbose] = await Promise.all([
91+
const [fqbn, { selectedProgrammer }, verify, verbose, sourceOverride] = await Promise.all([
9292
this.boardsDataStore.appendConfigToFqbn(boardsConfig.selectedBoard?.fqbn),
9393
this.boardsDataStore.getData(boardsConfig.selectedBoard?.fqbn),
9494
this.preferences.get('arduino.upload.verify'),
95-
this.preferences.get('arduino.upload.verbose')
95+
this.preferences.get('arduino.upload.verbose'),
96+
this.sourceOverride()
9697
]);
9798

9899
let options: CoreService.Upload.Options | undefined = undefined;
99-
const sketchUri = uri;
100+
const sketchUri = sketch.uri;
100101
const optimizeForDebug = this.editorMode.compileForDebug;
101102
const { selectedPort } = boardsConfig;
102103
const port = selectedPort?.address;
@@ -110,7 +111,8 @@ export class UploadSketch extends SketchContribution {
110111
programmer,
111112
port,
112113
verbose,
113-
verify
114+
verify,
115+
sourceOverride
114116
};
115117
} else {
116118
options = {
@@ -119,7 +121,8 @@ export class UploadSketch extends SketchContribution {
119121
optimizeForDebug,
120122
port,
121123
verbose,
122-
verify
124+
verify,
125+
sourceOverride
123126
};
124127
}
125128
this.outputChannelManager.getChannel('Arduino').clear();

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,25 @@ export class VerifySketch extends SketchContribution {
6969
}
7070

7171
async verifySketch(exportBinaries: boolean = false): Promise<void> {
72-
const uri = await this.sketchServiceClient.currentSketchFile();
73-
if (!uri) {
72+
const sketch = await this.sketchServiceClient.currentSketch();
73+
if (!sketch) {
7474
return;
7575
}
7676
try {
7777
const { boardsConfig } = this.boardsServiceClientImpl;
78-
const fqbn = await this.boardsDataStore.appendConfigToFqbn(boardsConfig.selectedBoard?.fqbn);
78+
const [fqbn, sourceOverride] = await Promise.all([
79+
this.boardsDataStore.appendConfigToFqbn(boardsConfig.selectedBoard?.fqbn),
80+
this.sourceOverride()
81+
]);
7982
const verbose = this.preferences.get('arduino.compile.verbose');
8083
this.outputChannelManager.getChannel('Arduino').clear();
8184
await this.coreService.compile({
82-
sketchUri: uri,
85+
sketchUri: sketch.uri,
8386
fqbn,
8487
optimizeForDebug: this.editorMode.compileForDebug,
8588
verbose,
86-
exportBinaries
89+
exportBinaries,
90+
sourceOverride
8791
});
8892
this.messageService.info('Done compiling.', { timeout: 1000 });
8993
} catch (e) {

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

+4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ export namespace CoreService {
1313

1414
export namespace Compile {
1515
export interface Options {
16+
/**
17+
* `file` URI to the sketch folder.
18+
*/
1619
readonly sketchUri: string;
1720
readonly fqbn?: string | undefined;
1821
readonly optimizeForDebug: boolean;
1922
readonly verbose: boolean;
23+
readonly sourceOverride: Record<string, string>;
2024
}
2125
}
2226

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

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FileUri } from '@theia/core/lib/node/file-uri';
22
import { inject, injectable } from 'inversify';
3-
import { dirname } from 'path';
3+
import { relative } from 'path';
4+
import * as jspb from 'google-protobuf';
45
import { CoreService } from '../common/protocol/core-service';
56
import { CompileReq, CompileResp } from './cli-protocol/commands/compile_pb';
67
import { CoreClientProvider } from './core-client-provider';
@@ -24,17 +25,15 @@ export class CoreServiceImpl implements CoreService {
2425
protected readonly notificationService: NotificationServiceServer;
2526

2627
async compile(options: CoreService.Compile.Options & { exportBinaries: boolean }): Promise<void> {
27-
this.outputService.append({ chunk: 'Compile...\n' + JSON.stringify(options, null, 2) + '\n--------------------------\n' });
2828
const { sketchUri, fqbn } = options;
29-
const sketchFilePath = FileUri.fsPath(sketchUri);
30-
const sketchpath = dirname(sketchFilePath);
29+
const sketchPath = FileUri.fsPath(sketchUri);
3130

3231
const coreClient = await this.coreClient();
3332
const { client, instance } = coreClient;
3433

3534
const compilerReq = new CompileReq();
3635
compilerReq.setInstance(instance);
37-
compilerReq.setSketchpath(sketchpath);
36+
compilerReq.setSketchpath(sketchPath);
3837
if (fqbn) {
3938
compilerReq.setFqbn(fqbn);
4039
}
@@ -43,6 +42,7 @@ export class CoreServiceImpl implements CoreService {
4342
compilerReq.setVerbose(options.verbose);
4443
compilerReq.setQuiet(false);
4544
compilerReq.setExportBinaries(options.exportBinaries);
45+
this.mergeSourceOverrides(compilerReq, options);
4646

4747
const result = client.compile(compilerReq);
4848
try {
@@ -76,18 +76,15 @@ export class CoreServiceImpl implements CoreService {
7676
task: string = 'upload'): Promise<void> {
7777

7878
await this.compile(Object.assign(options, { exportBinaries: false }));
79-
const chunk = firstToUpperCase(task) + '...\n';
80-
this.outputService.append({ chunk: chunk + JSON.stringify(options, null, 2) + '\n--------------------------\n' });
8179
const { sketchUri, fqbn, port, programmer } = options;
82-
const sketchFilePath = FileUri.fsPath(sketchUri);
83-
const sketchpath = dirname(sketchFilePath);
80+
const sketchPath = FileUri.fsPath(sketchUri);
8481

8582
const coreClient = await this.coreClient();
8683
const { client, instance } = coreClient;
8784

8885
const req = requestProvider();
8986
req.setInstance(instance);
90-
req.setSketchPath(sketchpath);
87+
req.setSketchPath(sketchPath);
9188
if (fqbn) {
9289
req.setFqbn(fqbn);
9390
}
@@ -169,4 +166,15 @@ export class CoreServiceImpl implements CoreService {
169166
return coreClient;
170167
}
171168

169+
private mergeSourceOverrides(req: { getSourceOverrideMap(): jspb.Map<string, string> }, options: CoreService.Compile.Options): void {
170+
const sketchPath = FileUri.fsPath(options.sketchUri);
171+
for (const uri of Object.keys(options.sourceOverride)) {
172+
const content = options.sourceOverride[uri];
173+
if (content) {
174+
const relativePath = relative(sketchPath, FileUri.fsPath(uri));
175+
req.getSourceOverrideMap().set(relativePath, content);
176+
}
177+
}
178+
}
179+
172180
}

0 commit comments

Comments
 (0)