Skip to content

Commit abb7ccc

Browse files
committed
- Begin searching for Pwsh in the "Program Files (Arm)" directory on windows arm64
- Add tests case documenting expected load order - Update test cases to refer to v7 instead of v6
1 parent 5d4929e commit abb7ccc

File tree

2 files changed

+218
-87
lines changed

2 files changed

+218
-87
lines changed

src/platform.ts

Lines changed: 87 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface IPlatformDetails {
4242
operatingSystem: OperatingSystem;
4343
isOS64Bit: boolean;
4444
isProcess64Bit: boolean;
45+
isArm64: boolean;
4546
}
4647

4748
export interface IPowerShellExeDetails {
@@ -68,6 +69,7 @@ export function getPlatformDetails(): IPlatformDetails {
6869
isOS64Bit:
6970
isProcess64Bit || process.env.PROCESSOR_ARCHITEW6432 !== undefined,
7071
isProcess64Bit,
72+
isArm64: process.arch === "arm64",
7173
};
7274
}
7375

@@ -562,93 +564,96 @@ export class PowerShellExeFinder {
562564
}: { useAlternateBitness?: boolean; findPreview?: boolean } = {}): Promise<
563565
IPossiblePowerShellExe | undefined
564566
> {
565-
const programFilesPath = this.getProgramFilesPath({
567+
for (const programFilesPath of this.getProgramFilesPath({
566568
useAlternateBitness,
567-
});
569+
})) {
568570

569-
if (!programFilesPath) {
570-
return undefined;
571-
}
571+
if (!programFilesPath) {
572+
continue;
573+
}
572574

573-
const powerShellInstallBaseDir = path.join(
574-
programFilesPath,
575-
"PowerShell",
576-
);
575+
const powerShellInstallBaseDir = path.join(
576+
programFilesPath,
577+
"PowerShell",
578+
);
577579

578-
// Ensure the base directory exists
579-
if (!(await utils.checkIfDirectoryExists(powerShellInstallBaseDir))) {
580-
return undefined;
581-
}
580+
// Ensure the base directory exists
581+
if (!(await utils.checkIfDirectoryExists(powerShellInstallBaseDir))) {
582+
continue;
583+
}
582584

583-
let highestSeenVersion = -1;
584-
let pwshExePath: string | undefined;
585-
for (const item of await utils.readDirectory(
586-
powerShellInstallBaseDir,
587-
)) {
588-
let currentVersion = -1;
589-
if (findPreview) {
590-
// We are looking for something like "7-preview"
591-
592-
// Preview dirs all have dashes in them
593-
const dashIndex: integer = item.indexOf("-");
594-
if (dashIndex < 0) {
595-
continue;
596-
}
585+
let highestSeenVersion = -1;
586+
let pwshExePath: string | undefined;
587+
for (const item of await utils.readDirectory(
588+
powerShellInstallBaseDir,
589+
)) {
590+
let currentVersion = -1;
591+
if (findPreview) {
592+
// We are looking for something like "7-preview"
593+
594+
// Preview dirs all have dashes in them
595+
const dashIndex: integer = item.indexOf("-");
596+
if (dashIndex < 0) {
597+
continue;
598+
}
597599

598-
// Verify that the part before the dash is an integer
599-
const intPart: string = item.substring(0, dashIndex);
600-
if (!PowerShellExeFinder.IntRegex.test(intPart)) {
601-
continue;
600+
// Verify that the part before the dash is an integer
601+
const intPart: string = item.substring(0, dashIndex);
602+
if (!PowerShellExeFinder.IntRegex.test(intPart)) {
603+
continue;
604+
}
605+
606+
// Verify that the part after the dash is "preview"
607+
if (item.substring(dashIndex + 1) !== "preview") {
608+
continue;
609+
}
610+
611+
currentVersion = parseInt(intPart, 10);
612+
} else {
613+
// Search for a directory like "6" or "7"
614+
if (!PowerShellExeFinder.IntRegex.test(item)) {
615+
continue;
616+
}
617+
618+
currentVersion = parseInt(item, 10);
602619
}
603620

604-
// Verify that the part after the dash is "preview"
605-
if (item.substring(dashIndex + 1) !== "preview") {
621+
// Ensure we haven't already seen a higher version
622+
if (currentVersion <= highestSeenVersion) {
606623
continue;
607624
}
608625

609-
currentVersion = parseInt(intPart, 10);
610-
} else {
611-
// Search for a directory like "6" or "7"
612-
if (!PowerShellExeFinder.IntRegex.test(item)) {
626+
// Now look for the file
627+
const exePath = path.join(
628+
powerShellInstallBaseDir,
629+
item,
630+
"pwsh.exe",
631+
);
632+
if (!(await utils.checkIfFileExists(exePath))) {
613633
continue;
614634
}
615635

616-
currentVersion = parseInt(item, 10);
636+
pwshExePath = exePath;
637+
highestSeenVersion = currentVersion;
617638
}
618639

619-
// Ensure we haven't already seen a higher version
620-
if (currentVersion <= highestSeenVersion) {
640+
if (!pwshExePath) {
621641
continue;
622642
}
623643

624-
// Now look for the file
625-
const exePath = path.join(
626-
powerShellInstallBaseDir,
627-
item,
628-
"pwsh.exe",
629-
);
630-
if (!(await utils.checkIfFileExists(exePath))) {
631-
continue;
632-
}
644+
const bitness: string = programFilesPath.includes("x86")
645+
? "(x86)"
646+
: "(x64)";
633647

634-
pwshExePath = exePath;
635-
highestSeenVersion = currentVersion;
636-
}
648+
const preview: string = findPreview ? " Preview" : "";
637649

638-
if (!pwshExePath) {
639-
return undefined;
650+
return new PossiblePowerShellExe(
651+
pwshExePath,
652+
`PowerShell${preview} ${bitness}`,
653+
);
640654
}
641655

642-
const bitness: string = programFilesPath.includes("x86")
643-
? "(x86)"
644-
: "(x64)";
645-
646-
const preview: string = findPreview ? " Preview" : "";
647-
648-
return new PossiblePowerShellExe(
649-
pwshExePath,
650-
`PowerShell${preview} ${bitness}`,
651-
);
656+
return undefined;
652657
}
653658

654659
private findWinPS({
@@ -707,24 +712,29 @@ export class PowerShellExeFinder {
707712

708713
private getProgramFilesPath({
709714
useAlternateBitness = false,
710-
}: { useAlternateBitness?: boolean } = {}): string | undefined {
715+
}: { useAlternateBitness?: boolean } = {}): (string | undefined)[] {
716+
const result: (string | undefined)[] = [];
717+
711718
if (!useAlternateBitness) {
712719
// Just use the native system bitness
713-
return process.env.ProgramFiles;
714-
}
720+
result.push(process.env.ProgramFiles);
715721

716-
// We might be a 64-bit process looking for 32-bit program files
717-
if (this.platformDetails.isProcess64Bit) {
718-
return process.env["ProgramFiles(x86)"];
719-
}
722+
// We might be a Arm64 process looking for Arm program files
723+
if (this.platformDetails.isArm64) {
724+
result.push(process.env["ProgramFiles(Arm)"]);
725+
}
726+
} else {
727+
// We might be a 64-bit process looking for 32-bit program files
728+
if (this.platformDetails.isProcess64Bit) {
729+
result.push(process.env["ProgramFiles(x86)"]);
730+
}
720731

721-
// We might be a 32-bit process looking for 64-bit program files
722-
if (this.platformDetails.isOS64Bit) {
723-
return process.env.ProgramW6432;
732+
// We might be a 32-bit process looking for 64-bit program files
733+
if (this.platformDetails.isOS64Bit) {
734+
result.push(process.env.ProgramW6432);
735+
}
724736
}
725-
726-
// We're a 32-bit process on 32-bit Windows, there is no other Program Files dir
727-
return undefined;
737+
return result;
728738
}
729739

730740
private getSystem32Path({

0 commit comments

Comments
 (0)