Skip to content

Commit 6444edc

Browse files
committed
WiP : Now nothing's working... :(
1 parent f41c1cd commit 6444edc

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

arduino-ide-extension/src/browser/arduino-preferences.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ export const ArduinoConfigSchema: PreferenceSchema = {
249249
),
250250
default: true,
251251
},
252+
'arduino.sketch.inoBlueprint': {
253+
type: 'string',
254+
description: nls.localize(
255+
'arduino/preferences/sketch/inoBlueprint',
256+
'Absolute filesystem path to the default `.ino` blueprint file. If specified, the content of the blueprint file will be used for every new sketch created by the IDE. The sketches will be generated with the default Arduino content if not specified. Unaccessible blueprint files are ignored. A restart of the IDE is needed for this setting to take effect.'
257+
),
258+
default: undefined,
259+
},
252260
},
253261
};
254262

@@ -278,6 +286,7 @@ export interface ArduinoConfiguration {
278286
'arduino.auth.registerUri': string;
279287
'arduino.survey.notification': boolean;
280288
'arduino.cli.daemon.debug': boolean;
289+
'arduino.sketch.inoBlueprint': string;
281290
'arduino.checkForUpdates': boolean;
282291
}
283292

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
maybeNormalizeDrive,
3333
TempSketchPrefix,
3434
} from './is-temp-sketch';
35+
import { join } from 'path';
3536

3637
const RecentSketches = 'recent-sketches.json';
3738

@@ -47,6 +48,7 @@ export class SketchesServiceImpl
4748
autoStart: true,
4849
concurrency: 1,
4950
});
51+
private bluePrintContent : string;
5052

5153
@inject(ILogger)
5254
@named('sketches-service')
@@ -447,10 +449,11 @@ export class SketchesServiceImpl
447449
const sketchDir = path.join(parentPath, sketchName);
448450
const sketchFile = path.join(sketchDir, `${sketchName}.ino`);
449451
await fs.mkdir(sketchDir, { recursive: true });
450-
let defaultContent = await this.loadDefault();
452+
this.bluePrintContent = this.bluePrintContent || await this.loadDefault();
453+
451454
await fs.writeFile(
452455
sketchFile,
453-
defaultContent,
456+
this.bluePrintContent,
454457
{ encoding: 'utf8' }
455458
);
456459
return this.loadSketch(FileUri.create(sketchDir).toString());
@@ -630,9 +633,21 @@ export class SketchesServiceImpl
630633
}
631634
}
632635

633-
// Returns the default.ino from the default folder
636+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
637+
private tryParse(raw: string): any | undefined {
638+
try {
639+
return JSON.parse(raw);
640+
} catch {
641+
return undefined;
642+
}
643+
}
644+
645+
// Returns the default.ino from the settings or from default folder.
634646
private async loadDefault(): Promise<string> {
635647
const root = await this.root(await this.sketchbookUri());
648+
const configDirUri = await this.envVariableServer.getConfigDirUri();
649+
const configDirPath = FileUri.fsPath(configDirUri);
650+
636651
let result : string;
637652
result = `void setup() {
638653
// put your setup code here, to run once:
@@ -644,23 +659,30 @@ void loop() {
644659
645660
}`;
646661

647-
let filename = `${root}/default/default.ino`;
648-
let exists = false;
649-
let raw = "";
662+
try {
663+
const raw = await fs.readFile(join(configDirPath, 'settings.json'), {
664+
encoding: 'utf8',
665+
});
666+
const json = this.tryParse(raw);
667+
if (json) {
668+
const value = json['arduino.sketch.inoBlueprint'];
650669

651-
try {
652-
raw = await fs.readFile(filename, {encoding: 'utf8', });
653-
exists = true;
654-
} catch {
655-
exists = false;
656-
}
670+
let filename = (typeof value === 'string' && !!value) ? value : `${root}/default/default.ino`;
657671

658-
if (exists) {
672+
let raw = '';
673+
674+
raw = await fs.readFile(filename, {encoding: 'utf8', });
659675
result = raw;
660-
this.logger.info (`-- Default found at ${filename}`)
661-
} else {
662-
this.logger.info (`-- Default sketch not found at ${filename}`)
676+
677+
this.logger.info (`-- Default found at ${filename}`)
678+
}
679+
} catch (error) {
680+
if ('code' in error && error.code === 'ENOENT') {
681+
return result;
682+
}
683+
throw error;
663684
}
685+
664686
return result;
665687
}
666688
}

0 commit comments

Comments
 (0)