Skip to content

Commit

Permalink
Refactor: Extract debugging commands to separate methods and register…
Browse files Browse the repository at this point in the history
… in command registration section
  • Loading branch information
JustinGrote committed Nov 19, 2024
1 parent 8fe47b9 commit a3ad00f
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
restartOnExtensionFileChanges(context);
}

// Register a command that waits for the Externsion Terminal to be active. Can be used by .NET Attach Tasks
vscode.commands.registerCommand(
"PowerShell.WaitForPsesActivationAndReturnProcessId",
async () => {
const pidFileName = `PSES-${vscode.env.sessionId}.pid`;
const pidFile = vscode.Uri.joinPath(context.globalStorageUri, "sessions", pidFileName);
const fs = vscode.workspace.fs;
// Wait for the file to be created
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
while (true) {
try {
const pidContent = await fs.readFile(pidFile);
const pid = parseInt(pidContent.toString(), 10);
try {
// Check if the process is still alive, delete the PID file if not and continue waiting.
process.kill(pid, 0);
} catch {
await fs.delete(pidFile);
continue;
}
return pidContent.toString();
} catch {
// File doesn't exist yet, wait and try again
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
}
);

vscode.commands.registerCommand(
"GetVsCodeSessionId",
() => vscode.env.sessionId
);


telemetryReporter = new TelemetryReporter(TELEMETRY_KEY);

Expand Down Expand Up @@ -187,7 +156,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
vscode.commands.registerCommand(
"PowerShell.ShowLogs",
() => {logger.showLogPanel();}
)
),
vscode.commands.registerCommand(
"GetVsCodeSessionId",
() => vscode.env.sessionId
),
// Register a command that waits for the Externsion Terminal to be active. Can be used by .NET Attach Tasks
registerWaitForPsesActivationCommand(context)
];

const externalApi = new ExternalApiFeature(context, sessionManager, logger);
Expand Down Expand Up @@ -220,6 +195,37 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
};
}

/** Registers a command that waits for PSES Activation and returns the PID, used to auto-attach the PSES debugger */
function registerWaitForPsesActivationCommand(context: vscode.ExtensionContext) {

Check failure on line 199 in src/extension.ts

View workflow job for this annotation

GitHub Actions / node (macos-latest)

Missing return type on function

Check failure on line 199 in src/extension.ts

View workflow job for this annotation

GitHub Actions / node (ubuntu-latest)

Missing return type on function
return vscode.commands.registerCommand(
"PowerShell.WaitForPsesActivationAndReturnProcessId",
async () => {
const pidFileName = `PSES-${vscode.env.sessionId}.pid`;
const pidFile = vscode.Uri.joinPath(context.globalStorageUri, "sessions", pidFileName);
const fs = vscode.workspace.fs;
// Wait for the file to be created
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
while (true) {
try {
const pidContent = await fs.readFile(pidFile);
const pid = parseInt(pidContent.toString(), 10);
try {
// Check if the process is still alive, delete the PID file if not and continue waiting.
process.kill(pid, 0);
} catch {
await fs.delete(pidFile);
continue;
}
return pidContent.toString();
} catch {
// File doesn't exist yet, wait and try again
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
}
);
}

/** Restarts the extension host when extension file changes are detected. Useful for development.. */
function restartOnExtensionFileChanges(context: vscode.ExtensionContext): void {
const watcher = vscode.workspace.createFileSystemWatcher(
Expand Down

0 comments on commit a3ad00f

Please sign in to comment.