-
Notifications
You must be signed in to change notification settings - Fork 5
/
p4j.js
412 lines (335 loc) · 8.13 KB
/
p4j.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
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
'use strict';
module.exports = function (config, log) {
if (!log) {
log = {
verbose: function (msg) {
console.log(msg);
},
info: function (msg) {
console.log(msg);
},
warn: function (msg) {
console.warn(msg);
},
error: function (msg) {
console.error(msg);
},
};
}
var exec = require('./exec')(config, log);
var spawn = require('./spawn')(config, log);
var Promise = require('promise');
var quote = require('quote');
var path = require('path');
var S = require('string');
var glob = require('glob');
var fs = require('fs');
var self = this;
//
// Validate configuration.
//
var validateConfig = function () {
if (!config) {
throw new Error("Config not supplied");
}
if (!config.p4User) {
throw new Error("p4User not specified in config");
}
if (!config.p4Workspace) {
throw new Error("p4Workspace not specified in config");
}
if (!config.p4Host) {
throw new Error("p4Host not specified in config");
}
if (!config.p4ExePath) {
throw new Error("p4ExePath not specified in config");
}
if (!config.workingDirectory) {
throw new Error("workingDirectory not specified in config");
}
};
//
// Python script that runs p4 and converts output to json.
//
var p4PythonScript = path.join(__dirname, 'p4_to_json.py');
var execBufferSize = 1024 * 1024;
//
// Run a p4 command and return a promise that delivers json results.
//
var p4Cmd = function (p4Args, stdin) {
var cmd = [
"python",
quote(p4PythonScript),
p4Args
].join(' ');
return exec(cmd, { cwd: config.workingDirectory, stdin: stdin })
.then(function (output) {
return JSON.parse(output);
});
};
//
// Get latest files for a particular path.
//
// options
// force: true|false Enables Perforce force get, overwriting all files (be careful with this!)
//
self.getLatest = function (path, options) {
if (!path) {
throw new Error('Path to get-latest not specified.');
}
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"sync",
];
if (options.force) {
p4Args.push('-f');
}
p4Args.push(path);
return spawn("p4", p4Args, {
cwd: config.workingDirectory,
maxBuffer: execBufferSize,
});
},
//
// Create a new change set with a specified name.
// Returns a promise that delivers the ID of the change set or an error.
//
self.createChangeSet = function (changeSetName) {
if (!changeSetName) {
throw new Error('Change set name not specified.');
}
var changeSpec =
"Change: new\n" +
"Description: " + changeSetName + "\n";
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"change",
"-i"
].join(' ');
return exec("p4 " + p4Args, {
cwd: config.workingDirectory,
stdin: changeSpec,
maxBuffer: execBufferSize,
})
.then(function (output) {
var matched = /Change (\d+) created/.exec(output);
if (!matched) {
throw new Error("Failed to create change set: " + changeSetName);
}
return matched[1];
});
};
//
// List all change sets for the user.
//
self.getPendingChangeSets = function () {
var p4Args = quote([
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"changes",
"-u", config.p4User,
"-c", config.p4Workspace,
"-s", 'pending',
"-l"
].join(' '));
return p4Cmd(p4Args);
};
//
// Get the ID of an existing named change set.
// Returns a promise that delivers the ID of the change set or an error.
//
self.findChangeSet = function (changeSetName) {
if (!changeSetName) {
throw new Error('Change set name not specified.');
}
return self.getPendingChangeSets()
.then(function (json) {
var matchingChangeSets = json.filter(function (item) {
return S(item.desc).contains(changeSetName);
});
if (matchingChangeSets.length == 0) {
throw new Error("No matching changes sets found that match name '" + changeSetName + "'");
}
else if (matchingChangeSets.length > 1) {
throw new Error("Multiple changes sets found that match name '" + changeSetName + "'");
}
else {
return matchingChangeSets[0].change;
}
});
};
//
// Check out specified files to the named changed set.
//
self.checkOut = function (changeSetId, path) {
if (!changeSetId) {
throw new Error('Change set id not specified.');
}
if (!path) {
throw new Error('Path to checkout not specified.');
}
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"edit",
"-c", changeSetId,
quote(path)
].join(' ');
return exec("p4 " + p4Args, {
cwd: config.workingDirectory,
maxBuffer: execBufferSize,
});
};
//
// Add dir to changeset.
//
self.addDirToChangeSet = function (changeSetId, dir, globPatern) {
if (!changeSetId) {
throw new Error('Change set id not specified.');
}
if (!dir) {
throw new Error('Directory to add not specified.');
}
globPatern = globPatern || "/**/*"
var filesListFileName = "files.txt";
var globOptions = {};
globOptions.sync = true;
globOptions.nodir = true;
var os = require('os');
fs.writeFileSync(filesListFileName, glob(dir + globPatern, globOptions).join(os.EOL));
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"-x", path.join(config.workingDirectory, filesListFileName),
"add",
"-c", changeSetId
]
return spawn("p4", p4Args, {
cwd: dir,
maxBuffer: execBufferSize,
});
}
//
// Revert all checked out files.
//
self.revertAll = function (path) {
if (!path) {
throw new Error('Path to revertAll not specified.');
}
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"revert",
quote(path)
].join(' ');
return exec("p4 " + p4Args, {
cwd: config.workingDirectory,
maxBuffer: execBufferSize,
});
};
//
// Revert files that have not changed.
//
self.revertUnchanged = function (path) {
if (!path) {
throw new Error('Path to revertUnchanged not specified.');
}
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"revert",
"-a",
quote(path)
].join(' ');
return exec("p4 " + p4Args, {
cwd: config.workingDirectory,
maxBuffer: execBufferSize,
});
};
//
// Delete all change lists that are empty.
//
self.deleteEmptyChangeSets = function () {
return self.getPendingChangeSets()
.then(function (changeSets) {
return Promise.all(changeSets.filter(function (changeSet) {
return self.deleteEmptyChangeSet(changeSet.change);
}));
});
};
//
// Delete a single empty change set specified by id.
//
self.deleteEmptyChangeSet = function (changeSetId) {
if (!changeSetId) {
throw new Error('Change set id not specified.');
}
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"change",
"-d", changeSetId,
].join(' ');
return exec("p4 " + p4Args, {
cwd: config.workingDirectory,
maxBuffer: execBufferSize,
});
};
//
// Submit checked-out files to the repo.
//
self.submit = function (changeSetId) {
if (!changeSetId) {
throw new Error('Change set id not specified.');
}
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"submit",
"-c", changeSetId,
];
return spawn("p4", p4Args, {
cwd: config.workingDirectory,
});
};
//
// Returns a JSON object containing all the information
// from running p4 info.
//
self.info = function () {
var p4Args = [
"-u", config.p4User,
"-c", config.p4Workspace,
"-p", config.p4Host,
"info"
].join(' ');
return exec("p4 " + p4Args, {
cwd: config.workingDirectory,
maxBuffer: execBufferSize
})
.then(function (output) {
var jsonObj = {};
output.split(/\r?\n/g).forEach(function (line) {
var entry = /(.+?)\: (.*)/.exec(line);
// Skip lines that don't have the format of <property name>: <value>
if (!entry) {
return;
}
jsonObj[entry[1]] = entry[2];
});
return jsonObj;
});
};
validateConfig();
};