different prefix for temp example sketch

This commit is contained in:
Francesco Spissu 2022-09-06 19:32:27 +02:00 committed by Francesco Spissu
parent 33ec67109b
commit a04527d3b8
3 changed files with 38 additions and 16 deletions

View File

@ -183,7 +183,10 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
); );
for (const workspace of workspaces) { for (const workspace of workspaces) {
if (await this.isValidSketchPath(workspace.file)) { 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( console.info(
`Skipped opening sketch. The sketch was detected as temporary. Workspace path: ${workspace.file}.` `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. // 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`. // 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; const file = workspaceUri.fsPath;
if (this.isTempSketch.is(file)) { if (this.isTempSketch.is(file) && !this.isTempSketch.isExample(file)) {
console.info( console.info(
`Ignored marking workspace as a closed sketch. The sketch was detected as temporary. Workspace URI: ${workspaceUri.toString()}.` `Ignored marking workspace as a closed sketch. The sketch was detected as temporary. Workspace URI: ${workspaceUri.toString()}.`
); );

View File

@ -3,9 +3,11 @@ import * as tempDir from 'temp-dir';
import { isWindows, isOSX } from '@theia/core/lib/common/os'; import { isWindows, isOSX } from '@theia/core/lib/common/os';
import { injectable } from '@theia/core/shared/inversify'; import { injectable } from '@theia/core/shared/inversify';
import { firstToLowerCase } from '../common/utils'; import { firstToLowerCase } from '../common/utils';
import { join } from 'path';
const Win32DriveRegex = /^[a-zA-Z]:\\/; const Win32DriveRegex = /^[a-zA-Z]:\\/;
export const TempSketchPrefix = '.arduinoIDE-unsaved'; export const TempSketchPrefix = '.arduinoIDE-unsaved';
export const ExampleTempSketchPrefix = `${TempSketchPrefix}-example`;
@injectable() @injectable()
export class IsTempSketch { export class IsTempSketch {
@ -33,6 +35,16 @@ export class IsTempSketch {
console.debug(`isTempSketch: ${result}. Input was ${normalizedSketchPath}`); console.debug(`isTempSketch: ${result}. Input was ${normalizedSketchPath}`);
return result; return result;
} }
isExample(sketchPath: string): boolean {
const normalizedSketchPath = maybeNormalizeDrive(sketchPath);
const result =
normalizedSketchPath.startsWith(this.tempDirRealpath) &&
normalizedSketchPath.includes(
join(this.tempDirRealpath, ExampleTempSketchPrefix)
);
return result;
}
} }
/** /**

View File

@ -29,6 +29,7 @@ import * as glob from 'glob';
import { Deferred } from '@theia/core/lib/common/promise-util'; import { Deferred } from '@theia/core/lib/common/promise-util';
import { ServiceError } from './service-error'; import { ServiceError } from './service-error';
import { import {
ExampleTempSketchPrefix,
IsTempSketch, IsTempSketch,
maybeNormalizeDrive, maybeNormalizeDrive,
TempSketchPrefix, TempSketchPrefix,
@ -277,7 +278,10 @@ export class SketchesServiceImpl
} catch { } catch {
return; return;
} }
if ((await this.isTemp(sketch)) && sketch.name.includes('sketch_')) { if (
(await this.isTemp(sketch)) &&
!this.isTempSketch.isExample(FileUri.fsPath(sketch.uri))
) {
return; return;
} }
@ -336,7 +340,7 @@ export class SketchesServiceImpl
async cloneExample(uri: string): Promise<Sketch> { async cloneExample(uri: string): Promise<Sketch> {
const sketch = await this.loadSketch(uri); const sketch = await this.loadSketch(uri);
const parentPath = await this.createTempFolder(); const parentPath = await this.createTempFolder(false);
const destinationUri = FileUri.create( const destinationUri = FileUri.create(
path.join(parentPath, sketch.name) path.join(parentPath, sketch.name)
).toString(); ).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. * 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` * `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<string> { private createTempFolder(isTemp = true): Promise<string> {
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
temp.mkdir({ prefix: TempSketchPrefix }, (createError, dirPath) => { temp.mkdir(
if (createError) { { prefix: isTemp ? TempSketchPrefix : ExampleTempSketchPrefix },
reject(createError); (createError, dirPath) => {
return; if (createError) {
} reject(createError);
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
if (resolveError) {
reject(resolveError);
return; return;
} }
resolve(resolvedDirPath); fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
}); if (resolveError) {
}); reject(resolveError);
return;
}
resolve(resolvedDirPath);
});
}
);
}); });
} }