forked from stef-levesque/vscode-perforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperforce.js
More file actions
462 lines (394 loc) · 11.6 KB
/
perforce.js
File metadata and controls
462 lines (394 loc) · 11.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
var vscode = require('vscode');
var CP = require('child_process');
var window = vscode.window;
var workspace = vscode.workspace;
var isWin = /^win/.test(process.platform);
var _channel = window.createOutputChannel('Perforce Log');
var _statusBarItem = null;
var _subscriptions = [];
var _watcher = null;
//Used for editOnFileModified, to cache file until a new file modified
var _lastCheckedFilePath = null;
function activate() {
_channel.appendLine("Perforce Log Output");
vscode.commands.registerCommand('perforce.showOutput', p_showOutput);
vscode.commands.registerCommand('perforce.add', p_add);
vscode.commands.registerCommand('perforce.edit', p_edit);
vscode.commands.registerCommand('perforce.revert', p_revert);
vscode.commands.registerCommand('perforce.diff', p_diff);
vscode.commands.registerCommand('perforce.info', p_info);
vscode.commands.registerCommand('perforce.menuFunctions', p_menuFunction);
w_onChangeEditor();
window.onDidChangeActiveTextEditor(w_onChangeEditor, this, _subscriptions);
var config = workspace.getConfiguration('perforce');
if(config) {
fileInClientRoot(workspace.rootPath, function() {
if(config.editOnFileSave) {
workspace.onDidSaveTextDocument(w_onFileSaved, this, _subscriptions);
}
if(config.editOnFileModified) {
workspace.onDidChangeTextDocument(w_onFileModified, this, _subscriptions);
}
if(config.deleteOnFileDelete || config.addOnFileCreate) {
_watcher = workspace.createFileSystemWatcher('**/*.*', false, true, false);
if(config.deleteOnFileDelete) {
_watcher.onDidDelete(w_onFileDeleted);
}
if(config.addOnFileCreate) {
_watcher.onDidCreate(w_onFileCreated);
}
}
});
}
}
exports.activate = activate;
function buildCmdline(command, args)
{
var cmdline = "p4";
if (isWin) {
cmdline += ".exe";
}
cmdline += " " + command;
if (args != undefined)
cmdline += " " + args;
return cmdline;
}
function checkFolderOpened() {
if (workspace.rootPath == undefined){
window.showInformationMessage("Perforce: No folder opened");
return false;
}
return true;
}
function checkFileSelected() {
var editor = window.activeTextEditor;
if (!editor) {
window.showInformationMessage("Perforce: No file selected");
return false;
}
return true;
}
function p_showOutput() {
_channel.show();
}
function p_add() {
var editor = window.activeTextEditor;
if (!checkFileSelected()) {
return false;
}
if(!checkFolderOpened()) {
return false;
}
var uri = editor.document.uri;
p_addUri(uri);
}
function p_addUri(uri) {
var cmdline = buildCmdline("add", '"' + uri.fsPath + '"');
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd: workspace.rootPath}, function (err, stdout, stderr) {
if(err){
_channel.show();
_channel.appendLine("ERROR:");
_channel.append(stderr.toString());
}
else {
window.setStatusBarMessage("Perforce: file opened for add", 3000);
_channel.append(stdout.toString());
w_onChangeEditor();
}
});
}
function p_edit() {
var editor = window.activeTextEditor;
if (!checkFileSelected()) {
return false;
}
if(!checkFolderOpened()) {
return false;
}
p_editUri(editor.document.uri);
}
function p_editUri(uri) {
var cmdline = buildCmdline("edit", '"' + uri.fsPath + '"');
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd: workspace.rootPath}, function (err, stdout, stderr) {
if(err){
_channel.show();
_channel.appendLine("ERROR:");
_channel.append(stderr.toString());
}
else {
window.setStatusBarMessage("Perforce: file opened for edit", 3000);
_channel.append(stdout.toString());
w_onChangeEditor();
}
});
}
function p_revert() {
var editor = window.activeTextEditor;
if (!checkFileSelected()) {
return false;
}
if(!checkFolderOpened()) {
return false;
}
var uri = editor.document.uri;
var cmdline = buildCmdline("revert", '"' + uri.fsPath + '"');
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd: workspace.rootPath}, function (err, stdout, stderr) {
if(err){
_channel.show();
_channel.appendLine("ERROR:");
_channel.append(stderr.toString());
}
else {
window.setStatusBarMessage("Perforce: file reverted", 3000);
_channel.append(stdout.toString());
w_onChangeEditor();
}
});
}
function p_diff() {
var editor = window.activeTextEditor;
if (!checkFileSelected()) {
return false;
}
if(!checkFolderOpened()) {
return false;
}
var uri = editor.document.uri;
var cmdline = buildCmdline("diff", '"' + uri.fsPath + '"');
//TODO: show in a 'compare' window
//vscode.commands.executeCommand("workbench.files.action.compareFileWith", uri.fsPath);
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd:workspace.rootPath}, function (err, stdout, stderr) {
if(err){
_channel.show();
_channel.appendLine("ERROR:");
_channel.append(stderr.toString());
}
else {
_channel.show();
_channel.append(stdout.toString());
}
});
}
function p_info() {
var cmdline = buildCmdline("info");
if(!checkFolderOpened()) {
return false;
}
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd:workspace.rootPath}, function (err, stdout, stderr) {
if(err){
_channel.show();
_channel.appendLine("ERROR:");
_channel.append(stderr.toString());
}
else {
_channel.show();
_channel.append(stdout.toString());
}
});
}
function p_deleteUri(uri) {
var cmdline = buildCmdline("delete", '"' + uri.fsPath + '"');
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd: workspace.rootPath}, function (err, stdout, stderr) {
if(err){
_channel.show();
_channel.appendLine("ERROR:");
_channel.append(stderr.toString());
}
else {
window.showInformationMessage("Perforce: file marked for delete");
_channel.append(stdout.toString());
}
});
}
function p_menuFunction() {
var items = [];
items.push({ label: "add", description: "Open a new file to add it to the depot" });
items.push({ label: "edit", description: "Open an existing file for edit" });
items.push({ label: "revert", description: "Discard changes from an opened file" });
items.push({ label: "diff", description: "Display diff of client file with depot file" });
items.push({ label: "info", description: "Display client/server information" });
window.showQuickPick(items, {matchOnDescription: true, placeHolder: "Choose a Perforce command:"}).then(function (selection) {
if(selection == undefined)
return;
switch (selection.label) {
case "add":
p_add();
break;
case "edit":
p_edit();
break;
case "revert":
p_revert();
break;
case "diff":
p_diff();
break;
case "info":
p_info();
break;
default:
break;
}
});
}
function p_checkFileOpened(uri, onSuccess) {
var cmdline = buildCmdline("opened", '"' + uri.fsPath + '"');
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd: workspace.rootPath}, function (err, stdout, stderr) {
if(err){
_channel.show();
_channel.appendLine("ERROR:");
_channel.append(stderr.toString());
}
else {
//stderr set if not opened
if(stderr) {
if (typeof onSuccess=="function") onSuccess(uri);
}
_channel.append(stdout.toString());
}
});
return true;
}
function p_getClientRoot(onSuccess, onFailure) {
var cmdline = buildCmdline("info");
if (!checkFolderOpened()){
return false;
}
_channel.appendLine(cmdline);
CP.exec(cmdline, {cwd: workspace.rootPath}, function (err, stdout, stderr) {
if(err){
// _channel.show();
// _channel.appendLine("ERROR:");
// _channel.append(stderr.toString());
if (typeof onFailure=="function") onFailure();
}
else {
var stdoutString = stdout.toString();
// _channel.append(stdoutString);
var clientRootIndex = stdoutString.indexOf('Client root: ');
if(clientRootIndex === -1) {
// _channel.appendLine("ERROR: P4 Info didn't specify a valid Client Root path");
if (typeof onFailure=="function") onFailure();
return -1;
}
//Set index to after 'Client Root: '
clientRootIndex += 'Client root: '.length;
var endClientRootIndex = stdoutString.indexOf('\n', clientRootIndex);
if(endClientRootIndex === -1) {
// _channel.appendLine("ERROR: P4 Info Client Root path contains unexpected format");
// _channel.show();
if (typeof onFailure=="function") onFailure();
return -1;
}
if (typeof onSuccess=="function") {
//call onSuccess with path as arg
onSuccess(stdoutString.substring(clientRootIndex, endClientRootIndex));
}
}
});
return true;
}
function fileInClientRoot(path, onSuccess, onFailure) {
p_getClientRoot(function(clientRoot) {
//Convert to lower and Strip newlines from paths
clientRoot = clientRoot.toLowerCase().replace(/(\r\n|\n|\r)/gm,"");
var filePath = path.toLowerCase().replace(/(\r\n|\n|\r)/gm,"");
//Check if p4 Client Root is in uri's path
if(filePath.indexOf(clientRoot) !== -1) {
if (typeof onSuccess=="function") onSuccess();
} else {
if (typeof onFailure=="function") onFailure();
}
}, onFailure);
}
function tryEditFile(uri) {
if (!checkFolderOpened()){
return false;
}
//The callbacks make me cry at night :(
fileInClientRoot(uri.fsPath, function() {
// onSuccess
p_checkFileOpened(uri, function(uri) {
p_editUri(uri);
});
}, function() {
// onFailure
window.showInformationMessage("Perforce: File not in P4 Client Root");
});
}
function w_onFileSaved(doc) {
tryEditFile(doc.uri);
}
function w_onFileModified(docChange) {
//If this doc has already been checked, then just return
if(docChange.document.uri.fsPath == _lastCheckedFilePath) {
return;
}
//If this doc is not the active file, return
var editor = window.activeTextEditor;
if (!editor || !editor.document || editor.document.uri.fsPath != docChange.document.uri.fsPath) {
return;
}
_lastCheckedFilePath = docChange.document.uri.fsPath;
tryEditFile(docChange.document.uri);
}
function w_onFileDeleted(uri) {
if (!checkFolderOpened()){
return false;
}
p_deleteUri(uri);
}
function w_onFileCreated(uri) {
if (!checkFolderOpened()){
return false;
}
var editor = window.activeTextEditor;
//Only add files open in text editor
if(editor.document && editor.document.uri.fsPath == uri.fsPath) {
p_addUri(uri);
}
}
function w_onChangeEditor() {
// Create as needed
if (!_statusBarItem) {
_statusBarItem = window.createStatusBarItem(vscode.StatusBarAlignment.Left);
_statusBarItem.command = 'perforce.menuFunctions';
}
// Get the current text editor
var editor = window.activeTextEditor;
if (!editor) {
_statusBarItem.hide();
return;
}
var doc = editor.document;
// Only update status if an MD file
if (!doc.isUntitled) {
var cmdline = buildCmdline("opened", '"' + doc.uri.fsPath + '"');
CP.exec(cmdline, {cwd: workspace.rootPath}, function (err, stdout, stderr) {
if(err){
// file is not under client's root ...
_statusBarItem.text = 'P4: $(circle-slash)';
_statusBarItem.tooltip = stderr.toString();
}
else if(stderr) {
// file not opened on this client.
_statusBarItem.text = 'P4: $(file-text)';
_statusBarItem.tooltip = stderr.toString();
} else if(stdout) {
// file opened in add or edit
_statusBarItem.text = 'P4: $(check)';
_statusBarItem.tooltip = stdout.toString();
}
});
_statusBarItem.show();
} else {
_statusBarItem.hide();
}
}