From 30b4b219329da5f2d838e474a3bcdcbaa2c26c2d Mon Sep 17 00:00:00 2001 From: Francesco Stasi Date: Wed, 7 Jul 2021 16:33:44 +0200 Subject: [PATCH 1/3] include arduino_secrets when needed --- .../src/browser/create/create-api.ts | 83 ++++++++++++++----- 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/arduino-ide-extension/src/browser/create/create-api.ts b/arduino-ide-extension/src/browser/create/create-api.ts index 3e02707ec..4abd2b8bc 100644 --- a/arduino-ide-extension/src/browser/create/create-api.ts +++ b/arduino-ide-extension/src/browser/create/create-api.ts @@ -32,15 +32,18 @@ export class CreateApi { return this; } - async findSketchByPath( - path: string, - trustCache = true - ): Promise { - const skatches = sketchCache; - const sketch = skatches.find((sketch) => { + public sketchCompareByPath = (param: string) => { + return (sketch: Create.Sketch) => { const [, spath] = splitSketchPath(sketch.path); - return path === spath; - }); + return param === spath; + } + } + + async findSketchInCache( + compareFn: (sketch: Create.Sketch) => boolean, + trustCache = true, + ): Promise { + const sketch = sketchCache.find((sketch) => compareFn(sketch)); if (trustCache) { return Promise.resolve(sketch); } @@ -148,7 +151,7 @@ export class CreateApi { } const [, spath] = createPaths.splitSketchPath(res.path); - const sketch = await this.findSketchByPath(spath); + const sketch = await this.findSketchInCache(this.sketchCompareByPath(spath)); if ( sketch && sketch.secrets && @@ -159,7 +162,7 @@ export class CreateApi { }); if (posixPath !== posix.sep) { - const sketch = await this.findSketchByPath(posixPath); + const sketch = await this.findSketchInCache(this.sketchCompareByPath(posixPath)); if ( sketch && sketch.secrets && @@ -214,7 +217,7 @@ export class CreateApi { let resources; if (basename === Create.arduino_secrets_file) { - const sketch = await this.findSketchByPath(parentPosixPath); + const sketch = await this.findSketchInCache(this.sketchCompareByPath(parentPosixPath)); resources = sketch ? [this.getSketchSecretStat(sketch)] : []; } else { resources = await this.readDirectory(parentPosixPath, { @@ -230,12 +233,46 @@ export class CreateApi { return resource; } + private async toggleSecretsInclude(path: string, data: string, mode: 'add' | 'remove') { + + const includeString = `#include "${Create.arduino_secrets_file}"`; + const includeRegexp = new RegExp(includeString + '\s*', "g"); + + const basename = createPaths.basename(path); + if ( + mode === 'add' + ) { + const doesIncludeSecrets = includeRegexp.test(data); + + if (doesIncludeSecrets) { + return data; + } + + const sketch = await this.findSketchInCache((sketch) => { + const [, spath] = splitSketchPath(sketch.path); + return spath === createPaths.parentPosix(path) + }, true); + + + if (sketch && + (sketch.name + '.ino' === basename || sketch.name + '.pde' === basename) && + sketch.secrets && + sketch.secrets.length > 0) { + return includeString + "\n" + data; + } + } + else if (mode === 'remove') { + return data.replace(includeRegexp, ''); + } + return data; + } + async readFile(posixPath: string): Promise { const basename = createPaths.basename(posixPath); if (basename === Create.arduino_secrets_file) { const parentPosixPath = createPaths.parentPosix(posixPath); - const sketch = await this.findSketchByPath(parentPosixPath, false); + const sketch = await this.findSketchInCache(this.sketchCompareByPath(parentPosixPath), false); let file = ''; if (sketch && sketch.secrets) { @@ -250,12 +287,15 @@ export class CreateApi { `${this.domain()}/files/f/$HOME/sketches_v2${posixPath}` ); const headers = await this.headers(); - const result = await this.run<{ data: string }>(url, { + const result = await this.run<{ data: string; path: string }>(url, { method: 'GET', headers, }); - const { data } = result; - return atob(data); + let { data } = result; + + // add includes to main arduino file + data = await this.toggleSecretsInclude(posixPath, atob(data), 'add'); + return data; } async writeFile( @@ -266,7 +306,7 @@ export class CreateApi { if (basename === Create.arduino_secrets_file) { const parentPosixPath = createPaths.parentPosix(posixPath); - const sketch = await this.findSketchByPath(parentPosixPath); + const sketch = await this.findSketchInCache(this.sketchCompareByPath(parentPosixPath)); if (sketch) { const url = new URL(`${this.domain()}/sketches/${sketch.id}`); const headers = await this.headers(); @@ -331,12 +371,11 @@ export class CreateApi { `${this.domain()}/files/f/$HOME/sketches_v2${posixPath}` ); const headers = await this.headers(); - const data = btoa( - typeof content === 'string' - ? content - : new TextDecoder().decode(content) - ); - const payload = { data }; + + let data: string = typeof content === 'string' ? content : new TextDecoder().decode(content); + data = await this.toggleSecretsInclude(posixPath, data, 'remove'); + + const payload = { data: btoa(data) }; const init = { method: 'POST', body: JSON.stringify(payload), From ba974aadd1fa269342042bb2cd2bf8eaf1c19da4 Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Wed, 7 Jul 2021 17:44:10 +0200 Subject: [PATCH 2/3] fix secret push --- .../src/browser/create/create-api.ts | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/arduino-ide-extension/src/browser/create/create-api.ts b/arduino-ide-extension/src/browser/create/create-api.ts index 4abd2b8bc..85950b6a5 100644 --- a/arduino-ide-extension/src/browser/create/create-api.ts +++ b/arduino-ide-extension/src/browser/create/create-api.ts @@ -36,12 +36,12 @@ export class CreateApi { return (sketch: Create.Sketch) => { const [, spath] = splitSketchPath(sketch.path); return param === spath; - } - } + }; + }; async findSketchInCache( compareFn: (sketch: Create.Sketch) => boolean, - trustCache = true, + trustCache = true ): Promise { const sketch = sketchCache.find((sketch) => compareFn(sketch)); if (trustCache) { @@ -151,7 +151,9 @@ export class CreateApi { } const [, spath] = createPaths.splitSketchPath(res.path); - const sketch = await this.findSketchInCache(this.sketchCompareByPath(spath)); + const sketch = await this.findSketchInCache( + this.sketchCompareByPath(spath) + ); if ( sketch && sketch.secrets && @@ -162,7 +164,9 @@ export class CreateApi { }); if (posixPath !== posix.sep) { - const sketch = await this.findSketchInCache(this.sketchCompareByPath(posixPath)); + const sketch = await this.findSketchInCache( + this.sketchCompareByPath(posixPath) + ); if ( sketch && sketch.secrets && @@ -217,7 +221,9 @@ export class CreateApi { let resources; if (basename === Create.arduino_secrets_file) { - const sketch = await this.findSketchInCache(this.sketchCompareByPath(parentPosixPath)); + const sketch = await this.findSketchInCache( + this.sketchCompareByPath(parentPosixPath) + ); resources = sketch ? [this.getSketchSecretStat(sketch)] : []; } else { resources = await this.readDirectory(parentPosixPath, { @@ -233,15 +239,16 @@ export class CreateApi { return resource; } - private async toggleSecretsInclude(path: string, data: string, mode: 'add' | 'remove') { - + private async toggleSecretsInclude( + path: string, + data: string, + mode: 'add' | 'remove' + ) { const includeString = `#include "${Create.arduino_secrets_file}"`; - const includeRegexp = new RegExp(includeString + '\s*', "g"); + const includeRegexp = new RegExp(includeString + 's*', 'g'); const basename = createPaths.basename(path); - if ( - mode === 'add' - ) { + if (mode === 'add') { const doesIncludeSecrets = includeRegexp.test(data); if (doesIncludeSecrets) { @@ -250,18 +257,19 @@ export class CreateApi { const sketch = await this.findSketchInCache((sketch) => { const [, spath] = splitSketchPath(sketch.path); - return spath === createPaths.parentPosix(path) + return spath === createPaths.parentPosix(path); }, true); - - if (sketch && - (sketch.name + '.ino' === basename || sketch.name + '.pde' === basename) && + if ( + sketch && + (sketch.name + '.ino' === basename || + sketch.name + '.pde' === basename) && sketch.secrets && - sketch.secrets.length > 0) { - return includeString + "\n" + data; + sketch.secrets.length > 0 + ) { + return includeString + '\n' + data; } - } - else if (mode === 'remove') { + } else if (mode === 'remove') { return data.replace(includeRegexp, ''); } return data; @@ -272,7 +280,10 @@ export class CreateApi { if (basename === Create.arduino_secrets_file) { const parentPosixPath = createPaths.parentPosix(posixPath); - const sketch = await this.findSketchInCache(this.sketchCompareByPath(parentPosixPath), false); + const sketch = await this.findSketchInCache( + this.sketchCompareByPath(parentPosixPath), + false + ); let file = ''; if (sketch && sketch.secrets) { @@ -306,7 +317,9 @@ export class CreateApi { if (basename === Create.arduino_secrets_file) { const parentPosixPath = createPaths.parentPosix(posixPath); - const sketch = await this.findSketchInCache(this.sketchCompareByPath(parentPosixPath)); + const sketch = await this.findSketchInCache( + this.sketchCompareByPath(parentPosixPath) + ); if (sketch) { const url = new URL(`${this.domain()}/sketches/${sketch.id}`); const headers = await this.headers(); @@ -340,7 +353,7 @@ export class CreateApi { ); } - if (name.length === 0 || value.length === 0) { + if (name.length === 0) { return prev; } @@ -372,7 +385,10 @@ export class CreateApi { ); const headers = await this.headers(); - let data: string = typeof content === 'string' ? content : new TextDecoder().decode(content); + let data: string = + typeof content === 'string' + ? content + : new TextDecoder().decode(content); data = await this.toggleSecretsInclude(posixPath, data, 'remove'); const payload = { data: btoa(data) }; From 7732dfc23f1c6186cabb8c8ca44c7d5d790b78c5 Mon Sep 17 00:00:00 2001 From: Francesco Stasi Date: Thu, 8 Jul 2021 10:28:43 +0200 Subject: [PATCH 3/3] fix CR while adding include --- arduino-ide-extension/src/browser/create/create-api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino-ide-extension/src/browser/create/create-api.ts b/arduino-ide-extension/src/browser/create/create-api.ts index 85950b6a5..a464b2416 100644 --- a/arduino-ide-extension/src/browser/create/create-api.ts +++ b/arduino-ide-extension/src/browser/create/create-api.ts @@ -245,7 +245,7 @@ export class CreateApi { mode: 'add' | 'remove' ) { const includeString = `#include "${Create.arduino_secrets_file}"`; - const includeRegexp = new RegExp(includeString + 's*', 'g'); + const includeRegexp = new RegExp(includeString + '\\s*', 'g'); const basename = createPaths.basename(path); if (mode === 'add') {