|
1 | | -import { rmSync } from "fs"; |
| 1 | +import { existsSync, readdirSync, rmSync } from "fs"; |
2 | 2 | import { CommandWithResult } from "./command.mjs"; |
3 | 3 | import { commands, type Uri, window, workspace } from "vscode"; |
4 | 4 | import { type ExecOptions, exec, spawnSync } from "child_process"; |
@@ -718,40 +718,56 @@ export class SetupZephyrCommand extends CommandWithResult<string | undefined> { |
718 | 718 | window.showInformationMessage("wget installed."); |
719 | 719 |
|
720 | 720 | this._logger.info("Installing 7zip"); |
721 | | - const szipURL = new URL("https://7-zip.org/a/7z2409-x64.exe"); |
722 | | - const szipResult = await downloadFileGot( |
723 | | - szipURL, |
724 | | - joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "7zip-x64.exe") |
725 | | - ); |
726 | | - |
727 | | - if (!szipResult) { |
728 | | - window.showErrorMessage( |
729 | | - "Could not download 7zip. Exiting Zephyr Setup" |
| 721 | + const szipTargetDirectory = joinPosix("C:\\Program Files\\7-Zip"); |
| 722 | + if ( |
| 723 | + existsSync(szipTargetDirectory) && |
| 724 | + readdirSync(szipTargetDirectory).length !== 0 |
| 725 | + ) { |
| 726 | + this._logger.info("7-Zip is already installed."); |
| 727 | + } else { |
| 728 | + const szipURL = new URL("https://7-zip.org/a/7z2409-x64.exe"); |
| 729 | + const szipResult = await downloadFileGot( |
| 730 | + szipURL, |
| 731 | + joinPosix( |
| 732 | + homedir().replaceAll("\\", "/"), |
| 733 | + ".pico-sdk", |
| 734 | + "7zip-x64.exe" |
| 735 | + ) |
730 | 736 | ); |
731 | 737 |
|
732 | | - return; |
733 | | - } |
| 738 | + if (!szipResult) { |
| 739 | + window.showErrorMessage( |
| 740 | + "Could not download 7-Zip. Exiting Zephyr Setup" |
| 741 | + ); |
734 | 742 |
|
735 | | - const szipCommand: string = "7zip-x64.exe"; |
736 | | - const szipInstallResult = await this._runCommand(szipCommand, { |
737 | | - cwd: joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk"), |
738 | | - }); |
| 743 | + return; |
| 744 | + } |
739 | 745 |
|
740 | | - if (szipInstallResult !== 0) { |
741 | | - window.showErrorMessage( |
742 | | - "Could not install 7zip. Please ensure 7-Zip is installed." + |
743 | | - "Exiting Zephyr Setup" |
744 | | - ); |
| 746 | + const szipCommand: string = "7zip-x64.exe"; |
| 747 | + const szipInstallResult = await this._runCommand(szipCommand, { |
| 748 | + cwd: joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk"), |
| 749 | + }); |
745 | 750 |
|
746 | | - return; |
747 | | - } |
| 751 | + if (szipInstallResult !== 0) { |
| 752 | + window.showErrorMessage( |
| 753 | + "Could not install 7-Zip. Please ensure 7-Zip is installed." + |
| 754 | + "Exiting Zephyr Setup" |
| 755 | + ); |
748 | 756 |
|
749 | | - // Clean up |
750 | | - rmSync( |
751 | | - joinPosix(homedir().replaceAll("\\", "/"), ".pico-sdk", "7zip-x64.exe") |
752 | | - ); |
| 757 | + return; |
| 758 | + } |
| 759 | + |
| 760 | + // Clean up |
| 761 | + rmSync( |
| 762 | + joinPosix( |
| 763 | + homedir().replaceAll("\\", "/"), |
| 764 | + ".pico-sdk", |
| 765 | + "7zip-x64.exe" |
| 766 | + ) |
| 767 | + ); |
753 | 768 |
|
754 | | - window.showInformationMessage("7zip installed."); |
| 769 | + window.showInformationMessage("7-Zip installed."); |
| 770 | + } |
755 | 771 | } |
756 | 772 |
|
757 | 773 | this._logger.info("Installing OpenOCD"); |
@@ -846,38 +862,76 @@ manifest: |
846 | 862 | Buffer.from(zephyrManifestContent) |
847 | 863 | ); |
848 | 864 |
|
849 | | - const command: string = [ |
| 865 | + const createVenvCommandVenv: string = [ |
| 866 | + `${process.env.ComSpec === "powershell.exe" ? "&" : ""}"${pythonExe}"`, |
| 867 | + "-m venv venv", |
| 868 | + ].join(" "); |
| 869 | + const createVenvCommandVirtualenv: string = [ |
850 | 870 | `${process.env.ComSpec === "powershell.exe" ? "&" : ""}"${pythonExe}"`, |
851 | | - "-m " + (process.platform === "win32" ? "virtualenv" : "venv") + " venv", |
| 871 | + "-m virtualenv venv", |
852 | 872 | ].join(" "); |
853 | 873 |
|
854 | 874 | // Create a Zephyr workspace, copy the west manifest in and initialise the workspace |
855 | 875 | workspace.fs.createDirectory(Uri.file(zephyrWorkspaceDirectory)); |
856 | 876 |
|
| 877 | + // Generic result to get value from runCommand calls |
| 878 | + let result: number | null; |
| 879 | + |
857 | 880 | this._logger.info("Setting up virtual environment for Zephyr"); |
858 | | - let result = await this._runCommand(command, { |
859 | | - cwd: zephyrWorkspaceDirectory, |
860 | | - windowsHide: true, |
861 | | - env: customEnv, |
862 | | - }); |
| 881 | + const venvPython: string = joinPosix( |
| 882 | + zephyrWorkspaceDirectory, |
| 883 | + "venv", |
| 884 | + process.platform === "win32" ? "Scripts" : "bin", |
| 885 | + process.platform === "win32" ? "python.exe" : "python" |
| 886 | + ); |
| 887 | + if (existsSync(venvPython)) { |
| 888 | + this._logger.info("Existing Python venv found."); |
| 889 | + } else { |
| 890 | + result = await this._runCommand(createVenvCommandVenv, { |
| 891 | + cwd: zephyrWorkspaceDirectory, |
| 892 | + windowsHide: true, |
| 893 | + env: customEnv, |
| 894 | + }); |
| 895 | + if (result !== 0) { |
| 896 | + this._logger.warn( |
| 897 | + "Could not create virtual environment with venv," + |
| 898 | + "trying with virtualenv..." |
| 899 | + ); |
863 | 900 |
|
864 | | - this._logger.info(`${result}`); |
| 901 | + result = await this._runCommand(createVenvCommandVirtualenv, { |
| 902 | + cwd: zephyrWorkspaceDirectory, |
| 903 | + windowsHide: true, |
| 904 | + env: customEnv, |
| 905 | + }); |
| 906 | + |
| 907 | + if (result !== 0) { |
| 908 | + this._logger.error("Could not create virtual environment. Exiting."); |
| 909 | + window.showErrorMessage( |
| 910 | + "Could not install venv. Exiting Zephyr Setup" |
| 911 | + ); |
| 912 | + |
| 913 | + return; |
| 914 | + } |
| 915 | + } |
| 916 | + |
| 917 | + this._logger.info("Venv created."); |
| 918 | + } |
865 | 919 |
|
866 | | - const venvPythonExe: string = joinPosix( |
| 920 | + const venvPythonCommand: string = joinPosix( |
867 | 921 | process.env.ComSpec === "powershell.exe" ? "&" : "", |
868 | 922 | zephyrWorkspaceDirectory, |
869 | 923 | "venv", |
870 | 924 | process.platform === "win32" ? "Scripts" : "bin", |
871 | 925 | process.platform === "win32" ? "python.exe" : "python" |
872 | 926 | ); |
873 | 927 |
|
874 | | - const command2: string = [ |
875 | | - venvPythonExe, |
| 928 | + const installWestCommand: string = [ |
| 929 | + venvPythonCommand, |
876 | 930 | "-m pip install west pyelftools", |
877 | 931 | ].join(" "); |
878 | 932 |
|
879 | 933 | this._logger.info("Installing Python dependencies for Zephyr"); |
880 | | - result = await this._runCommand(command2, { |
| 934 | + result = await this._runCommand(installWestCommand, { |
881 | 935 | cwd: zephyrWorkspaceDirectory, |
882 | 936 | windowsHide: true, |
883 | 937 | env: customEnv, |
|
0 commit comments