Skip to content

Added an optional user modifiable default sketch file when creating a new project. #1559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 26, 2022
9 changes: 9 additions & 0 deletions arduino-ide-extension/src/browser/arduino-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ export const ArduinoConfigSchema: PreferenceSchema = {
),
default: true,
},
'arduino.sketch.inoBlueprint': {
type: 'string',
markdownDescription: nls.localize(
'arduino/preferences/sketch/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.'
),
default: undefined,
},
},
};

Expand Down Expand Up @@ -278,6 +286,7 @@ export interface ArduinoConfiguration {
'arduino.auth.registerUri': string;
'arduino.survey.notification': boolean;
'arduino.cli.daemon.debug': boolean;
'arduino.sketch.inoBlueprint': string;
'arduino.checkForUpdates': boolean;
}

Expand Down
73 changes: 60 additions & 13 deletions arduino-ide-extension/src/node/sketches-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
maybeNormalizeDrive,
TempSketchPrefix,
} from './is-temp-sketch';
import { join } from 'path';

const RecentSketches = 'recent-sketches.json';

Expand All @@ -47,6 +48,7 @@ export class SketchesServiceImpl
autoStart: true,
concurrency: 1,
});
private bluePrintContent: string;

@inject(ILogger)
@named('sketches-service')
Expand Down Expand Up @@ -447,20 +449,9 @@ export class SketchesServiceImpl
const sketchDir = path.join(parentPath, sketchName);
const sketchFile = path.join(sketchDir, `${sketchName}.ino`);
await fs.mkdir(sketchDir, { recursive: true });
await fs.writeFile(
sketchFile,
`void setup() {
// put your setup code here, to run once:

}
this.bluePrintContent = this.bluePrintContent || (await this.loadDefault());

void loop() {
// put your main code here, to run repeatedly:

}
`,
{ encoding: 'utf8' }
);
await fs.writeFile(sketchFile, this.bluePrintContent, { encoding: 'utf8' });
return this.loadSketch(FileUri.create(sketchDir).toString());
}

Expand Down Expand Up @@ -637,6 +628,62 @@ void loop() {
return false;
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private tryParse(raw: string): any | undefined {
try {
return JSON.parse(raw);
} catch {
return undefined;
}
}

// Returns the default.ino from the settings or from default folder.
private async loadDefault(): Promise<string> {
const root = await this.root(await this.sketchbookUri());
const configDirUri = await this.envVariableServer.getConfigDirUri();
const configDirPath = FileUri.fsPath(configDirUri);

let result: string;
result = `void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are undesired whitespaces in the template.


try {
const raw = await fs.readFile(join(configDirPath, 'settings.json'), {
encoding: 'utf8',
});
const json = this.tryParse(raw);
if (json) {
const value = json['arduino.sketch.inoBlueprint'];

const filename =
typeof value === 'string' && !!value
? value
: `${root}/default/default.ino`;

let raw = '';

raw = await fs.readFile(filename, { encoding: 'utf8' });
result = raw;

this.logger.info(`-- Default found at ${filename}`);
}
} catch (error) {
if ('code' in error && error.code === 'ENOENT') {
return result;
}
throw error;
}

return result;
}
}

interface SketchWithDetails extends Sketch {
Expand Down
3 changes: 3 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@
"username": "Username"
},
"showVerbose": "Show verbose output during",
"sketch": {
"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."
},
"sketchbook.location": "Sketchbook location",
"sketchbook.showAllFiles": "True to show all sketch files inside the sketch. It is false by default.",
"survey.notification": "True if users should be notified if a survey is available. True by default.",
Expand Down