diff --git a/webapp/packages/plugin-resource-manager/src/NavResourceNodeService.ts b/webapp/packages/plugin-resource-manager/src/NavResourceNodeService.ts new file mode 100644 index 0000000000..f93a9249b0 --- /dev/null +++ b/webapp/packages/plugin-resource-manager/src/NavResourceNodeService.ts @@ -0,0 +1,86 @@ +/* + * CloudBeaver - Cloud Database Manager + * Copyright (C) 2020-2022 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0. + * you may not use this file except in compliance with the License. + */ + +import { NavTreeResource } from '@cloudbeaver/core-app'; +import { injectable } from '@cloudbeaver/core-di'; + +import { ProjectsResource } from './ProjectsResource'; +import { ResourceManagerResource } from './ResourceManagerResource'; +import { RESOURCES_NODE_PATH } from './RESOURCES_NODE_PATH'; +import { SCRIPT_EXTENSION } from './SCRIPT_EXTENSION'; + +interface IResourceData { + projectId: string; + resourcePath: string; +} + +@injectable() +export class NavResourceNodeService { + constructor( + private readonly navTreeResource: NavTreeResource, + private readonly projectsResource: ProjectsResource, + private readonly resourceManagerResource: ResourceManagerResource, + ) { } + + async saveScript(name: string, script: string) { + name = name + SCRIPT_EXTENSION; + const projectName = await this.getUserProjectName(); + await this.resourceManagerResource.createResource(projectName, name, false); + await this.resourceManagerResource.writeResource(projectName, name, script); + await this.syncNodes(); + + return name; + } + + async delete(nodeId: string) { + const resourceData = this.getResourceData(nodeId); + await this.resourceManagerResource.deleteResource(resourceData.projectId, resourceData.resourcePath); + await this.syncNodes(); + } + + async read(nodeId: string) { + const resourceData = this.getResourceData(nodeId); + return await this.resourceManagerResource.readResource(resourceData.projectId, resourceData.resourcePath); + } + + async write(nodeId: string, value: string) { + const resourceData = this.getResourceData(nodeId); + await this.resourceManagerResource.writeResource(resourceData.projectId, resourceData.resourcePath, value); + } + + private async syncNodes() { + const userProjectName = await this.getUserProjectName(); + let path = RESOURCES_NODE_PATH; + + if (userProjectName) { + path = path + '/' + userProjectName; + } + + await this.navTreeResource.refreshTree(path); + } + + private getResourceData(nodeId: string): IResourceData { + const parts = nodeId.replace('//', '\\').split('/'); + const projectId = parts[1]; + const resourcePath = parts.slice(2).join('/'); + + return { + projectId, + resourcePath, + }; + } + + private async getUserProjectName() { + await this.projectsResource.load(); + return this.projectsResource.userProject ? this.projectsResource.userProject.name : ''; + } +} + +export function isScript(nodeId: string) { + return nodeId.includes(SCRIPT_EXTENSION); +} \ No newline at end of file diff --git a/webapp/packages/plugin-resource-manager/src/ResourceManagerService.ts b/webapp/packages/plugin-resource-manager/src/ResourceManagerService.ts index f2b015ee0a..cf642ccd0a 100644 --- a/webapp/packages/plugin-resource-manager/src/ResourceManagerService.ts +++ b/webapp/packages/plugin-resource-manager/src/ResourceManagerService.ts @@ -8,21 +8,13 @@ import { makeObservable, observable } from 'mobx'; -import { NavNodeInfoResource } from '@cloudbeaver/core-app'; import { injectable } from '@cloudbeaver/core-di'; -interface IResourceData { - projectId: string; - resourcePath: string; -} - @injectable() export class ResourceManagerService { enabled = false; - constructor( - private readonly navNodeResource: NavNodeInfoResource - ) { + constructor() { this.toggleEnabled = this.toggleEnabled.bind(this); makeObservable(this, { @@ -33,15 +25,4 @@ export class ResourceManagerService { toggleEnabled() { this.enabled = !this.enabled; } - - async getResourceData(nodeId: string, parents: string[]): Promise { - const workspace = parents[1]; - const workspaceNode = await this.navNodeResource.load(workspace); - const resourcePath = nodeId.substring(workspace.length).slice(1); - - return { - projectId: workspaceNode.name ?? '', - resourcePath, - }; - } } \ No newline at end of file diff --git a/webapp/packages/plugin-resource-manager/src/SCRIPT_EXTENSION.ts b/webapp/packages/plugin-resource-manager/src/SCRIPT_EXTENSION.ts new file mode 100644 index 0000000000..a2d5373bf9 --- /dev/null +++ b/webapp/packages/plugin-resource-manager/src/SCRIPT_EXTENSION.ts @@ -0,0 +1,9 @@ +/* + * CloudBeaver - Cloud Database Manager + * Copyright (C) 2020-2022 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0. + * you may not use this file except in compliance with the License. + */ + +export const SCRIPT_EXTENSION = '.sql'; \ No newline at end of file diff --git a/webapp/packages/plugin-resource-manager/src/ScriptsManager/ScriptsManagerService.ts b/webapp/packages/plugin-resource-manager/src/ScriptsManager/ScriptsManagerService.ts deleted file mode 100644 index 404c6a2db1..0000000000 --- a/webapp/packages/plugin-resource-manager/src/ScriptsManager/ScriptsManagerService.ts +++ /dev/null @@ -1,94 +0,0 @@ -/* - * CloudBeaver - Cloud Database Manager - * Copyright (C) 2020-2022 DBeaver Corp and others - * - * Licensed under the Apache License, Version 2.0. - * you may not use this file except in compliance with the License. - */ - -import { NavTreeResource, NavNodeInfoResource } from '@cloudbeaver/core-app'; -import { injectable } from '@cloudbeaver/core-di'; - -import { ProjectsResource } from '../ProjectsResource'; -import { ResourceManagerResource } from '../ResourceManagerResource'; -import { ResourceManagerService } from '../ResourceManagerService'; -import { RESOURCES_NODE_PATH } from '../RESOURCES_NODE_PATH'; - -const SCRIPT_EXTENSION = '.sql'; - -@injectable() -export class ScriptsManagerService { - constructor( - private readonly navTreeResource: NavTreeResource, - private readonly projectsResource: ProjectsResource, - private readonly navNodeInfoResource: NavNodeInfoResource, - private readonly resourceManagerResource: ResourceManagerResource, - private readonly resourceManagerService: ResourceManagerService, - ) { } - - isScript(nodeId: string) { - return nodeId.includes(SCRIPT_EXTENSION); - } - - async saveScript(name: string, script: string) { - name = name + SCRIPT_EXTENSION; - const projectName = await this.getUserProjectName(); - await this.resourceManagerResource.createResource(projectName, name, false); - await this.resourceManagerResource.writeResource(projectName, name, script); - await this.syncNodes(); - - const nodeId = await this.getNodeIdFromScript(name); - const node = await this.navNodeInfoResource.load(nodeId); - - return node; - } - - async deleteScript(nodeId: string) { - const parents = await this.getScriptParents(nodeId); - const resourceData = await this.resourceManagerService.getResourceData(nodeId, parents); - - await this.resourceManagerResource.deleteResource(resourceData.projectId, resourceData.resourcePath); - await this.syncNodes(); - } - - async readScript(nodeId: string) { - const parents = await this.getScriptParents(nodeId); - const resourceData = await this.resourceManagerService.getResourceData(nodeId, parents); - - return await this.resourceManagerResource.readResource(resourceData.projectId, resourceData.resourcePath); - } - - async writeScript(nodeId: string, value: string) { - const parents = await this.getScriptParents(nodeId); - const resourceData = await this.resourceManagerService.getResourceData(nodeId, parents); - await this.resourceManagerResource.writeResource(resourceData.projectId, resourceData.resourcePath, value); - } - - private async getNodeIdFromScript(scriptName: string) { - const scriptsPath = await this.getScriptsPath(); - return `${scriptsPath}/${scriptName}`; - } - - private async syncNodes() { - await this.navTreeResource.refreshTree(await this.getScriptsPath()); - } - - private async getScriptsPath() { - const userProjectName = await this.getUserProjectName(); - - if (userProjectName) { - return `${RESOURCES_NODE_PATH}/${userProjectName}`; - } - - return RESOURCES_NODE_PATH; - } - - private async getUserProjectName() { - await this.projectsResource.load(); - return this.projectsResource.userProject ? this.projectsResource.userProject.name : ''; - } - - private async getScriptParents(nodeId: string) { - return [RESOURCES_NODE_PATH, await this.getScriptsPath()]; - } -} \ No newline at end of file diff --git a/webapp/packages/plugin-resource-manager/src/index.ts b/webapp/packages/plugin-resource-manager/src/index.ts index 49c1961a43..350eca37ff 100644 --- a/webapp/packages/plugin-resource-manager/src/index.ts +++ b/webapp/packages/plugin-resource-manager/src/index.ts @@ -2,4 +2,4 @@ import { resourceManagerPlugin } from './manifest'; export default resourceManagerPlugin; -export * from './ScriptsManager/ScriptsManagerService'; \ No newline at end of file +export * from './NavResourceNodeService'; \ No newline at end of file diff --git a/webapp/packages/plugin-resource-manager/src/manifest.ts b/webapp/packages/plugin-resource-manager/src/manifest.ts index f9bc76535b..0d238bc57c 100644 --- a/webapp/packages/plugin-resource-manager/src/manifest.ts +++ b/webapp/packages/plugin-resource-manager/src/manifest.ts @@ -9,11 +9,11 @@ import type { PluginManifest } from '@cloudbeaver/core-di'; import { LocaleService } from './LocaleService'; +import { NavResourceNodeService } from './NavResourceNodeService'; import { PluginBootstrap } from './PluginBootstrap'; import { ProjectsResource } from './ProjectsResource'; import { ResourceManagerResource } from './ResourceManagerResource'; import { ResourceManagerService } from './ResourceManagerService'; -import { ScriptsManagerService } from './ScriptsManager/ScriptsManagerService'; export const resourceManagerPlugin: PluginManifest = { info: { name: 'Resource manager plugin' }, @@ -23,6 +23,6 @@ export const resourceManagerPlugin: PluginManifest = { ResourceManagerService, ProjectsResource, ResourceManagerResource, - ScriptsManagerService, + NavResourceNodeService, ], }; \ No newline at end of file diff --git a/webapp/packages/plugin-sql-editor-navigation-tab-resource/src/PluginBootstrap.ts b/webapp/packages/plugin-sql-editor-navigation-tab-resource/src/PluginBootstrap.ts index 19f62dddb7..6dccbd4e0d 100644 --- a/webapp/packages/plugin-sql-editor-navigation-tab-resource/src/PluginBootstrap.ts +++ b/webapp/packages/plugin-sql-editor-navigation-tab-resource/src/PluginBootstrap.ts @@ -9,14 +9,14 @@ import { INodeNavigationData, NavigationTabsService, NavNodeInfoResource, NavNodeManagerService } from '@cloudbeaver/core-app'; import { Bootstrap, injectable } from '@cloudbeaver/core-di'; import { NotificationService } from '@cloudbeaver/core-events'; -import { ScriptsManagerService } from '@cloudbeaver/plugin-resource-manager'; +import { isScript, NavResourceNodeService } from '@cloudbeaver/plugin-resource-manager'; import { SqlEditorNavigatorService, SqlEditorTabService } from '@cloudbeaver/plugin-sql-editor-navigation-tab'; @injectable() export class PluginBootstrap extends Bootstrap { constructor( private readonly navNodeManagerService: NavNodeManagerService, - private readonly scriptsManagerService: ScriptsManagerService, + private readonly navResourceNodeService: NavResourceNodeService, private readonly sqlEditorTabService: SqlEditorTabService, private readonly navNodeInfoResource: NavNodeInfoResource, private readonly navigationTabsService: NavigationTabsService, @@ -33,7 +33,7 @@ export class PluginBootstrap extends Bootstrap { load(): void | Promise { } private async navigationHandler(data: INodeNavigationData) { - if (this.scriptsManagerService.isScript(data.nodeId)) { + if (isScript(data.nodeId)) { try { const existingTab = this.sqlEditorTabService.sqlEditorTabs.find( tab => tab.handlerState.associatedScriptId === data.nodeId @@ -42,12 +42,12 @@ export class PluginBootstrap extends Bootstrap { if (existingTab) { this.navigationTabsService.selectTab(existingTab.id); } else { - const scriptValue = await this.scriptsManagerService.readScript(data.nodeId); + const value = await this.navResourceNodeService.read(data.nodeId); const node = await this.navNodeInfoResource.load(data.nodeId); await this.sqlEditorNavigatorService.openNewEditor({ name: node.name ?? 'Unknown script', - query: scriptValue, + query: value, associatedScriptId: data.nodeId, }); }