From b54e651d52ddaad0f6eeb65be84afd1556eeac92 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 7 Nov 2024 15:30:28 +0000 Subject: [PATCH] Simplify withInterpreter signature (#1261) --- .../ansible-language-server/src/utils/commandRunner.ts | 10 ++++++---- packages/ansible-language-server/src/utils/misc.ts | 6 +++--- .../test/utils/withInterpreter.test.ts | 10 +++++----- src/extension.ts | 8 ++++---- .../contentCreator/createAnsibleCollectionPage.ts | 8 ++++---- .../contentCreator/createAnsibleProjectPage.ts | 4 ++-- src/features/contentCreator/utils.ts | 4 ++-- src/features/runner.ts | 8 ++++---- src/features/utils/commandRunner.ts | 6 +++--- 9 files changed, 33 insertions(+), 31 deletions(-) diff --git a/packages/ansible-language-server/src/utils/commandRunner.ts b/packages/ansible-language-server/src/utils/commandRunner.ts index 00e8ba3ab..e87803e93 100644 --- a/packages/ansible-language-server/src/utils/commandRunner.ts +++ b/packages/ansible-language-server/src/utils/commandRunner.ts @@ -30,8 +30,8 @@ export class CommandRunner { stderr: string; }> { let executablePath: string; - let command: string | undefined; - let runEnv: NodeJS.ProcessEnv | undefined; + let command: string | undefined = ""; + let runEnv: NodeJS.ProcessEnv; const isEEEnabled = this.settings.executionEnvironment.enabled; let interpreterPathFromConfig = this.settings.python.interpreterPath; if (interpreterPathFromConfig.includes("${workspaceFolder}")) { @@ -53,12 +53,14 @@ export class CommandRunner { // prepare command and env for local run if (!isEEEnabled) { - [command, runEnv] = withInterpreter( + const result = withInterpreter( executablePath, args, interpreterPath, this.settings.python.activationScript, ); + command = result.command; + runEnv = result.env; } else { // prepare command and env for execution environment run const executionEnvironment = await this.context.executionEnvironment; @@ -66,7 +68,7 @@ export class CommandRunner { `${executable} ${args}`, mountPaths, ); - runEnv = undefined; + runEnv = process.env; } if (command === undefined) { return { stdout: "", stderr: "" }; diff --git a/packages/ansible-language-server/src/utils/misc.ts b/packages/ansible-language-server/src/utils/misc.ts index b12f4dc2b..1bcef4bb5 100644 --- a/packages/ansible-language-server/src/utils/misc.ts +++ b/packages/ansible-language-server/src/utils/misc.ts @@ -46,7 +46,7 @@ export function withInterpreter( args: string, interpreterPath: string, activationScript: string, -): [string, NodeJS.ProcessEnv | undefined] { +): { command: string; env: NodeJS.ProcessEnv } { let command = `${executable} ${args}`; // base case const newEnv = Object.assign({}, process.env, { @@ -59,7 +59,7 @@ export function withInterpreter( if (activationScript) { command = `bash -c 'source ${activationScript} && ${executable} ${args}'`; - return [command, undefined]; + return { command: command, env: process.env }; } if (interpreterPath) { @@ -76,7 +76,7 @@ export function withInterpreter( newEnv["PATH"] = `${pathEntry}:${process.env.PATH}`; delete newEnv.PYTHONHOME; } - return [command, newEnv]; + return { command: command, env: newEnv }; } /** diff --git a/packages/ansible-language-server/test/utils/withInterpreter.test.ts b/packages/ansible-language-server/test/utils/withInterpreter.test.ts index 8daab189a..42c312fce 100644 --- a/packages/ansible-language-server/test/utils/withInterpreter.test.ts +++ b/packages/ansible-language-server/test/utils/withInterpreter.test.ts @@ -71,18 +71,18 @@ describe("withInterpreter", () => { interpreterPath, activationScript, ); - expect(actualCommand[0]).to.equal(expectedCommand); + expect(actualCommand.command).to.equal(expectedCommand); if (expectedEnv) { const expectedKeys = Object.keys(expectedEnv); expectedKeys.forEach((key) => { - expect(actualCommand[1]).to.haveOwnProperty(key); - expect(typeof actualCommand[1] === "object"); - if (!actualCommand[1] || typeof expectedEnv === "string") { + expect(actualCommand.env).to.haveOwnProperty(key); + expect(typeof actualCommand.env === "object"); + if (!actualCommand.env || typeof expectedEnv === "string") { expect(false); } else { - expect(actualCommand[1][key]).to.include(expectedEnv[key]); + expect(actualCommand.env[key]).to.include(expectedEnv[key]); } }); } diff --git a/src/extension.ts b/src/extension.ts index 5fc65463a..250d4cd12 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -450,7 +450,7 @@ export async function activate(context: ExtensionContext): Promise { const pythonInterpreter = extSettings.settings.interpreterPath; // specify the current python interpreter path in the pip installation - const [command, runEnv] = withInterpreter( + const { command, env } = withInterpreter( extSettings.settings, `${pythonInterpreter} -m pip install ansible-creator`, "--no-input", @@ -466,7 +466,7 @@ export async function activate(context: ExtensionContext): Promise { } terminal = vscode.window.createTerminal({ name: "Ansible Terminal", - env: runEnv, + env: env, }); terminal.show(); terminal.sendText(command); @@ -652,7 +652,7 @@ export async function activate(context: ExtensionContext): Promise { const pythonInterpreter = extSettings.settings.interpreterPath; // specify the current python interpreter path in the pip installation - const [command, runEnv] = withInterpreter( + const { command, env } = withInterpreter( extSettings.settings, `${pythonInterpreter} -m pip install ansible-dev-tools`, "--no-input", @@ -678,7 +678,7 @@ export async function activate(context: ExtensionContext): Promise { try { const result = execSync(command, { - env: runEnv, + env: env, }).toString(); commandOutput = result; outputChannel.append(commandOutput); diff --git a/src/features/contentCreator/createAnsibleCollectionPage.ts b/src/features/contentCreator/createAnsibleCollectionPage.ts index bb0338eb1..10d500238 100644 --- a/src/features/contentCreator/createAnsibleCollectionPage.ts +++ b/src/features/contentCreator/createAnsibleCollectionPage.ts @@ -424,7 +424,7 @@ export class CreateAnsibleCollection { const extSettings = new SettingsManager(); await extSettings.initialize(); - const [command, runEnv] = withInterpreter( + const { command, env } = withInterpreter( extSettings.settings, ansibleCreatorInitCommand, "", @@ -433,7 +433,7 @@ export class CreateAnsibleCollection { let commandOutput = ""; // execute ansible-creator command - const ansibleCreatorExecutionResult = await runCommand(command, runEnv); + const ansibleCreatorExecutionResult = await runCommand(command, env); commandOutput += `------------------------------------ ansible-creator logs ------------------------------------\n`; commandOutput += ansibleCreatorExecutionResult.output; const ansibleCreatorCommandPassed = ansibleCreatorExecutionResult.status; @@ -442,14 +442,14 @@ export class CreateAnsibleCollection { // ade command inherits only the verbosity options from ansible-creator command console.debug("[ade] command: ", adeCommand); - const [command, runEnv] = withInterpreter( + const { command, env } = withInterpreter( extSettings.settings, adeCommand, "", ); // execute ade command - const adeExecutionResult = await runCommand(command, runEnv); + const adeExecutionResult = await runCommand(command, env); commandOutput += `\n\n------------------------------- ansible-dev-environment logs --------------------------------\n`; commandOutput += adeExecutionResult.output; } diff --git a/src/features/contentCreator/createAnsibleProjectPage.ts b/src/features/contentCreator/createAnsibleProjectPage.ts index dd8c985d2..fe7ab32af 100644 --- a/src/features/contentCreator/createAnsibleProjectPage.ts +++ b/src/features/contentCreator/createAnsibleProjectPage.ts @@ -391,7 +391,7 @@ export class CreateAnsibleProject { const extSettings = new SettingsManager(); await extSettings.initialize(); - const [command, runEnv] = withInterpreter( + const { command, env } = withInterpreter( extSettings.settings, ansibleCreatorInitCommand, "", @@ -400,7 +400,7 @@ export class CreateAnsibleProject { let commandOutput = ""; // execute ansible-creator command - const ansibleCreatorExecutionResult = await runCommand(command, runEnv); + const ansibleCreatorExecutionResult = await runCommand(command, env); commandOutput += `------------------------------------ ansible-creator logs ------------------------------------\n`; commandOutput += ansibleCreatorExecutionResult.output; const commandPassed = ansibleCreatorExecutionResult.status; diff --git a/src/features/contentCreator/utils.ts b/src/features/contentCreator/utils.ts index 717912ac9..c419829a9 100644 --- a/src/features/contentCreator/utils.ts +++ b/src/features/contentCreator/utils.ts @@ -7,11 +7,11 @@ export async function getBinDetail(cmd: string, arg: string) { const extSettings = new SettingsManager(); await extSettings.initialize(); - const [command, runEnv] = withInterpreter(extSettings.settings, cmd, arg); + const { command, env } = withInterpreter(extSettings.settings, cmd, arg); try { const result = cp.execSync(command, { - env: runEnv, + env: env, }); return result; } catch { diff --git a/src/features/runner.ts b/src/features/runner.ts index db85b980a..0c63cc291 100644 --- a/src/features/runner.ts +++ b/src/features/runner.ts @@ -153,14 +153,14 @@ export class AnsiblePlaybookRunProvider { // replace spaces in file name with escape sequence '\ ' commandLineArgs.push(playbookFsPath.replace(/(\s)/, "\\ ")); const cmdArgs = commandLineArgs.map((arg) => arg).join(" "); - const [command, runEnv] = withInterpreter( + const { command, env } = withInterpreter( this.extensionSettings.settings, runExecutable, cmdArgs, ); console.debug(`Running command: ${command}`); - this.invokeInTerminal(command, runEnv); + this.invokeInTerminal(command, env); } /** @@ -185,13 +185,13 @@ export class AnsiblePlaybookRunProvider { const cmdArgs = commandLineArgs.map((arg) => arg).join(" "); const runCmdArgs = `run ${cmdArgs}`; - const [command, runEnv] = withInterpreter( + const { command, env } = withInterpreter( this.extensionSettings.settings, runExecutable, runCmdArgs, ); console.debug(`Running command: ${command}`); - this.invokeInTerminal(command, runEnv); + this.invokeInTerminal(command, env); } } diff --git a/src/features/utils/commandRunner.ts b/src/features/utils/commandRunner.ts index 8ae43b41f..9c75b8881 100644 --- a/src/features/utils/commandRunner.ts +++ b/src/features/utils/commandRunner.ts @@ -16,7 +16,7 @@ export function withInterpreter( settings: ExtensionSettings, runExecutable: string, cmdArgs: string, -): [string, NodeJS.ProcessEnv | undefined] { +): { command: string; env: NodeJS.ProcessEnv } { let command = `${runExecutable} ${cmdArgs}`; // base case const newEnv = Object.assign({}, process.env, { @@ -29,7 +29,7 @@ export function withInterpreter( const activationScript = settings.activationScript; if (activationScript) { command = `bash -c 'source ${activationScript} && ${runExecutable} ${cmdArgs}'`; - return [command, undefined]; + return { command: command, env: process.env }; } const interpreterPath = settings.interpreterPath; @@ -46,5 +46,5 @@ export function withInterpreter( newEnv["PATH"] = `${pathEntry}:${process.env.PATH}`; delete newEnv.PYTHONHOME; } - return [command, newEnv]; + return { command: command, env: newEnv } as const; }