From 955c8182345085a2a8ed6c6c597690832f4c2401 Mon Sep 17 00:00:00 2001 From: Brett Saviano Date: Tue, 13 Feb 2024 16:40:10 -0500 Subject: [PATCH] Fire source control hooks when creating/opening/editing/deleting projects --- src/commands/project.ts | 76 ++++++++++++++++++- src/commands/studio.ts | 55 ++++++++++---- src/explorer/models/projectNode.ts | 7 ++ .../FileSystemProvider/FileSystemProvider.ts | 13 +++- 4 files changed, 135 insertions(+), 16 deletions(-) diff --git a/src/commands/project.ts b/src/commands/project.ts index 99a5c9b8..8da78de2 100644 --- a/src/commands/project.ts +++ b/src/commands/project.ts @@ -12,6 +12,7 @@ import { isCSPFile } from "../providers/FileSystemProvider/FileSystemProvider"; import { notNull, outputChannel } from "../utils"; import { pickServerAndNamespace } from "./addServerNamespaceToWorkspace"; import { exportList } from "./export"; +import { OtherStudioAction, StudioActions } from "./studio"; export interface ProjectItem { Name: string; @@ -137,6 +138,20 @@ export async function createProject(node: NodeBase | undefined, api?: AtelierAPI return; } + // Technically a project is a "document", so tell the server that we created it + try { + await new StudioActions().fireProjectUserAction(api, name, OtherStudioAction.CreatedNewDocument); + await new StudioActions().fireProjectUserAction(api, name, OtherStudioAction.FirstTimeDocumentSave); + } catch (error) { + let message = `Source control actions failed for project '${name}'.`; + if (error && error.errorText && error.errorText !== "") { + outputChannel.appendLine("\n" + error.errorText); + outputChannel.show(true); + message += " Check 'ObjectScript' output channel for details."; + } + return vscode.window.showErrorMessage(message, "Dismiss"); + } + // Refresh the explorer projectsExplorerProvider.refresh(); @@ -179,6 +194,19 @@ export async function deleteProject(node: ProjectNode | undefined): Promise return vscode.window.showErrorMessage(message, "Dismiss"); } + // Technically a project is a "document", so tell the server that we deleted it + try { + await new StudioActions().fireProjectUserAction(api, project, OtherStudioAction.DeletedDocument); + } catch (error) { + let message = `'DeletedDocument' source control action failed for project '${project}'.`; + if (error && error.errorText && error.errorText !== "") { + outputChannel.appendLine("\n" + error.errorText); + outputChannel.show(true); + message += " Check 'ObjectScript' output channel for details."; + } + return vscode.window.showErrorMessage(message, "Dismiss"); + } + // Refresh the explorer projectsExplorerProvider.refresh(); @@ -706,6 +734,20 @@ export async function modifyProject( return; } } + + // Technically a project is a "document", so tell the server that we're opening it + try { + await new StudioActions().fireProjectUserAction(api, project, OtherStudioAction.OpenedDocument); + } catch (error) { + let message = `'OpenedDocument' source control action failed for project '${project}'.`; + if (error && error.errorText && error.errorText !== "") { + outputChannel.appendLine("\n" + error.errorText); + outputChannel.show(true); + message += " Check 'ObjectScript' output channel for details."; + } + return vscode.window.showErrorMessage(message, "Dismiss"); + } + let items: ProjectItem[] = await api .actionQuery("SELECT Name, Type FROM %Studio.Project_ProjectItemsList(?,?) WHERE Type != 'GBL'", [project, "1"]) .then((data) => data.result.content); @@ -862,6 +904,23 @@ export async function modifyProject( } try { + if (add.length || remove.length) { + // Technically a project is a "document", so tell the server that we're editing it + const studioActions = new StudioActions(); + await studioActions.fireProjectUserAction(api, project, OtherStudioAction.AttemptedEdit); + if (studioActions.projectEditAnswer != "1") { + // Don't perform the edit + if (studioActions.projectEditAnswer == "-1") { + // Source control action failed + vscode.window.showErrorMessage( + `'AttemptedEdit' source control action failed for project '${project}'. Check the 'ObjectScript' Output channel for details.`, + "Dismiss" + ); + } + return; + } + } + if (remove.length) { // Delete the obsolete items await api.actionQuery( @@ -900,7 +959,7 @@ export async function modifyProject( // Refresh the files explorer if there's an isfs folder for this project if (node == undefined && isfsFolderForProject(project, node ?? api.configName) != -1) { - await vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer"); + vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer"); } } } @@ -1070,6 +1129,21 @@ export async function addIsfsFileToProject( try { if (add.length) { + // Technically a project is a "document", so tell the server that we're editing it + const studioActions = new StudioActions(); + await studioActions.fireProjectUserAction(api, project, OtherStudioAction.AttemptedEdit); + if (studioActions.projectEditAnswer != "1") { + // Don't perform the edit + if (studioActions.projectEditAnswer == "-1") { + // Source control action failed + vscode.window.showErrorMessage( + `'AttemptedEdit' source control action failed for project '${project}'. Check the 'ObjectScript' Output channel for details.`, + "Dismiss" + ); + } + return; + } + // Add any new items await api.actionQuery( `INSERT INTO %Studio.ProjectItem (Project,Name,Type) SELECT * FROM (${add diff --git a/src/commands/studio.ts b/src/commands/studio.ts index b20e2822..ea6fd6a7 100644 --- a/src/commands/studio.ts +++ b/src/commands/studio.ts @@ -6,7 +6,6 @@ import { DocumentContentProvider } from "../providers/DocumentContentProvider"; import { ClassNode } from "../explorer/models/classNode"; import { PackageNode } from "../explorer/models/packageNode"; import { RoutineNode } from "../explorer/models/routineNode"; -import { NodeBase } from "../explorer/models/nodeBase"; import { importAndCompile } from "./compile"; import { ProjectNode } from "../explorer/models/projectNode"; import { openCustomEditors } from "../providers/RuleEditorProvider"; @@ -72,18 +71,17 @@ export class StudioActions { private uri: vscode.Uri; private api: AtelierAPI; private name: string; + public projectEditAnswer?: string; public constructor(uriOrNode?: vscode.Uri | PackageNode | ClassNode | RoutineNode) { if (uriOrNode instanceof vscode.Uri) { - const uri: vscode.Uri = uriOrNode; - this.uri = uri; - this.name = getServerName(uri); - this.api = new AtelierAPI(uri); + this.uri = uriOrNode; + this.name = getServerName(uriOrNode); + this.api = new AtelierAPI(uriOrNode); } else if (uriOrNode) { - const node: NodeBase = uriOrNode; - this.api = new AtelierAPI(node.workspaceFolder); - this.api.setNamespace(node.namespace); - this.name = node instanceof PackageNode ? node.fullName + ".PKG" : node.fullName; + this.api = new AtelierAPI(uriOrNode.workspaceFolder); + this.api.setNamespace(uriOrNode.namespace); + this.name = uriOrNode instanceof PackageNode ? uriOrNode.fullName + ".PKG" : uriOrNode.fullName; } else { this.api = new AtelierAPI(); } @@ -105,6 +103,22 @@ export class StudioActions { ); } + /** Fire UserAction `id` on server `api` for project `name`. */ + public async fireProjectUserAction(api: AtelierAPI, name: string, id: OtherStudioAction): Promise { + this.api = api; + this.name = `${name}.PRJ`; + return this.userAction( + { + id: id.toString(), + label: getOtherStudioActionLabel(id), + }, + false, + "", + "", + 1 + ); + } + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types public processUserAction(userAction): Thenable { const serverAction = parseInt(userAction.action || 0, 10); @@ -318,8 +332,14 @@ export class StudioActions { const attemptedEditLabel = getOtherStudioActionLabel(OtherStudioAction.AttemptedEdit); if (afterUserAction && actionToProcess.errorText !== "") { if (action.label === attemptedEditLabel) { - suppressEditListenerMap.set(this.uri.toString(), true); - await vscode.commands.executeCommand("workbench.action.files.revert", this.uri); + if (this.name.toUpperCase().endsWith(".PRJ")) { + // Store the "answer" so the caller knows there was an error + this.projectEditAnswer = "-1"; + } else if (this.uri) { + // Only revert if we have a URI + suppressEditListenerMap.set(this.uri.toString(), true); + await vscode.commands.executeCommand("workbench.action.files.revert", this.uri); + } } outputChannel.appendLine(actionToProcess.errorText); outputChannel.show(); @@ -327,9 +347,16 @@ export class StudioActions { if (actionToProcess && !afterUserAction) { const answer = await this.processUserAction(actionToProcess); // call AfterUserAction only if there is a valid answer - if (action.label === attemptedEditLabel && answer !== "1") { - suppressEditListenerMap.set(this.uri.toString(), true); - await vscode.commands.executeCommand("workbench.action.files.revert", this.uri); + if (action.label === attemptedEditLabel) { + if (answer != "1" && this.uri) { + // Only revert if we have a URI + suppressEditListenerMap.set(this.uri.toString(), true); + await vscode.commands.executeCommand("workbench.action.files.revert", this.uri); + } + if (this.name.toUpperCase().endsWith(".PRJ")) { + // Store the answer + this.projectEditAnswer = answer; + } } if (answer) { answer.msg || answer.msg === "" diff --git a/src/explorer/models/projectNode.ts b/src/explorer/models/projectNode.ts index 9823c489..ef365578 100644 --- a/src/explorer/models/projectNode.ts +++ b/src/explorer/models/projectNode.ts @@ -1,6 +1,8 @@ import * as vscode from "vscode"; import { NodeBase, NodeOptions } from "./nodeBase"; import { ProjectRootNode } from "./projectRootNode"; +import { OtherStudioAction, StudioActions } from "../../commands/studio"; +import { AtelierAPI } from "../../api"; export class ProjectNode extends NodeBase { private description: string; @@ -13,6 +15,11 @@ export class ProjectNode extends NodeBase { const children = []; let node: ProjectRootNode; + // Technically a project is a "document", so tell the server that we're opening it + const api = new AtelierAPI(this.workspaceFolderUri); + api.setNamespace(this.namespace); + await new StudioActions().fireProjectUserAction(api, this.label, OtherStudioAction.OpenedDocument); + node = new ProjectRootNode( "Classes", "", diff --git a/src/providers/FileSystemProvider/FileSystemProvider.ts b/src/providers/FileSystemProvider/FileSystemProvider.ts index dd140ab8..ad78f3cc 100644 --- a/src/providers/FileSystemProvider/FileSystemProvider.ts +++ b/src/providers/FileSystemProvider/FileSystemProvider.ts @@ -3,7 +3,7 @@ import * as vscode from "vscode"; import { AtelierAPI } from "../../api"; import { Directory } from "./Directory"; import { File } from "./File"; -import { fireOtherStudioAction, OtherStudioAction } from "../../commands/studio"; +import { fireOtherStudioAction, OtherStudioAction, StudioActions } from "../../commands/studio"; import { projectContentsFromUri, studioOpenDialogFromURI } from "../../utils/FileProviderUtil"; import { classNameRegex, @@ -202,6 +202,17 @@ export class FileSystemProvider implements vscode.FileSystemProvider { } const params = new URLSearchParams(uri.query); if (params.has("project") && params.get("project").length) { + if (["", "/"].includes(uri.path)) { + // Technically a project is a "document", so tell the server that we're opening it + try { + await new StudioActions().fireProjectUserAction(api, params.get("project"), OtherStudioAction.OpenedDocument); + } catch { + throw vscode.FileSystemError.Unavailable( + `'OpenedDocument' source control action failed for '${params.get("project")}.PRJ'` + ); + } + } + // Get all items in the project return projectContentsFromUri(uri).then((entries) => entries.map((entry) => {