import * as fse from 'fs-extra' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { advancedPolicyAuthoringExperienceConfigKey , extensionName } from '../constants' ; import { ext } from '../extensionVariables' ; import { localize } from '../localize' ; import { getDefaultWorkspacePath } from './fsUtil' ; export async function writeToEditor ( editor , data ) { await editor . edit ( ( editBuilder ) => { if ( editor . document . lineCount > 0 ) { const lastLine = editor . document . lineAt ( editor . document . lineCount - 1 ) ; editBuilder . delete ( new vscode . Range ( new vscode . Position ( 0 , 0 ) , new vscode . Position ( lastLine . range . start . line , lastLine . range . end . character ) ) ) ; } editBuilder . insert ( new vscode . Position ( 0 , 0 ) , data ) ; } ) ; } export async function promptOpenWorkingFolder ( ) { const showPrompt = vscode . workspace . getConfiguration ( ) . get ( advancedPolicyAuthoringExperienceConfigKey ) ; if ( showPrompt ) { const dontAskAgain = { title : localize ( 'dontAskAgain' , "Don't Ask Again" ) } ; if ( ! workingFolderOpenedInWorkspace ( ) ) { if ( ! await workingFolderInitialized ( ) ) { const message = localize ( 'initializeAndOpenFolderInWorkspace' , 'For advanced policy authoring experience, initialize and open extension workspace folder "{0}".' , getDefaultWorkspacePath ( ) ) ; const btn = { title : localize ( 'initializeAndOpenFolder' , 'Initialize and Open' ) } ; ext . ui . showWarningMessage ( message , btn , dontAskAgain ) . then ( async ( result ) => { if ( result === btn ) { vscode . commands . executeCommand ( "azureApiManagement.initializeExtensionWorkspaceFolder" ) . then ( ( ) => { vscode . commands . executeCommand ( "azureApiManagement.openExtensionWorkspaceFolder" ) ; } ) ; } else if ( result === dontAskAgain ) { await vscode . workspace . getConfiguration ( ) . update ( advancedPolicyAuthoringExperienceConfigKey , false , vscode . ConfigurationTarget . Global ) ; } } ) ; } else { const message = localize ( 'openFolderInWorkspace' , 'For advanved policy authoring experience, open extension workspace folder "{0}".' , getDefaultWorkspacePath ( ) ) ; const btn = { title : localize ( 'openFolder' , 'Open Folder' ) } ; ext . ui . showWarningMessage ( message , btn , dontAskAgain ) . then ( async ( result ) => { if ( result === btn ) { vscode . commands . executeCommand ( "azureApiManagement.openExtensionWorkspaceFolder" ) ; } else if ( result === dontAskAgain ) { await vscode . workspace . getConfiguration ( ) . update ( advancedPolicyAuthoringExperienceConfigKey , false , vscode . ConfigurationTarget . Global ) ; } } ) ; } } else { if ( ! await workingFolderInitialized ( ) ) { const message = localize ( 'setupWorkingFolder' , 'For advanved policy authoring experience, initialize extension workspace folder "{0}".' , getDefaultWorkspacePath ( ) ) ; const btn = { title : localize ( 'initialize' , 'Initialize' ) } ; ext . ui . showWarningMessage ( message , btn , dontAskAgain ) . then ( async ( result ) => { if ( result === btn ) { vscode . commands . executeCommand ( "azureApiManagement.initializeExtensionWorkspaceFolder" ) ; } else if ( result === dontAskAgain ) { await vscode . workspace . getConfiguration ( ) . update ( advancedPolicyAuthoringExperienceConfigKey , false , vscode . ConfigurationTarget . Global ) ; } } ) ; } } } } export function workingFolderOpenedInWorkspace ( ) { let folderInWorkspace = true ; if ( vscode . workspace . workspaceFolders !== undefined && vscode . workspace . workspaceFolders . length > 0 ) { const folder = vscode . workspace . workspaceFolders . find ( ( w ) => w . uri . fsPath === getDefaultWorkspacePath ( ) ) ; if ( ! folder ) { folderInWorkspace = false ; } } else { folderInWorkspace = false ; } return folderInWorkspace ; } async function workingFolderInitialized ( ) { const workingFolderPath = getDefaultWorkspacePath ( ) ; return await fse . pathExists ( path . join ( workingFolderPath , `${ extensionName }.csproj` ) ) ; }