Skip to content

Commit f271842

Browse files
author
Akos Kitta
committed
fall back to new temp sketch URI when no recent WS
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent 174411c commit f271842

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

arduino-ide-extension/src/browser/theia/core/frontend-application.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ export class FrontendApplication extends TheiaFrontendApplication {
2222

2323
protected override async initializeLayout(): Promise<void> {
2424
await super.initializeLayout();
25-
const roots = await this.workspaceService.roots;
26-
for (const root of roots) {
27-
const exists = await this.fileService.exists(root.resource);
28-
if (exists) {
29-
this.sketchesService.markAsRecentlyOpened(root.resource.toString()); // no await, will get the notification later and rebuild the menu
25+
this.workspaceService.roots.then(async (roots) => {
26+
for (const root of roots) {
3027
await this.commandService.executeCommand(
3128
ArduinoCommands.OPEN_SKETCH_FILES.id,
3229
root.resource
3330
);
31+
this.sketchesService.markAsRecentlyOpened(root.resource.toString()); // no await, will get the notification later and rebuild the menu
3432
}
35-
}
33+
});
3634
}
3735

3836
protected override getStartupIndicator(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ export class WorkspaceService extends TheiaWorkspaceService {
7272
// Remove the leading # and decode the URI.
7373
const wpPath = decodeURI(window.location.hash.substring(1));
7474
const workspaceUri = new URI().withPath(wpPath).withScheme('file');
75-
// Customization! Here, we do no check if the workspace exists.
75+
// ### Customization! Here, we do no check if the workspace exists.
7676
return workspaceUri.toString();
7777
} else {
7878
// Else, ask the server for its suggested workspace (usually the one
7979
// specified on the CLI, or the most recent).
80+
// ### Customization! the default workspace server will create a new sketch and will return with its URI if no recent workspaces are available.
8081
return this.server.getMostRecentlyUsedWorkspace();
8182
}
8283
}
Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { promises as fs, constants } from 'fs';
12
import { injectable, inject } from '@theia/core/shared/inversify';
23
import { ILogger } from '@theia/core/lib/common/logger';
34
import { DefaultWorkspaceServer as TheiaDefaultWorkspaceServer } from '@theia/workspace/lib/node/default-workspace-server';
45
import { ConfigService } from '../../../common/protocol/config-service';
6+
import { SketchesService } from '../../../common/protocol';
7+
import { FileUri } from '@theia/core/lib/node';
58

69
@injectable()
710
export class DefaultWorkspaceServer extends TheiaDefaultWorkspaceServer {
@@ -11,13 +14,49 @@ export class DefaultWorkspaceServer extends TheiaDefaultWorkspaceServer {
1114
@inject(ILogger)
1215
protected readonly logger: ILogger;
1316

14-
protected override async getWorkspaceURIFromCli(): Promise<string | undefined> {
17+
@inject(SketchesService)
18+
private readonly sketchesService: SketchesService;
19+
20+
override async onStart(): Promise<void> {
21+
// NOOP
22+
// No need to remove untitled workspaces. IDE2 does not use workspaces.
23+
}
24+
25+
override async getMostRecentlyUsedWorkspace(): Promise<string | undefined> {
26+
const uri = await super.getMostRecentlyUsedWorkspace();
27+
if (!uri) {
28+
const { uri } = await this.sketchesService.createNewSketch();
29+
return uri;
30+
}
31+
return uri;
32+
}
33+
34+
/**
35+
* This is the async re-implementation of the default Theia behavior.
36+
*/
37+
override async getRecentWorkspaces(): Promise<string[]> {
38+
const listUri: string[] = [];
39+
const data = await this.readRecentWorkspacePathsFromUserHome();
40+
if (data && data.recentRoots) {
41+
await Promise.all(
42+
data.recentRoots
43+
.filter((element) => Boolean(element))
44+
.map(async (element) => {
45+
if (await this.exists(element)) {
46+
listUri.push(element);
47+
}
48+
})
49+
);
50+
}
51+
return listUri;
52+
}
53+
54+
private async exists(uri: string): Promise<boolean> {
1555
try {
16-
const config = await this.configService.getConfiguration();
17-
return config.sketchDirUri;
18-
} catch (err) {
19-
this.logger.error(`Failed to determine the sketch directory: ${err}`);
20-
return super.getWorkspaceURIFromCli();
56+
await fs.access(FileUri.fsPath(uri), constants.R_OK | constants.W_OK);
57+
return true;
58+
} catch {
59+
return false;
2160
}
2261
}
2362
}

0 commit comments

Comments
 (0)