1616 * limitations under the License.
1717 */
1818import { join } from "path" ;
19- import * as idfConf from "../../idfConfiguration" ;
19+ import { readParameter , writeParameter } from "../../idfConfiguration" ;
2020import { readJSON } from "fs-extra" ;
2121import { Logger } from "../../logger/logger" ;
22- import { Uri } from "vscode" ;
22+ import { commands , ConfigurationTarget , l10n , Uri , window } from "vscode" ;
2323import { defaultBoards } from "./defaultBoards" ;
2424import { IdfToolsManager } from "../../idfToolsManager" ;
25+ import { getIdfTargetFromSdkconfig } from "../../workspaceConfig" ;
2526
2627export interface IdfBoard {
2728 name : string ;
@@ -31,15 +32,11 @@ export interface IdfBoard {
3132}
3233
3334export async function getOpenOcdScripts ( workspace : Uri ) : Promise < string > {
34- const idfPathDir = idfConf . readParameter (
35- "idf.espIdfPath" ,
36- workspace
37- ) as string ;
38- const toolsPath = idfConf . readParameter ( "idf.toolsPath" , workspace ) as string ;
39- const userExtraVars = idfConf . readParameter (
40- "idf.customExtraVars" ,
41- workspace
42- ) as { [ key : string ] : string } ;
35+ const idfPathDir = readParameter ( "idf.espIdfPath" , workspace ) as string ;
36+ const toolsPath = readParameter ( "idf.toolsPath" , workspace ) as string ;
37+ const userExtraVars = readParameter ( "idf.customExtraVars" , workspace ) as {
38+ [ key : string ] : string ;
39+ } ;
4340 const idfToolsManager = await IdfToolsManager . createIdfToolsManager (
4441 idfPathDir
4542 ) ;
@@ -113,3 +110,98 @@ export async function getBoards(
113110 return idfTarget ? filteredDefaultBoards : defaultBoards ;
114111 }
115112}
113+
114+ export async function selectOpenOcdConfigFiles (
115+ workspaceFolder : Uri ,
116+ idfTarget ?: string
117+ ) {
118+ try {
119+ const openOcdScriptsPath = await getOpenOcdScripts ( workspaceFolder ) ;
120+ if ( ! idfTarget ) {
121+ idfTarget = await getIdfTargetFromSdkconfig ( workspaceFolder ) ;
122+ if ( ! idfTarget ) {
123+ commands . executeCommand ( "espIdf.setTarget" ) ;
124+ return ;
125+ }
126+ }
127+ const currentOpenOcdConfigs = readParameter (
128+ "idf.openOcdConfigs" ,
129+ workspaceFolder
130+ ) as string [ ] ;
131+ const boards = await getBoards ( openOcdScriptsPath , idfTarget ) ;
132+ const choices = boards . map ( ( b ) => {
133+ return {
134+ description : `${ b . description } (${ b . configFiles } )` ,
135+ label : b . name ,
136+ target : b ,
137+ picked : currentOpenOcdConfigs
138+ . join ( "," )
139+ . includes ( b . configFiles . join ( "," ) ) ,
140+ } ;
141+ } ) ;
142+ const selectOpenOCdConfigsMsg = l10n . t (
143+ "Enter OpenOCD Configuration File Paths list"
144+ ) ;
145+ const boardQuickPick = window . createQuickPick < {
146+ description : string ;
147+ label : string ;
148+ target : IdfBoard ;
149+ picked : boolean ;
150+ } > ( ) ;
151+ boardQuickPick . items = choices ;
152+ boardQuickPick . placeholder = selectOpenOCdConfigsMsg ;
153+ boardQuickPick . onDidHide ( ( ) => {
154+ boardQuickPick . dispose ( ) ;
155+ } ) ;
156+ boardQuickPick . activeItems = boardQuickPick . items . filter (
157+ ( item ) => item . picked
158+ ) ;
159+
160+ return new Promise < void > ( ( resolve ) => {
161+ boardQuickPick . onDidHide ( ( ) => {
162+ boardQuickPick . dispose ( ) ;
163+ resolve ( ) ;
164+ } ) ;
165+ boardQuickPick . onDidAccept ( async ( ) => {
166+ const selectedBoard = boardQuickPick . selectedItems [ 0 ] ;
167+ if ( ! selectedBoard ) {
168+ Logger . infoNotify (
169+ `ESP-IDF board not selected. Remember to set the configuration files for OpenOCD with idf.openOcdConfigs`
170+ ) ;
171+ } else if ( selectedBoard && selectedBoard . target ) {
172+ if ( selectedBoard . label . indexOf ( "Custom board" ) !== - 1 ) {
173+ const inputBoard = await window . showInputBox ( {
174+ placeHolder : "Enter comma-separated configuration files" ,
175+ value : selectedBoard . target . configFiles . join ( "," ) ,
176+ } ) ;
177+ if ( inputBoard ) {
178+ selectedBoard . target . configFiles = inputBoard . split ( "," ) ;
179+ }
180+ }
181+ await writeParameter (
182+ "idf.openOcdConfigs" ,
183+ selectedBoard . target . configFiles ,
184+ ConfigurationTarget . WorkspaceFolder ,
185+ workspaceFolder
186+ ) ;
187+ Logger . infoNotify (
188+ l10n . t ( "OpenOCD Board configuration files are updated." )
189+ ) ;
190+ boardQuickPick . dispose ( ) ;
191+ resolve ( ) ;
192+ }
193+ boardQuickPick . hide ( ) ;
194+ } ) ;
195+ boardQuickPick . show ( ) ;
196+ } ) ;
197+ } catch ( error ) {
198+ const errMsg =
199+ error . message || "Failed to select openOCD configuration files" ;
200+ Logger . errorNotify (
201+ errMsg ,
202+ error ,
203+ "boardConfiguration selectOpenOcdConfigFiles"
204+ ) ;
205+ return ;
206+ }
207+ }
0 commit comments