-
Notifications
You must be signed in to change notification settings - Fork 93
/
PerforceService.ts
267 lines (224 loc) · 9.76 KB
/
PerforceService.ts
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
import { IPerforceConfig } from './PerforceService';
import {
workspace,
window,
TextDocument,
Uri
} from 'vscode';
import Bottleneck from 'bottleneck';
import { Utils } from './Utils';
import { Display } from './Display';
import { PerforceSCMProvider } from './ScmProvider';
import * as CP from 'child_process';
export interface IPerforceConfig {
// p4 standard configuration variables
p4Client?: string;
p4Host?: string;
p4Pass?: string;
p4Port?: number;
p4Tickets?: string;
p4User?: string;
// specific to this exension
// use this value as the clientRoot PWD for this .p4config file's location
p4Dir?: string;
// root directory of the user space (or .p4config)
localDir: string;
// whether to strip the localDir when calling espansePath
stripLocalDir?: boolean;
}
export function matchConfig(config: IPerforceConfig, uri: Uri): boolean {
// path fixups:
const trailingSlash = /^(.*)(\/)$/;
let compareDir = Utils.normalize(uri.fsPath);
if (!trailingSlash.exec(compareDir)) compareDir += '/';
if (config.localDir === compareDir) {
return true;
}
return false;
}
export namespace PerforceService {
const limiter: Bottleneck = new Bottleneck({
maxConcurrent: workspace.getConfiguration('perforce').get<number>('bottleneck.maxConcurrent'),
minTime: workspace.getConfiguration('perforce').get<number>('bottleneck.minTime'),
highWater: workspace.getConfiguration('perforce').get<number>('bottleneck.highWater'),
strategy: Bottleneck.strategy[
workspace.getConfiguration('perforce').get<string>('bottleneck.strategy')
],
penalty: workspace.getConfiguration('perforce').get<number>('bottleneck.penalty'),
});
const debugModeActive: boolean = workspace.getConfiguration('perforce').get('debugModeActive');
let debugModeSetup: boolean = false;
let _configs: {[key: string]: IPerforceConfig} = {};
export function addConfig(inConfig: IPerforceConfig, workspacePath: string): void {
_configs[workspacePath] = inConfig;
}
export function removeConfig(workspacePath: string): void {
delete _configs[workspacePath];
}
export function getConfig(workspacePath): IPerforceConfig {
return _configs[workspacePath];
}
export function convertToRel(path: string): string {
const wksFolder = workspace.getWorkspaceFolder(Uri.file(path));
const config = wksFolder ? _configs[wksFolder.uri.fsPath] : null;
if (!config
|| !config.stripLocalDir
|| !config.localDir || config.localDir.length === 0
|| !config.p4Dir || config.p4Dir.length === 0) {
return path;
}
const pathN = Utils.normalize(path);
if (pathN.startsWith(config.localDir)) {
path = pathN.slice(config.localDir.length);
}
return path;
}
export function getPerforceCmdPath(resource: Uri): string {
var p4Path = workspace.getConfiguration('perforce').get('command', 'none');
var p4User = workspace.getConfiguration('perforce', resource).get('user', 'none');
var p4Client = workspace.getConfiguration('perforce', resource).get('client', 'none');
var p4Port = workspace.getConfiguration('perforce', resource).get('port', 'none');
var p4Pass = workspace.getConfiguration('perforce', resource).get('password', 'none');
var p4Dir = workspace.getConfiguration('perforce', resource).get('dir', 'none');
const buildCmd = (value, arg): string => {
if (!value || value === 'none')
return '';
return ` ${arg} ${value}`;
}
if (p4Path == 'none') {
var isWindows = /^win/.test(process.platform);
p4Path = isWindows ? 'p4.exe' : 'p4';
} else {
const toUNC = (path: string): string => {
let uncPath = path;
if (uncPath.indexOf('\\\\') !== 0) {
const replaceable = uncPath.split('\\');
uncPath = replaceable.join('\\\\');
}
uncPath = `"${uncPath}"`;
return uncPath;
}
p4Path = toUNC(p4Path);
}
p4Path += buildCmd(p4User, '-u');
p4Path += buildCmd(p4Client, '-c');
p4Path += buildCmd(p4Port, '-p');
p4Path += buildCmd(p4Pass, '-P');
p4Path += buildCmd(p4Dir, '-d');
// later args override earlier args
const wksFolder = workspace.getWorkspaceFolder(resource);
const config = wksFolder ? getConfig(wksFolder.uri.fsPath) : null;
if (config) {
p4Path += buildCmd(config.p4User, '-u');
p4Path += buildCmd(config.p4Client, '-c');
p4Path += buildCmd(config.p4Port, '-p');
p4Path += buildCmd(config.p4Pass, '-P');
p4Path += buildCmd(config.p4Dir, '-d');
}
return p4Path;
}
export function execute(resource: Uri, command: string, responseCallback: (err: Error, stdout: string, stderr: string) => void, args?: string, directoryOverride?: string, input?: string): void {
if (debugModeActive && !debugModeSetup) {
limiter.on('debug', (message, data) => {
console.log('Bottleneck Debug:', message, data);
});
debugModeSetup = true;
}
limiter.submit({ id: `<JOB_ID:${Date.now()}:${command}>`}, execCommand, resource, command, responseCallback, args, directoryOverride, input, null);
}
export function executeAsPromise(resource: Uri, command: string, args?: string, directoryOverride?: string, input?: string): Promise<string> {
return new Promise((resolve, reject) => {
execute(resource, command, (err, stdout, stderr) => {
if (err) {
reject(err.message);
} else if (stderr) {
reject(stderr);
} else {
resolve(stdout.toString());
}
}, args, directoryOverride, input);
});
}
function execCommand(resource: Uri, command: string, responseCallback: (err: Error, stdout: string, stderr: string) => void, args?: string, directoryOverride?: string, input?: string): void {
const wksFolder = workspace.getWorkspaceFolder(resource);
const config = wksFolder ? getConfig(wksFolder.uri.fsPath) : null;
const wksPath = wksFolder ? wksFolder.uri.fsPath : '';
var cmdLine = getPerforceCmdPath(resource);
const maxBuffer = workspace.getConfiguration('perforce').get('maxBuffer', 200 * 1024);
if (directoryOverride != null) {
cmdLine += ' -d ' + directoryOverride;
}
cmdLine += ' ' + command;
if (args != null) {
if (config && config.stripLocalDir) {
args = args.replace(config.localDir, '');
}
cmdLine += ' ' + args;
}
Display.channel.appendLine(cmdLine);
const cmdArgs = { cwd: config ? config.localDir : wksPath, maxBuffer: maxBuffer };
var child = CP.exec(cmdLine, cmdArgs, responseCallback);
if (input != null) {
child.stdin.end(input, 'utf8');
}
}
export function handleInfoServiceResponse(err: Error, stdout: string, stderr: string) {
if (err) {
Display.showError(stderr.toString());
} else {
Display.channel.append(stdout.toString());
}
}
export function handleCommonServiceResponse(err: Error, stdout: string, stderr: string) {
if (err) {
Display.showError(stderr.toString());
} else {
Display.channel.append(stdout.toString());
Display.updateEditor();
PerforceSCMProvider.RefreshAll();
}
}
export function getClientRoot(resource: Uri): Promise<string> {
return new Promise((resolve, reject) => {
PerforceService.executeAsPromise(resource, 'info').then((stdout) => {
var clientRootIndex = stdout.indexOf('Client root: ');
if (clientRootIndex === -1) {
reject("P4 Info didn't specify a valid Client Root path");
return;
}
clientRootIndex += 'Client root: '.length;
var endClientRootIndex = stdout.indexOf('\n', clientRootIndex);
if (endClientRootIndex === -1) {
reject("P4 Info Client Root path contains unexpected format");
return;
}
//Resolve with client root as string
resolve(stdout.substring(clientRootIndex, endClientRootIndex));
}).catch((err) => {
reject(err);
});
});
}
export function getConfigFilename(resource: Uri): Promise<string> {
return new Promise((resolve, reject) => {
PerforceService.executeAsPromise(resource, 'set', '-q').then((stdout) => {
var configIndex = stdout.indexOf('P4CONFIG=');
if (configIndex === -1) {
resolve('.p4config');
return;
}
configIndex += 'P4CONFIG='.length;
var endConfigIndex = stdout.indexOf('\n', configIndex);
if (endConfigIndex === -1) {
//reject("P4 set -q parsing for P4CONFIG contains unexpected format");
resolve('.p4config');
return;
}
//Resolve with p4 config filename as string
resolve(stdout.substring(configIndex, endConfigIndex));
}).catch((err) => {
reject(err);
});
});
}
}