]> Git Repo - pico-vscode.git/blob - src/utils/installSDKUtil.mts
Test system gdb for arm_any support
[pico-vscode.git] / src / utils / installSDKUtil.mts
1 import { createWriteStream, unlink } from "fs";
2 import { tmpdir } from "os";
3 import { get as httpsGet } from "https";
4 import { basename, join } from "path";
5 import Logger from "../logger.mjs";
6 import { spawn } from "child_process";
7
8 async function downloadWindowsInstaller(
9   version: string,
10   destinationPath: string
11 ): Promise<void> {
12   const url =
13     "https://github.com/raspberrypi/pico-setup-windows/releases" +
14     `/download/v${version}/pico-setup-windows-x64-standalone.exe`;
15
16   //const installerFile = createWriteStream(destinationPath);
17
18   const installerFile = createWriteStream(destinationPath);
19
20   return new Promise<void>((resolve, reject) => {
21     const request = httpsGet(url, response => {
22       if (response.statusCode === 302) {
23         const redirectedUrl = response.headers.location || "";
24         httpsGet(redirectedUrl, redirectedResponse => {
25           redirectedResponse.pipe(installerFile);
26
27           redirectedResponse.on("end", () => {
28             installerFile.on("finish", () => {
29               installerFile.close();
30               console.log(`File downloaded!`);
31               resolve();
32             });
33           });
34
35           redirectedResponse.on("error", error => {
36             unlink(destinationPath, () => {
37               /* nothing */
38             });
39
40             reject(error);
41           });
42         });
43       } else {
44         response.pipe(installerFile);
45
46         response.on("end", () => {
47           installerFile.on("finish", () => {
48             installerFile.close();
49             console.log(`File downloaded!`);
50             resolve();
51           });
52         });
53
54         response.on("error", error => {
55           unlink(destinationPath, () => {
56             /* nothing */
57           });
58
59           reject(error);
60         });
61       }
62     });
63
64     request.on("error", error => {
65       unlink(destinationPath, () => {
66         /* nothing */
67       });
68
69       reject(error);
70     });
71   });
72 }
73
74 async function runInstaller(installerPath: string): Promise<void> {
75   return new Promise<void>((resolve, reject) => {
76     const installerName = basename(installerPath);
77     // cant spawn installer directly and with silent falg because error EACCESS
78     //const silentArgs = "/S"; // Specify the silent mode argument
79
80     const child = spawn("explorer.exe", [installerPath], {
81       cwd: tmpdir(),
82       windowsHide: false,
83       env: process.env,
84       detached: true,
85     });
86
87     child.on("error", error => {
88       console.error(
89         `Failed to execute installer "${installerName}": ${error.message}`
90       );
91       reject(error);
92     });
93
94     child.on("exit", code => {
95       if (code === 0) {
96         console.log(`Installer "${installerName}" started successfully.`);
97         resolve();
98       } else {
99         console.error(
100           `Installer "${installerName}" exited with code ${code ?? "N/A"}.`
101         );
102         reject(
103           new Error(
104             `Installer "${installerName}" exited with code ${code ?? "N/A"}.`
105           )
106         );
107       }
108     });
109   });
110 }
111
112 export async function downloadAndInstallPicoSDKWindows(
113   version: string
114 ): Promise<boolean> {
115   const destinationPath = join(
116     tmpdir(),
117     "pico-setup-windows-x64-standalone.exe"
118   );
119
120   try {
121     await downloadWindowsInstaller(version, destinationPath);
122     Logger.log(`Download of installer v${version} finished.`);
123
124     await runInstaller(destinationPath);
125     Logger.log(`Installation of SDK v${version} started.`);
126
127     return true;
128   } catch (error) {
129     Logger.log(
130       `[ERROR] Download and/or installation of SDK v${version} failed: ${
131         (error as Error).message
132       }`
133     );
134
135     return false;
136   }
137 }
This page took 0.032461 seconds and 4 git commands to generate.