Skip to content

Commit 5add077

Browse files
authored
Rework ManimGL version detection (#121)
* Rethink manim version determination & make non-blocking * Add "Try again" option during "feature not available" * Add option to view latest release if user version not the latest * Add docstrings to constants * Add option to open walkthrough if version could not be determined * Check for early extension activation in testRunner * Fix redetection of version * Use `python` instead of `python.exe` * Use await syntax & early returns
1 parent ad76016 commit 5add077

File tree

7 files changed

+182
-240
lines changed

7 files changed

+182
-240
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"pycache",
113113
"Pyglet",
114114
"pyperclip",
115+
"Redetects",
115116
"Sanderson",
116117
"Sanderson's",
117118
"setuptools",

src/extension.ts

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,13 @@ import { exportScene } from "./export";
88
import { Logger, Window, LogRecorder } from "./logger";
99
import { registerWalkthroughCommands } from "./walkthrough/commands";
1010
import { ExportSceneCodeLens } from "./export";
11-
import { tryToDetermineManimVersion, LAST_WARNING_NO_VERSION_KEY } from "./manimVersion";
11+
import { determineManimVersion } from "./manimVersion";
1212
import { setupTestEnvironment } from "./utils/testing";
1313
import { EventEmitter } from "events";
1414
import { applyWindowsPastePatch } from "./patches/applyPatches";
1515
import { getBinaryPathInPythonEnv } from "./utils/venv";
1616

1717
export let manimNotebookContext: vscode.ExtensionContext;
18-
class WaitingForPythonExtensionCancelled extends Error {}
19-
20-
/**
21-
* Resets the global state of the extension.
22-
* @param context The extension context.
23-
*/
24-
function restoreGlobalState(context: vscode.ExtensionContext) {
25-
const globalState = context.globalState;
26-
globalState.update(LAST_WARNING_NO_VERSION_KEY, 0);
27-
}
2818

2919
export async function activate(context: vscode.ExtensionContext) {
3020
if (process.env.IS_TESTING === "true") {
@@ -36,14 +26,12 @@ export async function activate(context: vscode.ExtensionContext) {
3626
}
3727

3828
manimNotebookContext = context;
39-
restoreGlobalState(context);
4029

4130
// Trigger the Manim shell to start listening to the terminal
4231
ManimShell.instance;
4332

4433
// Register the open walkthrough command earlier, so that it can be used
45-
// even while the Python extension is loading and the ManimGL version is
46-
// being detected.
34+
// even while other activation tasks are still running
4735
const openWalkthroughCommand = vscode.commands.registerCommand(
4836
"manim-notebook.openWalkthrough", async () => {
4937
Logger.info("💠 Command requested: Open Walkthrough");
@@ -52,31 +40,27 @@ export async function activate(context: vscode.ExtensionContext) {
5240
});
5341
context.subscriptions.push(openWalkthroughCommand);
5442

55-
let pythonEnvPath: string | undefined = undefined;
43+
let pythonBinary: string;
5644
try {
57-
pythonEnvPath = await waitForPythonExtension();
58-
} catch (err) {
59-
if (err instanceof WaitingForPythonExtensionCancelled) {
60-
Logger.info("💠 Waiting for Python extension cancelled, therefore"
61-
+ " we cancel the extension activation");
62-
return;
63-
}
64-
}
45+
waitForPythonExtension().then((pythonEnvPath: string | undefined) => {
46+
// (These tasks here can be performed in the background)
6547

66-
if (process.platform === "win32") {
67-
// Note that we shouldn't call `python3` on Windows,
68-
// see https://github.com/Manim-Notebook/manim-notebook/pull/117#discussion_r1932764875
69-
const pythonPath = pythonEnvPath
70-
? getBinaryPathInPythonEnv(pythonEnvPath, "python.exe")
71-
: "python";
72-
// not necessary to await here, can run in background
73-
applyWindowsPastePatch(context, pythonPath);
74-
}
48+
// also see https://github.com/Manim-Notebook/manim-notebook/pull/117#discussion_r1932764875
49+
const pythonBin = process.platform === "win32" ? "python" : "python3";
50+
pythonBinary = pythonEnvPath
51+
? getBinaryPathInPythonEnv(pythonEnvPath, pythonBin)
52+
: pythonBin;
7553

76-
const manimglBinary = pythonEnvPath
77-
? getBinaryPathInPythonEnv(pythonEnvPath, "manimgl")
78-
: "manimgl";
79-
await tryToDetermineManimVersion(manimglBinary);
54+
if (process.platform === "win32") {
55+
applyWindowsPastePatch(context, pythonBinary);
56+
}
57+
58+
determineManimVersion(pythonBinary);
59+
});
60+
} catch (err) {
61+
Logger.error("Error in background activation processing"
62+
+ ` (python extension waiting, windows paste patch or manim version check): ${err}`);
63+
}
8064

8165
const previewManimCellCommand = vscode.commands.registerCommand(
8266
"manim-notebook.previewManimCell", (cellCode?: string, startLine?: number) => {
@@ -146,7 +130,11 @@ export async function activate(context: vscode.ExtensionContext) {
146130
const redetectManimVersionCommand = vscode.commands.registerCommand(
147131
"manim-notebook.redetectManimVersion", async () => {
148132
Logger.info("💠 Command requested: Redetect Manim Version");
149-
await tryToDetermineManimVersion("manimgl");
133+
if (!pythonBinary) {
134+
Window.showWarningMessage("Please wait for Manim Notebook to have finished activating.");
135+
return;
136+
}
137+
await determineManimVersion(pythonBinary);
150138
});
151139

152140
registerWalkthroughCommands(context);
@@ -176,44 +164,31 @@ export async function activate(context: vscode.ExtensionContext) {
176164
* installed.
177165
*
178166
* @returns The path to the Python environment, if it is available.
179-
* @throws {WaitingForPythonExtensionCancelled} If the user cancels the
180-
* waiting process.
181167
*/
182168
async function waitForPythonExtension(): Promise<string | undefined> {
183169
const pythonExtension = vscode.extensions.getExtension("ms-python.python");
184170
if (!pythonExtension) {
185-
Logger.info("💠 Python extension not installed, skip waiting for it");
171+
Logger.info("💠 Python extension not installed");
186172
return;
187173
}
188174

189-
const progressOptions = {
190-
location: vscode.ProgressLocation.Notification,
191-
title: "Waiting for Python extension to be fully loaded...",
192-
cancellable: true,
193-
};
194-
195-
return await window.withProgress(progressOptions, async (progress, token) => {
196-
token.onCancellationRequested(() => {
197-
Window.showInformationMessage("Manim Notebook activation cancelled."
198-
+ " Open any Python file to again activate the extension.");
199-
throw new WaitingForPythonExtensionCancelled();
200-
});
175+
// Waiting for Python extension
176+
const pythonApi = await pythonExtension.activate();
177+
Logger.info("💠 Python extension activated");
201178

202-
// Waiting for Python extension
203-
const pythonApi = await pythonExtension.activate();
204-
Logger.info("💠 Python extension activated");
179+
// Path to venv
180+
const environmentPath = pythonApi.environments.getActiveEnvironmentPath();
181+
if (!environmentPath) {
182+
Logger.debug("No active environment path found");
183+
return;
184+
}
185+
const environment = await pythonApi.environments.resolveEnvironment(environmentPath);
186+
if (!environment) {
187+
Logger.debug("Environment could not be resolved");
188+
return;
189+
}
205190

206-
// Path to venv
207-
const environmentPath = pythonApi.environments.getActiveEnvironmentPath();
208-
if (!environmentPath) {
209-
return;
210-
}
211-
const environment = await pythonApi.environments.resolveEnvironment(environmentPath);
212-
if (!environment) {
213-
return;
214-
}
215-
return environment.path;
216-
});
191+
return environment.path;
217192
}
218193

219194
/**

0 commit comments

Comments
 (0)