|
35 | 35 | "nu": ".nu", |
36 | 36 | } |
37 | 37 |
|
| 38 | +powershell_cmd = None |
| 39 | + |
| 40 | +if plat == "win": |
| 41 | + |
| 42 | + def detect_powershell(): |
| 43 | + try: |
| 44 | + result = subprocess.run( |
| 45 | + ["pwsh", "-Command", "$PSVersionTable.PSVersion.Major"], |
| 46 | + capture_output=True, |
| 47 | + text=True, |
| 48 | + check=True, |
| 49 | + ) |
| 50 | + version = int(result.stdout.strip()) |
| 51 | + if version >= 7: |
| 52 | + return "pwsh" |
| 53 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 54 | + pass |
| 55 | + |
| 56 | + try: |
| 57 | + result = subprocess.run( |
| 58 | + ["powershell", "-Command", "$PSVersionTable.PSVersion.Major"], |
| 59 | + capture_output=True, |
| 60 | + text=True, |
| 61 | + check=True, |
| 62 | + ) |
| 63 | + version = int(result.stdout.strip()) |
| 64 | + if version >= 5: |
| 65 | + return "powershell" |
| 66 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 67 | + pass |
| 68 | + |
| 69 | + raise OSError("No compatible PowerShell installation found.") |
| 70 | + |
| 71 | + powershell_cmd = detect_powershell() |
| 72 | + |
38 | 73 |
|
39 | 74 | class WindowsProfiles: |
40 | 75 | def __getitem__(self, shell: str) -> str: |
41 | 76 | if shell == "powershell": |
42 | 77 | # find powershell profile path dynamically |
43 | 78 | args = [ |
44 | | - "powershell", |
| 79 | + powershell_cmd, |
45 | 80 | "-NoProfile", |
46 | 81 | "-Command", |
47 | 82 | "$PROFILE.CurrentUserAllHosts", |
@@ -172,7 +207,7 @@ def call_interpreter(s, tmp_path, interpreter, interactive=False, env=None): |
172 | 207 | if interpreter == "cmd.exe": |
173 | 208 | args = ["cmd.exe", "/Q", "/C", f] |
174 | 209 | elif interpreter == "powershell": |
175 | | - args = ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", f] |
| 210 | + args = [powershell_cmd, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", f] |
176 | 211 | elif interpreter == "bash" and plat == "win": |
177 | 212 | args = [os.path.join(os.environ["PROGRAMFILES"], "Git", "bin", "bash.exe"), f] |
178 | 213 | else: |
|
0 commit comments