fix(plugin-resource-manager): CB-2017 remove scripts service

This commit is contained in:
Naumov Alexey
2022-05-11 19:23:09 +02:00
parent f84380ba5a
commit 963099f6f0
7 changed files with 104 additions and 122 deletions
@@ -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);
}
@@ -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<IResourceData> {
const workspace = parents[1];
const workspaceNode = await this.navNodeResource.load(workspace);
const resourcePath = nodeId.substring(workspace.length).slice(1);
return {
projectId: workspaceNode.name ?? '',
resourcePath,
};
}
}
@@ -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';
@@ -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()];
}
}
@@ -2,4 +2,4 @@ import { resourceManagerPlugin } from './manifest';
export default resourceManagerPlugin;
export * from './ScriptsManager/ScriptsManagerService';
export * from './NavResourceNodeService';
@@ -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,
],
};
@@ -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<void> { }
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,
});
}