Skip to content

Commit 955c818

Browse files
committed
Fire source control hooks when creating/opening/editing/deleting projects
1 parent 792a12e commit 955c818

File tree

4 files changed

+135
-16
lines changed

4 files changed

+135
-16
lines changed

src/commands/project.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { isCSPFile } from "../providers/FileSystemProvider/FileSystemProvider";
1212
import { notNull, outputChannel } from "../utils";
1313
import { pickServerAndNamespace } from "./addServerNamespaceToWorkspace";
1414
import { exportList } from "./export";
15+
import { OtherStudioAction, StudioActions } from "./studio";
1516

1617
export interface ProjectItem {
1718
Name: string;
@@ -137,6 +138,20 @@ export async function createProject(node: NodeBase | undefined, api?: AtelierAPI
137138
return;
138139
}
139140

141+
// Technically a project is a "document", so tell the server that we created it
142+
try {
143+
await new StudioActions().fireProjectUserAction(api, name, OtherStudioAction.CreatedNewDocument);
144+
await new StudioActions().fireProjectUserAction(api, name, OtherStudioAction.FirstTimeDocumentSave);
145+
} catch (error) {
146+
let message = `Source control actions failed for project '${name}'.`;
147+
if (error && error.errorText && error.errorText !== "") {
148+
outputChannel.appendLine("\n" + error.errorText);
149+
outputChannel.show(true);
150+
message += " Check 'ObjectScript' output channel for details.";
151+
}
152+
return vscode.window.showErrorMessage(message, "Dismiss");
153+
}
154+
140155
// Refresh the explorer
141156
projectsExplorerProvider.refresh();
142157

@@ -179,6 +194,19 @@ export async function deleteProject(node: ProjectNode | undefined): Promise<any>
179194
return vscode.window.showErrorMessage(message, "Dismiss");
180195
}
181196

197+
// Technically a project is a "document", so tell the server that we deleted it
198+
try {
199+
await new StudioActions().fireProjectUserAction(api, project, OtherStudioAction.DeletedDocument);
200+
} catch (error) {
201+
let message = `'DeletedDocument' source control action failed for project '${project}'.`;
202+
if (error && error.errorText && error.errorText !== "") {
203+
outputChannel.appendLine("\n" + error.errorText);
204+
outputChannel.show(true);
205+
message += " Check 'ObjectScript' output channel for details.";
206+
}
207+
return vscode.window.showErrorMessage(message, "Dismiss");
208+
}
209+
182210
// Refresh the explorer
183211
projectsExplorerProvider.refresh();
184212

@@ -706,6 +734,20 @@ export async function modifyProject(
706734
return;
707735
}
708736
}
737+
738+
// Technically a project is a "document", so tell the server that we're opening it
739+
try {
740+
await new StudioActions().fireProjectUserAction(api, project, OtherStudioAction.OpenedDocument);
741+
} catch (error) {
742+
let message = `'OpenedDocument' source control action failed for project '${project}'.`;
743+
if (error && error.errorText && error.errorText !== "") {
744+
outputChannel.appendLine("\n" + error.errorText);
745+
outputChannel.show(true);
746+
message += " Check 'ObjectScript' output channel for details.";
747+
}
748+
return vscode.window.showErrorMessage(message, "Dismiss");
749+
}
750+
709751
let items: ProjectItem[] = await api
710752
.actionQuery("SELECT Name, Type FROM %Studio.Project_ProjectItemsList(?,?) WHERE Type != 'GBL'", [project, "1"])
711753
.then((data) => data.result.content);
@@ -862,6 +904,23 @@ export async function modifyProject(
862904
}
863905

864906
try {
907+
if (add.length || remove.length) {
908+
// Technically a project is a "document", so tell the server that we're editing it
909+
const studioActions = new StudioActions();
910+
await studioActions.fireProjectUserAction(api, project, OtherStudioAction.AttemptedEdit);
911+
if (studioActions.projectEditAnswer != "1") {
912+
// Don't perform the edit
913+
if (studioActions.projectEditAnswer == "-1") {
914+
// Source control action failed
915+
vscode.window.showErrorMessage(
916+
`'AttemptedEdit' source control action failed for project '${project}'. Check the 'ObjectScript' Output channel for details.`,
917+
"Dismiss"
918+
);
919+
}
920+
return;
921+
}
922+
}
923+
865924
if (remove.length) {
866925
// Delete the obsolete items
867926
await api.actionQuery(
@@ -900,7 +959,7 @@ export async function modifyProject(
900959

901960
// Refresh the files explorer if there's an isfs folder for this project
902961
if (node == undefined && isfsFolderForProject(project, node ?? api.configName) != -1) {
903-
await vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer");
962+
vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer");
904963
}
905964
}
906965
}
@@ -1070,6 +1129,21 @@ export async function addIsfsFileToProject(
10701129

10711130
try {
10721131
if (add.length) {
1132+
// Technically a project is a "document", so tell the server that we're editing it
1133+
const studioActions = new StudioActions();
1134+
await studioActions.fireProjectUserAction(api, project, OtherStudioAction.AttemptedEdit);
1135+
if (studioActions.projectEditAnswer != "1") {
1136+
// Don't perform the edit
1137+
if (studioActions.projectEditAnswer == "-1") {
1138+
// Source control action failed
1139+
vscode.window.showErrorMessage(
1140+
`'AttemptedEdit' source control action failed for project '${project}'. Check the 'ObjectScript' Output channel for details.`,
1141+
"Dismiss"
1142+
);
1143+
}
1144+
return;
1145+
}
1146+
10731147
// Add any new items
10741148
await api.actionQuery(
10751149
`INSERT INTO %Studio.ProjectItem (Project,Name,Type) SELECT * FROM (${add

src/commands/studio.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { DocumentContentProvider } from "../providers/DocumentContentProvider";
66
import { ClassNode } from "../explorer/models/classNode";
77
import { PackageNode } from "../explorer/models/packageNode";
88
import { RoutineNode } from "../explorer/models/routineNode";
9-
import { NodeBase } from "../explorer/models/nodeBase";
109
import { importAndCompile } from "./compile";
1110
import { ProjectNode } from "../explorer/models/projectNode";
1211
import { openCustomEditors } from "../providers/RuleEditorProvider";
@@ -72,18 +71,17 @@ export class StudioActions {
7271
private uri: vscode.Uri;
7372
private api: AtelierAPI;
7473
private name: string;
74+
public projectEditAnswer?: string;
7575

7676
public constructor(uriOrNode?: vscode.Uri | PackageNode | ClassNode | RoutineNode) {
7777
if (uriOrNode instanceof vscode.Uri) {
78-
const uri: vscode.Uri = uriOrNode;
79-
this.uri = uri;
80-
this.name = getServerName(uri);
81-
this.api = new AtelierAPI(uri);
78+
this.uri = uriOrNode;
79+
this.name = getServerName(uriOrNode);
80+
this.api = new AtelierAPI(uriOrNode);
8281
} else if (uriOrNode) {
83-
const node: NodeBase = uriOrNode;
84-
this.api = new AtelierAPI(node.workspaceFolder);
85-
this.api.setNamespace(node.namespace);
86-
this.name = node instanceof PackageNode ? node.fullName + ".PKG" : node.fullName;
82+
this.api = new AtelierAPI(uriOrNode.workspaceFolder);
83+
this.api.setNamespace(uriOrNode.namespace);
84+
this.name = uriOrNode instanceof PackageNode ? uriOrNode.fullName + ".PKG" : uriOrNode.fullName;
8785
} else {
8886
this.api = new AtelierAPI();
8987
}
@@ -105,6 +103,22 @@ export class StudioActions {
105103
);
106104
}
107105

106+
/** Fire UserAction `id` on server `api` for project `name`. */
107+
public async fireProjectUserAction(api: AtelierAPI, name: string, id: OtherStudioAction): Promise<void> {
108+
this.api = api;
109+
this.name = `${name}.PRJ`;
110+
return this.userAction(
111+
{
112+
id: id.toString(),
113+
label: getOtherStudioActionLabel(id),
114+
},
115+
false,
116+
"",
117+
"",
118+
1
119+
);
120+
}
121+
108122
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
109123
public processUserAction(userAction): Thenable<any> {
110124
const serverAction = parseInt(userAction.action || 0, 10);
@@ -318,18 +332,31 @@ export class StudioActions {
318332
const attemptedEditLabel = getOtherStudioActionLabel(OtherStudioAction.AttemptedEdit);
319333
if (afterUserAction && actionToProcess.errorText !== "") {
320334
if (action.label === attemptedEditLabel) {
321-
suppressEditListenerMap.set(this.uri.toString(), true);
322-
await vscode.commands.executeCommand("workbench.action.files.revert", this.uri);
335+
if (this.name.toUpperCase().endsWith(".PRJ")) {
336+
// Store the "answer" so the caller knows there was an error
337+
this.projectEditAnswer = "-1";
338+
} else if (this.uri) {
339+
// Only revert if we have a URI
340+
suppressEditListenerMap.set(this.uri.toString(), true);
341+
await vscode.commands.executeCommand("workbench.action.files.revert", this.uri);
342+
}
323343
}
324344
outputChannel.appendLine(actionToProcess.errorText);
325345
outputChannel.show();
326346
}
327347
if (actionToProcess && !afterUserAction) {
328348
const answer = await this.processUserAction(actionToProcess);
329349
// call AfterUserAction only if there is a valid answer
330-
if (action.label === attemptedEditLabel && answer !== "1") {
331-
suppressEditListenerMap.set(this.uri.toString(), true);
332-
await vscode.commands.executeCommand("workbench.action.files.revert", this.uri);
350+
if (action.label === attemptedEditLabel) {
351+
if (answer != "1" && this.uri) {
352+
// Only revert if we have a URI
353+
suppressEditListenerMap.set(this.uri.toString(), true);
354+
await vscode.commands.executeCommand("workbench.action.files.revert", this.uri);
355+
}
356+
if (this.name.toUpperCase().endsWith(".PRJ")) {
357+
// Store the answer
358+
this.projectEditAnswer = answer;
359+
}
333360
}
334361
if (answer) {
335362
answer.msg || answer.msg === ""

src/explorer/models/projectNode.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as vscode from "vscode";
22
import { NodeBase, NodeOptions } from "./nodeBase";
33
import { ProjectRootNode } from "./projectRootNode";
4+
import { OtherStudioAction, StudioActions } from "../../commands/studio";
5+
import { AtelierAPI } from "../../api";
46

57
export class ProjectNode extends NodeBase {
68
private description: string;
@@ -13,6 +15,11 @@ export class ProjectNode extends NodeBase {
1315
const children = [];
1416
let node: ProjectRootNode;
1517

18+
// Technically a project is a "document", so tell the server that we're opening it
19+
const api = new AtelierAPI(this.workspaceFolderUri);
20+
api.setNamespace(this.namespace);
21+
await new StudioActions().fireProjectUserAction(api, this.label, OtherStudioAction.OpenedDocument);
22+
1623
node = new ProjectRootNode(
1724
"Classes",
1825
"",

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from "vscode";
33
import { AtelierAPI } from "../../api";
44
import { Directory } from "./Directory";
55
import { File } from "./File";
6-
import { fireOtherStudioAction, OtherStudioAction } from "../../commands/studio";
6+
import { fireOtherStudioAction, OtherStudioAction, StudioActions } from "../../commands/studio";
77
import { projectContentsFromUri, studioOpenDialogFromURI } from "../../utils/FileProviderUtil";
88
import {
99
classNameRegex,
@@ -202,6 +202,17 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
202202
}
203203
const params = new URLSearchParams(uri.query);
204204
if (params.has("project") && params.get("project").length) {
205+
if (["", "/"].includes(uri.path)) {
206+
// Technically a project is a "document", so tell the server that we're opening it
207+
try {
208+
await new StudioActions().fireProjectUserAction(api, params.get("project"), OtherStudioAction.OpenedDocument);
209+
} catch {
210+
throw vscode.FileSystemError.Unavailable(
211+
`'OpenedDocument' source control action failed for '${params.get("project")}.PRJ'`
212+
);
213+
}
214+
}
215+
205216
// Get all items in the project
206217
return projectContentsFromUri(uri).then((entries) =>
207218
entries.map((entry) => {

0 commit comments

Comments
 (0)