Skip to content

Commit

Permalink
Switch to Python extension as dependency for python env detection
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <44974737+paulober@users.noreply.github.com>
  • Loading branch information
paulober committed Aug 30, 2024
1 parent 2e62457 commit bde71ce
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
21 changes: 16 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"description": "Why would you want to use MicroPico in an untrusted workspace?"
}
},
"extensionDependencies": [
"ms-python.python"
],
"activationEvents": [
"workspaceContains:.picowgo",
"workspaceContains:.micropico",
Expand Down Expand Up @@ -608,7 +611,8 @@
"typescript-eslint": "^8.3.0"
},
"dependencies": {
"@paulober/pico-mpy-com": "^0.0.4",
"@paulober/pico-mpy-com": "^1.0.0",
"@vscode/python-extension": "^1.0.5",
"axios": "^1.7.5",
"fs-extra": "^11.2.0",
"lodash": "^4.17.21",
Expand Down
58 changes: 50 additions & 8 deletions src/activator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import {
PicoMpyCom,
PicoSerialEvents,
} from "@paulober/pico-mpy-com";
import {
type ActiveEnvironmentPathChangeEvent,
PythonExtension,
} from "@vscode/python-extension";

/*const pkg: {} | undefined = vscode.extensions.getExtension("paulober.pico-w-go")
?.packageJSON as object;*/
Expand All @@ -45,6 +49,7 @@ export default class Activator {
private stubs?: Stubs;
private picoFs?: PicoWFs;
private terminal?: Terminal;
private pythonPath?: string;

private autoConnectTimer?: NodeJS.Timeout;
private comDevice?: string;
Expand All @@ -59,6 +64,19 @@ export default class Activator {
// TODO: maybe store the PicoMpyCom.getInstance() in a class variable
const settings = new Settings(context.workspaceState);

// get the python env to be used
const pythonApi = await PythonExtension.api();

context.subscriptions.push(
pythonApi.environments.onDidChangeActiveEnvironmentPath(
(e: ActiveEnvironmentPathChangeEvent) => {
this.pythonPath = e.path;
}
)
);
// get currently selected environment
this.pythonPath = pythonApi.environments.getActiveEnvironmentPath()?.path;

// execute async not await
void vscode.commands.executeCommand(
"setContext",
Expand Down Expand Up @@ -115,7 +133,7 @@ export default class Activator {
"\x1b[38;2;255;165;0m" + // Set text color to orange (RGB: 255, 165, 0)
"Failed to get MicroPython version and machine type.\r\n" +
"Waiting for board to connect...\r\n" +
"\x1b[0m" // Reset text color to default
"\x1b[0m\r\n" // Reset text color to default
);
});
let commandExecuting = false;
Expand All @@ -126,6 +144,12 @@ export default class Activator {
return;
}

if (!this.pythonPath) {
this.showNoActivePythonError();

return;
}

// TODO: maybe this.ui?.userOperationStarted();
// this will make waiting for prompt falsethis.terminal.freeze();
commandExecuting = true;
Expand All @@ -140,14 +164,13 @@ export default class Activator {
this.terminal?.write(data.toString("utf-8"));
}
},
// TODO: proper python detection
process.platform === "win32" ? "python" : "python3"
this.pythonPath
);
if (result.type !== OperationResultType.commandResult || !result.result) {
// write red text into terminal
this.terminal?.write("\x1b[31mException occured\x1b[0m\r\n");
this.terminal?.write("\r\n");
}
this.terminal?.write("\r\n");
commandExecuting = false;
this.terminal?.prompt();
});
Expand Down Expand Up @@ -391,6 +414,12 @@ export default class Activator {
return;
}

if (!this.pythonPath) {
this.showNoActivePythonError();

return;
}

const file =
(fileOverride !== undefined && typeof fileOverride === "string"
? fileOverride
Expand Down Expand Up @@ -429,8 +458,7 @@ export default class Activator {
this.terminal?.write(data.toString("utf-8"));
}
},
// TODO: better python detection
process.platform === "win32" ? "python" : "python3"
this.pythonPath
);
this.ui?.userOperationStopped();
commandExecuting = false;
Expand All @@ -453,6 +481,12 @@ export default class Activator {
return;
}

if (!this.pythonPath) {
this.showNoActivePythonError();

return;
}

const code = getSelectedCodeOrLine();

if (code === undefined) {
Expand All @@ -479,8 +513,7 @@ export default class Activator {
this.terminal?.write(data.toString("utf-8"));
}
},
// TODO: better python detection
process.platform === "win32" ? "python" : "python3"
this.pythonPath
);
commandExecuting = false;
this.ui?.userOperationStopped();
Expand Down Expand Up @@ -1457,6 +1490,15 @@ export default class Activator {
);*/
}

private showNoActivePythonError(): void {
// TODO: add details button taking them to the python extension docs
void vscode.window.showWarningMessage(
"Python path not found. Please check your Python environment.\n" +
"See the Python extension for instructions on how to select " +
"a Python interpreter."
);
}

private pyboardOnError(data: Buffer | undefined): void {
if (
data === undefined &&
Expand Down

0 comments on commit bde71ce

Please sign in to comment.