Skip to content

Commit 650230a

Browse files
author
Akos Kitta
committed
refined open logic when sketch name is invalid
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
1 parent aadd403 commit 650230a

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

arduino-ide-extension/src/browser/contributions/open-sketch.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,34 @@ export class OpenSketch extends SketchContribution {
116116
if (filePaths.length > 1) {
117117
this.logger.warn(`Multiple sketches were selected: ${filePaths}. Using the first one.`);
118118
}
119-
// TODO: validate sketch file name against the sketch folder. Move the file if required.
120119
const sketchFilePath = filePaths[0];
121120
const sketchFileUri = await this.fileSystemExt.getUri(sketchFilePath);
122-
return this.sketchService.getSketchFolder(sketchFileUri);
121+
const sketch = await this.sketchService.getSketchFolder(sketchFileUri);
122+
if (!sketch && sketchFileUri.endsWith('.ino')) {
123+
const name = new URI(sketchFileUri).path.name;
124+
const nameWithExt = this.labelProvider.getName(new URI(sketchFileUri));
125+
const { response } = await remote.dialog.showMessageBox({
126+
title: 'Moving',
127+
type: 'question',
128+
buttons: ['Cancel', 'OK'],
129+
message: `The file "${nameWithExt}" needs to be inside a sketch folder named as "${name}".\nCreate this folder, move the file, and continue?`
130+
});
131+
if (response === 1) { // OK
132+
const newSketchUri = new URI(sketchFileUri).parent.resolve(name);
133+
const exists = await this.fileSystem.exists(newSketchUri.toString());
134+
if (exists) {
135+
await remote.dialog.showMessageBox({
136+
type: 'error',
137+
title: 'Error',
138+
message: `A folder named "${name}" already exists. Can't open sketch.`
139+
});
140+
return undefined;
141+
}
142+
await this.fileSystem.createFolder(newSketchUri.toString());
143+
await this.fileSystem.move(sketchFileUri, newSketchUri.resolve(nameWithExt).toString());
144+
return this.sketchService.getSketchFolder(newSketchUri.toString());
145+
}
146+
}
123147
}
124148

125149
}

arduino-ide-extension/src/browser/theia/workspace/workspace-delete-handler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export class WorkspaceDeleteHandler extends TheiaWorkspaceDeleteHandler {
1818
// Deleting the main sketch file.
1919
if (uris.map(uri => uri.toString()).some(uri => uri === sketch.mainFileUri)) {
2020
const { response } = await remote.dialog.showMessageBox({
21+
title: 'Delete',
2122
type: 'question',
2223
buttons: ['Cancel', 'OK'],
23-
title: 'Delete',
2424
message: 'Do you want to delete the current sketch?'
2525
});
2626
if (response === 1) { // OK

arduino-ide-extension/src/node/sketches-service-impl.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export class SketchesServiceImpl implements SketchesService, BackendApplicationC
255255
const monthNames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
256256
const today = new Date();
257257
const parent = await new Promise<string>((resolve, reject) => {
258-
this.temp.mkdir({ prefix: '.arduinoProIDE' }, (err, dirPath) => {
258+
this.temp.mkdir({ prefix: '.arduinoProIDE-unsaved' }, (err, dirPath) => {
259259
if (err) {
260260
reject(err);
261261
return;
@@ -319,7 +319,10 @@ void loop() {
319319
const files = await fs.readdir(fsPath);
320320
for (let i = 0; i < files.length; i++) {
321321
if (files[i] === basename + '.ino') {
322-
return true;
322+
try {
323+
await this.loadSketch(fsPath);
324+
return true;
325+
} catch { }
323326
}
324327
}
325328
}
@@ -328,7 +331,7 @@ void loop() {
328331

329332
async isTemp(sketch: Sketch): Promise<boolean> {
330333
const sketchPath = FileUri.fsPath(sketch.uri);
331-
return sketchPath.indexOf('.arduinoProIDE') !== -1 && sketchPath.startsWith(os.tmpdir());
334+
return sketchPath.indexOf('.arduinoProIDE-unsaved') !== -1 && sketchPath.startsWith(os.tmpdir());
332335
}
333336

334337
async copy(sketch: Sketch, { destinationUri }: { destinationUri: string }): Promise<string> {

0 commit comments

Comments
 (0)