Skip to content

Commit d5589c4

Browse files
committed
Get the default sketchbook path from backend
Signed-off-by: jbicker <jan.bicker@typefox.io>
1 parent d809daa commit d5589c4

8 files changed

+65
-27
lines changed

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { BoardsConfigDialog } from './boards/boards-config-dialog';
4646
import { BoardsToolBarItem } from './boards/boards-toolbar-item';
4747
import { BoardsConfig } from './boards/boards-config';
4848
import { MonitorService } from '../common/protocol/monitor-service';
49+
import { ConfigService } from '../common/protocol/config-service';
4950

5051
export namespace ArduinoMenus {
5152
export const SKETCH = [...MAIN_MENU_BAR, '3_sketch'];
@@ -140,6 +141,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
140141
@inject(WorkspaceService)
141142
protected readonly workspaceService: WorkspaceService;
142143

144+
@inject(ConfigService)
145+
protected readonly configService: ConfigService;
146+
143147
protected boardsToolbarItem: BoardsToolBarItem | null;
144148
protected wsSketchCount: number = 0;
145149

@@ -452,11 +456,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
452456
protected async getWorkspaceSketches(): Promise<Sketch[]> {
453457

454458
let sketches: Sketch[] = [];
455-
const userHome = await this.fileSystem.getCurrentUserHome();
456-
console.log('userHome', userHome);
457-
if (!!userHome) {
458-
// TODO: get this from the backend
459-
const result = new URI(userHome.uri).resolve('Arduino-PoC').resolve('Sketches').toString();
459+
const config = await this.configService.getConfiguration();
460+
const result = config.sketchDirPath;
461+
if (!!result) {
460462
const stat = await this.fileSystem.getFileStat(result);
461463
if (!!stat) {
462464
sketches = await this.sketches.getSketches(stat);

arduino-ide-extension/src/browser/arduino-frontend-module.ts

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { LibraryItemRenderer } from './library/library-item-renderer';
5656
import { BoardItemRenderer } from './boards/boards-item-renderer';
5757
import { MonitorServiceClientImpl } from './monitor/monitor-service-client-impl';
5858
import { MonitorServicePath, MonitorService, MonitorServiceClient } from '../common/protocol/monitor-service';
59+
import { ConfigService, ConfigServicePath } from '../common/protocol/config-service';
5960
const ElementQueries = require('css-element-queries/src/ElementQueries');
6061

6162
if (!ARDUINO_PRO_MODE) {
@@ -96,6 +97,9 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un
9697
// Sketch list service
9798
bind(SketchesService).toDynamicValue(context => WebSocketConnectionProvider.createProxy(context.container, SketchesServicePath)).inSingletonScope();
9899

100+
// Config service
101+
bind(ConfigService).toDynamicValue(context => WebSocketConnectionProvider.createProxy(context.container, ConfigServicePath)).inSingletonScope();
102+
99103
// Boards service
100104
bind(BoardsService).toDynamicValue(context => {
101105
const connection = context.container.get(WebSocketConnectionProvider);

arduino-ide-extension/src/browser/arduino-workspace-service.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { WorkspaceServer } from "@theia/workspace/lib/common";
44
import { FileSystem, FileStat } from "@theia/filesystem/lib/common";
55
import URI from "@theia/core/lib/common/uri";
66
import { SketchFactory } from "./sketch-factory";
7+
import { ConfigService } from "../common/protocol/config-service";
78

89
/**
910
* This is workaround to have custom frontend binding for the default workspace, although we
@@ -21,16 +22,14 @@ export class AWorkspaceService extends WorkspaceService {
2122
@inject(SketchFactory)
2223
protected readonly sketchFactory: SketchFactory;
2324

25+
@inject(ConfigService)
26+
protected readonly configService: ConfigService;
27+
2428
protected async getDefaultWorkspacePath(): Promise<string | undefined> {
2529
let result = await super.getDefaultWorkspacePath();
2630
if (!result) {
27-
const userHome = await this.fileSystem.getCurrentUserHome();
28-
if (!userHome) {
29-
return;
30-
}
31-
32-
// The backend has created this location if it was missing.
33-
result = new URI(userHome.uri).resolve('Arduino-PoC').resolve('Sketches').toString();
31+
const config = await this.configService.getConfiguration();
32+
result = config.sketchDirPath;
3433
}
3534

3635
const stat = await this.fileSystem.getFileStat(result);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
export const ConfigServicePath = '/services/config-service';
3+
export const ConfigService = Symbol('ConfigService');
4+
5+
export interface ConfigService {
6+
getConfiguration(): Promise<Config>;
7+
}
8+
9+
export interface Config {
10+
sketchDirPath: string;
11+
dataDirPath: string;
12+
}

arduino-ide-extension/src/node/arduino-backend-module.ts

+11
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import { DefaultWorkspaceServerExt } from './default-workspace-server-ext';
1919
import { WorkspaceServer } from '@theia/workspace/lib/common';
2020
import { SketchesServiceImpl } from './sketches-service-impl';
2121
import { SketchesService, SketchesServicePath } from '../common/protocol/sketches-service';
22+
import { ConfigService, ConfigServicePath } from '../common/protocol/config-service';
2223
import { MonitorServiceImpl } from './monitor/monitor-service-impl';
2324
import { MonitorService, MonitorServicePath, MonitorServiceClient } from '../common/protocol/monitor-service';
2425
import { MonitorClientProvider } from './monitor/monitor-client-provider';
2526
import { ArduinoCli } from './arduino-cli';
2627
import { ArduinoCliContribution } from './arduino-cli-contribution';
2728
import { CliContribution } from '@theia/core/lib/node';
29+
import { ConfigServiceImpl } from './config-service-impl';
2830

2931
export default new ContainerModule((bind, unbind, isBound, rebind) => {
3032
// Theia backend CLI contribution.
@@ -53,6 +55,15 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
5355
bindBackendService(SketchesServicePath, SketchesService);
5456
});
5557
bind(ConnectionContainerModule).toConstantValue(sketchesServiceConnectionModule);
58+
59+
bind(ConfigServiceImpl).toSelf().inSingletonScope();
60+
bind(ConfigService).toService(ConfigServiceImpl);
61+
62+
// Config service
63+
const configServiceConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
64+
bindBackendService(ConfigServicePath, ConfigService);
65+
});
66+
bind(ConnectionContainerModule).toConstantValue(configServiceConnectionModule);
5667

5768
// Boards service
5869
const boardsServiceConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {

arduino-ide-extension/src/node/arduino-cli.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as which from 'which';
33
import * as cp from 'child_process';
44
import { join, delimiter } from 'path';
55
import { injectable } from 'inversify';
6+
import { Config } from '../common/protocol/config-service';
67

78
@injectable()
89
export class ArduinoCli {
@@ -20,9 +21,9 @@ export class ArduinoCli {
2021
});
2122
}
2223

23-
async getDefaultConfig(): Promise<ArduinoCli.Config> {
24+
async getDefaultConfig(): Promise<Config> {
2425
const command = await this.getExecPath();
25-
return new Promise<ArduinoCli.Config>((resolve, reject) => {
26+
return new Promise<Config>((resolve, reject) => {
2627
cp.execFile(
2728
command,
2829
['config', 'dump', '--format', 'json'],
@@ -41,7 +42,7 @@ export class ArduinoCli {
4142

4243
// https://github.com/arduino/arduino-cli/issues/342
4344
// XXX: this is a hack. The CLI provides a non-valid JSON.
44-
const config: Partial<ArduinoCli.Config> = {};
45+
const config: Partial<Config> = {};
4546
const raw = stdout.trim();
4647
for (const line of raw.split(/\r?\n/) || []) {
4748
// TODO: Named capture groups are avail from ES2018.
@@ -69,11 +70,4 @@ export class ArduinoCli {
6970
});
7071
}
7172

72-
}
73-
74-
export namespace ArduinoCli {
75-
export interface Config {
76-
sketchDirPath: string;
77-
dataDirPath: string;
78-
}
79-
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { injectable, inject } from "inversify";
2+
import { ConfigService, Config } from "../common/protocol/config-service";
3+
import { ArduinoCli } from "./arduino-cli";
4+
5+
@injectable()
6+
export class ConfigServiceImpl implements ConfigService {
7+
8+
@inject(ArduinoCli)
9+
protected readonly cli: ArduinoCli;
10+
11+
async getConfiguration(): Promise<Config> {
12+
return this.cli.getDefaultConfig();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import * as os from 'os';
2-
import * as path from 'path';
3-
import { injectable } from 'inversify';
1+
import { injectable, inject } from 'inversify';
42
import { FileUri } from '@theia/core/lib/node/file-uri';
53
import { DefaultWorkspaceServer } from '@theia/workspace/lib/node/default-workspace-server';
4+
import { ConfigService } from '../common/protocol/config-service';
65

76
@injectable()
87
export class DefaultWorkspaceServerExt extends DefaultWorkspaceServer {
98

9+
@inject(ConfigService) protected readonly configService: ConfigService;
10+
1011
protected async getWorkspaceURIFromCli(): Promise<string | undefined> {
11-
return FileUri.create(path.join(os.homedir(), 'Arduino-PoC', 'Sketches')).toString();
12+
const config = await this.configService.getConfiguration();
13+
return FileUri.create(config.sketchDirPath).toString();
1214
}
1315

1416
}

0 commit comments

Comments
 (0)