]> Git Repo - pico-vscode.git/blob - src/commands/getPaths.mts
Update dependencies + auto-format
[pico-vscode.git] / src / commands / getPaths.mts
1 import { CommandWithResult } from "./command.mjs";
2 import { commands, workspace } from "vscode";
3 import {
4   getPythonPath,
5   getPath,
6   cmakeGetSelectedToolchainAndSDKVersions,
7   cmakeGetPicoVar,
8 } from "../utils/cmakeUtil.mjs";
9 import { join } from "path";
10 import { buildToolchainPath } from "../utils/download.mjs";
11 import Settings, { SettingsKey } from "../settings.mjs";
12
13 export class GetPythonPathCommand extends CommandWithResult<string> {
14   constructor() {
15     super("getPythonPath");
16   }
17
18   async execute(): Promise<string> {
19     if (
20       workspace.workspaceFolders === undefined ||
21       workspace.workspaceFolders.length === 0
22     ) {
23       return "";
24     }
25
26     const pythonPath = await getPythonPath();
27
28     return pythonPath;
29   }
30 }
31
32 export class GetEnvPathCommand extends CommandWithResult<string> {
33   constructor() {
34     super("getEnvPath");
35   }
36
37   async execute(): Promise<string> {
38     if (
39       workspace.workspaceFolders === undefined ||
40       workspace.workspaceFolders.length === 0
41     ) {
42       return "";
43     }
44
45     const path = await getPath();
46
47     return path;
48   }
49 }
50
51 export class GetGDBPathCommand extends CommandWithResult<string> {
52   constructor() {
53     super("getGDBPath");
54   }
55
56   execute(): string {
57     if (
58       workspace.workspaceFolders === undefined ||
59       workspace.workspaceFolders.length === 0
60     ) {
61       return "";
62     }
63
64     const workspaceFolder = workspace.workspaceFolders?.[0];
65
66     const selectedToolchainAndSDKVersions =
67       cmakeGetSelectedToolchainAndSDKVersions(
68         join(workspaceFolder.uri.fsPath, "CMakeLists.txt")
69       );
70     if (selectedToolchainAndSDKVersions === null) {
71       return "";
72     }
73     const toolchainVersion = selectedToolchainAndSDKVersions[1];
74
75     let triple = "arm-none-eabi";
76     if (toolchainVersion.includes("RISCV")) {
77       if (toolchainVersion.includes("COREV")) {
78         triple = "riscv32-corev-elf";
79       } else {
80         triple = "riscv32-unknown-elf";
81       }
82     } else if (process.platform === "linux") {
83       // Arm toolchains have incorrect libncurses versions on Linux (specifically RPiOS)
84       if (process.arch === "arm64") {
85         return "gdb";
86       } else {
87         return "gdb-multiarch";
88       }
89     }
90
91     return join(buildToolchainPath(toolchainVersion), "bin", triple + "-gdb");
92   }
93 }
94
95 export class GetChipCommand extends CommandWithResult<string> {
96   constructor() {
97     super("getChip");
98   }
99
100   async execute(): Promise<string> {
101     if (
102       workspace.workspaceFolders === undefined ||
103       workspace.workspaceFolders.length === 0
104     ) {
105       return "";
106     }
107
108     const workspaceFolder = workspace.workspaceFolders?.[0];
109
110     const settings = Settings.getInstance();
111     let buildDir = join(workspaceFolder.uri.fsPath, "build");
112     if (
113       settings !== undefined &&
114       settings.getBoolean(SettingsKey.useCmakeTools)
115     ) {
116       // Compiling with CMake Tools
117       const cmakeBuildDir: string = await commands.executeCommand(
118         "cmake.buildDirectory"
119       );
120
121       if (cmakeBuildDir) {
122         buildDir = cmakeBuildDir;
123       }
124     }
125
126     const platform = cmakeGetPicoVar(
127       join(buildDir, "CMakeCache.txt"),
128       "PICO_PLATFORM"
129     );
130     if (platform === null) {
131       return "rp2040";
132     }
133
134     if (platform === "rp2350-arm-s" || platform === "rp2350-riscv") {
135       return "rp2350";
136     } else {
137       return "rp2040";
138     }
139   }
140 }
141
142 export class GetTargetCommand extends CommandWithResult<string> {
143   constructor() {
144     super("getTarget");
145   }
146
147   async execute(): Promise<string> {
148     if (
149       workspace.workspaceFolders === undefined ||
150       workspace.workspaceFolders.length === 0
151     ) {
152       return "";
153     }
154
155     const workspaceFolder = workspace.workspaceFolders?.[0];
156
157     const settings = Settings.getInstance();
158     let buildDir = join(workspaceFolder.uri.fsPath, "build");
159     if (
160       settings !== undefined &&
161       settings.getBoolean(SettingsKey.useCmakeTools)
162     ) {
163       // Compiling with CMake Tools
164       const cmakeBuildDir: string = await commands.executeCommand(
165         "cmake.buildDirectory"
166       );
167
168       if (cmakeBuildDir) {
169         buildDir = cmakeBuildDir;
170       }
171     }
172
173     const platform = cmakeGetPicoVar(
174       join(buildDir, "CMakeCache.txt"),
175       "PICO_PLATFORM"
176     );
177     if (platform === null) {
178       return "rp2040";
179     }
180
181     if (platform === "rp2350-arm-s") {
182       return "rp2350";
183     } else if (platform === "rp2350-riscv") {
184       return "rp2350-riscv";
185     } else {
186       return "rp2040";
187     }
188   }
189 }
This page took 0.035434 seconds and 4 git commands to generate.