import * as fs from "fs-extra" ; import * as path from "path" ; import * as vscode from "vscode" ; import { OpenDialogOptions , Uri , window } from "vscode" ; import { instrumentOperation } from "vscode-extension-telemetry-wrapper" ; import { mavenExplorerProvider } from "../explorer/mavenExplorerProvider" ; import { MavenProject } from "../explorer/model/MavenProject" ; import { mavenOutputChannel } from "../mavenOutputChannel" ; import { generalErrorHandler } from "./errorUtils" ; const TROUBLESHOOTING_LINK = "https://github.com/Microsoft/vscode-maven/blob/master/Troubleshooting.md" ; export async function openDialogForFolder ( customOptions ) { const options = { canSelectFiles : false , canSelectFolders : true , canSelectMany : false } ; const result = await window . showOpenDialog ( Object . assign ( options , customOptions ) ) ; if ( result && result . length > 0 ) { return Promise . resolve ( result [ 0 ] ) ; } else { return Promise . resolve ( undefined ) ; } } export async function openDialogForFile ( customOptions ? ) { const options = { canSelectFiles : true , canSelectFolders : false , canSelectMany : false } ; const result = await window . showOpenDialog ( Object . assign ( options , customOptions ) ) ; if ( result && result . length > 0 ) { return Promise . resolve ( result [ 0 ] ) ; } else { return Promise . resolve ( undefined ) ; } } export async function openFileIfExists ( filepath ) { if ( await fs . pathExists ( filepath ) ) { window . showTextDocument ( Uri . file ( filepath ) ) ; } } export async function showTroubleshootingDialog ( errorMessage ) { const OPTION_SHOW_OUTPUT = "Show Output" ; const OPTION_LEARN_MORE = "Learn More" ; const choiceForDetails = await window . showErrorMessage ( errorMessage , OPTION_SHOW_OUTPUT , OPTION_LEARN_MORE ) ; if ( choiceForDetails === OPTION_LEARN_MORE ) { vscode . commands . executeCommand ( "vscode.open" , vscode . Uri . parse ( TROUBLESHOOTING_LINK ) ) ; } else if ( choiceForDetails === OPTION_SHOW_OUTPUT ) { mavenOutputChannel . show ( ) ; } } export async function selectProjectIfNecessary ( ) { if ( mavenExplorerProvider . mavenProjectNodes === undefined || mavenExplorerProvider . mavenProjectNodes . length === 0 ) { return undefined ; } if ( mavenExplorerProvider . mavenProjectNodes . length === 1 ) { return mavenExplorerProvider . mavenProjectNodes [ 0 ] ; } return await window . showQuickPick ( mavenExplorerProvider . mavenProjectNodes . map ( item => ( { value : item , label : `$(primitive-dot) ${ item . name }` , description : undefined , detail : item . pomPath } ) ) , { placeHolder : "Select a Maven project ..." , ignoreFocusOut : true } ) . then ( item => item ? item . value : undefined ) ; } export function registerCommand ( context , commandName , func , withOperationIdAhead ? ) { const callbackWithTroubleshooting = instrumentOperation ( commandName , async ( _operationId , ... args ) => { try { return withOperationIdAhead ? await func ( _operationId , ... args ) : await func ( ... args ) ; } catch ( error ) { await generalErrorHandler ( commandName , error ) ; } } ) ; context . subscriptions . push ( vscode . commands . registerCommand ( commandName , callbackWithTroubleshooting ) ) ; } export function registerCommandRequiringTrust ( context , commandName , func , withOperationIdAhead ? ) { const instrumentedFunc = async ( ... args ) => { if ( vscode . workspace . isTrusted ) { await func ( ... args ) ; } else { await promptToManageWorkspaceTrust ( ) ; } } ; registerCommand ( context , commandName , instrumentedFunc , withOperationIdAhead ) ; } async function promptToManageWorkspaceTrust ( ) { const COMMAND_MANAGE_TRUST = "workbench.action.manageTrust" ; const OPTION_MANAGE_TRUST = "Manage Workspace Trust" ; const information = "For security concern, this command requires your trust on current workspace before it can be executed." ; const choiceForDetails = await window . showInformationMessage ( information , OPTION_MANAGE_TRUST ) ; if ( choiceForDetails === OPTION_MANAGE_TRUST ) { vscode . commands . executeCommand ( COMMAND_MANAGE_TRUST ) ; } } export function effectivePomContentUri ( pomPath ) { const displayName = "EffectivePOM.xml" ; return Uri . parse ( "vscode-maven://effective-pom" ) . with ( { path : `/${ path . join ( pomPath , displayName ) }` , query : pomPath } ) ; } export function dependenciesContentUri ( pomPath ) { const displayName = "Dependencies" ; return vscode . Uri . parse ( "vscode-maven://dependencies" ) . with ( { path : `/${ path . join ( pomPath , displayName ) }` , query : pomPath } ) ; }