Skip to content

Commit

Permalink
chore: auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 4, 2023
1 parent 21a855d commit 5b8c7a9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 73 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,10 @@
"attach": {
"properties": {
"processId": {
"type": ["string", "number"],
"type": [
"string",
"number"
],
"description": "The process id of the ansible-playbook process to attach to.",
"default": "${command:PickAnsibleProcess}"
},
Expand Down
6 changes: 4 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ export async function activate(context: ExtensionContext): Promise<void> {
);

context.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider("ansible",
new AnsibleDebugConfigurationProvider())
vscode.debug.registerDebugConfigurationProvider(
"ansible",
new AnsibleDebugConfigurationProvider()
)
);

context.subscriptions.push(
Expand Down
111 changes: 57 additions & 54 deletions src/features/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export namespace DebuggerCommands {
}

export class DebuggerManager {
constructor() {
}
constructor() {}

/**
* Prompt the user for a playbook filename.
Expand All @@ -22,7 +21,7 @@ export class DebuggerManager {
return vscode.window.showInputBox({
title: "Enter Ansible Playbook File",
placeHolder: "Enter the name of the playbook file to debug.",
})
});
}

/**
Expand All @@ -33,9 +32,9 @@ export class DebuggerManager {
// See get_pid_info_path() in ansibug
const tmpDir = process.env.TMPDIR || "/tmp";

const playbookProcesses: {label: string, description: string}[] = [];
for (let procFile of glob.globIterateSync(`${tmpDir}/ANSIBUG-*`)) {
let procInfoRaw = fs.readFileSync(procFile, 'utf8');
const playbookProcesses: { label: string; description: string }[] = [];
for (const procFile of glob.globIterateSync(`${tmpDir}/ANSIBUG-*`)) {
const procInfoRaw = fs.readFileSync(procFile, "utf8");
var procInfo;
try {
procInfo = JSON.parse(procInfoRaw);
Expand All @@ -44,47 +43,48 @@ export class DebuggerManager {
}

if (procInfo.pid && this.isAlive(procInfo.pid)) {
playbookProcesses.push(
{ label: procInfo.pid.toString(), description: procInfo.playbook_file || "Unknown playbook" }
);
playbookProcesses.push({
label: procInfo.pid.toString(),
description: procInfo.playbook_file || "Unknown playbook",
});
}
}

if (playbookProcesses.length == 0) {
return vscode.window.showInformationMessage(
"Cannot find an available ansible-playbook process to debug"
).then(_ => {
return undefined; // abort
});
return vscode.window
.showInformationMessage(
"Cannot find an available ansible-playbook process to debug"
)
.then((_) => {
return undefined; // abort
});
}

return vscode.window.showQuickPick(
playbookProcesses,
{
canPickMany: false
}
).then((v) => v?.label);
return vscode.window
.showQuickPick(playbookProcesses, {
canPickMany: false,
})
.then((v) => v?.label);
}

private isAlive(
pid: number
): boolean {
try {
// Signal of 0 checks if the process exists or not.
return process.kill(pid, 0);
} catch {
return false;
}
private isAlive(pid: number): boolean {
try {
// Signal of 0 checks if the process exists or not.
return process.kill(pid, 0);
} catch {
return false;
}
}
}

export class AnsibleDebugConfigurationProvider implements vscode.DebugConfigurationProvider {

export class AnsibleDebugConfigurationProvider
implements vscode.DebugConfigurationProvider
{
/**
* Massage a debug configuration just before a debug session is being
* launched. This is used to provide the default debug launch configuration
* that launches the current file.
*/
*/
resolveDebugConfiguration(
folder: vscode.WorkspaceFolder | undefined,
config: vscode.DebugConfiguration,
Expand All @@ -96,13 +96,16 @@ export class AnsibleDebugConfigurationProvider implements vscode.DebugConfigurat

// Both ansible and yaml is used incase the file hasn't been explicitly
// marked as ansible and is just yaml.
if (editor && editor.document.languageId === 'ansible' || editor?.document.languageId === 'yaml') {
if (
(editor && editor.document.languageId === "ansible") ||
editor?.document.languageId === "yaml"
) {
config = {
request: "launch",
type: "ansible",
name: "Ansible: Run Current Playbook File",
playbook: "${file}",
}
};
}
}

Expand All @@ -120,26 +123,26 @@ export class AnsibleDebugConfigurationProvider implements vscode.DebugConfigurat
* @returns The debug adapter executable.
*/
export function createAnsibleDebugAdapter(
settings: ExtensionSettings
settings: ExtensionSettings
): vscode.DebugAdapterExecutable {
const ansibugArgs = ["dap"];
if (settings.debugger.logFile) {
ansibugArgs.push(
"--log-file",
settings.debugger.logFile,
"--log-level",
settings.debugger.logLevel,
);
}

// FUTURE: inject PYTHONPATH with embedded ansibug module
const [command, commandArgs, newEnv] = withPythonModule(
settings,
"ansibug",
ansibugArgs
const ansibugArgs = ["dap"];
if (settings.debugger.logFile) {
ansibugArgs.push(
"--log-file",
settings.debugger.logFile,
"--log-level",
settings.debugger.logLevel
);
const dapOptions: vscode.DebugAdapterExecutableOptions = {
env: newEnv
};
return new vscode.DebugAdapterExecutable(command, commandArgs, dapOptions);
}

// FUTURE: inject PYTHONPATH with embedded ansibug module
const [command, commandArgs, newEnv] = withPythonModule(
settings,
"ansibug",
ansibugArgs
);
const dapOptions: vscode.DebugAdapterExecutableOptions = {
env: newEnv,
};
return new vscode.DebugAdapterExecutable(command, commandArgs, dapOptions);
}
33 changes: 18 additions & 15 deletions src/features/utils/commandRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export function withInterpreter(

const activationScript = settings.activationScript;
if (activationScript) {
const [command, shellArgs] = wrapWithActivationScript(activationScript, runExecutable, cmdArgs);
const [command, shellArgs] = wrapWithActivationScript(
activationScript,
runExecutable,
cmdArgs
);
return [`${command} ${shellArgs.join(" ")}`, undefined];
}

Expand All @@ -41,7 +45,7 @@ export function withInterpreter(

// emulating virtual environment activation script
const venvVars = buildPythonVirtualEnvVars(interpreterPath);
for (let key in venvVars) {
for (const key in venvVars) {
newEnv[key] = venvVars[key];
}
delete newEnv.PYTHONHOME;
Expand All @@ -60,14 +64,14 @@ export function withPythonModule(
settings: ExtensionSettings,
module: string,
moduleArgs: string[]
): [string, string[], {[key: string]: string}] {
): [string, string[], { [key: string]: string }] {
let command: string = "python";

let commandArgs: string[] = ["-m", module];
commandArgs.push(...moduleArgs);

const newEnv: {[key: string]: string} = {};
for (let e in process.env) {
const newEnv: { [key: string]: string } = {};
for (const e in process.env) {
newEnv[e] = process.env[e] ?? "";
}

Expand All @@ -79,10 +83,9 @@ export function withPythonModule(
command,
commandArgs.join(" ")
);
}
else if (interpreterPath) {
} else if (interpreterPath) {
const venvVars = buildPythonVirtualEnvVars(interpreterPath);
for (let key in venvVars) {
for (const key in venvVars) {
newEnv[key] = venvVars[key];
}
delete newEnv.PYTHONHOME;
Expand All @@ -104,11 +107,11 @@ export function withPythonModule(
function wrapWithActivationScript(
activationScript: string,
executable: string,
cmdArgs: string,
cmdArgs: string
): [string, string[]] {
return [
"bash",
["-c", `source ${activationScript} && ${executable} ${cmdArgs}`]
["-c", `source ${activationScript} && ${executable} ${cmdArgs}`],
];
}

Expand All @@ -118,14 +121,14 @@ function wrapWithActivationScript(
* @param interpreterPath The Python interpreter path.
* @returns The env vars that need to be set to run in the venv.
*/
function buildPythonVirtualEnvVars(
interpreterPath: string,
): { [key: string]: string } {
function buildPythonVirtualEnvVars(interpreterPath: string): {
[key: string]: string;
} {
const virtualEnv = path.resolve(interpreterPath, "../..");
const pathEntry = path.join(virtualEnv, "bin");

return {
VIRTUAL_ENV: virtualEnv,
PATH: `${pathEntry}${path.delimiter}${process.env.PATH}`
PATH: `${pathEntry}${path.delimiter}${process.env.PATH}`,
};
}
}
2 changes: 1 addition & 1 deletion src/interfaces/extensionSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ export interface LightSpeedServiceSettings {
export interface DebuggerSettings {
logFile: string | null;
logLevel: string;
}
}

0 comments on commit 5b8c7a9

Please sign in to comment.