'use strict' ; import * as nls from 'vscode-nls' ; nls . config ( { messageFormat : nls . MessageFormat . both } ) ( ) ; import * as vscode from 'vscode' ; import * as semver from 'semver' ; import * as path from 'path' ; import { Reporter } from './util/Reporter' ; import { ExtensionUtil } from './util/ExtensionUtil' ; import { VSCodeBlockchainOutputAdapter } from './logging/VSCodeBlockchainOutputAdapter' ; import { DependencyManager } from './dependencies/DependencyManager' ; import { TemporaryCommandRegistry } from './dependencies/TemporaryCommandRegistry' ; import { ExtensionCommands } from '../ExtensionCommands' ; import { version as currentExtensionVersion } from '../package.json' ; import { SettingConfigurations } from './configurations' ; import { UserInputUtil } from './commands/UserInputUtil' ; import { GlobalState , ExtensionData } from './util/GlobalState' ; import { FabricWalletRegistry , FabricEnvironmentRegistry , LogType , FabricGatewayRegistry , FileSystemUtil , FabricEnvironmentRegistryEntry , EnvironmentFlags , FabricRuntimeUtil , EnvironmentType } from 'ibm-blockchain-platform-common' ; import { RepositoryRegistry } from './registries/RepositoryRegistry' ; import { Dependencies } from './dependencies/Dependencies' ; import { LocalMicroEnvironmentManager } from './fabric/environments/LocalMicroEnvironmentManager' ; export async function activate ( context ) { GlobalState . setExtensionContext ( context ) ; const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; const originalExtensionData = GlobalState . get ( ) ; if ( originalExtensionData . createOneOrgLocalFabric === undefined ) { originalExtensionData . createOneOrgLocalFabric = true ; } if ( originalExtensionData . deletedOneOrgLocalFabric === undefined ) { originalExtensionData . deletedOneOrgLocalFabric = false ; } const newExtensionData = { activationCount : originalExtensionData . activationCount + 1 , version : currentExtensionVersion , migrationCheck : 2 , generatorVersion : originalExtensionData . generatorVersion , preReqPageShown : originalExtensionData . preReqPageShown , dockerForWindows : originalExtensionData . dockerForWindows , createOneOrgLocalFabric : originalExtensionData . createOneOrgLocalFabric , deletedOneOrgLocalFabric : originalExtensionData . deletedOneOrgLocalFabric , shownFirstSubmissionSurveyURL : originalExtensionData . shownFirstSubmissionSurveyURL , } ; let extDir = SettingConfigurations . getExtensionDir ( ) ; extDir = FileSystemUtil . getDirPath ( extDir ) ; FabricWalletRegistry . instance ( ) . setRegistryPath ( extDir ) ; FabricGatewayRegistry . instance ( ) . setRegistryPath ( extDir ) ; FabricEnvironmentRegistry . instance ( ) . setRegistryPath ( extDir ) ; RepositoryRegistry . instance ( ) . setRegistryPath ( extDir ) ; const isIBMer = ExtensionUtil . checkIfIBMer ( ) ; if ( originalExtensionData . migrationCheck !== newExtensionData . migrationCheck ) { } const extensionUpdated = newExtensionData . version !== originalExtensionData . version ; let extensionUpgraded = false ; if ( originalExtensionData . version && newExtensionData . version ) { const semverOriginal = semver . major ( originalExtensionData . version ) ; const semverNew = semver . major ( newExtensionData . version ) ; extensionUpgraded = semverNew > semverOriginal ; } await GlobalState . update ( newExtensionData ) ; const packageJson = ExtensionUtil . getPackageJSON ( ) ; if ( packageJson . production !== true ) { await Reporter . instance ( ) . dispose ( ) ; } if ( extensionUpdated ) { outputAdapter . show ( ) ; if ( ! originalExtensionData . version ) { Reporter . instance ( ) . sendTelemetryEvent ( 'newInstall' , { IBM : isIBMer + '' } ) ; } else { Reporter . instance ( ) . sendTelemetryEvent ( 'updatedInstall' , { IBM : isIBMer + '' } ) ; } } outputAdapter . log ( LogType . IMPORTANT , undefined , 'Log files can be found by running the `Developer: Open Logs Folder` command from the palette' , undefined , true ) ; outputAdapter . log ( LogType . INFO , undefined , 'Starting IBM Blockchain Platform Extension' ) ; try { const dependencyManager = DependencyManager . instance ( ) ; const dependencies = await dependencyManager . getPreReqVersions ( ) ; const bypassPreReqs = vscode . workspace . getConfiguration ( ) . get ( SettingConfigurations . EXTENSION_BYPASS_PREREQS ) ; let dependenciesInstalled ; if ( ! bypassPreReqs ) { dependenciesInstalled = await dependencyManager . hasPreReqsInstalled ( dependencies ) ; } const tempCommandRegistry = TemporaryCommandRegistry . instance ( ) ; context = await ExtensionUtil . registerPreActivationCommands ( context ) ; if ( extensionUpgraded ) { const v1ExtDir = extDir . replace ( `${ path . sep }v2` , '' ) ; FabricEnvironmentRegistry . instance ( ) . setRegistryPath ( v1ExtDir ) ; let v1EnvironmentEntries = await FabricEnvironmentRegistry . instance ( ) . getAll ( ) ; if ( v1EnvironmentEntries . length > 0 ) { v1EnvironmentEntries = v1EnvironmentEntries . filter ( ( entry ) => entry . environmentType === EnvironmentType . MANAGED ) ; if ( v1EnvironmentEntries . length > 0 ) { const teardown = await UserInputUtil . showConfirmationWarningMessage ( 'Detected old local environments which can not be used with this upgrade. Would you like to teardown these environments?' ) ; if ( teardown ) { for ( const entry of v1EnvironmentEntries ) { try { await vscode . commands . executeCommand ( ExtensionCommands . TEARDOWN_FABRIC , undefined , true , entry . name , true ) ; outputAdapter . log ( LogType . INFO , undefined , `Successful teardown of ${ entry . name } environment.` ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , undefined , `Unable to teardown ${ entry . name } environment: ${ error . toString ( ) }.` ) ; } } } } } FabricEnvironmentRegistry . instance ( ) . setRegistryPath ( extDir ) ; } let createLocalEnvironment = false ; const oldAnsibleEnvironmentEntries = await FabricEnvironmentRegistry . instance ( ) . getAll ( [ EnvironmentFlags . ANSIBLE ] ) ; if ( oldAnsibleEnvironmentEntries . length > 0 ) { for ( const entry of oldAnsibleEnvironmentEntries ) { await vscode . commands . executeCommand ( ExtensionCommands . DELETE_ENVIRONMENT , entry , true , true ) ; if ( entry . name === FabricRuntimeUtil . LOCAL_FABRIC ) { createLocalEnvironment = true ; } } } const _settings = vscode . workspace . getConfiguration ( ) . get ( SettingConfigurations . FABRIC_RUNTIME , vscode . ConfigurationTarget . Global ) ; const localSettings = JSON . parse ( JSON . stringify ( _settings ) ) ; let updateSettings = false ; for ( const [ key , value ] of Object . entries ( localSettings ) ) { if ( typeof value !== 'number' ) { delete localSettings [ key ] ; updateSettings = true ; } } if ( updateSettings ) { await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . FABRIC_RUNTIME , localSettings , vscode . ConfigurationTarget . Global ) ; } if ( createLocalEnvironment ) { await LocalMicroEnvironmentManager . instance ( ) . initialize ( FabricRuntimeUtil . LOCAL_FABRIC , 1 ) ; } if ( extensionUpdated && originalExtensionData . version ) { await vscode . commands . executeCommand ( ExtensionCommands . OPEN_RELEASE_NOTES ) ; } if ( dependenciesInstalled || bypassPreReqs ) { tempCommandRegistry . createTempCommands ( true ) ; await ExtensionUtil . setupCommands ( ) ; } else { tempCommandRegistry . createTempCommands ( false , ExtensionCommands . OPEN_PRE_REQ_PAGE ) ; } if ( ! bypassPreReqs ) { if ( ! originalExtensionData . preReqPageShown ) { await vscode . commands . executeCommand ( ExtensionCommands . OPEN_PRE_REQ_PAGE , dependencies ) ; newExtensionData . preReqPageShown = true ; await GlobalState . update ( newExtensionData ) ; } else if ( ! dependenciesInstalled ) { await vscode . commands . executeCommand ( ExtensionCommands . OPEN_PRE_REQ_PAGE , dependencies ) ; } } GlobalState . setExtensionContext ( context ) ; if ( dependenciesInstalled || bypassPreReqs ) { await ExtensionUtil . completeActivation ( extensionUpdated ) ; if ( extensionUpgraded ) { const whatsNewPrompt = 'Learn more' ; const response = await vscode . window . showInformationMessage ( `You have successfully updated to version 2 of the IBM Blockchain Platform extension. Lots of changes have happened since version 1, so be sure to check what's new!` , whatsNewPrompt ) ; if ( response === whatsNewPrompt ) { await vscode . commands . executeCommand ( ExtensionCommands . OPEN_FABRIC_2_PAGE ) ; } } } } catch ( error ) { outputAdapter . log ( LogType . ERROR , undefined , `Failed to activate extension: ${ error . toString ( ) }` , error . stack ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'activationFailed' , { activationError : error . message } ) ; await UserInputUtil . failedActivationWindow ( error . message ) ; } } export async function deactivate ( ) { const context = GlobalState . getExtensionContext ( ) ; await Reporter . instance ( ) . dispose ( ) ; ExtensionUtil . disposeExtension ( context ) ; }