Skip to content

Commit

Permalink
Implement python extension version selection + added python ext as de…
Browse files Browse the repository at this point in the history
…pendency as MicroPico already has it so this doesn't make a difference

Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Sep 5, 2024
1 parent a317feb commit a4c7303
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 36 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"ms-vscode.cpptools-extension-pack",
"ms-vscode.vscode-serial-monitor",
"marus25.cortex-debug",
"paulober.pico-w-go"
"paulober.pico-w-go",
"ms-python.python"
],
"main": "./dist/extension.cjs",
"markdown": "github",
Expand All @@ -64,7 +65,8 @@
},
"activationEvents": [
"workspaceContains:./pico_sdk_import.cmake",
"onWebviewPanel:newPicoProject"
"onWebviewPanel:newPicoProject",
"onWebviewPanel:newPicoMicroPythonProject"
],
"contributes": {
"commands": [
Expand Down Expand Up @@ -273,6 +275,7 @@
"typescript-eslint": "^8.1.0"
},
"dependencies": {
"@vscode/python-extension": "^1.0.5",
"adm-zip": "^0.5.14 <0.5.15",
"ini": "^4.1.3",
"rimraf": "^5.0.7",
Expand Down
6 changes: 2 additions & 4 deletions src/utils/githubREST.mts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ export const PYENV_REPOSITORY_URL = "https://github.com/pyenv/pyenv.git";
export function ownerOfRepository(repository: GithubRepository): string {
switch (repository) {
case GithubRepository.picoSDK:
case GithubRepository.tools:
case GithubRepository.picotool:
return "raspberrypi";
case GithubRepository.cmake:
return "Kitware";
case GithubRepository.ninja:
return "ninja-build";
case GithubRepository.tools:
return "raspberrypi";
case GithubRepository.picotool:
return "raspberrypi";
}
}

Expand Down
85 changes: 60 additions & 25 deletions src/webview/newMicroPythonProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import which from "which";
import { existsSync } from "fs";
import { join } from "path";
import { PythonExtension } from "@vscode/python-extension";

interface SubmitMessageValue {
projectName: string;
Expand All @@ -37,10 +38,11 @@ export class NewMicroPythonProjectPanel {
private readonly _panel: WebviewPanel;
private readonly _extensionUri: Uri;
private readonly _settings: Settings;
private readonly _logger: Logger = new Logger("NewProjectPanel");
private readonly _logger: Logger = new Logger("NewMicroPythonProjectPanel");
private _disposables: Disposable[] = [];

private _projectRoot?: Uri;
private _pythonExtensionApi?: PythonExtension;

public static createOrShow(extensionUri: Uri, projectUri?: Uri): void {
const column = window.activeTextEditor
Expand Down Expand Up @@ -347,6 +349,9 @@ print("Finished.")\r\n`;
"web",
"raspberry-128.png"
);
if (!this._pythonExtensionApi) {
this._pythonExtensionApi = await PythonExtension.api();
}
const html = await this._getHtmlForWebview(this._panel.webview);

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

// TODO: maybe use python extension api
// TODO: check python version, workaround, ownly allow python3 commands on unix
// TODO: add support for onDidChangeActiveEnvironment and filter envs that don't directly point
// to an executable
const environments = this._pythonExtensionApi?.environments;
const knownEnvironments = environments?.known;
const activeEnv = environments?.getActiveEnvironmentPath();

// TODO: check python version, workaround, only allow python3 commands on unix
const isPythonSystemAvailable =
(await which("python3", { nothrow: true })) !== null ||
(await which("python", { nothrow: true })) !== null;
Expand Down Expand Up @@ -484,29 +494,54 @@ print("Finished.")\r\n`;
</div>
<div class="ms-6">
${
process.platform === "darwin" || process.platform === "win32"
? `
<div class="col-span-2">
<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Python Version:</label>
${
isPythonSystemAvailable
? `<div class="flex items-center mb-2" >
<input type="radio" id="python-radio-system-version" name="python-version-radio" value="1" class="mr-1 text-blue-500">
<label for="python-radio-system-version" class="text-gray-900 dark:text-white">Use system version</label>
</div>`
: ""
}
<div class="flex items-center mb-2">
<input type="radio" id="python-radio-path-executable" name="python-version-radio" value="2" class="mr-1 text-blue-500">
<label for="python-radio-path-executable" class="text-gray-900 dark:text-white">Path to executable:</label>
<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">
</div>
<div class="col-span-2">
<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Python Version:</label>
${
knownEnvironments && knownEnvironments.length > 0
? `
<div class="flex items-center mb-2">
<input type="radio" id="python-radio-known" name="python-version-radio" value="0" class="mr-1 text-blue-500">
<label for="python-radio-known" class="text-gray-900 dark:text-white">From Python extension</label>
<!-- show a select dropdown with python environments -->
<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">
${knownEnvironments
.map(
env =>
`<option value="${env.path}" ${
(activeEnv?.id ?? "") === env.id
? "selected"
: ""
}>${env.path}</option>`
)
.join("")}
</select>
</div>
`
: ""
}
${
process.platform === "darwin" ||
process.platform === "win32"
? `
${
isPythonSystemAvailable
? `<div class="flex items-center mb-2" >
<input type="radio" id="python-radio-system-version" name="python-version-radio" value="1" class="mr-1 text-blue-500">
<label for="python-radio-system-version" class="text-gray-900 dark:text-white">Use system version</label>
</div>`
: ""
}
: ""
}
<div class="flex items-center mb-2">
<input type="radio" id="python-radio-path-executable" name="python-version-radio" value="2" class="mr-1 text-blue-500">
<label for="python-radio-path-executable" class="text-gray-900 dark:text-white">Path to executable:</label>
<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">
</div>`
: ""
}
</div>
</div>
<div class="mt-6 mb-4 col-span-2">
Expand Down
12 changes: 7 additions & 5 deletions web/mpy/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ var submitted = false;
}
}
if (pythonVersionRadio.length == 0) {
// default to python mode 1 == System version
pythonMode = 1;
// default to python mode 0 == python ext version
pythonMode = 0;
}

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

return;
}
if (pythonMode == 2) {
if (pythonMode === 0) {
const pyenvKnownSel = document.getElementById("sel-pyenv-known");
pythonPath = pyenvKnownSel.value;
} else if (pythonMode === 2) {
const files = document.getElementById('python-path-executable').files;

if (files.length == 1) {
cmakePath = files[0].name;
pythonPath = files[0].name;
} else {
console.debug("Please select a valid python executable file");
vscode.postMessage({
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,13 @@ __metadata:
languageName: node
linkType: hard

"@vscode/python-extension@npm:^1.0.5":
version: 1.0.5
resolution: "@vscode/python-extension@npm:1.0.5"
checksum: 10/aeaa1ee9275f8679aa745c3096a68e1952328b8c7de90e1665f369e321dafb3e1271d42c32e4f8d6a3c92f6cdbb4aaef22cc45101764a623900efc4f3dfd3270
languageName: node
linkType: hard

"abbrev@npm:^1.0.0":
version: 1.1.1
resolution: "abbrev@npm:1.1.1"
Expand Down Expand Up @@ -2197,6 +2204,7 @@ __metadata:
"@types/uuid": "npm:^10.0.0"
"@types/vscode": "npm:^1.92.0"
"@types/which": "npm:^3.0.4"
"@vscode/python-extension": "npm:^1.0.5"
adm-zip: "npm:^0.5.14 <0.5.15"
eslint: "npm:^9.9.0"
eslint-config-prettier: "npm:^9.1.0"
Expand Down

0 comments on commit a4c7303

Please sign in to comment.