Skip to content

Commit

Permalink
Redo handle Windows PowerShell when detecting or building (#999) (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray authored Oct 27, 2022
1 parent b3df8b1 commit b1e982f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
11 changes: 9 additions & 2 deletions packages/extension/src/language-client/detect-node-path.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { getDataPath } from "@sqltools/util/path";
import { window } from 'vscode';
import { commands, window } from 'vscode';
import fs from "fs";
import getShellExitCommand from "@sqltools/vscode/utils/get-shell-exit-cmd";

const nodeRuntimeTmpFile = getDataPath(".node-runtime");


const detectNodePath = async (): Promise<string | null> => {
const failureMessageTimer = setTimeout(() => {
window.showWarningMessage("Check Terminal view for an erroring 'detect node runtime' session. Capture details for investigation, then kill the terminal to continue SQLTools extension startup. Change the 'sqltools.detectNodeRuntime' setting to disable runtime detection.",
{ modal: true });
commands.executeCommand("terminal.focus");
}, 5000);
try {
const terminal = window.createTerminal({ name: "detect node runtime" });
const shellExitCommand = await getShellExitCommand();
await new Promise<void>(async (resolve) => {
window.onDidCloseTerminal((e => e.processId === terminal.processId && resolve()));
const nodeCmd = `require("fs").writeFileSync("${nodeRuntimeTmpFile}", process.execPath)`;
const nodeCmdWindows = nodeCmd.replace(/\\/g, '\\\\').replace(/\"/g, '\\"');
terminal.sendText(`node -e '${process.platform === 'win32' ? nodeCmdWindows : nodeCmd}' && ${shellExitCommand}`);
terminal.sendText(`node -e '${process.platform === 'win32' ? nodeCmdWindows : nodeCmd}' ${shellExitCommand}`);
})
return fs.readFileSync(nodeRuntimeTmpFile).toString();
} catch (error) {
return null
} finally {
clearTimeout(failureMessageTimer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class DependencyManager implements IExtensionPlugin {
})
progress.report({ message: `Installing "${depNamesString.join(", ")}". Please wait until it finishes. Check the opened terminal for more info.` });

terminal.sendText(`${dependencyManagerSettings.packageManager} ${args.join(" ")} && ${await exitCmdPromise}`);
terminal.sendText(`${dependencyManagerSettings.packageManager} ${args.join(" ")} ${await exitCmdPromise}`);
});
progress.report({ increment: 100, message: `Finished installing ${depNamesString.join(", ")}` });
});
Expand Down Expand Up @@ -108,4 +108,4 @@ export default class DependencyManager implements IExtensionPlugin {
}
} catch (error) { }
}
}
}
19 changes: 15 additions & 4 deletions packages/vscode/utils/get-shell-exit-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ export default async function getShellExitCommand(code = 0) {
throw new Error('No env.shell retrieved despite retries');
}

const isPowerShell = shell.match(/[\\/]pwsh(\.exe)?$/g);
if (isPowerShell) return `$(exit ${code})`;
return `exit ${code}`;
}
if (shell.match(/[\\/]pwsh(\.exe)?$/g)) {

// PowerShell 7+ supports the && pipeline chain operator but needs the exit command wrapped
return `&& $(exit ${code})`;
} else if (shell.match(/[\\]powershell.exe$/g)) {

// Bundled PowerShell of a Windows workstation where PowerShell 7+ hasn't been installed doesn't support the && pipeline chain operator,
// so use ; followed by a conditional command
return `; if ($?) { exit ${code} }`;
} else {

// Everything else
return `&& exit ${code}`;
}
}

0 comments on commit b1e982f

Please sign in to comment.