-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathactions.ts
More file actions
199 lines (172 loc) Β· 7.28 KB
/
actions.ts
File metadata and controls
199 lines (172 loc) Β· 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import path from "path";
import { commands, Disposable, env, l10n, TreeItem, Uri, window, WorkspaceFolder } from "vscode";
import IBMi from "../api/IBMi";
import { Tools } from "../api/Tools";
import Instance from "../Instance";
import { Action, ActionResult, DeploymentMethod } from "../typings";
import { runAction } from "../ui/actions";
import { refreshDiagnosticsFromServer } from "../ui/diagnostics";
import { BrowserItem } from "../ui/types";
type CommandOrigin = "editor" | "objectBrowser" | "ifsBrowser" | "commandPalette";
export function registerActionsCommands(instance: Instance): Disposable[] {
return [
commands.registerCommand(`code-for-ibmi.runAction`, async (item: (CommandOrigin | TreeItem | BrowserItem | Uri), items?: (TreeItem | BrowserItem | Uri)[], action?: Action, method?: DeploymentMethod, workspaceFolder?: WorkspaceFolder) => {
let actionMessage: string;
if (!item && !items && !action && !method && !workspaceFolder) {
item = "commandPalette";
}
const connection = instance.getConnection()!;
if (connection) {
const editor = window.activeTextEditor;
const browserItems: BrowserItem[] = [];
const uris: Uri[] = [];
const addTreeItem = (target: TreeItem | BrowserItem) => {
if (target.resourceUri) {
uris.push(target.resourceUri);
if (target instanceof BrowserItem) {
browserItems.push(target);
}
}
};
if (typeof item === "string") {
switch (item) {
case "objectBrowser":
(await commands.executeCommand<BrowserItem[]>("code-for-ibmi.objectBrowser.selection")).forEach(addTreeItem);
break;
case "ifsBrowser":
(await commands.executeCommand<BrowserItem[]>("code-for-ibmi.ifsBrowser.selection")).forEach(addTreeItem);
break;
default:
if (editor?.document.uri) {
uris.push(editor.document.uri);
}
}
}
else {
for (const target of (Array.isArray(items) ? items : [item])) {
if (target instanceof Uri) {
uris.push(target);
}
else {
addTreeItem(target);
}
}
}
const scheme = uris[0]?.scheme;
if (scheme) {
if (!uris.every(uri => uri.scheme === scheme)) {
actionMessage = l10n.t("Actions can't be run on multiple items of different natures. ({0})", uris.map(uri => uri.scheme).filter(Tools.distinct).join(", "));
window.showWarningMessage(actionMessage);
return { success: false, output: [], error: actionMessage };
}
const config = connection.getConfig();
for (const openedEditor of window.visibleTextEditors) {
const path = openedEditor.document.uri.path;
if (uris.some(uri => uri.path === path) && openedEditor.document.isDirty) {
if (config.autoSaveBeforeAction) {
await openedEditor.document.save();
} else {
actionMessage = l10n.t(`File {0} must be saved to run Actions.`, path);
const result = await window.showWarningMessage(actionMessage, `Save`, `Save automatically`, `Cancel`);
switch (result) {
case `Save`:
await openedEditor.document.save();
break;
case `Save automatically`:
config.autoSaveBeforeAction = true;
await IBMi.connectionManager.update(config);
await openedEditor.document.save();
break;
default:
return { success: false, output: [], error: actionMessage }
}
}
}
}
if ([`member`, `streamfile`, `file`, 'object'].includes(scheme)) {
return await runAction(instance, uris, action, method, browserItems, workspaceFolder);
}
actionMessage = l10n.t("Unsupported file scheme: {0}. Must be 'member', 'streamfile', 'file', or 'object'", scheme);
return { success: false, output: [], error: actionMessage };
}
actionMessage = l10n.t("Failed to retrieve file scheme");
return { success: false, output: [], error: actionMessage };
}
else {
actionMessage = l10n.t("Please connect to an IBM i first");
window.showErrorMessage(actionMessage);
return { success: false, output: [], error: actionMessage };
}
}),
commands.registerCommand(`code-for-ibmi.openErrors`, async (options: { qualifiedObject?: string, workspace?: WorkspaceFolder, keepDiagnostics?: boolean }) => {
interface ObjectDetail {
asp?: string;
lib: string;
object: string;
ext?: string;
}
const detail: ObjectDetail = {
asp: undefined,
lib: ``,
object: ``,
ext: undefined
};
let inputPath: string | undefined
if (options.qualifiedObject) {
// Value passed in via parameter
inputPath = options.qualifiedObject;
} else {
// Value collected from user input
let initialPath = ``;
const editor = window.activeTextEditor;
const connection = instance.getConnection();
if (editor && connection) {
const config = connection.getConfig();
const uri = editor.document.uri;
if ([`member`, `streamfile`].includes(uri.scheme)) {
switch (uri.scheme) {
case `member`:
const memberPath = uri.path.split(`/`);
if (memberPath.length === 4) {
detail.lib = memberPath[1];
} else if (memberPath.length === 5) {
detail.asp = memberPath[1];
detail.lib = memberPath[2];
}
break;
case `streamfile`:
detail.asp = connection.getCurrentIAspName();
detail.lib = config.currentLibrary;
break;
}
const pathDetail = path.parse(editor.document.uri.path);
detail.object = pathDetail.name;
detail.ext = pathDetail.ext.substring(1);
initialPath = `${detail.lib}/${pathDetail.base}`;
}
}
inputPath = await window.showInputBox({
prompt: `Enter object path (LIB/OBJECT)`,
value: initialPath
});
}
if (inputPath) {
const [library, object] = inputPath.split(`/`);
if (library && object) {
const nameDetail = path.parse(object);
refreshDiagnosticsFromServer(instance, [{ library, object: nameDetail.name, extension: (nameDetail.ext.length > 1 ? nameDetail.ext.substring(1) : undefined), workspace: options.workspace }], options.keepDiagnostics);
}
}
}),
commands.registerCommand(`code-for-ibmi.copyJobId`, async () => {
const connection = instance.getConnection();
const sqlJobId = connection?.getSqlJobId();
if (sqlJobId) {
await env.clipboard.writeText(sqlJobId);
window.showInformationMessage(`Job ID copied: ${sqlJobId}`);
} else {
window.showWarningMessage(`No job ID available`);
}
}),
]
}