From 0b9569a1e888e3538930348fe53213ee8457dd2d Mon Sep 17 00:00:00 2001 From: huajiezhang Date: Fri, 2 Aug 2024 13:20:07 +0800 Subject: [PATCH] feat: set env command can parse space value in script driver --- .../src/component/driver/script/scriptDriver.ts | 16 ++++++++++------ .../component/driver/script/scriptDriver.test.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/fx-core/src/component/driver/script/scriptDriver.ts b/packages/fx-core/src/component/driver/script/scriptDriver.ts index fac3e2a001..6faa252c46 100644 --- a/packages/fx-core/src/component/driver/script/scriptDriver.ts +++ b/packages/fx-core/src/component/driver/script/scriptDriver.ts @@ -212,15 +212,13 @@ export function convertScriptErrorToFxError( } export function parseSetOutputCommand(stdout: string): DotenvOutput { - const regex = /(::set-teamsfx-env|::set-output)\s+([^"'\s]+)=([^"'\s]+)/g; + const regex = /::(set-teamsfx-env|set-output)\s+(\w+)=((["'])(.*?)\4|[^"'\s]+)/g; const output: DotenvOutput = {}; let match; while ((match = regex.exec(stdout))) { - if (match && match.length === 4) { - const key = match[2].trim(); - const value = match[3].trim(); - output[key] = value; - } + const key = match[2]; + const value = match[5] !== undefined ? match[5] : match[3]; + output[key] = value; } return output; } @@ -228,3 +226,9 @@ export function parseSetOutputCommand(stdout: string): DotenvOutput { export function capitalizeFirstLetter(raw: string): string { return raw.charAt(0).toUpperCase() + raw.slice(1); } + +console.log( + parseSetOutputCommand(`Write-Host ::set-teamsfx-env Test0="multi word variable" +Write-Host ::set-teamsfx-env Test1=' multi word variable' +Write-Host ::set-teamsfx-env Test2=multi+word+variable`) +); diff --git a/packages/fx-core/tests/component/driver/script/scriptDriver.test.ts b/packages/fx-core/tests/component/driver/script/scriptDriver.test.ts index 30a7b28c40..ae209d617d 100644 --- a/packages/fx-core/tests/component/driver/script/scriptDriver.test.ts +++ b/packages/fx-core/tests/component/driver/script/scriptDriver.test.ts @@ -166,6 +166,18 @@ describe("parseSetOutputCommand", () => { TAB_ENDPOINT: "https://localhost:53000", }); }); + it("parse value that contains space", async () => { + const res = parseSetOutputCommand( + `Write-Host ::set-teamsfx-env Test0="multi word variable" + Write-Host ::set-teamsfx-env Test1=' multi word variable' + Write-Host ::set-teamsfx-env Test2=multi+word+variable` + ); + assert.deepEqual(res, { + Test0: "multi word variable", + Test1: " multi word variable", + Test2: "multi+word+variable", + }); + }); }); describe("getStderrHandler", () => {