'use strict' ; import * as vscode from 'vscode' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { FabricGatewayRegistryEntry , FabricGatewayRegistry } from 'ibm-blockchain-platform-common' ; import { IBlockchainQuickPickItem , UserInputUtil } from './UserInputUtil' ; import { LogType } from 'ibm-blockchain-platform-common' ; import { InstantiatedTreeItem } from '../explorer/model/InstantiatedTreeItem' ; import { ContractTreeItem } from '../explorer/model/ContractTreeItem' ; import { FabricGatewayConnectionManager } from '../fabric/FabricGatewayConnectionManager' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { IAssociateFromViewOptions } from '../interfaces/IAssociateFromViewOptions' ; export async function associateTransactionDataDirectory ( chaincode ? , associateFromViewOptions ? ) { let gateway ; let chosenChaincode ; let chaincodeName ; let chaincodeLabel ; let channelName ; let chosenDirectory ; const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'associateTestDataDirectory' ) ; if ( associateFromViewOptions ) { chaincodeLabel = associateFromViewOptions . label ; chaincodeName = associateFromViewOptions . name ; channelName = associateFromViewOptions . channel ; gateway = await FabricGatewayConnectionManager . instance ( ) . getGatewayRegistryEntry ( ) ; const openDialogOptions = { canSelectFiles : false , canSelectFolders : true , canSelectMany : false , openLabel : 'Select' , filters : undefined } ; chosenDirectory = await UserInputUtil . openFileBrowser ( openDialogOptions ) as string ; } else { if ( ! chaincode ) { if ( ! FabricGatewayConnectionManager . instance ( ) . getConnection ( ) ) { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_GATEWAY ) ; if ( ! FabricGatewayConnectionManager . instance ( ) . getConnection ( ) ) { return ; } } chosenChaincode = await UserInputUtil . showClientInstantiatedSmartContractsQuickPick ( 'Please choose an instantiated smart contract to associate a transaction data directory with' ) ; if ( ! chosenChaincode ) { return ; } chaincodeLabel = chosenChaincode . label ; chaincodeName = chosenChaincode . data . name ; channelName = chosenChaincode . data . channel ; } else { if ( chaincode instanceof ContractTreeItem ) { chaincodeLabel = chaincode . instantiatedChaincode . label ; chaincodeName = chaincode . instantiatedChaincode . name ; channelName = chaincode . channelName ; } else { chaincodeLabel = chaincode . label ; chaincodeName = chaincode . name ; channelName = chaincode . channels [ 0 ] . label ; } } gateway = await FabricGatewayConnectionManager . instance ( ) . getGatewayRegistryEntry ( ) ; const quickPickItems = [ ] ; const workspaceFolders = UserInputUtil . getWorkspaceFolders ( ) ; for ( const folder of workspaceFolders ) { const txnDataPath = path . join ( folder . uri . path , 'transaction_data' ) ; if ( await fs . pathExists ( txnDataPath ) ) { quickPickItems . push ( { label : `Transaction data directory for ${ folder . name }` , description : txnDataPath , data : txnDataPath } ) ; } } quickPickItems . push ( { label : UserInputUtil . BROWSE_LABEL , data : '' , description : '' } ) ; const openDialogOptions = { canSelectFiles : false , canSelectFolders : true , canSelectMany : false , openLabel : 'Select' , filters : undefined } ; chosenDirectory = await UserInputUtil . browseWithOptions ( `Choose a directory to associate with ${ chaincodeLabel }` , quickPickItems , openDialogOptions ) as string | IBlockchainQuickPickItem < string > ; } if ( ! chosenDirectory ) { return ; } else { try { const transactionDataPath = ( typeof chosenDirectory === 'string' ) ? chosenDirectory : chosenDirectory . description ; const fabricGatewayRegistry = FabricGatewayRegistry . instance ( ) ; if ( ! gateway . transactionDataDirectories ) { gateway . transactionDataDirectories = [ ] ; } const newAssociation = { chaincodeName , channelName , transactionDataPath } ; const indexToUpdate = gateway . transactionDataDirectories . findIndex ( ( item ) => { return item . chaincodeName === chaincodeName ; } ) ; if ( indexToUpdate > - 1 ) { gateway . transactionDataDirectories [ indexToUpdate ] = newAssociation ; } else { gateway . transactionDataDirectories . push ( newAssociation ) ; } await fabricGatewayRegistry . update ( gateway ) ; outputAdapter . log ( LogType . SUCCESS , `Successfully associated the directory "${ transactionDataPath }" with "${ chaincodeLabel }"` ) ; if ( associateFromViewOptions ) { return newAssociation ; } } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Unable to associate transaction data directory: ${ error . message }` , `Unable to associate transaction data directory: ${ error . toString ( ) }` ) ; } } }