-
Notifications
You must be signed in to change notification settings - Fork 0
/
addMethods.js
289 lines (259 loc) · 8.21 KB
/
addMethods.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
var _ = require('lodash'),
uuid = require('node-uuid');
module.exports = function addMethods(object) {
var result = Object.assign({}, object);
// basic commands for finding and adding entries into the property list
result.allUuids = allUuids.bind(result);
result.generateUuid = generateUuid.bind(result);
result.findObject = findObject.bind(result);
result.addDict = addDict.bind(result);
// commands that add a property and its reference in some parent property
result.addFileReferenceToParent = addFileReferenceToParent.bind(result);
result.addGroupToParent = addGroupToParent.bind(result);
result.addChildToParent = addChildToParent.bind(result);
result.setProvisioningStyle = setProvisioningStyle.bind(result);
// add build phases
result.addBuildPhase = addBuildPhase.bind(result);
result.addShellScript = addShellScript.bind(result);
// add files that need search paths and appropriate parend properties
result.addFrameworkFile = addFrameworkFile.bind(result);
result.addLibraryFile = addLibraryFile.bind(result);
return result;
}
// returns list of uuids for all properties
function allUuids() { return Object.keys(this.plistData.objects); }
// create a new uuid that is unique and matches the pattern that xcode requires
function generateUuid() {
var id = uuid.v4()
.replace(/-/g, '')
.substr(0, 24)
.toUpperCase()
if (this.allUuids().indexOf(id) >= 0) {
return this.generateUuid();
} else {
return id;
}
}
// returns the first object that matches the entered searchParams
// the searchParams can take the following forms
// - string: either the name or the path of an object
// - object: all of the key value pairs in the object match key value pairs in
// the property
function findObject(searchParams) {
var objects = this.plistData.objects;
var objectUuid = this.allUuids().find(function(uuid) {
if (typeof(searchParams) === 'string') {
return (
(_.get(objects[uuid], 'name') === searchParams) ||
(_.get(objects[uuid], 'path') === searchParams)
);
}
return !Object.keys(searchParams).some(function(key) {
return !_.has(objects[uuid], key) || objects[uuid][key] !== searchParams[key];
});
});
if (!objectUuid) {
return;
}
return objects[objectUuid];
}
// add a new property to the plist
function addDict(uuid, isa, otherProps) {
this.plistData.objects[uuid] = Object.assign({}, { isa: isa }, otherProps)
}
// determines whether the fileName belongs to a header, archive, or framework
// and adds the appropriate file reference to the specified parent
function addFileReferenceToParent(parentName, fileName) {
var typeObject;
var isLibraryFile = false;
var fileNameBroken = fileName.split('.');
if (fileNameBroken.length === 2 && fileNameBroken[1] === 'h') {
typeObject = {
fileEncoding: 4,
lastKnownFileType: 'sourcecode.c.h'
};
} else if (fileNameBroken.length === 2 && fileNameBroken[1] === 'a') {
isLibraryFile = true;
typeObject = {
lastKnownFileType: 'archive.ar'
};
} else if (fileNameBroken.length === 2 && fileNameBroken[1] === 'framework') {
typeObject = {
lastKnownFileType: 'wrapper.framework'
};
} else {
return;
}
return this.addChildToParent(
parentName,
'PBXFileReference',
Object.assign(
{},
{
sourceTree: '"<group>"',
path: fileName
},
typeObject
),
isLibraryFile
);
}
// adds a new group to a parent
// The parameter isPath designates whether the group represents a directory
function addGroupToParent(parentName, groupName, isPath) {
var reference = isPath ? 'path' : 'name';
this.addChildToParent(parentName, 'PBXGroup', {
[reference]: groupName,
sourceTree: '"<group>"',
children: []
});
}
// adds a new uuid to the parent that matches the supplied searchParams
// then adds a child object with the same uuid and returns the uuid
// - isLibrary specifies whether to add a new build phase and search path for the file
function addChildToParent(searchParams, isa, childObject, isLibraryFile) {
var newUuid = this.generateUuid();
var parent = this.findObject(searchParams);
if (_.has(parent, 'children')) {
parent
.children
.push(newUuid);
} else if (_.has(parent, 'files')) {
parent
.files
.push(newUuid);
} else {
return console.log('Illegal target parent ' + searchParams);
}
this.addDict(newUuid, isa, childObject);
if (isLibraryFile) {
this.addLibraryFile(newUuid, searchParams);
}
return newUuid;
}
// Add property to list of build phases
function addBuildPhase(isa, options) {
var newUuid = this.generateUuid();
var parent = this.findObject({isa: 'PBXNativeTarget'});
if (!parent) {
console.log('Can\'t find a PBXNativeTarget to add buildPhase.');
return;
}
parent
.buildPhases
.push(newUuid);
this.addDict(newUuid, isa, options);
}
// adds a shellScript with custom text
function addShellScript(scriptContents) {
this.addBuildPhase('PBXShellScriptBuildPhase', {
buildActionMask: 2147483647,
files: [],
inputPaths: [],
outputPaths: [],
runOnlyForDeploymentPostprocessing: 0,
shellPath: '/bin/sh',
shellScript: '"' + scriptContents + '"'
})
}
// Adds a new framework file to the custom template and adds the appropriate
// search path.
// - embed specifies whether this will be added as an "Embed Framework" as well
function addFrameworkFile(fileName, directoryName, embed) {
const newUuid = this.addFileReferenceToParent(
'CustomTemplate',
fileName
);
// if there was an issue, stop
if (!uuid) {
return;
}
const searchPath = directoryName ?
'"$(PROJECT_DIR)/' + directoryName + '"' :
'"$(PROJECT_DIR)"';
[
this.findObject({ isa: 'XCBuildConfiguration', name: 'Debug'}),
this.findObject({ isa: 'XCBuildConfiguration', name: 'Release'})
].forEach(function(buildConfiguration) {
if (!_.has(buildConfiguration, 'buildSettings.FRAMEWORK_SEARCH_PATHS')) {
buildConfiguration.buildSettings.FRAMEWORK_SEARCH_PATHS = [
'"$(inherited)"',
searchPath,
];
} else if (
buildConfiguration
.buildSettings
.FRAMEWORK_SEARCH_PATHS
.indexOf(searchPath) < 0
) {
buildConfiguration
.buildSettings
.FRAMEWORK_SEARCH_PATHS
.push(searchPath);
}
});
this.addChildToParent(
{ isa: 'PBXFrameworksBuildPhase'},
'PBXBuildFile',
{ fileRef: newUuid }
);
if (embed) {
if (!this.findObject('"Embed Frameworks"')) {
this.addBuildPhase('PBXCopyFilesBuildPhase', {
name: '"Embed Frameworks"',
buildActionMask: 2147483647,
dstPath: '""',
dstSubfolderSpec: 10,
files: [],
runOnlyForDeploymentPostprocessing: 0
});
}
this.addChildToParent(
'"Embed Frameworks"',
'PBXBuildFile',
{
fileRef: newUuid,
settings: { ATTRIBUTES: ['CodeSignOnCopy', 'RemoveHeadersOnCopy'] }
}
);
}
}
// adds the appropriate search path and build phase for the library file
function addLibraryFile(newUuid, directoryName) {
const searchPath = directoryName ?
'"$(PROJECT_DIR)/' + directoryName + '"' :
'"$(PROJECT_DIR)"';
[
this.findObject({ isa: 'XCBuildConfiguration', name: 'Debug'}),
this.findObject({ isa: 'XCBuildConfiguration', name: 'Release'})
].forEach(function(buildConfiguration) {
if (!_.has(buildConfiguration, 'buildSettings.LIBRARY_SEARCH_PATHS')) {
buildConfiguration.buildSettings.LIBRARY_SEARCH_PATHS = [
'"$(inherited)"',
searchPath,
];
} else if (
buildConfiguration
.buildSettings
.LIBRARY_SEARCH_PATHS
.indexOf(searchPath) < 0
) {
buildConfiguration
.buildSettings
.LIBRARY_SEARCH_PATHS
.push(searchPath);
}
});
this.addChildToParent(
{ isa: 'PBXFrameworksBuildPhase'},
'PBXBuildFile',
{ fileRef: newUuid }
);
}
function setProvisioningStyle(style) {
var projectObject = this.findObject({isa: 'PBXProject'});
projectObject.attributes.TargetAttributes = projectObject.targets
.reduce((acc, target) => {
return Object.assign({}, acc, {[target]: {ProvisioningStyle: style}});
}, {});
}