Skip to content

Commit a04527d

Browse files
different prefix for temp example sketch
1 parent 33ec671 commit a04527d

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
183183
);
184184
for (const workspace of workspaces) {
185185
if (await this.isValidSketchPath(workspace.file)) {
186-
if (this.isTempSketch.is(workspace.file)) {
186+
if (
187+
this.isTempSketch.is(workspace.file) &&
188+
!this.isTempSketch.isExample(workspace.file)
189+
) {
187190
console.info(
188191
`Skipped opening sketch. The sketch was detected as temporary. Workspace path: ${workspace.file}.`
189192
);
@@ -427,7 +430,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
427430
// Do not try to reopen the sketch if it was temp.
428431
// Unfortunately, IDE2 has two different logic of restoring recent sketches: the Theia default `recentworkspace.json` and there is the `recent-sketches.json`.
429432
const file = workspaceUri.fsPath;
430-
if (this.isTempSketch.is(file)) {
433+
if (this.isTempSketch.is(file) && !this.isTempSketch.isExample(file)) {
431434
console.info(
432435
`Ignored marking workspace as a closed sketch. The sketch was detected as temporary. Workspace URI: ${workspaceUri.toString()}.`
433436
);

arduino-ide-extension/src/node/is-temp-sketch.ts

+12
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import * as tempDir from 'temp-dir';
33
import { isWindows, isOSX } from '@theia/core/lib/common/os';
44
import { injectable } from '@theia/core/shared/inversify';
55
import { firstToLowerCase } from '../common/utils';
6+
import { join } from 'path';
67

78
const Win32DriveRegex = /^[a-zA-Z]:\\/;
89
export const TempSketchPrefix = '.arduinoIDE-unsaved';
10+
export const ExampleTempSketchPrefix = `${TempSketchPrefix}-example`;
911

1012
@injectable()
1113
export class IsTempSketch {
@@ -33,6 +35,16 @@ export class IsTempSketch {
3335
console.debug(`isTempSketch: ${result}. Input was ${normalizedSketchPath}`);
3436
return result;
3537
}
38+
39+
isExample(sketchPath: string): boolean {
40+
const normalizedSketchPath = maybeNormalizeDrive(sketchPath);
41+
const result =
42+
normalizedSketchPath.startsWith(this.tempDirRealpath) &&
43+
normalizedSketchPath.includes(
44+
join(this.tempDirRealpath, ExampleTempSketchPrefix)
45+
);
46+
return result;
47+
}
3648
}
3749

3850
/**

arduino-ide-extension/src/node/sketches-service-impl.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as glob from 'glob';
2929
import { Deferred } from '@theia/core/lib/common/promise-util';
3030
import { ServiceError } from './service-error';
3131
import {
32+
ExampleTempSketchPrefix,
3233
IsTempSketch,
3334
maybeNormalizeDrive,
3435
TempSketchPrefix,
@@ -277,7 +278,10 @@ export class SketchesServiceImpl
277278
} catch {
278279
return;
279280
}
280-
if ((await this.isTemp(sketch)) && sketch.name.includes('sketch_')) {
281+
if (
282+
(await this.isTemp(sketch)) &&
283+
!this.isTempSketch.isExample(FileUri.fsPath(sketch.uri))
284+
) {
281285
return;
282286
}
283287

@@ -336,7 +340,7 @@ export class SketchesServiceImpl
336340

337341
async cloneExample(uri: string): Promise<Sketch> {
338342
const sketch = await this.loadSketch(uri);
339-
const parentPath = await this.createTempFolder();
343+
const parentPath = await this.createTempFolder(false);
340344
const destinationUri = FileUri.create(
341345
path.join(parentPath, sketch.name)
342346
).toString();
@@ -417,21 +421,24 @@ void loop() {
417421
* 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.
418422
* `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`
419423
*/
420-
private createTempFolder(): Promise<string> {
424+
private createTempFolder(isTemp = true): Promise<string> {
421425
return new Promise<string>((resolve, reject) => {
422-
temp.mkdir({ prefix: TempSketchPrefix }, (createError, dirPath) => {
423-
if (createError) {
424-
reject(createError);
425-
return;
426-
}
427-
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
428-
if (resolveError) {
429-
reject(resolveError);
426+
temp.mkdir(
427+
{ prefix: isTemp ? TempSketchPrefix : ExampleTempSketchPrefix },
428+
(createError, dirPath) => {
429+
if (createError) {
430+
reject(createError);
430431
return;
431432
}
432-
resolve(resolvedDirPath);
433-
});
434-
});
433+
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
434+
if (resolveError) {
435+
reject(resolveError);
436+
return;
437+
}
438+
resolve(resolvedDirPath);
439+
});
440+
}
441+
);
435442
});
436443
}
437444

0 commit comments

Comments
 (0)