From a4134daa3e1b94939d4f3d1c99c64524f5b537e3 Mon Sep 17 00:00:00 2001 From: Anthony Dresser Date: Thu, 1 Aug 2019 11:15:17 -0700 Subject: [PATCH] Abstract scripting actions from oe (#6550) * wip * add rest of oe scripting * remove linting --- .../commandLine.test.ts | 0 .../browser/objectExplorerActions.ts | 260 +----------------- .../browser/serverTreeActionProvider.ts | 17 +- .../objectExplorerScripting.contribution.ts | 111 ++++++++ .../objectExplorerScriptingActions.ts | 146 ++++++++++ src/vs/workbench/workbench.main.ts | 1 + 6 files changed, 260 insertions(+), 275 deletions(-) rename src/sql/workbench/parts/commandLine/test/{common => electron-browser}/commandLine.test.ts (100%) create mode 100644 src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScripting.contribution.ts create mode 100644 src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScriptingActions.ts diff --git a/src/sql/workbench/parts/commandLine/test/common/commandLine.test.ts b/src/sql/workbench/parts/commandLine/test/electron-browser/commandLine.test.ts similarity index 100% rename from src/sql/workbench/parts/commandLine/test/common/commandLine.test.ts rename to src/sql/workbench/parts/commandLine/test/electron-browser/commandLine.test.ts diff --git a/src/sql/workbench/parts/objectExplorer/browser/objectExplorerActions.ts b/src/sql/workbench/parts/objectExplorer/browser/objectExplorerActions.ts index 95207cf966..24ec1e235e 100644 --- a/src/sql/workbench/parts/objectExplorer/browser/objectExplorerActions.ts +++ b/src/sql/workbench/parts/objectExplorer/browser/objectExplorerActions.ts @@ -12,20 +12,13 @@ import { ICommandService } from 'vs/platform/commands/common/commands'; import * as azdata from 'azdata'; import { IConnectionManagementService, IConnectionCompletionOptions } from 'sql/platform/connection/common/connectionManagement'; import { TreeNode } from 'sql/workbench/parts/objectExplorer/common/treeNode'; -import { NodeType } from 'sql/workbench/parts/objectExplorer/common/nodeType'; import { TreeUpdateUtils } from 'sql/workbench/parts/objectExplorer/browser/treeUpdateUtils'; import { TreeSelectionHandler } from 'sql/workbench/parts/objectExplorer/browser/treeSelectionHandler'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; -import { IScriptingService } from 'sql/platform/scripting/common/scriptingService'; -import { IQueryEditorService } from 'sql/workbench/services/queryEditor/common/queryEditorService'; import { IObjectExplorerService } from 'sql/workbench/services/objectExplorer/common/objectExplorerService'; -import * as Constants from 'sql/platform/connection/common/constants'; import { IConnectionProfile } from 'sql/platform/connection/common/interfaces'; import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService'; import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile'; -import { IErrorMessageService } from 'sql/platform/errorMessage/common/errorMessageService'; -import { ScriptSelectAction, EditDataAction, ScriptCreateAction, ScriptExecuteAction, ScriptAlterAction, ScriptDeleteAction } from 'sql/workbench/electron-browser/scriptingActions'; - export class ObjectExplorerActionsContext implements azdata.ObjectExplorerContext { public connectionProfile: azdata.IConnectionProfile; @@ -33,7 +26,7 @@ export class ObjectExplorerActionsContext implements azdata.ObjectExplorerContex public isConnectionNode: boolean = false; } -async function getTreeNode(context: ObjectExplorerActionsContext, objectExplorerService: IObjectExplorerService): Promise { +export async function getTreeNode(context: ObjectExplorerActionsContext, objectExplorerService: IObjectExplorerService): Promise { if (context.isConnectionNode) { return Promise.resolve(undefined); } @@ -155,254 +148,3 @@ export class ManageConnectionAction extends Action { super.dispose(); } } - -export class OEScriptSelectAction extends ScriptSelectAction { - public static ID = 'objectExplorer.' + ScriptSelectAction.ID; - private _objectExplorerTreeNode: TreeNode; - private _treeSelectionHandler: TreeSelectionHandler; - - constructor( - id: string, label: string, - @IQueryEditorService protected _queryEditorService: IQueryEditorService, - @IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, - @IScriptingService protected _scriptingService: IScriptingService, - @IObjectExplorerService private _objectExplorerService: IObjectExplorerService, - @IInstantiationService private _instantiationService: IInstantiationService - ) { - super(id, label, _queryEditorService, _connectionManagementService, _scriptingService); - } - - public async run(actionContext: any): Promise { - this._treeSelectionHandler = this._instantiationService.createInstance(TreeSelectionHandler); - if (actionContext instanceof ObjectExplorerActionsContext) { - //set objectExplorerTreeNode for context menu clicks - this._objectExplorerTreeNode = await getTreeNode(actionContext, this._objectExplorerService); - } - this._treeSelectionHandler.onTreeActionStateChange(true); - let connectionProfile = TreeUpdateUtils.getConnectionProfile(this._objectExplorerTreeNode); - let ownerUri = this._connectionManagementService.getConnectionUri(connectionProfile); - ownerUri = this._connectionManagementService.getFormattedUri(ownerUri, connectionProfile); - let metadata = this._objectExplorerTreeNode.metadata; - - return super.run({ profile: connectionProfile, object: metadata }).then((result) => { - this._treeSelectionHandler.onTreeActionStateChange(false); - return result; - }); - } -} - -export class OEEditDataAction extends EditDataAction { - public static ID = 'objectExplorer.' + EditDataAction.ID; - private _objectExplorerTreeNode: TreeNode; - private _treeSelectionHandler: TreeSelectionHandler; - - constructor( - id: string, label: string, - @IQueryEditorService protected _queryEditorService: IQueryEditorService, - @IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, - @IScriptingService protected _scriptingService: IScriptingService, - @IObjectExplorerService private _objectExplorerService: IObjectExplorerService, - @IInstantiationService private _instantiationService: IInstantiationService - ) { - super(id, label, _queryEditorService, _connectionManagementService, _scriptingService); - } - - public async run(actionContext: any): Promise { - this._treeSelectionHandler = this._instantiationService.createInstance(TreeSelectionHandler); - if (actionContext instanceof ObjectExplorerActionsContext) { - //set objectExplorerTreeNode for context menu clicks - this._objectExplorerTreeNode = await getTreeNode(actionContext, this._objectExplorerService); - } - this._treeSelectionHandler.onTreeActionStateChange(true); - let connectionProfile = TreeUpdateUtils.getConnectionProfile(this._objectExplorerTreeNode); - let metadata = (this._objectExplorerTreeNode).metadata; - - return super.run({ profile: connectionProfile, object: metadata }).then((result) => { - this._treeSelectionHandler.onTreeActionStateChange(false); - return true; - }); - } -} - -export class OEScriptCreateAction extends ScriptCreateAction { - public static ID = 'objectExplorer.' + ScriptCreateAction.ID; - private _objectExplorerTreeNode: TreeNode; - private _treeSelectionHandler: TreeSelectionHandler; - - constructor( - id: string, label: string, - @IQueryEditorService protected _queryEditorService: IQueryEditorService, - @IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, - @IScriptingService protected _scriptingService: IScriptingService, - @IObjectExplorerService private _objectExplorerService: IObjectExplorerService, - @IInstantiationService private _instantiationService: IInstantiationService, - @IErrorMessageService protected _errorMessageService: IErrorMessageService, - ) { - super(id, label, _queryEditorService, _connectionManagementService, _scriptingService, _errorMessageService); - } - - public async run(actionContext: any): Promise { - this._treeSelectionHandler = this._instantiationService.createInstance(TreeSelectionHandler); - if (actionContext instanceof ObjectExplorerActionsContext) { - //set objectExplorerTreeNode for context menu clicks - this._objectExplorerTreeNode = await getTreeNode(actionContext, this._objectExplorerService); - } - this._treeSelectionHandler.onTreeActionStateChange(true); - let connectionProfile = TreeUpdateUtils.getConnectionProfile(this._objectExplorerTreeNode); - let metadata = (this._objectExplorerTreeNode).metadata; - let ownerUri = this._connectionManagementService.getConnectionUri(connectionProfile); - ownerUri = this._connectionManagementService.getFormattedUri(ownerUri, connectionProfile); - - return super.run({ profile: connectionProfile, object: metadata }).then((result) => { - this._treeSelectionHandler.onTreeActionStateChange(false); - return result; - }); - } -} - -export class OEScriptExecuteAction extends ScriptExecuteAction { - public static ID = 'objectExplorer.' + ScriptExecuteAction.ID; - private _objectExplorerTreeNode: TreeNode; - private _treeSelectionHandler: TreeSelectionHandler; - - constructor( - id: string, label: string, - @IQueryEditorService protected _queryEditorService: IQueryEditorService, - @IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, - @IScriptingService protected _scriptingService: IScriptingService, - @IObjectExplorerService private _objectExplorerService: IObjectExplorerService, - @IInstantiationService private _instantiationService: IInstantiationService, - @IErrorMessageService protected _errorMessageService: IErrorMessageService - ) { - super(id, label, _queryEditorService, _connectionManagementService, _scriptingService, _errorMessageService); - } - - public async run(actionContext: any): Promise { - this._treeSelectionHandler = this._instantiationService.createInstance(TreeSelectionHandler); - if (actionContext instanceof ObjectExplorerActionsContext) { - //set objectExplorerTreeNode for context menu clicks - this._objectExplorerTreeNode = await getTreeNode(actionContext, this._objectExplorerService); - } - this._treeSelectionHandler.onTreeActionStateChange(true); - let connectionProfile = TreeUpdateUtils.getConnectionProfile(this._objectExplorerTreeNode); - let metadata = (this._objectExplorerTreeNode).metadata; - let ownerUri = this._connectionManagementService.getConnectionUri(connectionProfile); - ownerUri = this._connectionManagementService.getFormattedUri(ownerUri, connectionProfile); - - return super.run({ profile: connectionProfile, object: metadata }).then((result) => { - this._treeSelectionHandler.onTreeActionStateChange(false); - return result; - }); - } -} - -export class OEScriptAlterAction extends ScriptAlterAction { - public static ID = 'objectExplorer.' + ScriptAlterAction.ID; - private _objectExplorerTreeNode: TreeNode; - private _treeSelectionHandler: TreeSelectionHandler; - - constructor( - id: string, label: string, - @IQueryEditorService protected _queryEditorService: IQueryEditorService, - @IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, - @IScriptingService protected _scriptingService: IScriptingService, - @IObjectExplorerService private _objectExplorerService: IObjectExplorerService, - @IInstantiationService private _instantiationService: IInstantiationService, - @IErrorMessageService protected _errorMessageService: IErrorMessageService - ) { - super(id, label, _queryEditorService, _connectionManagementService, _scriptingService, _errorMessageService); - } - - public async run(actionContext: any): Promise { - this._treeSelectionHandler = this._instantiationService.createInstance(TreeSelectionHandler); - if (actionContext instanceof ObjectExplorerActionsContext) { - //set objectExplorerTreeNode for context menu clicks - this._objectExplorerTreeNode = await getTreeNode(actionContext, this._objectExplorerService); - } - this._treeSelectionHandler.onTreeActionStateChange(true); - let connectionProfile = TreeUpdateUtils.getConnectionProfile(this._objectExplorerTreeNode); - let metadata = (this._objectExplorerTreeNode).metadata; - let ownerUri = this._connectionManagementService.getConnectionUri(connectionProfile); - ownerUri = this._connectionManagementService.getFormattedUri(ownerUri, connectionProfile); - - return super.run({ profile: connectionProfile, object: metadata }).then((result) => { - this._treeSelectionHandler.onTreeActionStateChange(false); - return result; - }); - } -} - -export class OEScriptDeleteAction extends ScriptDeleteAction { - public static ID = 'objectExplorer.' + ScriptDeleteAction.ID; - private _objectExplorerTreeNode: TreeNode; - private _treeSelectionHandler: TreeSelectionHandler; - - constructor( - id: string, label: string, - @IQueryEditorService protected _queryEditorService: IQueryEditorService, - @IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, - @IScriptingService protected _scriptingService: IScriptingService, - @IObjectExplorerService private _objectExplorerService: IObjectExplorerService, - @IInstantiationService private _instantiationService: IInstantiationService, - @IErrorMessageService protected _errorMessageService: IErrorMessageService - ) { - super(id, label, _queryEditorService, _connectionManagementService, _scriptingService, _errorMessageService); - } - - public async run(actionContext: any): Promise { - this._treeSelectionHandler = this._instantiationService.createInstance(TreeSelectionHandler); - if (actionContext instanceof ObjectExplorerActionsContext) { - //set objectExplorerTreeNode for context menu clicks - this._objectExplorerTreeNode = await getTreeNode(actionContext, this._objectExplorerService); - } - this._treeSelectionHandler.onTreeActionStateChange(true); - let connectionProfile = TreeUpdateUtils.getConnectionProfile(this._objectExplorerTreeNode); - let metadata = (this._objectExplorerTreeNode).metadata; - let ownerUri = this._connectionManagementService.getConnectionUri(connectionProfile); - ownerUri = this._connectionManagementService.getFormattedUri(ownerUri, connectionProfile); - - return super.run({ profile: connectionProfile, object: metadata }).then((result) => { - this._treeSelectionHandler.onTreeActionStateChange(false); - return result; - }); - } -} - -export class ObjectExplorerActionUtilities { - - public static readonly objectExplorerElementClass = 'object-element-group'; - public static readonly connectionElementClass = 'connection-tile'; - - public static getScriptMap(treeNode: TreeNode): Map { - let scriptMap = new Map(); - - let isMssqlProvider: boolean = true; - if (treeNode) { - let connectionProfile = treeNode.getConnectionProfile(); - if (connectionProfile) { - isMssqlProvider = connectionProfile.providerName === Constants.mssqlProviderName; - } - } - - let basicScripting = [OEScriptCreateAction, OEScriptDeleteAction]; - let storedProcedureScripting = isMssqlProvider ? [OEScriptCreateAction, OEScriptAlterAction, OEScriptDeleteAction, OEScriptExecuteAction] : - basicScripting; - - let viewScripting = isMssqlProvider ? [OEScriptSelectAction, OEScriptCreateAction, OEScriptAlterAction, OEScriptDeleteAction] : - [OEScriptSelectAction, OEScriptCreateAction, OEScriptDeleteAction]; - - let functionScripting = isMssqlProvider ? [OEScriptCreateAction, OEScriptAlterAction, OEScriptDeleteAction] : - basicScripting; - scriptMap.set(NodeType.AggregateFunction, functionScripting); - scriptMap.set(NodeType.PartitionFunction, functionScripting); - scriptMap.set(NodeType.ScalarValuedFunction, functionScripting); - scriptMap.set(NodeType.Schema, basicScripting); - scriptMap.set(NodeType.StoredProcedure, storedProcedureScripting); - scriptMap.set(NodeType.Table, [OEScriptSelectAction, OEEditDataAction, OEScriptCreateAction, OEScriptDeleteAction]); - scriptMap.set(NodeType.TableValuedFunction, functionScripting); - scriptMap.set(NodeType.User, basicScripting); - scriptMap.set(NodeType.UserDefinedTableType, basicScripting); - scriptMap.set(NodeType.View, viewScripting); - return scriptMap; - } -} diff --git a/src/sql/workbench/parts/objectExplorer/browser/serverTreeActionProvider.ts b/src/sql/workbench/parts/objectExplorer/browser/serverTreeActionProvider.ts index f41ea2da9c..09a15bdbfd 100644 --- a/src/sql/workbench/parts/objectExplorer/browser/serverTreeActionProvider.ts +++ b/src/sql/workbench/parts/objectExplorer/browser/serverTreeActionProvider.ts @@ -14,7 +14,7 @@ import { DeleteConnectionAction, RefreshAction, EditServerGroupAction } from 'sql/workbench/parts/objectExplorer/browser/connectionTreeAction'; import { - ObjectExplorerActionUtilities, ManageConnectionAction, OEAction + ManageConnectionAction, OEAction } from 'sql/workbench/parts/objectExplorer/browser/objectExplorerActions'; import { TreeNode } from 'sql/workbench/parts/objectExplorer/common/treeNode'; import { NodeType } from 'sql/workbench/parts/objectExplorer/common/nodeType'; @@ -172,8 +172,6 @@ export class ServerTreeActionProvider extends ContributableActionProvider { } } - this.addScriptingActions(context, actions); - let serverInfo = this._connectionManagementService.getServerInfo(context.profile.id); let isCloud = serverInfo && serverInfo.isCloud; @@ -207,19 +205,6 @@ export class ServerTreeActionProvider extends ContributableActionProvider { actions.push(this._instantiationService.createInstance(OEAction, RestoreAction.ID, RestoreAction.LABEL)); } } - - private addScriptingActions(context: ObjectExplorerContext, actions: IAction[]): void { - if (this._scriptingService.isProviderRegistered(context.profile.providerName)) { - let scriptMap: Map = ObjectExplorerActionUtilities.getScriptMap(context.treeNode); - let supportedActions = scriptMap.get(context.treeNode.nodeTypeId); - let self = this; - if (supportedActions !== null && supportedActions !== undefined) { - supportedActions.forEach(action => { - actions.push(self._instantiationService.createInstance(action, action.ID, action.LABEL)); - }); - } - } - } } interface ObjectExplorerContext { diff --git a/src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScripting.contribution.ts b/src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScripting.contribution.ts new file mode 100644 index 0000000000..2142898893 --- /dev/null +++ b/src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScripting.contribution.ts @@ -0,0 +1,111 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the Source EULA. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { SCRIPT_AS_SELECT_COMMAND_ID, EDIT_DATA_COMMAND_ID, SCRIPT_AS_CREATE_COMMAND_ID, SCRIPT_AS_EXECUTE_COMMAND_ID, SCRIPT_AS_ALTER_COMMAND_ID, SCRIPT_AS_DELETE_COMMAND_ID } from 'sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScriptingActions'; +import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions'; +import { localize } from 'vs/nls'; +import { ConnectionContextKey } from 'sql/workbench/parts/connection/common/connectionContextKey'; +import { TreeNodeContextKey } from 'sql/workbench/parts/objectExplorer/common/treeNodeContextKey'; +import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; + +MenuRegistry.appendMenuItem(MenuId.ObjectExplorerItemContext, { + group: 'scripting', + order: 3, + command: { + id: SCRIPT_AS_SELECT_COMMAND_ID, + title: localize('scriptSelect', "Select Top 1000") + }, + when: ContextKeyExpr.or(TreeNodeContextKey.NodeType.isEqualTo('Table'), TreeNodeContextKey.NodeType.isEqualTo('View')) +}); + +MenuRegistry.appendMenuItem(MenuId.ObjectExplorerItemContext, { + group: 'scripting', + order: 3, + command: { + id: EDIT_DATA_COMMAND_ID, + title: localize('editData', "Edit Data") + }, + when: TreeNodeContextKey.NodeType.isEqualTo('Table') +}); + +MenuRegistry.appendMenuItem(MenuId.ObjectExplorerItemContext, { + group: 'scripting', + order: 3, + command: { + id: SCRIPT_AS_CREATE_COMMAND_ID, + title: localize('scriptCreate', "Script as Create") + }, + when: ContextKeyExpr.or( + TreeNodeContextKey.NodeType.isEqualTo('Table'), + TreeNodeContextKey.NodeType.isEqualTo('View'), + TreeNodeContextKey.NodeType.isEqualTo('Schema'), + TreeNodeContextKey.NodeType.isEqualTo('User'), + TreeNodeContextKey.NodeType.isEqualTo('UserDefinedTableType'), + TreeNodeContextKey.NodeType.isEqualTo('StoredProcedure'), + TreeNodeContextKey.NodeType.isEqualTo('AggregateFunction'), + TreeNodeContextKey.NodeType.isEqualTo('PartitionFunction'), + TreeNodeContextKey.NodeType.isEqualTo('ScalarValuedFunction'), + TreeNodeContextKey.NodeType.isEqualTo('TableValuedFunction')) +}); + +MenuRegistry.appendMenuItem(MenuId.ObjectExplorerItemContext, { + group: 'scripting', + order: 3, + command: { + id: SCRIPT_AS_EXECUTE_COMMAND_ID, + title: localize('scriptExecute', "Script as Execute") + }, + when: ContextKeyExpr.and(ConnectionContextKey.Provider.isEqualTo('MSSQL'), TreeNodeContextKey.NodeType.isEqualTo('StoredProcedure')) +}); + +MenuRegistry.appendMenuItem(MenuId.ObjectExplorerItemContext, { + group: 'scripting', + order: 3, + command: { + id: SCRIPT_AS_ALTER_COMMAND_ID, + title: localize('scriptAlter', "Script as Alter") + }, + when: + ContextKeyExpr.or( + ContextKeyExpr.and( + ConnectionContextKey.Provider.isEqualTo('MSSQL'), + TreeNodeContextKey.NodeType.isEqualTo('StoredProcedure')), + ContextKeyExpr.and( + ConnectionContextKey.Provider.isEqualTo('MSSQL'), + TreeNodeContextKey.NodeType.isEqualTo('View')), + ContextKeyExpr.and( + ConnectionContextKey.Provider.isEqualTo('MSSQL'), + TreeNodeContextKey.NodeType.isEqualTo('AggregateFunction')), + ContextKeyExpr.and( + ConnectionContextKey.Provider.isEqualTo('MSSQL'), + TreeNodeContextKey.NodeType.isEqualTo('PartitionFunction')), + ContextKeyExpr.and( + ConnectionContextKey.Provider.isEqualTo('MSSQL'), + TreeNodeContextKey.NodeType.isEqualTo('ScalarValuedFunction')), + ContextKeyExpr.and( + ConnectionContextKey.Provider.isEqualTo('MSSQL'), + TreeNodeContextKey.NodeType.isEqualTo('TableValuedFunction')), + ) +}); + +MenuRegistry.appendMenuItem(MenuId.ObjectExplorerItemContext, { + group: 'scripting', + order: 3, + command: { + id: SCRIPT_AS_DELETE_COMMAND_ID, + title: localize('scriptDelete', "Script as Drop") + }, + when: ContextKeyExpr.or( + TreeNodeContextKey.NodeType.isEqualTo('Table'), + TreeNodeContextKey.NodeType.isEqualTo('View'), + TreeNodeContextKey.NodeType.isEqualTo('Schema'), + TreeNodeContextKey.NodeType.isEqualTo('User'), + TreeNodeContextKey.NodeType.isEqualTo('UserDefinedTableType'), + TreeNodeContextKey.NodeType.isEqualTo('StoredProcedure'), + TreeNodeContextKey.NodeType.isEqualTo('AggregateFunction'), + TreeNodeContextKey.NodeType.isEqualTo('PartitionFunction'), + TreeNodeContextKey.NodeType.isEqualTo('ScalarValuedFunction'), + TreeNodeContextKey.NodeType.isEqualTo('TableValuedFunction')) +}); diff --git a/src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScriptingActions.ts b/src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScriptingActions.ts new file mode 100644 index 0000000000..1cb24629ca --- /dev/null +++ b/src/sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScriptingActions.ts @@ -0,0 +1,146 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the Source EULA. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { ScriptSelectAction, EditDataAction, ScriptCreateAction, ScriptExecuteAction, ScriptAlterAction, ScriptDeleteAction } from 'sql/workbench/electron-browser/scriptingActions'; +import { TreeSelectionHandler } from 'sql/workbench/parts/objectExplorer/browser/treeSelectionHandler'; +import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement'; +import { IObjectExplorerService } from 'sql/workbench/services/objectExplorer/common/objectExplorerService'; +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { ObjectExplorerActionsContext, getTreeNode } from 'sql/workbench/parts/objectExplorer/browser/objectExplorerActions'; +import { TreeUpdateUtils } from 'sql/workbench/parts/objectExplorer/browser/treeUpdateUtils'; +import { TreeNode } from 'sql/workbench/parts/objectExplorer/common/treeNode'; +import { CommandsRegistry } from 'vs/platform/commands/common/commands'; + +export const SCRIPT_AS_CREATE_COMMAND_ID = 'objectExplorer.scriptAsCreate'; +export const SCRIPT_AS_DELETE_COMMAND_ID = 'objectExplorer.scriptAsDelete'; +export const SCRIPT_AS_SELECT_COMMAND_ID = 'objectExplorer.scriptAsSelect'; +export const SCRIPT_AS_EXECUTE_COMMAND_ID = 'objectExplorer.scriptAsExecute'; +export const SCRIPT_AS_ALTER_COMMAND_ID = 'objectExplorer.scriptAsAlter'; +export const EDIT_DATA_COMMAND_ID = 'objectExplorer.scriptAsAlter'; + +// Script as Create +CommandsRegistry.registerCommand({ + id: SCRIPT_AS_SELECT_COMMAND_ID, + handler: async (accessor, args: ObjectExplorerActionsContext) => { + const instantiationService = accessor.get(IInstantiationService); + const connectionManagementService = accessor.get(IConnectionManagementService); + const objectExplorerService = accessor.get(IObjectExplorerService); + const selectionHandler = instantiationService.createInstance(TreeSelectionHandler); + const node = await getTreeNode(args, objectExplorerService); + selectionHandler.onTreeActionStateChange(true); + let connectionProfile = TreeUpdateUtils.getConnectionProfile(node); + let ownerUri = connectionManagementService.getConnectionUri(connectionProfile); + ownerUri = connectionManagementService.getFormattedUri(ownerUri, connectionProfile); + let metadata = node.metadata; + + return instantiationService.createInstance(ScriptSelectAction, ScriptSelectAction.ID, ScriptSelectAction.LABEL).run({ profile: connectionProfile, object: metadata }).then((result) => { + selectionHandler.onTreeActionStateChange(false); + return result; + }); + } +}); + +CommandsRegistry.registerCommand({ + id: EDIT_DATA_COMMAND_ID, + handler: async (accessor, args: ObjectExplorerActionsContext) => { + const instantiationService = accessor.get(IInstantiationService); + const objectExplorerService = accessor.get(IObjectExplorerService); + const selectionHandler = instantiationService.createInstance(TreeSelectionHandler); + const node = await getTreeNode(args, objectExplorerService); + selectionHandler.onTreeActionStateChange(true); + let connectionProfile = TreeUpdateUtils.getConnectionProfile(node); + let metadata = node.metadata; + + return instantiationService.createInstance(EditDataAction, EditDataAction.ID, EditDataAction.LABEL).run({ profile: connectionProfile, object: metadata }).then((result) => { + selectionHandler.onTreeActionStateChange(false); + return true; + }); + } +}); + +CommandsRegistry.registerCommand({ + id: SCRIPT_AS_CREATE_COMMAND_ID, + handler: async (accessor, args: ObjectExplorerActionsContext) => { + const instantiationService = accessor.get(IInstantiationService); + const connectionManagementService = accessor.get(IConnectionManagementService); + const objectExplorerService = accessor.get(IObjectExplorerService); + const selectionHandler = instantiationService.createInstance(TreeSelectionHandler); + const node = await getTreeNode(args, objectExplorerService); + selectionHandler.onTreeActionStateChange(true); + let connectionProfile = TreeUpdateUtils.getConnectionProfile(node); + let metadata = node.metadata; + let ownerUri = connectionManagementService.getConnectionUri(connectionProfile); + ownerUri = connectionManagementService.getFormattedUri(ownerUri, connectionProfile); + + return instantiationService.createInstance(ScriptCreateAction, ScriptCreateAction.ID, ScriptCreateAction.LABEL).run({ profile: connectionProfile, object: metadata }).then((result) => { + selectionHandler.onTreeActionStateChange(false); + return result; + }); + } +}); + +CommandsRegistry.registerCommand({ + id: SCRIPT_AS_EXECUTE_COMMAND_ID, + handler: async (accessor, args: ObjectExplorerActionsContext) => { + const instantiationService = accessor.get(IInstantiationService); + const connectionManagementService = accessor.get(IConnectionManagementService); + const objectExplorerService = accessor.get(IObjectExplorerService); + const selectionHandler = instantiationService.createInstance(TreeSelectionHandler); + const node = await getTreeNode(args, objectExplorerService); + selectionHandler.onTreeActionStateChange(true); + let connectionProfile = TreeUpdateUtils.getConnectionProfile(node); + let metadata = node.metadata; + let ownerUri = connectionManagementService.getConnectionUri(connectionProfile); + ownerUri = connectionManagementService.getFormattedUri(ownerUri, connectionProfile); + + return instantiationService.createInstance(ScriptExecuteAction, ScriptExecuteAction.ID, ScriptExecuteAction.LABEL).run({ profile: connectionProfile, object: metadata }).then((result) => { + selectionHandler.onTreeActionStateChange(false); + return result; + }); + } +}); + +CommandsRegistry.registerCommand({ + id: SCRIPT_AS_ALTER_COMMAND_ID, + handler: async (accessor, args: ObjectExplorerActionsContext) => { + const instantiationService = accessor.get(IInstantiationService); + const connectionManagementService = accessor.get(IConnectionManagementService); + const objectExplorerService = accessor.get(IObjectExplorerService); + const selectionHandler = instantiationService.createInstance(TreeSelectionHandler); + const node = await getTreeNode(args, objectExplorerService); + selectionHandler.onTreeActionStateChange(true); + let connectionProfile = TreeUpdateUtils.getConnectionProfile(node); + let metadata = node.metadata; + let ownerUri = connectionManagementService.getConnectionUri(connectionProfile); + ownerUri = connectionManagementService.getFormattedUri(ownerUri, connectionProfile); + + return instantiationService.createInstance(ScriptAlterAction, ScriptAlterAction.ID, ScriptAlterAction.LABEL).run({ profile: connectionProfile, object: metadata }).then((result) => { + selectionHandler.onTreeActionStateChange(false); + return result; + }); + } +}); + +CommandsRegistry.registerCommand({ + id: SCRIPT_AS_DELETE_COMMAND_ID, + handler: async (accessor, args: ObjectExplorerActionsContext) => { + const instantiationService = accessor.get(IInstantiationService); + const connectionManagementService = accessor.get(IConnectionManagementService); + const objectExplorerService = accessor.get(IObjectExplorerService); + const selectionHandler = instantiationService.createInstance(TreeSelectionHandler); + //set objectExplorerTreeNode for context menu clicks + const node = await getTreeNode(args, objectExplorerService); + selectionHandler.onTreeActionStateChange(true); + const connectionProfile = TreeUpdateUtils.getConnectionProfile(node); + const metadata = node.metadata; + let ownerUri = connectionManagementService.getConnectionUri(connectionProfile); + ownerUri = connectionManagementService.getFormattedUri(ownerUri, connectionProfile); + + return instantiationService.createInstance(ScriptDeleteAction, ScriptDeleteAction.ID, ScriptDeleteAction.LABEL).run({ profile: connectionProfile, object: metadata }).then((result) => { + selectionHandler.onTreeActionStateChange(false); + return result; + }); + } +}); diff --git a/src/vs/workbench/workbench.main.ts b/src/vs/workbench/workbench.main.ts index 572c46ceb1..7b165fc4e3 100644 --- a/src/vs/workbench/workbench.main.ts +++ b/src/vs/workbench/workbench.main.ts @@ -490,6 +490,7 @@ import 'sql/workbench/parts/accounts/browser/accounts.contribution'; import 'sql/workbench/parts/profiler/browser/profiler.contribution'; import 'sql/workbench/parts/profiler/browser/profilerActions.contribution'; import 'sql/workbench/parts/objectExplorer/common/serverGroup.contribution'; +import 'sql/workbench/parts/objectExplorer/electron-browser/objectExplorerScripting.contribution'; import 'sql/platform/accounts/browser/accountManagement.contribution'; // dashboard