From a04527d3b8be61a542dbaff415e0659a5845655e Mon Sep 17 00:00:00 2001 From: Francesco Spissu Date: Tue, 6 Sep 2022 19:32:27 +0200 Subject: [PATCH] different prefix for temp example sketch --- .../theia/electron-main-application.ts | 7 ++-- .../src/node/is-temp-sketch.ts | 12 +++++++ .../src/node/sketches-service-impl.ts | 35 +++++++++++-------- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts b/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts index bb85405b..12f397a0 100644 --- a/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts +++ b/arduino-ide-extension/src/electron-main/theia/electron-main-application.ts @@ -183,7 +183,10 @@ export class ElectronMainApplication extends TheiaElectronMainApplication { ); for (const workspace of workspaces) { if (await this.isValidSketchPath(workspace.file)) { - if (this.isTempSketch.is(workspace.file)) { + if ( + this.isTempSketch.is(workspace.file) && + !this.isTempSketch.isExample(workspace.file) + ) { console.info( `Skipped opening sketch. The sketch was detected as temporary. Workspace path: ${workspace.file}.` ); @@ -427,7 +430,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication { // Do not try to reopen the sketch if it was temp. // Unfortunately, IDE2 has two different logic of restoring recent sketches: the Theia default `recentworkspace.json` and there is the `recent-sketches.json`. const file = workspaceUri.fsPath; - if (this.isTempSketch.is(file)) { + if (this.isTempSketch.is(file) && !this.isTempSketch.isExample(file)) { console.info( `Ignored marking workspace as a closed sketch. The sketch was detected as temporary. Workspace URI: ${workspaceUri.toString()}.` ); diff --git a/arduino-ide-extension/src/node/is-temp-sketch.ts b/arduino-ide-extension/src/node/is-temp-sketch.ts index 5c62716e..5c4bd47f 100644 --- a/arduino-ide-extension/src/node/is-temp-sketch.ts +++ b/arduino-ide-extension/src/node/is-temp-sketch.ts @@ -3,9 +3,11 @@ import * as tempDir from 'temp-dir'; import { isWindows, isOSX } from '@theia/core/lib/common/os'; import { injectable } from '@theia/core/shared/inversify'; import { firstToLowerCase } from '../common/utils'; +import { join } from 'path'; const Win32DriveRegex = /^[a-zA-Z]:\\/; export const TempSketchPrefix = '.arduinoIDE-unsaved'; +export const ExampleTempSketchPrefix = `${TempSketchPrefix}-example`; @injectable() export class IsTempSketch { @@ -33,6 +35,16 @@ export class IsTempSketch { console.debug(`isTempSketch: ${result}. Input was ${normalizedSketchPath}`); return result; } + + isExample(sketchPath: string): boolean { + const normalizedSketchPath = maybeNormalizeDrive(sketchPath); + const result = + normalizedSketchPath.startsWith(this.tempDirRealpath) && + normalizedSketchPath.includes( + join(this.tempDirRealpath, ExampleTempSketchPrefix) + ); + return result; + } } /** diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index bc72a297..7a0b4b77 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -29,6 +29,7 @@ import * as glob from 'glob'; import { Deferred } from '@theia/core/lib/common/promise-util'; import { ServiceError } from './service-error'; import { + ExampleTempSketchPrefix, IsTempSketch, maybeNormalizeDrive, TempSketchPrefix, @@ -277,7 +278,10 @@ export class SketchesServiceImpl } catch { return; } - if ((await this.isTemp(sketch)) && sketch.name.includes('sketch_')) { + if ( + (await this.isTemp(sketch)) && + !this.isTempSketch.isExample(FileUri.fsPath(sketch.uri)) + ) { return; } @@ -336,7 +340,7 @@ export class SketchesServiceImpl async cloneExample(uri: string): Promise { const sketch = await this.loadSketch(uri); - const parentPath = await this.createTempFolder(); + const parentPath = await this.createTempFolder(false); const destinationUri = FileUri.create( path.join(parentPath, sketch.name) ).toString(); @@ -417,21 +421,24 @@ void loop() { * For example, on Windows, instead of getting an [8.3 filename](https://en.wikipedia.org/wiki/8.3_filename), callers will get a fully resolved path. * `C:\\Users\\KITTAA~1\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2022615-21100-iahybb.yyvh\\sketch_jul15a` will be `C:\\Users\\kittaakos\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2022615-21100-iahybb.yyvh\\sketch_jul15a` */ - private createTempFolder(): Promise { + private createTempFolder(isTemp = true): Promise { return new Promise((resolve, reject) => { - temp.mkdir({ prefix: TempSketchPrefix }, (createError, dirPath) => { - if (createError) { - reject(createError); - return; - } - fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => { - if (resolveError) { - reject(resolveError); + temp.mkdir( + { prefix: isTemp ? TempSketchPrefix : ExampleTempSketchPrefix }, + (createError, dirPath) => { + if (createError) { + reject(createError); return; } - resolve(resolvedDirPath); - }); - }); + fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => { + if (resolveError) { + reject(resolveError); + return; + } + resolve(resolvedDirPath); + }); + } + ); }); }