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) {
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()}.`
);

View File

@ -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;
}
}
/**

View File

@ -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<Sketch> {
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<string> {
private createTempFolder(isTemp = true): Promise<string> {
return new Promise<string>((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);
});
}
);
});
}