forked from stef-levesque/vscode-perforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perforce.js
204 lines (183 loc) · 5.38 KB
/
perforce.js
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
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');
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);
}
exports.activate = activate;
function buildCmdline(command, args)
{
var cmdline = "p4";
if (isWin) {
cmdline += ".exe";
}
cmdline += " " + command;
if (args != undefined)
cmdline += " " + args;
return cmdline;
}
function p_showOutput() {
_channel.show();
}
function p_add() {
var editor = window.activeTextEditor;
if (!editor) {
window.showInformationMessage("Perforce: no file selected");
return;
}
if (workspace.rootPath == undefined){
window.showInformationMessage("Perforce: no folder opened");
return;
}
var uri = editor.document.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.showInformationMessage("Perforce: file opened for add");
_channel.append(stdout.toString());
}
});
}
function p_edit() {
var editor = window.activeTextEditor;
if (!editor) {
window.showInformationMessage("Perforce: no file selected");
return;
}
if (workspace.rootPath == undefined){
window.showInformationMessage("Perforce: no folder opened");
return;
}
var uri = editor.document.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.showInformationMessage("Perforce: file opened for edit");
_channel.append(stdout.toString());
}
});
}
function p_revert() {
var editor = window.activeTextEditor;
if (!editor) {
window.showInformationMessage("Perforce: no file selected");
return;
}
if (workspace.rootPath == undefined){
window.showInformationMessage("Perforce: no folder opened");
return;
}
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.showInformationMessage("Perforce: file reverted");
_channel.append(stdout.toString());
}
});
}
function p_diff() {
var editor = window.activeTextEditor;
if (!editor) {
window.showInformationMessage("Perforce: no file selected");
return;
}
if (workspace.rootPath == undefined){
window.showInformationMessage("Perforce: no folder opened");
return;
}
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 (workspace.rootPath == undefined){
window.showInformationMessage("Perforce: no folder opened");
return;
}
_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_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;
}
});
}