From d28398ed3fea5321aed8b91d1f813b63ce28a451 Mon Sep 17 00:00:00 2001 From: Nick B Date: Thu, 13 Oct 2022 21:05:14 -0400 Subject: [PATCH 01/10] Added a modifiable default sketch for new project --- .../src/node/sketches-service-impl.ts | 46 +++++++++++++++---- compile_output.txt | 17 +++++++ 2 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 compile_output.txt diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index 06c7931b6..603e36d3d 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -447,18 +447,10 @@ export class SketchesServiceImpl const sketchDir = path.join(parentPath, sketchName); const sketchFile = path.join(sketchDir, `${sketchName}.ino`); await fs.mkdir(sketchDir, { recursive: true }); + let defaultContent = await this.loadDefault(); await fs.writeFile( sketchFile, - `void setup() { - // put your setup code here, to run once: - -} - -void loop() { - // put your main code here, to run repeatedly: - -} -`, + defaultContent, { encoding: 'utf8' } ); return this.loadSketch(FileUri.create(sketchDir).toString()); @@ -637,6 +629,40 @@ void loop() { return false; } } + + // Returns the default.ino from the default folder + private async loadDefault(): Promise { + const root = await this.root(await this.sketchbookUri()); + let result : string; + result = `void setup() { + // put your setup code here, to run once: + +} + +void loop() { + // put your main code here, to run repeatedly: + +}`; + + let filename = `${root}/default/default.ino`; + let exists = false; + let raw = ""; + + try { + raw = await fs.readFile(filename, {encoding: 'utf8', }); + exists = true; + } catch { + exists = false; + } + + if (exists) { + result = raw; + this.logger.info (`-- Default found at ${filename}`) + } else { + this.logger.info (`-- Default sketch not found at ${filename}`) + } + return result; + } } interface SketchWithDetails extends Sketch { diff --git a/compile_output.txt b/compile_output.txt new file mode 100644 index 000000000..789cca1db --- /dev/null +++ b/compile_output.txt @@ -0,0 +1,17 @@ +yarn install v1.22.19 +[1/5] Validating package.json... +[2/5] Resolving packages... +success Already up-to-date. +$ lerna run prepare && yarn download:plugins +arduino-ide-extension: yarn run v1.22.19 +arduino-ide-extension: $ yarn download-cli && yarn download-fwuploader && yarn download-ls && yarn copy-i18n && yarn clean && yarn download-examples && yarn build && yarn test +arduino-ide-extension: $ node ./scripts/download-cli.js +arduino-ide-extension: Building CLI from https://github.com/arduino/arduino-cli.git. Commitish: c8ff042 +arduino-ide-extension: >>> Cloning CLI source to C:\Users\nicol\AppData\Local\Temp\d-2022913-10576-1gzrhxh.8o37... +arduino-ide-extension: <<< Cloned CLI repo. +arduino-ide-extension: >>> Checking out c8ff042... +arduino-ide-extension: <<< Checked out c8ff042. +arduino-ide-extension: >>> Building the CLI... +arduino-ide-extension: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. +arduino-ide-extension: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. +info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command. From f41c1cdbc198725d020f240708a980a3ad0511e1 Mon Sep 17 00:00:00 2001 From: Nick B Date: Thu, 13 Oct 2022 21:08:53 -0400 Subject: [PATCH 02/10] Removed unused file --- compile_output.txt | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 compile_output.txt diff --git a/compile_output.txt b/compile_output.txt deleted file mode 100644 index 789cca1db..000000000 --- a/compile_output.txt +++ /dev/null @@ -1,17 +0,0 @@ -yarn install v1.22.19 -[1/5] Validating package.json... -[2/5] Resolving packages... -success Already up-to-date. -$ lerna run prepare && yarn download:plugins -arduino-ide-extension: yarn run v1.22.19 -arduino-ide-extension: $ yarn download-cli && yarn download-fwuploader && yarn download-ls && yarn copy-i18n && yarn clean && yarn download-examples && yarn build && yarn test -arduino-ide-extension: $ node ./scripts/download-cli.js -arduino-ide-extension: Building CLI from https://github.com/arduino/arduino-cli.git. Commitish: c8ff042 -arduino-ide-extension: >>> Cloning CLI source to C:\Users\nicol\AppData\Local\Temp\d-2022913-10576-1gzrhxh.8o37... -arduino-ide-extension: <<< Cloned CLI repo. -arduino-ide-extension: >>> Checking out c8ff042... -arduino-ide-extension: <<< Checked out c8ff042. -arduino-ide-extension: >>> Building the CLI... -arduino-ide-extension: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. -arduino-ide-extension: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. -info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command. From 6444edc0c6609dff8aace4f1c6dda339febb8008 Mon Sep 17 00:00:00 2001 From: Nick B Date: Sat, 22 Oct 2022 14:54:41 -0400 Subject: [PATCH 03/10] WiP : Now nothing's working... :( --- .../src/browser/arduino-preferences.ts | 9 ++++ .../src/node/sketches-service-impl.ts | 54 +++++++++++++------ 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/arduino-ide-extension/src/browser/arduino-preferences.ts b/arduino-ide-extension/src/browser/arduino-preferences.ts index 5fef59072..9fdda7ea1 100644 --- a/arduino-ide-extension/src/browser/arduino-preferences.ts +++ b/arduino-ide-extension/src/browser/arduino-preferences.ts @@ -249,6 +249,14 @@ export const ArduinoConfigSchema: PreferenceSchema = { ), default: true, }, + 'arduino.sketch.inoBlueprint': { + type: 'string', + description: 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, + }, }, }; @@ -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; } diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index 603e36d3d..4ab0541fd 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -32,6 +32,7 @@ import { maybeNormalizeDrive, TempSketchPrefix, } from './is-temp-sketch'; +import { join } from 'path'; const RecentSketches = 'recent-sketches.json'; @@ -47,6 +48,7 @@ export class SketchesServiceImpl autoStart: true, concurrency: 1, }); + private bluePrintContent : string; @inject(ILogger) @named('sketches-service') @@ -447,10 +449,11 @@ export class SketchesServiceImpl const sketchDir = path.join(parentPath, sketchName); const sketchFile = path.join(sketchDir, `${sketchName}.ino`); await fs.mkdir(sketchDir, { recursive: true }); - let defaultContent = await this.loadDefault(); + this.bluePrintContent = this.bluePrintContent || await this.loadDefault(); + await fs.writeFile( sketchFile, - defaultContent, + this.bluePrintContent, { encoding: 'utf8' } ); return this.loadSketch(FileUri.create(sketchDir).toString()); @@ -630,9 +633,21 @@ export class SketchesServiceImpl } } - // Returns the default.ino from the default folder + // 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 { 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: @@ -644,23 +659,30 @@ void loop() { }`; - let filename = `${root}/default/default.ino`; - let exists = false; - let raw = ""; + 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']; - try { - raw = await fs.readFile(filename, {encoding: 'utf8', }); - exists = true; - } catch { - exists = false; - } + let filename = (typeof value === 'string' && !!value) ? value : `${root}/default/default.ino`; - if (exists) { + let raw = ''; + + raw = await fs.readFile(filename, {encoding: 'utf8', }); result = raw; - this.logger.info (`-- Default found at ${filename}`) - } else { - this.logger.info (`-- Default sketch not found at ${filename}`) + + this.logger.info (`-- Default found at ${filename}`) + } + } catch (error) { + if ('code' in error && error.code === 'ENOENT') { + return result; + } + throw error; } + return result; } } From 9169e20aec9e50b8f0b0a856d25130710e50e802 Mon Sep 17 00:00:00 2001 From: Nick B Date: Sat, 22 Oct 2022 15:00:43 -0400 Subject: [PATCH 04/10] yarn i18n:generate for the settings --- i18n/en.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/i18n/en.json b/i18n/en.json index 3480a2215..89fcc0125 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -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.", From 2190f81fbaddfbaa1fb3d8327582ff29b7f558e5 Mon Sep 17 00:00:00 2001 From: Nick B Date: Sat, 22 Oct 2022 15:35:04 -0400 Subject: [PATCH 05/10] Updated the desription for markdown description. --- arduino-ide-extension/src/browser/arduino-preferences.ts | 6 +++--- i18n/en.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arduino-ide-extension/src/browser/arduino-preferences.ts b/arduino-ide-extension/src/browser/arduino-preferences.ts index 9fdda7ea1..f01b76159 100644 --- a/arduino-ide-extension/src/browser/arduino-preferences.ts +++ b/arduino-ide-extension/src/browser/arduino-preferences.ts @@ -251,10 +251,10 @@ export const ArduinoConfigSchema: PreferenceSchema = { }, 'arduino.sketch.inoBlueprint': { type: 'string', - description: nls.localize( + 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.' - ), + '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, }, }, diff --git a/i18n/en.json b/i18n/en.json index 89fcc0125..165f34fb0 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -349,7 +349,7 @@ }, "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." + "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.", From 12ab5d91e2f0f037db841703474c8eb23614f0c7 Mon Sep 17 00:00:00 2001 From: Nick B Date: Sat, 22 Oct 2022 15:41:58 -0400 Subject: [PATCH 06/10] Lintered the code --- .../src/node/sketches-service-impl.ts | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index 4ab0541fd..73fe7e79f 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -48,7 +48,7 @@ export class SketchesServiceImpl autoStart: true, concurrency: 1, }); - private bluePrintContent : string; + private bluePrintContent: string; @inject(ILogger) @named('sketches-service') @@ -449,13 +449,9 @@ export class SketchesServiceImpl const sketchDir = path.join(parentPath, sketchName); const sketchFile = path.join(sketchDir, `${sketchName}.ino`); await fs.mkdir(sketchDir, { recursive: true }); - this.bluePrintContent = this.bluePrintContent || await this.loadDefault(); + this.bluePrintContent = this.bluePrintContent || (await this.loadDefault()); - await fs.writeFile( - sketchFile, - this.bluePrintContent, - { encoding: 'utf8' } - ); + await fs.writeFile(sketchFile, this.bluePrintContent, { encoding: 'utf8' }); return this.loadSketch(FileUri.create(sketchDir).toString()); } @@ -645,11 +641,11 @@ export class SketchesServiceImpl // Returns the default.ino from the settings or from default folder. private async loadDefault(): Promise { const root = await this.root(await this.sketchbookUri()); - const configDirUri = await this.envVariableServer.getConfigDirUri(); + const configDirUri = await this.envVariableServer.getConfigDirUri(); const configDirPath = FileUri.fsPath(configDirUri); - let result : string; - result = `void setup() { + let result: string; + result = `void setup() { // put your setup code here, to run once: } @@ -659,28 +655,31 @@ void loop() { }`; - 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']; + 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']; - let filename = (typeof value === 'string' && !!value) ? value : `${root}/default/default.ino`; + const filename = + typeof value === 'string' && !!value + ? value + : `${root}/default/default.ino`; let raw = ''; - raw = await fs.readFile(filename, {encoding: 'utf8', }); + 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') { + this.logger.info(`-- Default found at ${filename}`); + } + } catch (error) { + if ('code' in error && error.code === 'ENOENT') { return result; - } - throw error; + } + throw error; } return result; From 016eb2a7002d3ad57fcf39ca125e99ac909f1d34 Mon Sep 17 00:00:00 2001 From: Nick B Date: Mon, 24 Oct 2022 22:01:42 -0400 Subject: [PATCH 07/10] Remove undesirable whitespaces --- arduino-ide-extension/src/node/sketches-service-impl.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index 73fe7e79f..d2c3ebb2c 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -649,10 +649,10 @@ export class SketchesServiceImpl // put your setup code here, to run once: } - + void loop() { // put your main code here, to run repeatedly: - + }`; try { From f38ee108f858e2ab26971d254188e26c376fb0e8 Mon Sep 17 00:00:00 2001 From: Nick B Date: Mon, 24 Oct 2022 22:16:29 -0400 Subject: [PATCH 08/10] Applied kittaakos suggestions --- .../src/node/sketches-service-impl.ts | 96 ++++++++++--------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index d2c3ebb2c..05996d2c4 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -35,6 +35,12 @@ import { import { join } from 'path'; const RecentSketches = 'recent-sketches.json'; +const DefaultIno = `void setup() { + // put your setup code here, to run once: +} +void loop() { + // put your main code here, to run repeatedly: +}`; @injectable() export class SketchesServiceImpl @@ -48,7 +54,7 @@ export class SketchesServiceImpl autoStart: true, concurrency: 1, }); - private bluePrintContent: string; + private inoContent: Deferred | undefined; @inject(ILogger) @named('sketches-service') @@ -448,10 +454,11 @@ export class SketchesServiceImpl const sketchDir = path.join(parentPath, sketchName); const sketchFile = path.join(sketchDir, `${sketchName}.ino`); - await fs.mkdir(sketchDir, { recursive: true }); - this.bluePrintContent = this.bluePrintContent || (await this.loadDefault()); - - await fs.writeFile(sketchFile, this.bluePrintContent, { encoding: 'utf8' }); + const [inoContent] = await Promise.all([ + this.loadInoContent(), + fs.mkdir(sketchDir, { recursive: true }), + ]); + await fs.writeFile(sketchFile, inoContent, { encoding: 'utf8' }); return this.loadSketch(FileUri.create(sketchDir).toString()); } @@ -629,60 +636,59 @@ export class SketchesServiceImpl } } - // 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 { - const root = await this.root(await this.sketchbookUri()); + private async readSettings(): Promise | undefined> { 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: - -}`; - 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 = ''; + return this.tryParse(raw); + } catch (err) { + if ('code' in err && err.code === 'ENOENT') { + return undefined; + } + throw err; + } + } - raw = await fs.readFile(filename, { encoding: 'utf8' }); - result = raw; + private tryParse(raw: string): Record | undefined { + try { + return JSON.parse(raw); + } catch { + return undefined; + } + } - this.logger.info(`-- Default found at ${filename}`); - } - } catch (error) { - if ('code' in error && error.code === 'ENOENT') { - return result; + // Returns the default.ino from the settings or from default folder. + private async loadInoContent(): Promise { + if (!this.inoContent) { + this.inoContent = new Deferred(); + const settings = await this.readSettings(); + if (settings) { + const inoBlueprintPath = settings['arduino.sketch.inoBlueprint']; + if (inoBlueprintPath && typeof inoBlueprintPath === 'string') { + try { + const inoContent = await fs.readFile(inoBlueprintPath, { + encoding: 'utf8', + }); + this.inoContent.resolve(inoContent); + } catch (err) { + if ('code' in err && err.code === 'ENOENT') { + // Ignored. The custom `.ino` blueprint file is optional. + } else { + throw err; + } + } + } } - throw error; + this.inoContent.resolve(DefaultIno); } - return result; + return this.inoContent.promise; } } From 558a71ea89a1a275813f85328672dda567fca42e Mon Sep 17 00:00:00 2001 From: Nick B Date: Mon, 24 Oct 2022 22:18:24 -0400 Subject: [PATCH 09/10] Removed extra whitespaces --- arduino-ide-extension/src/browser/arduino-preferences.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arduino-ide-extension/src/browser/arduino-preferences.ts b/arduino-ide-extension/src/browser/arduino-preferences.ts index f01b76159..ecd45735d 100644 --- a/arduino-ide-extension/src/browser/arduino-preferences.ts +++ b/arduino-ide-extension/src/browser/arduino-preferences.ts @@ -252,11 +252,11 @@ export const ArduinoConfigSchema: PreferenceSchema = { '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.' - ), + '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, - }, + }, }, }; From 4f2010e665b1166f593145492f9bd4aedddf4da1 Mon Sep 17 00:00:00 2001 From: Nick B Date: Tue, 25 Oct 2022 15:12:50 -0400 Subject: [PATCH 10/10] Fixed default `.ino` for the missings empty lines. --- arduino-ide-extension/src/node/sketches-service-impl.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index 05996d2c4..e6793e39a 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -37,10 +37,14 @@ import { join } from 'path'; const RecentSketches = 'recent-sketches.json'; const DefaultIno = `void setup() { // put your setup code here, to run once: + } + void loop() { // put your main code here, to run repeatedly: -}`; + +} +`; @injectable() export class SketchesServiceImpl