'use strict' ; import * as vscode from 'vscode' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { LogType , FabricEnvironmentRegistryEntry , IFabricEnvironmentConnection , FabricSmartContractDefinition } from 'ibm-blockchain-platform-common' ; import { DeployView } from '../webview/DeployView' ; import { GlobalState } from '../util/GlobalState' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { FabricEnvironmentManager } from '../fabric/environments/FabricEnvironmentManager' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { PackageRegistryEntry } from '../registries/PackageRegistryEntry' ; import { PackageRegistry } from '../registries/PackageRegistry' ; export async function openDeployView ( fabricRegistryEntry ? , channelName ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; try { if ( ! fabricRegistryEntry ) { const _chosenEnvironment = await UserInputUtil . showFabricEnvironmentQuickPickBox ( 'Select an environment' , false , false ) as IBlockchainQuickPickItem < FabricEnvironmentRegistryEntry > ; if ( ! _chosenEnvironment ) { return ; } fabricRegistryEntry = _chosenEnvironment . data ; } const selectedEnvironmentName = fabricRegistryEntry . name ; let connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( connection ) { const connectedName = connection . environmentName ; if ( connectedName !== selectedEnvironmentName ) { await vscode . commands . executeCommand ( ExtensionCommands . DISCONNECT_ENVIRONMENT ) ; await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_ENVIRONMENT , fabricRegistryEntry ) ; connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; } } else { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_ENVIRONMENT , fabricRegistryEntry ) ; connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; } if ( ! connection ) { throw new Error ( `Unable to connect to environment: ${ selectedEnvironmentName }` ) ; } if ( ! channelName ) { const _chosenChannel = await UserInputUtil . showChannelQuickPickBox ( 'Select a channel' ) ; if ( ! _chosenChannel ) { return ; } channelName = _chosenChannel . label ; } const channelMap = await connection . createChannelMap ( ) ; const channelPeers = channelMap . get ( channelName ) ; let hasV1Capabilities = false ; for ( const peer of channelPeers ) { const capabilities = await connection . getChannelCapabilityFromPeer ( channelName , peer ) ; if ( ! capabilities . includes ( 'V2_0' ) ) { hasV1Capabilities = true ; } } let packageEntries = [ ] ; const allPackageEntries = await PackageRegistry . instance ( ) . getAll ( ) ; packageEntries = allPackageEntries . filter ( ( entry ) => { const fileExtension = UserInputUtil . getPackageFileExtension ( entry . path ) ; return hasV1Capabilities ? fileExtension === '.cds' : fileExtension !== '.cds' ; } ) ; const workspaceFolders = UserInputUtil . getWorkspaceFolders ( ) ; const workspaceNames = workspaceFolders . map ( ( item ) => item . name ) ; const allCommittedContracts = await connection . getCommittedSmartContractDefinitions ( channelPeers , channelName ) ; const committedDefinitions = allCommittedContracts . map ( ( definition ) => `${ definition . name }@${ definition . version }` ) ; const allDiscoveredPeers = await connection . getAllDiscoveredPeerNames ( channelName ) ; const environmentPeers = [ ] ; const discoveredPeers = [ ] ; for ( const peer of allDiscoveredPeers ) { if ( channelPeers . includes ( peer ) ) { environmentPeers . push ( peer ) ; } else { discoveredPeers . push ( peer ) ; } } const _orgMap = await connection . getDiscoveredOrgs ( channelName ) ; const orgMap = { } ; _orgMap . forEach ( ( peers , org ) => { orgMap [ org ] = peers ; } ) ; const appState = { channelName , hasV1Capabilities , environmentName : selectedEnvironmentName , packageEntries , workspaceNames , committedDefinitions , environmentPeers , discoveredPeers , orgMap } ; const context = GlobalState . getExtensionContext ( ) ; const deployView = new DeployView ( context , appState ) ; await deployView . openView ( true ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , error . message , error . toString ( ) ) ; } }