Repository navigation
Expand file tree
/
Copy pathUpdateModels.js
More file actions
224 lines (191 loc) · 8.1 KB
/
Copy pathUpdateModels.js
File metadata and controls
224 lines (191 loc) · 8.1 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
// API is a bit wonky right now
var project = require('../../lib/pbxProject.js'),
fs = require('fs'),
util = require('util'),
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 = 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 = myProj.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];
}