From 32995c326e6b8584b416629071d465b55f91edcd Mon Sep 17 00:00:00 2001 From: Christella Cidolit Date: Wed, 21 Aug 2019 15:51:00 -0700 Subject: [PATCH 1/3] Checking if the config file was created to prevent crashing the extension on activation without a folder --- locales/en/out/constants.i18n.json | 1 + src/constants.ts | 6 +- src/extension.ts | 138 +++++++++++++++++++++-------- 3 files changed, 107 insertions(+), 38 deletions(-) diff --git a/locales/en/out/constants.i18n.json b/locales/en/out/constants.i18n.json index d438b129e..2b958c85d 100644 --- a/locales/en/out/constants.i18n.json +++ b/locales/en/out/constants.i18n.json @@ -15,6 +15,7 @@ "error.invalidFileNameDebug": "The file you tried to debug isn\\'t named \"code.py\" or \"main.py\\. Rename your file if you want your code to work on your actual device.", "error.noDevice": "No plugged in boards detected. Please double check if your board is connected and/or properly formatted", "error.noFileToRun": "\n[ERROR] We can't find the .py file to run on simulator. Open up a new .py file, or browse through some examples\n", + "error.noFolderCreated": "In order to use the Serial Monitor, you need to open a folder and reload VS Code.", "error.noProgramFoundDebug": "Cannot find a program to debug.", "error.noPythonPath": "We found that you don't have Python 3 installed on your computer, please install the latest version, add it to your PATH and try again.", "error.stderr": "\n[ERROR] {0} \n", diff --git a/src/constants.ts b/src/constants.ts index e946ce048..a8d496fd7 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -72,6 +72,10 @@ export const CONSTANTS = { "error.noFileToRun", '[ERROR] We can\'t find a Python file to run. Please make sure you select or open a new ".py" code file, or use the "New File" command to get started and see useful links.\n' ), + NO_FOLDER_OPENED: localize( + "error.noFolderCreated", + "In order to use the Serial Monitor, you need to open a folder and reload VS Code." + ), NO_PROGRAM_FOUND_DEBUG: localize( "error.noProgramFoundDebug", "Cannot find a program to debug." @@ -218,7 +222,7 @@ export const CONSTANTS = { ), SERIAL_PORT_NOT_STARTED: localize( "warning.serialPortNotStarted", - "Serial port has not been started." + "Serial port has not been started.\n" ) } }; diff --git a/src/extension.ts b/src/extension.ts index 5e620a585..214c21d49 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -26,6 +26,7 @@ let currentFileAbsPath: string = ""; let currentTextDocument: vscode.TextDocument; let telemetryAI: TelemetryAI; let pythonExecutableName: string = "python"; +let configFileCreated: boolean = false; let inDebugMode: boolean = false; let debuggerCommunicationHandler: DebuggerCommunicationServer; // Notification booleans @@ -41,13 +42,16 @@ function loadScript(context: vscode.ExtensionContext, scriptPath: string) { .toString()}">`; } -const setPathAndSendMessage = (currentPanel: vscode.WebviewPanel, newFilePath: string) => { +const setPathAndSendMessage = ( + currentPanel: vscode.WebviewPanel, + newFilePath: string +) => { currentFileAbsPath = newFilePath; currentPanel.webview.postMessage({ command: "current-file", state: { running_file: newFilePath } }); -} +}; // Extension activation export async function activate(context: vscode.ExtensionContext) { @@ -63,7 +67,14 @@ export async function activate(context: vscode.ExtensionContext) { updatePythonExtraPaths(); // Generate cpx.json - utils.generateCPXConfig(); + try { + utils.generateCPXConfig(); + configFileCreated = true; + } catch (err) { + console.info("Failed to create the CPX config file."); + configFileCreated = false; + } + pythonExecutableName = await utils.setPythonExectuableName(); if (pythonExecutableName === "") { @@ -75,9 +86,11 @@ export async function activate(context: vscode.ExtensionContext) { utils.logToOutputChannel(outChannel, CONSTANTS.INFO.WELCOME_OUTPUT_TAB); } - vscode.workspace.onDidSaveTextDocument(async (document: vscode.TextDocument) => { - await updateCurrentFileIfPython(document, currentPanel); - }); + vscode.workspace.onDidSaveTextDocument( + async (document: vscode.TextDocument) => { + await updateCurrentFileIfPython(document, currentPanel); + } + ); const openWebview = () => { if (currentPanel) { @@ -133,14 +146,21 @@ export async function activate(context: vscode.ExtensionContext) { case WebviewMessages.PLAY_SIMULATOR: console.log(`Play button ${messageJson} \n`); if (message.text.state as boolean) { - setPathAndSendMessage(currentPanel, message.text.selected_file); + setPathAndSendMessage( + currentPanel, + message.text.selected_file + ); if (currentFileAbsPath) { - const foundDocument = utils.getActiveEditorFromPath(currentFileAbsPath); + const foundDocument = utils.getActiveEditorFromPath( + currentFileAbsPath + ); if (foundDocument !== undefined) { currentTextDocument = foundDocument; } } - telemetryAI.trackFeatureUsage(TelemetryEventName.COMMAND_RUN_SIMULATOR_BUTTON); + telemetryAI.trackFeatureUsage( + TelemetryEventName.COMMAND_RUN_SIMULATOR_BUTTON + ); runSimulatorCommand(); } else { killProcessIfRunning(); @@ -173,14 +193,19 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions ); - activeEditorListener = utils.addVisibleTextEditorCallback(currentPanel, context); + activeEditorListener = utils.addVisibleTextEditorCallback( + currentPanel, + context + ); console.log("sent"); } currentPanel.onDidDispose( () => { currentPanel = undefined; - debuggerCommunicationHandler.setWebview(undefined); + if (debuggerCommunicationHandler) { + debuggerCommunicationHandler.setWebview(undefined); + } killProcessIfRunning(); if (firstTimeClosed) { vscode.window.showInformationMessage( @@ -324,15 +349,21 @@ export async function activate(context: vscode.ExtensionContext) { killProcessIfRunning(); - await updateCurrentFileIfPython(vscode.window.activeTextEditor!.document, currentPanel); + await updateCurrentFileIfPython( + vscode.window.activeTextEditor!.document, + currentPanel + ); if (currentFileAbsPath === "") { - utils.logToOutputChannel(outChannel, CONSTANTS.ERROR.NO_FILE_TO_RUN, true); - vscode.window - .showErrorMessage( - CONSTANTS.ERROR.NO_FILE_TO_RUN, - DialogResponses.MESSAGE_UNDERSTOOD - ) + utils.logToOutputChannel( + outChannel, + CONSTANTS.ERROR.NO_FILE_TO_RUN, + true + ); + vscode.window.showErrorMessage( + CONSTANTS.ERROR.NO_FILE_TO_RUN, + DialogResponses.MESSAGE_UNDERSTOOD + ); } else { // Save on run await currentTextDocument.save(); @@ -409,7 +440,7 @@ export async function activate(context: vscode.ExtensionContext) { case "print": console.log( `Process print statement output = ${ - messageToWebview.data + messageToWebview.data }` ); utils.logToOutputChannel( @@ -476,15 +507,21 @@ export async function activate(context: vscode.ExtensionContext) { utils.logToOutputChannel(outChannel, CONSTANTS.INFO.DEPLOY_DEVICE, true); - await updateCurrentFileIfPython(vscode.window.activeTextEditor!.document, currentPanel); + await updateCurrentFileIfPython( + vscode.window.activeTextEditor!.document, + currentPanel + ); if (currentFileAbsPath === "") { - utils.logToOutputChannel(outChannel, CONSTANTS.ERROR.NO_FILE_TO_RUN, true); - vscode.window - .showErrorMessage( - CONSTANTS.ERROR.NO_FILE_TO_RUN, - DialogResponses.MESSAGE_UNDERSTOOD - ); + utils.logToOutputChannel( + outChannel, + CONSTANTS.ERROR.NO_FILE_TO_RUN, + true + ); + vscode.window.showErrorMessage( + CONSTANTS.ERROR.NO_FILE_TO_RUN, + DialogResponses.MESSAGE_UNDERSTOOD + ); } else if (!utils.validCodeFileName(currentFileAbsPath)) { // Save on run await currentTextDocument.save(); @@ -595,34 +632,57 @@ export async function activate(context: vscode.ExtensionContext) { } ); - const serialMonitor: SerialMonitor = SerialMonitor.getInstance(); - context.subscriptions.push(serialMonitor); + let serialMonitor: SerialMonitor | undefined; + if (configFileCreated) { + serialMonitor = SerialMonitor.getInstance(); + context.subscriptions.push(serialMonitor); + } + const selectSerialPort: vscode.Disposable = vscode.commands.registerCommand( "pacifica.selectSerialPort", () => { - // todo add telemetry after - serialMonitor.selectSerialPort(null, null); + if (serialMonitor) { + serialMonitor.selectSerialPort(null, null); + } else { + vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED); + console.info("Serial monitor is not defined."); + } } ); const openSerialMonitor: vscode.Disposable = vscode.commands.registerCommand( "pacifica.openSerialMonitor", () => { - serialMonitor.openSerialMonitor(); + if (serialMonitor) { + serialMonitor.openSerialMonitor(); + } else { + vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED); + console.info("Serial monitor is not defined."); + } } ); const changeBaudRate: vscode.Disposable = vscode.commands.registerCommand( "pacifica.changeBaudRate", () => { - serialMonitor.changeBaudRate(); + if (serialMonitor) { + serialMonitor.changeBaudRate(); + } else { + vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED); + console.info("Serial monitor is not defined."); + } } ); const closeSerialMonitor: vscode.Disposable = vscode.commands.registerCommand( "pacifica.closeSerialMonitor", (port, showWarning = true) => { - serialMonitor.closeSerialMonitor(port, showWarning); + if (serialMonitor) { + serialMonitor.closeSerialMonitor(port, showWarning); + } else { + vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED); + console.info("Serial monitor is not defined."); + } } ); @@ -739,11 +799,15 @@ const updateCurrentFileIfPython = async ( setPathAndSendMessage(currentPanel, activeTextDocument.fileName); currentTextDocument = activeTextDocument; } else if (currentFileAbsPath === "") { - setPathAndSendMessage(currentPanel, - getActivePythonFile() || ""); + setPathAndSendMessage(currentPanel, getActivePythonFile() || ""); } - if (utils.getActiveEditorFromPath(currentTextDocument.fileName) === undefined) { - await vscode.window.showTextDocument(currentTextDocument, vscode.ViewColumn.One); + if ( + utils.getActiveEditorFromPath(currentTextDocument.fileName) === undefined + ) { + await vscode.window.showTextDocument( + currentTextDocument, + vscode.ViewColumn.One + ); } }; From 2663cec06c313af0c1aae2fa7203a3c8022a67d8 Mon Sep 17 00:00:00 2001 From: Christella Cidolit Date: Wed, 21 Aug 2019 16:38:39 -0700 Subject: [PATCH 2/3] Updating doc for serial monitor limitation --- docs/how-to-use.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/docs/how-to-use.md b/docs/how-to-use.md index ee1b2405d..4a925c5ae 100644 --- a/docs/how-to-use.md +++ b/docs/how-to-use.md @@ -3,7 +3,7 @@ Commands are accessible through : - **The command palette** (`Ctrl+shift+P` or `View->Command Palette`) and type 'Pacifica : `command_name`' -- **The extension buttons** available on the top right of the Text Editor Panel when you have a Python file open\* +- **The extension buttons** available on the top right of the Text Editor Panel when you have a Python file open (1) ## Available commands @@ -18,15 +18,15 @@ Commands are accessible through : - **Deploy to Device** : saves the code to a Circuit Playground Express. _(**Note :** the board needs to be correctly formatted to a `CIRCUITPY` drive first if it's not the case : [Installing CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython/installing-circuitpython))_. -- **Select Serial Port** : selects the serial port of the board you want the serial monitor to interact with. +- **Select Serial Port** : selects the serial port of the board you want the serial monitor to interact with. (2) _(**Note :** USB detection must be enabled in the extension settings.)_ -- **Open Serial Monitor** : opens the serial monitor. +- **Open Serial Monitor** : opens the serial monitor. (2) _(**Note :** A serial port must have been selected already)_. -- **Change Baud Rate** : changes the baud rate of the serial monitor. +- **Change Baud Rate** : changes the baud rate of the serial monitor. (2) -- **Close Serial Montitor** : closes the serial monitor. +- **Close Serial Montitor** : closes the serial monitor. (2) ## Available features @@ -56,8 +56,8 @@ Commands are accessible through : - Device's features - Sound sensor - Tones - - Sound detection\*\* - - IR transmitter\*\* + - Sound detection (3) + - IR transmitter (3) - Motion sensors - Tap detection @@ -65,7 +65,7 @@ Commands are accessible through : Here are the settings you can change in the Pacifica configuration: -- **Debugger Server Port:** allows you to change the port used to communicate with the debugger. Default value is _5577_. +- **Debugger Server Port:** allows you to change the port used to communicate with the debugger. Default value is _5577_. (4) - **Enable USBDetection:** when disabled, prevents the serial monitor from listening to messages from the serial port. @@ -84,8 +84,9 @@ Here are the settings you can change in the Pacifica configuration: - If you try to deploy to the device while it's plugged in but you still get an error saying it cannot find the board, make sure your Circuit Playground Express is formatted correctly and that its name matches `CIRCUITPY`. - If you can't get the Simulator communication working while debugging, try to open you `Settings` and check the port used under `'Pacifica: Debugger Server Port'`. You can either change it (usually ports above 5000 could work) or try to free it, then start debugging again. -### Note +### Notes -\* Can be changed in settings. -\*\* Sensors currently not supported by the official adafruit_circuit_playground.express library (v2.1.2). -\*\*\* The regular communication is using the stdout and stdin of the Python process. But when you debug your code, it will communicate over sockets on port 5577. This is the default port that you can change in your `Settings` : `'Pacifica: Debugger Server Port'`. +(1) Can be changed in settings. +(2) To use the Serial Monitor commands, you'll need to open a folder because this saves the configuration file for the serial communication. You can still use the rest of the extension without opening a folder. +(3) Sensors currently not supported by the official adafruit_circuit_playground.express library (v2.1.2). +(4) The regular communication is using the stdout and stdin of the Python process. But when you debug your code, it will communicate over sockets on port 5577. This is the default port that you can change in your `Settings` : `'Pacifica: Debugger Server Port'`. From fe984febb62cf4093439fbe54ca4db19800cf084 Mon Sep 17 00:00:00 2001 From: Christella Cidolit Date: Thu, 22 Aug 2019 09:55:36 -0700 Subject: [PATCH 3/3] Update doc for dependency --- docs/how-to-use.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/how-to-use.md b/docs/how-to-use.md index 4a925c5ae..a4f3c68ba 100644 --- a/docs/how-to-use.md +++ b/docs/how-to-use.md @@ -69,11 +69,13 @@ Here are the settings you can change in the Pacifica configuration: - **Enable USBDetection:** when disabled, prevents the serial monitor from listening to messages from the serial port. -- **Show Device Icon In Editor Title Menu:** allows you to chose whether the _`Deploy to Device`_ button should be in the editor title. +- **Show Device Icon In Editor Title Menu:** allows you to choose whether the _`Deploy to Device`_ button should be in the editor title. -- **Show Open Icon In Editor Title Menu:** allows you to chose whether the _`Open Simulator`_ button should be in the editor title. +- **Show Open Icon In Editor Title Menu:** allows you to choose whether the _`Open Simulator`_ button should be in the editor title. -- **Show Simulator Icon In Editor Title Menu:** allows you to chose whether the _`Run Simulator`_ button should be in the editor title. +- **Show Simulator Icon In Editor Title Menu:** allows you to choose whether the _`Run Simulator`_ button should be in the editor title. + +- **Show Dependency Install:** allows you to choose whether you want to be prompted to install the Python dependencies. ## Troubleshooting Tips