import * as path from "path" ; import * as vscode from "vscode" ; import { OperationCanceledError } from "./common/Error/OperationCanceledError" ; import { ConfigHandler } from "./configHandler" ; import { ConfigKey , OSPlatform } from "./constants" ; import { PickWithData } from "./Models/Interfaces/UI" ; import { getHomeDir , getPlatform } from "./utils" ; export class IoTWorkbenchSettings { private workbenchPath = "" ; private static instance : IoTWorkbenchSettings | undefined ; private constructor ( ) { } static async getInstance ( ) { if ( ! IoTWorkbenchSettings . instance ) { IoTWorkbenchSettings . instance = new IoTWorkbenchSettings ( ) ; IoTWorkbenchSettings . instance . workbenchPath = ConfigHandler . get < string > ( ConfigKey . workbench ) || ( await this . getDefaultWorkbenchPath ( ) ) ; await ConfigHandler . update ( ConfigKey . workbench , IoTWorkbenchSettings . instance . workbenchPath , vscode . ConfigurationTarget . Global ) ; } return IoTWorkbenchSettings . instance ; } static async getDefaultWorkbenchPath ( ) { const platform = await getPlatform ( ) ; const homeDir = await getHomeDir ( ) ; let workbenchPath = "" ; if ( platform === OSPlatform . WIN32 ) { workbenchPath = path . join ( homeDir , "Documents" , "IoTWorkbenchProjects" ) ; } else if ( platform === OSPlatform . LINUX ) { workbenchPath = path . join ( homeDir , "IoTWorkbenchProjects" ) ; } else if ( platform === OSPlatform . DARWIN ) { workbenchPath = path . join ( homeDir , "Documents" , "IoTWorkbenchProjects" ) ; } else { workbenchPath = "/IoTWorkbenchProjects" ; } return workbenchPath ; } getWorkbenchPath ( ) { return ConfigHandler . get < string > ( ConfigKey . workbench ) || this . workbenchPath ; } async setWorkbenchPath ( ) { const selection = await this . selectWorkbenchPath ( ) ; let userWorkbenchPath ; if ( selection . data === "$" ) { userWorkbenchPath = await this . selectFolder ( ) ; } else { userWorkbenchPath = selection . data ; } if ( userWorkbenchPath ) { await ConfigHandler . update ( ConfigKey . workbench , userWorkbenchPath , vscode . ConfigurationTarget . Global ) ; await vscode . window . showInformationMessage ( "Change workbench successfully." ) ; } } private async selectWorkbenchPath ( ) { const userWorkbenchPath = this . getWorkbenchPath ( ) ; const workbenchPicks = [ { label : userWorkbenchPath , description : "" , data : userWorkbenchPath } , { label : "$(file-directory) Browse..." , description : "" , data : "$" } ] ; const selection = await vscode . window . showQuickPick ( workbenchPicks , { ignoreFocusOut : true , matchOnDescription : true , matchOnDetail : true , placeHolder : "Select workbench folder" } ) ; if ( ! selection ) { throw new OperationCanceledError ( "Workbench path selection cancelled." ) ; } return selection ; } private async selectFolder ( ) { const options = { canSelectMany : false , openLabel : "Select" , canSelectFolders : true , canSelectFiles : false } ; const folderUri = await vscode . window . showOpenDialog ( options ) ; if ( ! folderUri || folderUri . length === 0 ) { throw new OperationCanceledError ( "Folder selection cancelled." ) ; } return folderUri [ 0 ] . fsPath ; } }