Skip to content

Commit 38172ea

Browse files
committed
Implement python extension version selection + added python ext as dependency as MicroPico already has it so this doesn't make a difference
Signed-off-by: paulober <[email protected]>
1 parent 0aeccea commit 38172ea

File tree

5 files changed

+82
-36
lines changed

5 files changed

+82
-36
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"ms-vscode.cpptools-extension-pack",
4848
"ms-vscode.vscode-serial-monitor",
4949
"marus25.cortex-debug",
50-
"paulober.pico-w-go"
50+
"paulober.pico-w-go",
51+
"ms-python.python"
5152
],
5253
"main": "./dist/extension.cjs",
5354
"markdown": "github",
@@ -64,7 +65,8 @@
6465
},
6566
"activationEvents": [
6667
"workspaceContains:./pico_sdk_import.cmake",
67-
"onWebviewPanel:newPicoProject"
68+
"onWebviewPanel:newPicoProject",
69+
"onWebviewPanel:newPicoMicroPythonProject"
6870
],
6971
"contributes": {
7072
"commands": [
@@ -293,6 +295,7 @@
293295
"typescript-eslint": "^8.1.0"
294296
},
295297
"dependencies": {
298+
"@vscode/python-extension": "^1.0.5",
296299
"adm-zip": "^0.5.14 <0.5.15",
297300
"got": "^14.4.2",
298301
"ini": "^4.1.3",

src/utils/githubREST.mts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ export const PYENV_REPOSITORY_URL = "https://github.com/pyenv/pyenv.git";
6161
export function ownerOfRepository(repository: GithubRepository): string {
6262
switch (repository) {
6363
case GithubRepository.picoSDK:
64+
case GithubRepository.tools:
65+
case GithubRepository.picotool:
6466
return "raspberrypi";
6567
case GithubRepository.cmake:
6668
return "Kitware";
6769
case GithubRepository.ninja:
6870
return "ninja-build";
69-
case GithubRepository.tools:
70-
return "raspberrypi";
71-
case GithubRepository.picotool:
72-
return "raspberrypi";
7371
}
7472
}
7573

src/webview/newMicroPythonProjectPanel.mts

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import which from "which";
2323
import { existsSync } from "fs";
2424
import { join } from "path";
25+
import { PythonExtension } from "@vscode/python-extension";
2526

2627
interface SubmitMessageValue {
2728
projectName: string;
@@ -37,10 +38,11 @@ export class NewMicroPythonProjectPanel {
3738
private readonly _panel: WebviewPanel;
3839
private readonly _extensionUri: Uri;
3940
private readonly _settings: Settings;
40-
private readonly _logger: Logger = new Logger("NewProjectPanel");
41+
private readonly _logger: Logger = new Logger("NewMicroPythonProjectPanel");
4142
private _disposables: Disposable[] = [];
4243

4344
private _projectRoot?: Uri;
45+
private _pythonExtensionApi?: PythonExtension;
4446

4547
public static createOrShow(extensionUri: Uri, projectUri?: Uri): void {
4648
const column = window.activeTextEditor
@@ -347,6 +349,9 @@ print("Finished.")\r\n`;
347349
"web",
348350
"raspberry-128.png"
349351
);
352+
if (!this._pythonExtensionApi) {
353+
this._pythonExtensionApi = await PythonExtension.api();
354+
}
350355
const html = await this._getHtmlForWebview(this._panel.webview);
351356

352357
if (html !== "") {
@@ -407,8 +412,13 @@ print("Finished.")\r\n`;
407412
Uri.joinPath(this._extensionUri, "web", "raspberrypi-nav-header-dark.svg")
408413
);
409414

410-
// TODO: maybe use python extension api
411-
// TODO: check python version, workaround, ownly allow python3 commands on unix
415+
// TODO: add support for onDidChangeActiveEnvironment and filter envs that don't directly point
416+
// to an executable
417+
const environments = this._pythonExtensionApi?.environments;
418+
const knownEnvironments = environments?.known;
419+
const activeEnv = environments?.getActiveEnvironmentPath();
420+
421+
// TODO: check python version, workaround, only allow python3 commands on unix
412422
const isPythonSystemAvailable =
413423
(await which("python3", { nothrow: true })) !== null ||
414424
(await which("python", { nothrow: true })) !== null;
@@ -484,29 +494,54 @@ print("Finished.")\r\n`;
484494
</div>
485495
486496
<div class="ms-6">
487-
${
488-
process.platform === "darwin" || process.platform === "win32"
489-
? `
490-
<div class="col-span-2">
491-
<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Python Version:</label>
492-
493-
${
494-
isPythonSystemAvailable
495-
? `<div class="flex items-center mb-2" >
496-
<input type="radio" id="python-radio-system-version" name="python-version-radio" value="1" class="mr-1 text-blue-500">
497-
<label for="python-radio-system-version" class="text-gray-900 dark:text-white">Use system version</label>
498-
</div>`
499-
: ""
500-
}
501-
502-
<div class="flex items-center mb-2">
503-
<input type="radio" id="python-radio-path-executable" name="python-version-radio" value="2" class="mr-1 text-blue-500">
504-
<label for="python-radio-path-executable" class="text-gray-900 dark:text-white">Path to executable:</label>
505-
<input type="file" id="python-path-executable" multiple="false" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 ms-2">
506-
</div>
497+
<div class="col-span-2">
498+
<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Python Version:</label>
499+
500+
${
501+
knownEnvironments && knownEnvironments.length > 0
502+
? `
503+
<div class="flex items-center mb-2">
504+
<input type="radio" id="python-radio-known" name="python-version-radio" value="0" class="mr-1 text-blue-500">
505+
<label for="python-radio-known" class="text-gray-900 dark:text-white">From Python extension</label>
506+
<!-- show a select dropdown with python environments -->
507+
<select id="sel-pyenv-known" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
508+
${knownEnvironments
509+
.map(
510+
env =>
511+
`<option value="${env.path}" ${
512+
(activeEnv?.id ?? "") === env.id
513+
? "selected"
514+
: ""
515+
}>${env.path}</option>`
516+
)
517+
.join("")}
518+
</select>
519+
</div>
520+
`
521+
: ""
522+
}
523+
524+
${
525+
process.platform === "darwin" ||
526+
process.platform === "win32"
527+
? `
528+
${
529+
isPythonSystemAvailable
530+
? `<div class="flex items-center mb-2" >
531+
<input type="radio" id="python-radio-system-version" name="python-version-radio" value="1" class="mr-1 text-blue-500">
532+
<label for="python-radio-system-version" class="text-gray-900 dark:text-white">Use system version</label>
507533
</div>`
508-
: ""
509-
}
534+
: ""
535+
}
536+
537+
<div class="flex items-center mb-2">
538+
<input type="radio" id="python-radio-path-executable" name="python-version-radio" value="2" class="mr-1 text-blue-500">
539+
<label for="python-radio-path-executable" class="text-gray-900 dark:text-white">Path to executable:</label>
540+
<input type="file" id="python-path-executable" multiple="false" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 ms-2">
541+
</div>`
542+
: ""
543+
}
544+
</div>
510545
</div>
511546
512547
<div class="mt-6 mb-4 col-span-2">

web/mpy/main.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ var submitted = false;
8686
}
8787
}
8888
if (pythonVersionRadio.length == 0) {
89-
// default to python mode 1 == System version
90-
pythonMode = 1;
89+
// default to python mode 0 == python ext version
90+
pythonMode = 0;
9191
}
9292

9393
// if python version is null or not a number, smaller than 0 or bigger than 3, set it to 0
9494
if (pythonMode === null || isNaN(pythonMode) || pythonMode < 0 || pythonMode > 3) {
95-
// TODO: first check if defaul is supported
9695
pythonMode = 0;
9796
console.debug('Invalid python version value: ' + pythonMode.toString());
9897
vscode.postMessage({
@@ -103,11 +102,14 @@ var submitted = false;
103102

104103
return;
105104
}
106-
if (pythonMode == 2) {
105+
if (pythonMode === 0) {
106+
const pyenvKnownSel = document.getElementById("sel-pyenv-known");
107+
pythonPath = pyenvKnownSel.value;
108+
} else if (pythonMode === 2) {
107109
const files = document.getElementById('python-path-executable').files;
108110

109111
if (files.length == 1) {
110-
cmakePath = files[0].name;
112+
pythonPath = files[0].name;
111113
} else {
112114
console.debug("Please select a valid python executable file");
113115
vscode.postMessage({

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,13 @@ __metadata:
653653
languageName: node
654654
linkType: hard
655655

656+
"@vscode/python-extension@npm:^1.0.5":
657+
version: 1.0.5
658+
resolution: "@vscode/python-extension@npm:1.0.5"
659+
checksum: 10/aeaa1ee9275f8679aa745c3096a68e1952328b8c7de90e1665f369e321dafb3e1271d42c32e4f8d6a3c92f6cdbb4aaef22cc45101764a623900efc4f3dfd3270
660+
languageName: node
661+
linkType: hard
662+
656663
"abbrev@npm:^1.0.0":
657664
version: 1.1.1
658665
resolution: "abbrev@npm:1.1.1"
@@ -2452,6 +2459,7 @@ __metadata:
24522459
"@types/uuid": "npm:^10.0.0"
24532460
"@types/vscode": "npm:^1.92.0"
24542461
"@types/which": "npm:^3.0.4"
2462+
"@vscode/python-extension": "npm:^1.0.5"
24552463
adm-zip: "npm:^0.5.14 <0.5.15"
24562464
eslint: "npm:^9.9.0"
24572465
eslint-config-prettier: "npm:^9.1.0"

0 commit comments

Comments
 (0)