diff --git a/arduino-ide-extension/src/browser/contributions/open-sketch.ts b/arduino-ide-extension/src/browser/contributions/open-sketch.ts index 74323009..ca4452ac 100644 --- a/arduino-ide-extension/src/browser/contributions/open-sketch.ts +++ b/arduino-ide-extension/src/browser/contributions/open-sketch.ts @@ -116,10 +116,34 @@ export class OpenSketch extends SketchContribution { if (filePaths.length > 1) { this.logger.warn(`Multiple sketches were selected: ${filePaths}. Using the first one.`); } - // TODO: validate sketch file name against the sketch folder. Move the file if required. const sketchFilePath = filePaths[0]; const sketchFileUri = await this.fileSystemExt.getUri(sketchFilePath); - return this.sketchService.getSketchFolder(sketchFileUri); + const sketch = await this.sketchService.getSketchFolder(sketchFileUri); + if (!sketch && sketchFileUri.endsWith('.ino')) { + const name = new URI(sketchFileUri).path.name; + const nameWithExt = this.labelProvider.getName(new URI(sketchFileUri)); + const { response } = await remote.dialog.showMessageBox({ + title: 'Moving', + type: 'question', + buttons: ['Cancel', 'OK'], + message: `The file "${nameWithExt}" needs to be inside a sketch folder named as "${name}".\nCreate this folder, move the file, and continue?` + }); + if (response === 1) { // OK + const newSketchUri = new URI(sketchFileUri).parent.resolve(name); + const exists = await this.fileSystem.exists(newSketchUri.toString()); + if (exists) { + await remote.dialog.showMessageBox({ + type: 'error', + title: 'Error', + message: `A folder named "${name}" already exists. Can't open sketch.` + }); + return undefined; + } + await this.fileSystem.createFolder(newSketchUri.toString()); + await this.fileSystem.move(sketchFileUri, newSketchUri.resolve(nameWithExt).toString()); + return this.sketchService.getSketchFolder(newSketchUri.toString()); + } + } } } diff --git a/arduino-ide-extension/src/browser/theia/workspace/workspace-delete-handler.ts b/arduino-ide-extension/src/browser/theia/workspace/workspace-delete-handler.ts index 8f0577f6..8c828795 100644 --- a/arduino-ide-extension/src/browser/theia/workspace/workspace-delete-handler.ts +++ b/arduino-ide-extension/src/browser/theia/workspace/workspace-delete-handler.ts @@ -18,9 +18,9 @@ export class WorkspaceDeleteHandler extends TheiaWorkspaceDeleteHandler { // Deleting the main sketch file. if (uris.map(uri => uri.toString()).some(uri => uri === sketch.mainFileUri)) { const { response } = await remote.dialog.showMessageBox({ + title: 'Delete', type: 'question', buttons: ['Cancel', 'OK'], - title: 'Delete', message: 'Do you want to delete the current sketch?' }); if (response === 1) { // OK diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index 13402873..b694c20e 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -255,7 +255,7 @@ export class SketchesServiceImpl implements SketchesService, BackendApplicationC const monthNames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; const today = new Date(); const parent = await new Promise((resolve, reject) => { - this.temp.mkdir({ prefix: '.arduinoProIDE' }, (err, dirPath) => { + this.temp.mkdir({ prefix: '.arduinoProIDE-unsaved' }, (err, dirPath) => { if (err) { reject(err); return; @@ -319,7 +319,10 @@ void loop() { const files = await fs.readdir(fsPath); for (let i = 0; i < files.length; i++) { if (files[i] === basename + '.ino') { - return true; + try { + await this.loadSketch(fsPath); + return true; + } catch { } } } } @@ -328,7 +331,7 @@ void loop() { async isTemp(sketch: Sketch): Promise { const sketchPath = FileUri.fsPath(sketch.uri); - return sketchPath.indexOf('.arduinoProIDE') !== -1 && sketchPath.startsWith(os.tmpdir()); + return sketchPath.indexOf('.arduinoProIDE-unsaved') !== -1 && sketchPath.startsWith(os.tmpdir()); } async copy(sketch: Sketch, { destinationUri }: { destinationUri: string }): Promise {