refined open logic when sketch name is invalid

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2020-08-03 17:19:52 +02:00
parent aadd403cdb
commit 650230a571
3 changed files with 33 additions and 6 deletions

View File

@ -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());
}
}
}
}

View File

@ -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

View File

@ -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<string>((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<boolean> {
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<string> {