Skip to content

Commit 0773c39

Browse files
authored
Added an optional user modifiable default sketch file when creating a new project. (#1559)
* Added a modifiable default sketch for new project * Removed unused file * WiP : Now nothing's working... :( * yarn i18n:generate for the settings * Updated the desription for markdown description. * Lintered the code * Remove undesirable whitespaces * Applied kittaakos suggestions * Removed extra whitespaces * Fixed default `.ino` for the missings empty lines.
1 parent 2f5afe0 commit 0773c39

File tree

3 files changed

+84
-15
lines changed

3 files changed

+84
-15
lines changed

Diff for: arduino-ide-extension/src/browser/arduino-preferences.ts

+9
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+
markdownDescription: 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

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

+72-15
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ import {
3232
maybeNormalizeDrive,
3333
TempSketchPrefix,
3434
} from './is-temp-sketch';
35+
import { join } from 'path';
3536

3637
const RecentSketches = 'recent-sketches.json';
38+
const DefaultIno = `void setup() {
39+
// put your setup code here, to run once:
40+
41+
}
42+
43+
void loop() {
44+
// put your main code here, to run repeatedly:
45+
46+
}
47+
`;
3748

3849
@injectable()
3950
export class SketchesServiceImpl
@@ -47,6 +58,7 @@ export class SketchesServiceImpl
4758
autoStart: true,
4859
concurrency: 1,
4960
});
61+
private inoContent: Deferred<string> | undefined;
5062

5163
@inject(ILogger)
5264
@named('sketches-service')
@@ -446,21 +458,11 @@ export class SketchesServiceImpl
446458

447459
const sketchDir = path.join(parentPath, sketchName);
448460
const sketchFile = path.join(sketchDir, `${sketchName}.ino`);
449-
await fs.mkdir(sketchDir, { recursive: true });
450-
await fs.writeFile(
451-
sketchFile,
452-
`void setup() {
453-
// put your setup code here, to run once:
454-
455-
}
456-
457-
void loop() {
458-
// put your main code here, to run repeatedly:
459-
460-
}
461-
`,
462-
{ encoding: 'utf8' }
463-
);
461+
const [inoContent] = await Promise.all([
462+
this.loadInoContent(),
463+
fs.mkdir(sketchDir, { recursive: true }),
464+
]);
465+
await fs.writeFile(sketchFile, inoContent, { encoding: 'utf8' });
464466
return this.loadSketch(FileUri.create(sketchDir).toString());
465467
}
466468

@@ -637,6 +639,61 @@ void loop() {
637639
return false;
638640
}
639641
}
642+
643+
// Returns the default.ino from the settings or from default folder.
644+
private async readSettings(): Promise<Record<string, unknown> | undefined> {
645+
const configDirUri = await this.envVariableServer.getConfigDirUri();
646+
const configDirPath = FileUri.fsPath(configDirUri);
647+
648+
try {
649+
const raw = await fs.readFile(join(configDirPath, 'settings.json'), {
650+
encoding: 'utf8',
651+
});
652+
653+
return this.tryParse(raw);
654+
} catch (err) {
655+
if ('code' in err && err.code === 'ENOENT') {
656+
return undefined;
657+
}
658+
throw err;
659+
}
660+
}
661+
662+
private tryParse(raw: string): Record<string, unknown> | undefined {
663+
try {
664+
return JSON.parse(raw);
665+
} catch {
666+
return undefined;
667+
}
668+
}
669+
670+
// Returns the default.ino from the settings or from default folder.
671+
private async loadInoContent(): Promise<string> {
672+
if (!this.inoContent) {
673+
this.inoContent = new Deferred<string>();
674+
const settings = await this.readSettings();
675+
if (settings) {
676+
const inoBlueprintPath = settings['arduino.sketch.inoBlueprint'];
677+
if (inoBlueprintPath && typeof inoBlueprintPath === 'string') {
678+
try {
679+
const inoContent = await fs.readFile(inoBlueprintPath, {
680+
encoding: 'utf8',
681+
});
682+
this.inoContent.resolve(inoContent);
683+
} catch (err) {
684+
if ('code' in err && err.code === 'ENOENT') {
685+
// Ignored. The custom `.ino` blueprint file is optional.
686+
} else {
687+
throw err;
688+
}
689+
}
690+
}
691+
}
692+
this.inoContent.resolve(DefaultIno);
693+
}
694+
695+
return this.inoContent.promise;
696+
}
640697
}
641698

642699
interface SketchWithDetails extends Sketch {

Diff for: i18n/en.json

+3
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@
349349
"username": "Username"
350350
},
351351
"showVerbose": "Show verbose output during",
352+
"sketch": {
353+
"inoBlueprint": "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."
354+
},
352355
"sketchbook.location": "Sketchbook location",
353356
"sketchbook.showAllFiles": "True to show all sketch files inside the sketch. It is false by default.",
354357
"survey.notification": "True if users should be notified if a survey is available. True by default.",

0 commit comments

Comments
 (0)