Repository navigation
Expand file tree
/
Copy pathUpdateNEIProject.js
More file actions
451 lines (380 loc) · 14.6 KB
/
Copy pathUpdateNEIProject.js
File metadata and controls
451 lines (380 loc) · 14.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
// 这个JS直接引用xcode, 所有额外添加的东西都加在这个JS文件中.
var xcode = require('xcode'),
fs = require('fs'),
util = require('util'),
COMMENT_KEY = /_comment$/,
f = util.format;
// TODO:参数缺少时, 需要给予警告和提示. 参数的处理需要更灵活
// node UpdateModels.js projectName groupParentPath folderParentPath
var projectName = process.argv[2];
var groupParentPath = process.argv[3];
var folderParentPath = process.argv[4];
var projectPath = projectName + '.xcodeproj/project.pbxproj';
//var projectPath = 'HTJSGeneratorCode.xcodeproj/project.pbxproj';
// Get Project.
var myProj = xcode.project(projectPath);
// Debug with parseSync as it is impossible to debug async parsing.
// For release version, it is OK to use parse function.
myProj.parseSync();
updateCode();
function updateCode () {
if (undefined == projectName || undefined == groupParentPath || undefined == folderParentPath || undefined == myProj) {
console.log("Please correct parameters, the command is like \r\n node UpdateModels.js projectName groupParentPath folderParentPath, please specify your own projectName, parent group path and pareng folder path");
return;
}
// TODO: 现在暂时不支持更换路径,即之前要删除的路径和后面要添加的路径不相同的Case.
console.log("Begin to update project " + projectName + ", Models and Requests will be added under group " + groupParentPath);
// 固定添加Models和Requests, 不需要参数配置
var autoGroupNames = ["Models", "Requests"];
autoGroupNames.forEach(function (groupName){
console.log("Begin to check folder " + groupName);
// 获取parent Group Key 和 要处理的Group Key
var absoluteGroupPath = groupParentPath + "/" + groupName;
var parentGroupKey = findGroupByAbsolutePath(groupParentPath);
var groupKey = findGroupByAbsolutePath(absoluteGroupPath);
if (undefined == groupKey) {
// group不存在, 新建Group. GroupName与Path相同.
console.log("Group " + groupName + " does not exist, create group " + groupName + " under group " + groupParentPath);
groupKey = myProj.pbxCreateGroup(groupName, groupName);
// Add new created groups into parent group.
// 将Group加到parent Group中
myProj.addToPbxGroup(groupKey, parentGroupKey, {});
} else {
// group存在, 删除其中的所有文件.
console.log("Group " + groupName + " already exists, remove all files in this group first.");
removeFilesInGroup(groupName);
}
// 获取目录下的所有头文件和可执行文件
var folderPath = folderParentPath + "/" + groupName;
var headerFiles = headerFilesInFilePath(folderPath);
var sourceFiles = sourceFilesInFilePath(folderPath);
// 将头文件添加到group中.
headerFiles.forEach(function(headerFile) {
myProj.addHeaderFile(headerFile, {}, groupKey);
});
// 将源文件添加到group中
sourceFiles.forEach(function(sourceFile) {
myProj.addSourceFile(sourceFile, {}, groupKey);
});
console.log("Add Source Files and Header Files from Path " + folderPath + " to group " + absoluteGroupPath + " successfully");
});
// 将内容写回到工程文件中.
fs.writeFileSync(projectPath, myProj.writeSync());
console.log('Project ' + projectPath + " is updated successfully !");
}
// 根据完整路径获取到对应的Group.
function findGroupByAbsolutePath(fullPath) {
var pathList = fullPath.split('/');
if (pathList.length == 0) {
return;
}
var root = pathList[0];
var groupKey = myProj.findPBXGroupKey({ path: root});
pathList.splice(0, 1);
while (pathList.length > 0 && undefined != groupKey) {
root = pathList[0];
groupKey = findPBXGroupKeyInParentGroup({path: root}, groupKey);
pathList.splice(0, 1);
}
// TODO: 下面的代码只用校验是否能够取到正确的Group.
if (undefined != groupKey) {
var group = myProj.getPBXGroupByKey(groupKey);
}
return groupKey;
}
// removeFilesInGroup已经OK.
// Done. 根据FileRef的UUID从Project的File Reference Section中删除.
function removeFromPbxFileReferenceSectionWithKey (fileRef) {
for (i in myProj.pbxFileReferenceSection()) {
if (i == fileRef) {
delete myProj.pbxFileReferenceSection()[i];
break;
}
}
var commentKey = f("%s_comment", fileRef);
if (myProj.pbxFileReferenceSection()[commentKey] != undefined) {
delete myProj.pbxFileReferenceSection()[commentKey];
}
}
// TODO: 这里必须返回BuildFileUUID. 否则的话,无法从BuildPhase中删除.
function removeFromPbxBuildFileSectionWithKey (fileRef) {
var uuid;
var buildFileUUID;
for (uuid in myProj.pbxBuildFileSection()) {
var buildFile = myProj.pbxBuildFileSection()[uuid];
if (buildFile.fileRef == fileRef) {
// TODO: delete buildFile没有从这个字典中真正删除这个文件.
//delete buildFile;
delete myProj.pbxBuildFileSection()[uuid];
buildFileUUID = uuid;
break;
}
}
var commentKey = f("%s_comment", fileRef);
if (myProj.pbxBuildFileSection()[commentKey] != undefined) {
delete myProj.pbxBuildFileSection()[commentKey];
}
return buildFileUUID;
}
// Note: 这里只取了firstTarget, 理论上所有的Target都需要获取. 这里所有的参数都是fileRef的uuid.
// Note: File Reference里面的uuid和BuildPhase中的uuid不相同. 所以这个方法不正确.
function removeFromPbxSourcesBuildPhaseWithKey (fileRef) {
// var target = myProj.getFirstTarget();
// TODO: 用FirstTarget会抛出异常,原因未知.
var target = undefined;
var sources = myProj.pbxSourcesBuildPhaseObj(target), i;
for (i in sources.files) {
if (sources.files[i].value == fileRef) {
sources.files.splice(i, 1);
break;
}
}
}
function removeFilesInGroup(groupName) {
var groupKey = myProj.findPBXGroupKey({ path: groupName});
var group = myProj.getPBXGroupByKey(groupKey);
if (group) {
var groupChildren = group.children, i;
for (i in groupChildren) {
file = groupChildren[i];
var uuid = file.value;
if (uuid != undefined) {
removeFromPbxFileReferenceSectionWithKey(uuid); // PBXFileReference
}
// 从当前Group中删除. TODO: 这里删除了后不可以继续遍历了.
//groupChildren.splice(i, 1);
// TODO: 如果是子Group, 还要继续删除. 暂时不考虑文件夹.
//myProj.removeFromPbxGroup(file, group); // PBXGroup
// 从Build File Ref中删除
var buildFileUUID = removeFromPbxBuildFileSectionWithKey(uuid);
// 从Build Phase中删除
if (undefined != buildFileUUID) {
removeFromPbxSourcesBuildPhaseWithKey(buildFileUUID);
}
}
// 删除groupChildren.
groupChildren.splice(0, groupChildren.length);
}
}
// 找到所有的头文件.
function headerFilesInFilePath(root) {
var res = [];
var files = fs.readdirSync(root);
files.forEach(function(file) {
var extensionFileName = GetExtensionFileName(file);
if ("h" == extensionFileName) {
res.push(file);
}
});
return res;
}
// 找到所有的源文件
// TODO: 性能可以改进, 可以一次性找出头文件和源文件,不需要每次都遍历一遍.
function sourceFilesInFilePath(root) {
var res = [];
var files = fs.readdirSync(root);
files.forEach(function(file) {
var extensionFileName = GetExtensionFileName(file);
if ("m" == extensionFileName) {
res.push(file);
}
});
return res;
}
function GetExtensionFileName(pathfilename)
{
var reg = /(\\+)/g;
var pfn = pathfilename.replace(reg, "#");
var arrpfn = pfn.split("#");
var fn = arrpfn[arrpfn.length - 1];
var arrfn = fn.split(".");
return arrfn[arrfn.length - 1];
}
// Projects Extension
// 这些最好添加到pbxProject.js中, 仅供当前流程使用.
function removePbxGroupByKey(groupKey) {
var section = myProj.hash.project.objects['PBXGroup'],
key, itemKey;
for (key in section) {
// only look for comments
if (!COMMENT_KEY.test(key)) continue;
if (key == groupKey) {
itemKey = key.split(COMMENT_KEY)[0];
delete section[itemKey];
}
}
}
function removeGroupFromPbxGroup(removeGroupKey, groupKey) {
var group = myProj.getPBXGroupByKey(groupKey);
var removeGroup = myProj.getPBXGroupByKey(removeGroupKey);
if (group) {
var groupChildren = group.children, i;
for(i in groupChildren) {
if(removeGroupKey == groupChildren[i].value && removeGroup != undefined) {
groupChildren.splice(i, 1);
clearGroup(removeGroup);
break;
}
}
}
delete removeGroup;
}
function clearGroup(group) {
var groupChildren = group.children, i;
for (i in groupChildren) {
file = groupChildren[i];
if (file.fileRef != undefined) {
myProj.removeFromPbxFileReferenceSection(file); // PBXFileReference
}
// TODO: 如果是子Group, 还要继续删除.
myProj.removeFromPbxGroup(file, group); // PBXGroup
}
}
function removeGroupByKey(key, parentGroupKey) {
var group = getPBXGroupByKey(key, parentGroupKey);
if (group) {
var groupChildren = group.children, i;
for (i in groupChildren) {
file = groupChildren[i];
if (file.fileRef != undefined) {
myProj.removeFromPbxFileReferenceSection(file); // PBXFileReference
}
// TODO: 如果是子Group, 还要继续删除.
myProj.removeFromPbxGroup(file, group); // PBXGroup
}
}
}
// Remove a group and all files in this group.
function removeGroup(groupPath, parentGroupKey) {
var group = getPBXGroupByPath(groupPath, parentGroupKey);
if (group) {
var groupChildren = group.children, i;
for (i in groupChildren) {
file = groupChildren[i];
if (file.fileRef != undefined) {
myProj.removeFromPbxFileReferenceSection(file); // PBXFileReference
}
// TODO: 如果是子Group, 还要继续删除.
myProj.removeFromPbxGroup(file, group); // PBXGroup
}
}
}
function getPBXGroupByPath(path, parentGroupKey) {
var groups = (undefined == parentGroupKey) ? myProj.hash.project.objects['PBXGroup'] : myProj.getPBXGroupByKey(parentGroupKey);
for (var key in groups) {
var group = groups[key];
if (group.path === path) {
return group;
}
}
return undefined;
};
function getPBXGroupByKey(key, parentGroupKey) {
var groups = (undefined == parentGroupKey) ? myProj.hash.project.objects['PBXGroup'] : myProj.getPBXGroupByKey(parentGroupKey);
var group = groups[key];
return group;
};
function findPBXGroupInParent(criteria, parentCriteria) {
if (undefined == parentCriteria) {
return myProj.findPBXGroupKey(criteria);
}
var target;
var groupKey = myProj.findPBXGroupKey(parentCriteria);
var group = myProj.getPBXGroupByKey(groupKey);
var groupChildren = group.children, i;
for(i in groupChildren) {
var child = groupChildren[i];
var childGroup = myProj.getPBXGroupByKey(child.value);
if (undefined == childGroup) {
// 这不是一个对应的Group.
continue;
}
if (criteria && criteria.path && criteria.name) {
if (criteria.path === childGroup.path && criteria.name === childGroup.name) {
target = childGroup;
break
}
}
else if (criteria && criteria.path) {
if (criteria.path === childGroup.path) {
target = childGroup;
break
}
}
else if (criteria && criteria.name) {
if (criteria.name === childGroup.name) {
target = childGroup;
break
}
}
}
return target;
}
function findPBXGroupKeyInParent(criteria, parentCriteria) {
if (undefined == parentCriteria) {
return myProj.findPBXGroupKey(criteria);
}
var target;
var groupKey = myProj.findPBXGroupKey(parentCriteria);
var group = myProj.getPBXGroupByKey(groupKey);
var groupChildren = group.children, i;
for(i in groupChildren) {
var child = groupChildren[i];
var childGroup = myProj.getPBXGroupByKey(child.value);
if (undefined == childGroup) {
// 这不是一个对应的Group.
continue;
}
if (criteria && criteria.path && criteria.name) {
if (criteria.path === childGroup.path && criteria.name === childGroup.name) {
target = child.value;
break
}
}
else if (criteria && criteria.path) {
if (criteria.path === childGroup.path) {
target = child.value;
break
}
}
else if (criteria && criteria.name) {
if (criteria.name === childGroup.name) {
target = child.value;
break
}
}
}
return target;
}
function findPBXGroupKeyInParentGroup(criteria, parentGroupKey) {
var target;
var group = getPBXGroupByKey(parentGroupKey);
var groupChildren = group.children, i;
for(i in groupChildren) {
var child = groupChildren[i];
var childGroup = myProj.getPBXGroupByKey(child.value);
if (undefined == childGroup) {
// 这不是一个对应的Group.
continue;
}
if (criteria && criteria.path && criteria.name) {
if (criteria.path === childGroup.path && criteria.name === childGroup.name) {
target = child.value;
break
}
}
else if (criteria && criteria.path) {
if (criteria.path === childGroup.path) {
target = child.value;
break
}
}
else if (criteria && criteria.name) {
if (criteria.name === childGroup.name) {
target = child.value;
break
}
}
}
return target;
}