|
| 1 | +/* |
| 2 | + * Project: ESP-IDF VSCode Extension |
| 3 | + * File Created: Friday, 8th January 2021 5:34:24 pm |
| 4 | + * Copyright 2021 Espressif Systems (Shanghai) CO LTD |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import * as vscode from "vscode"; |
| 20 | +import * as fs from "fs" |
| 21 | +import { join } from "path"; |
| 22 | +import * as idfConf from "../../idfConfiguration"; |
| 23 | +import { Logger } from "../../logger/logger"; |
| 24 | +import { getVirtualEnvPythonPath } from "../../pythonManager"; |
| 25 | +import { OpenOCDManager } from "../openOcd/openOcdManager"; |
| 26 | +import { appendIdfAndToolsToPath, execChildProcess } from "../../utils"; |
| 27 | +import { OutputChannel } from "../../logger/outputChannel"; |
| 28 | +import { getOpenOcdScripts } from "../openOcd/boardConfiguration"; |
| 29 | + |
| 30 | +export class DevkitsCommand { |
| 31 | + private workspaceRoot: vscode.Uri; |
| 32 | + |
| 33 | + constructor(workspaceRoot: vscode.Uri) { |
| 34 | + this.workspaceRoot = workspaceRoot; |
| 35 | + } |
| 36 | + |
| 37 | + public async runDevkitsScript(): Promise<string> { |
| 38 | + try { |
| 39 | + const workspaceFolder = vscode.workspace.getWorkspaceFolder( |
| 40 | + this.workspaceRoot |
| 41 | + ); |
| 42 | + if (!workspaceFolder) { |
| 43 | + throw new Error("No workspace folder found"); |
| 44 | + } |
| 45 | + |
| 46 | + const toolsPath = idfConf.readParameter( |
| 47 | + "idf.toolsPath", |
| 48 | + this.workspaceRoot |
| 49 | + ) as string; |
| 50 | + const openOCDManager = OpenOCDManager.init(); |
| 51 | + const openOCDVersion = await openOCDManager.version(); |
| 52 | + |
| 53 | + if (!toolsPath || !openOCDVersion) { |
| 54 | + throw new Error("Could not get toolsPath or OpenOCD version"); |
| 55 | + } |
| 56 | + |
| 57 | + const scriptPath = join( |
| 58 | + toolsPath, |
| 59 | + "tools", |
| 60 | + "openocd-esp32", |
| 61 | + openOCDVersion, |
| 62 | + "openocd-esp32", |
| 63 | + "share", |
| 64 | + "openocd", |
| 65 | + "espressif", |
| 66 | + "tools", |
| 67 | + "esp_detect_config.py" |
| 68 | + ); |
| 69 | + |
| 70 | + const openOcdScriptsPath = await getOpenOcdScripts(this.workspaceRoot); |
| 71 | + if (!openOcdScriptsPath) { |
| 72 | + throw new Error("Could not get OpenOCD scripts path"); |
| 73 | + } |
| 74 | + |
| 75 | + const espConfigPath = join(openOcdScriptsPath, "esp-config.json"); |
| 76 | + |
| 77 | + const notificationMode = idfConf.readParameter( |
| 78 | + "idf.notificationMode", |
| 79 | + this.workspaceRoot |
| 80 | + ) as string; |
| 81 | + |
| 82 | + const ProgressLocation = |
| 83 | + notificationMode === idfConf.NotificationMode.All || |
| 84 | + notificationMode === idfConf.NotificationMode.Notifications |
| 85 | + ? vscode.ProgressLocation.Notification |
| 86 | + : vscode.ProgressLocation.Window; |
| 87 | + |
| 88 | + const pythonBinPath = await getVirtualEnvPythonPath(this.workspaceRoot); |
| 89 | + const modifiedEnv = await appendIdfAndToolsToPath(this.workspaceRoot); |
| 90 | + |
| 91 | + OutputChannel.init(); |
| 92 | + OutputChannel.appendLine( |
| 93 | + "Running ESP Detect Config...", |
| 94 | + "ESP Detect Config" |
| 95 | + ); |
| 96 | + OutputChannel.show(); |
| 97 | + |
| 98 | + return await vscode.window.withProgress( |
| 99 | + { |
| 100 | + cancellable: true, |
| 101 | + location: ProgressLocation, |
| 102 | + title: "ESP-IDF: Running ESP Detect Config", |
| 103 | + }, |
| 104 | + async ( |
| 105 | + progress: vscode.Progress<{ message: string; increment: number }>, |
| 106 | + cancelToken: vscode.CancellationToken |
| 107 | + ) => { |
| 108 | + try { |
| 109 | + const result = await execChildProcess( |
| 110 | + pythonBinPath, |
| 111 | + [scriptPath, "--esp-config", espConfigPath], |
| 112 | + this.workspaceRoot.fsPath, |
| 113 | + OutputChannel.init(), |
| 114 | + { env: modifiedEnv }, |
| 115 | + cancelToken |
| 116 | + ); |
| 117 | + |
| 118 | + OutputChannel.appendLine(result, "ESP Detect Config"); |
| 119 | + OutputChannel.show(); |
| 120 | + vscode.window.showInformationMessage("ESP Detect Config completed"); |
| 121 | + return result; |
| 122 | + } catch (error) { |
| 123 | + const msg = error.message |
| 124 | + ? error.message |
| 125 | + : "Error running ESP Detect Config"; |
| 126 | + Logger.errorNotify(msg, error, "DevkitsCommand"); |
| 127 | + OutputChannel.appendLine(msg, "ESP Detect Config"); |
| 128 | + OutputChannel.show(); |
| 129 | + throw error; |
| 130 | + } |
| 131 | + } |
| 132 | + ); |
| 133 | + } catch (error) { |
| 134 | + const msg = error.message |
| 135 | + ? error.message |
| 136 | + : "Error running ESP Detect Config"; |
| 137 | + Logger.errorNotify(msg, error, "DevkitsCommand"); |
| 138 | + OutputChannel.appendLine(msg, "ESP Detect Config"); |
| 139 | + OutputChannel.show(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public async getScriptPath(): Promise<string | null> { |
| 144 | + try { |
| 145 | + const toolsPath = idfConf.readParameter( |
| 146 | + "idf.toolsPath", |
| 147 | + this.workspaceRoot |
| 148 | + ) as string; |
| 149 | + const openOCDManager = OpenOCDManager.init(); |
| 150 | + const openOCDVersion = await openOCDManager.version(); |
| 151 | + |
| 152 | + if (!toolsPath || !openOCDVersion) { |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + const scriptPath = join( |
| 157 | + toolsPath, |
| 158 | + "tools", |
| 159 | + "openocd-esp32", |
| 160 | + openOCDVersion, |
| 161 | + "openocd-esp32", |
| 162 | + "share", |
| 163 | + "openocd", |
| 164 | + "espressif", |
| 165 | + "tools", |
| 166 | + "esp_detect_config.py" |
| 167 | + ); |
| 168 | + |
| 169 | + return fs.existsSync(scriptPath) ? scriptPath : null; |
| 170 | + } catch (error) { |
| 171 | + return null; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments