forked from cordova-rtc/cordova-plugin-iosrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iosrtc-swift-support.js
executable file
·328 lines (261 loc) · 11.8 KB
/
iosrtc-swift-support.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
#!/usr/bin/env node
'use strict';
// This hook automates this:
// https://github.com/cordova-rtc/cordova-plugin-iosrtc/blob/master/docs/Building.md
var
fs = require("fs"),
path = require("path"),
xcode = require('xcode'),
xmlEntities = new (require('html-entities').XmlEntities)(),
IPHONEOS_DEPLOYMENT_TARGET = '10.2',
IPHONEOS_DEPLOYMENT_TARGET_XCODE = '"' + IPHONEOS_DEPLOYMENT_TARGET + '"',
SWIFT_VERSION = '4.2',
SWIFT_VERSION_XCODE = '"' + SWIFT_VERSION + '"',
RUNPATH_SEARCH_PATHS = '@executable_path/Frameworks',
RUNPATH_SEARCH_PATHS_XCODE = '"' + RUNPATH_SEARCH_PATHS + '"',
ENABLE_BITCODE = 'NO',
ENABLE_BITCODE_XCODE = '"' + ENABLE_BITCODE + '"',
UNIFIED_BRIDGING_HEADER = 'Plugins/Unified-Bridging-Header.h',
IOSRTC_BRIDGING_HEADER = "cordova-plugin-iosrtc-Bridging-Header.h",
BRIDGING_HEADER_END = '/Plugins/cordova-plugin-iosrtc/' + IOSRTC_BRIDGING_HEADER,
TEST_UNIFIED_BRIDGING_HEADER = false; // Set to true to test handling of existing swift bridging header
// Helpers
// Returns the project name
function getProjectName(protoPath) {
var
cordovaConfigPath = path.join(protoPath, 'config.xml'),
content = fs.readFileSync(cordovaConfigPath, 'utf-8');
var name = /<name>([\s\S]*)<\/name>/mi.exec(content)[1].trim();
return xmlEntities.decode(name);
}
// Drops the comments
var COMMENT_KEY = /_comment$/;
function nonComments(obj) {
var
keys = Object.keys(obj),
newObj = {},
i = 0;
for (i; i < keys.length; i += 1) {
if (!COMMENT_KEY.test(keys[i])) {
newObj[keys[i]] = obj[keys[i]];
}
}
return newObj;
}
function matchBuildSettingsValue(value, expectedValue) {
return value === expectedValue;
}
function hasBuildSettingsValue(value, expectedValue) {
return value && (matchBuildSettingsValue(value, expectedValue) || value.indexOf(expectedValue) !== -1);
}
function convertToFloat(value) {
return parseFloat(value.replace(/[^\d.-]/g,''), 10);
}
function matchMinValue(value, minValue) {
//console.log('matchMinValue', value, minValue)
return value && convertToFloat(value) >= convertToFloat(minValue);
}
function matchBuildSettingsMinValue(value, expectedValue) {
return value && (matchBuildSettingsValue(value, expectedValue) || matchMinValue(value, expectedValue));
}
function matchXcconfigPathValue(swiftOptions, path, expectedValue) {
return swiftOptions.filter(function (swiftOption) {
return swiftOption === path + ' = ' + expectedValue;
}).length > 0;
}
function getXcconfigPathValue(swiftOptions, path) {
var swiftOption = swiftOptions.filter(function (swiftOption) {
return swiftOption.indexOf(path + ' = ') === 0;
})[0];
return swiftOption ? swiftOption.split(' = ')[1] : '';
}
function matchXcconfigMinValue(swiftOptions, path, expectedValue) {
var swiftOption = swiftOptions.find(function (swiftOption) {
return swiftOption.split(' = ')[0] === path;
});
return swiftOption && matchMinValue(swiftOption.split(' = ')[1], expectedValue);
}
function getRelativeToProjectRootPath(path, projectRoot) {
return path.replace(projectRoot, '.');
}
function readFileLines(filePath) {
return fs.readFileSync(filePath).toString().split("\n");
}
function debug(msg) {
console.log('iosrtc-swift-support.js [INFO] ' + msg);
}
function debugError(msg) {
console.error('iosrtc-swift-support.js [ERROR] ' + msg);
process.exit(1);
}
// Starting here
module.exports = function (context) {
// This script has to be executed depending on the command line arguments, not
// on the hook execution cycle.
/*
if (context.hook !== 'before_build' && !context.cmdLine.includes('build')) {
return;
}
*/
var
projectRoot = context.opts.projectRoot,
projectName = getProjectName(projectRoot),
platformPath = path.join(projectRoot, 'platforms', 'ios'),
platformProjectPath = path.join(platformPath, projectName),
xcconfigPath = path.join(platformPath, '/cordova/build.xcconfig'),
xcodeProjectName = projectName + '.xcodeproj',
xcodeProjectConfigPath = path.join(platformPath, xcodeProjectName, 'project.pbxproj'),
swiftBridgingHeaderPath = projectName + BRIDGING_HEADER_END,
swiftBridgingHeaderPathXcode = '"' + swiftBridgingHeaderPath + '"',
swiftOptions = [''], // <-- begin to file appending AFTER initial newline
xcodeProject;
// Showing info about the tasks to do
debug('cordova-plugin-iosrtc is checking issues in the generated project files:');
debug('- Minimum "iOS Deployment Target" and "Deployment Target" to: ' + IPHONEOS_DEPLOYMENT_TARGET_XCODE);
debug('- "Runpath Search Paths" to: ' + RUNPATH_SEARCH_PATHS_XCODE);
if (TEST_UNIFIED_BRIDGING_HEADER) {
debug('- "Objective-C Bridging Header" to: ' + UNIFIED_BRIDGING_HEADER);
} else {
debug('- "Objective-C Bridging Header" to: ' + swiftBridgingHeaderPathXcode);
}
debug('- "ENABLE_BITCODE" set to: ' + ENABLE_BITCODE_XCODE);
debug('- "SWIFT_VERSION" set to: ' + SWIFT_VERSION_XCODE);
// Checking if the project files are in the right place
if (!fs.existsSync(xcodeProjectConfigPath)) {
debugError('an error occurred searching the project file at: "' + xcodeProjectConfigPath + '"');
return;
}
debug('".pbxproj" project file found: ' + getRelativeToProjectRootPath(xcodeProjectConfigPath, projectRoot));
if (!fs.existsSync(xcconfigPath)) {
debugError('an error occurred searching the project file at: "' + xcconfigPath + '"');
return;
}
debug('".xcconfig" project file found: ' + getRelativeToProjectRootPath(xcconfigPath, projectRoot));
xcodeProject = xcode.project(xcodeProjectConfigPath);
// Massaging the files
// "build.xcconfig"
debug('checking file: ' + getRelativeToProjectRootPath(xcconfigPath, projectRoot));
var xcconfigPathChanged = false;
// Parsing it
var currentSwiftOptions = [];
if (fs.existsSync(xcconfigPath)) {
currentSwiftOptions = readFileLines(xcconfigPath);
}
if (!matchXcconfigPathValue(currentSwiftOptions, 'LD_RUNPATH_SEARCH_PATHS', RUNPATH_SEARCH_PATHS)) {
swiftOptions.push('LD_RUNPATH_SEARCH_PATHS = ' + RUNPATH_SEARCH_PATHS);
xcconfigPathChanged = true;
}
if (!matchXcconfigMinValue(currentSwiftOptions, 'IPHONEOS_DEPLOYMENT_TARGET', IPHONEOS_DEPLOYMENT_TARGET)) {
swiftOptions.push('IPHONEOS_DEPLOYMENT_TARGET = ' + IPHONEOS_DEPLOYMENT_TARGET);
xcconfigPathChanged = true;
}
if (!matchXcconfigPathValue(currentSwiftOptions, 'ENABLE_BITCODE', ENABLE_BITCODE)) {
swiftOptions.push('ENABLE_BITCODE = ' + ENABLE_BITCODE);
xcconfigPathChanged = true;
}
if (!matchXcconfigPathValue(currentSwiftOptions, 'SWIFT_VERSION', SWIFT_VERSION)) {
swiftOptions.push('SWIFT_VERSION = ' + SWIFT_VERSION);
xcconfigPathChanged = true;
}
var existingSwiftBridgingHeaderPath;
if (TEST_UNIFIED_BRIDGING_HEADER) {
existingSwiftBridgingHeaderPath = path.join(platformProjectPath, UNIFIED_BRIDGING_HEADER);
} else {
if (!matchXcconfigPathValue(currentSwiftOptions, 'SWIFT_OBJC_BRIDGING_HEADER', swiftBridgingHeaderPath)) {
var currentSwiftBridgingHeader = getXcconfigPathValue(currentSwiftOptions, 'SWIFT_OBJC_BRIDGING_HEADER').replace('$(PROJECT_DIR)/$(PROJECT_NAME)/', '');
if (!existingSwiftBridgingHeaderPath) {
swiftOptions.push('SWIFT_OBJC_BRIDGING_HEADER = ' + swiftBridgingHeaderPath);
xcconfigPathChanged = true;
} else {
existingSwiftBridgingHeaderPath = path.join(platformProjectPath, currentSwiftBridgingHeader);
}
}
}
// NOTE: Not needed
// swiftOptions.push('EMBEDDED_CONTENT_CONTAINS_SWIFT = YES');
if (xcconfigPathChanged) {
fs.appendFileSync(xcconfigPath, swiftOptions.join('\n'));
debug('file correctly fixed: ' + getRelativeToProjectRootPath(xcconfigPath, projectRoot));
} else {
debug('file is correct: ' + getRelativeToProjectRootPath(xcconfigPath, projectRoot));
}
// "project.pbxproj"
debug('checking file: ' + getRelativeToProjectRootPath(xcodeProjectConfigPath, projectRoot));
// Parsing it
xcodeProject.parse(function (error) {
if (error) {
debugError('an error occurred during the parsing of the project file: ' + xcodeProjectConfigPath);
return;
}
// Adding or changing the parameters only if we need
var buildSettingsChanged = false;
var hasSwiftBridgingHeaderPathXcodes = [];
var configurations = nonComments(xcodeProject.pbxXCBuildConfigurationSection());
Object.keys(configurations).forEach(function (config) {
var buildSettings = configurations[config].buildSettings;
if (!hasBuildSettingsValue(buildSettings.LD_RUNPATH_SEARCH_PATHS, RUNPATH_SEARCH_PATHS_XCODE)) {
buildSettings.LD_RUNPATH_SEARCH_PATHS = RUNPATH_SEARCH_PATHS_XCODE;
buildSettingsChanged = true;
}
if (!matchBuildSettingsMinValue(buildSettings.IPHONEOS_DEPLOYMENT_TARGET, IPHONEOS_DEPLOYMENT_TARGET_XCODE)) {
buildSettings.IPHONEOS_DEPLOYMENT_TARGET = IPHONEOS_DEPLOYMENT_TARGET_XCODE;
buildSettingsChanged = true;
}
if (!matchBuildSettingsValue(buildSettings.ENABLE_BITCODE, ENABLE_BITCODE_XCODE)) {
buildSettings.ENABLE_BITCODE = ENABLE_BITCODE_XCODE;
buildSettingsChanged = true;
}
if (!matchBuildSettingsValue(buildSettings.SWIFT_VERSION, SWIFT_VERSION_XCODE)) {
buildSettings.SWIFT_VERSION = SWIFT_VERSION_XCODE;
buildSettingsChanged = true;
}
if (TEST_UNIFIED_BRIDGING_HEADER) {
buildSettings.SWIFT_OBJC_BRIDGING_HEADER = '"' + UNIFIED_BRIDGING_HEADER + '"';
xcodeProject.addHeaderFile(UNIFIED_BRIDGING_HEADER);
}
if (!hasBuildSettingsValue(buildSettings.SWIFT_OBJC_BRIDGING_HEADER, swiftBridgingHeaderPathXcode)) {
// Play nice with existing Swift Bridging Header value
if (existingSwiftBridgingHeaderPath) {
// Sync SWIFT_OBJC_BRIDGING_HEADER with existingSwiftBridgingHeaderPath if do not match
var existingSwiftBridgingHeaderPathXcode = '"' + existingSwiftBridgingHeaderPath + '"';
if (buildSettings.SWIFT_OBJC_BRIDGING_HEADER !== existingSwiftBridgingHeaderPathXcode) {
buildSettings.SWIFT_OBJC_BRIDGING_HEADER = existingSwiftBridgingHeaderPathXcode;
buildSettingsChanged = true;
}
if (hasSwiftBridgingHeaderPathXcodes.indexOf(existingSwiftBridgingHeaderPath) === -1) {
// Check if existing existingSwiftBridgingHeaderPath exists and get file lines
debug('checking file: ' + getRelativeToProjectRootPath(existingSwiftBridgingHeaderPath, projectRoot));
var existingSwiftBridgingHeaderFileLines = [];
if (fs.existsSync(existingSwiftBridgingHeaderPath)) {
existingSwiftBridgingHeaderFileLines = readFileLines(existingSwiftBridgingHeaderPath);
}
// Check if existing existingSwiftBridgingHeaderFileLines contains swiftBridgingHeaderPath
var swiftBridgingHeaderImport = '#import "' + IOSRTC_BRIDGING_HEADER + '"';
var hasSwiftBridgingHeaderPathXcode = existingSwiftBridgingHeaderFileLines.filter(function (line) {
return line === swiftBridgingHeaderImport;
}).length > 0;
if (!hasSwiftBridgingHeaderPathXcode) {
debug('updating existing swift bridging header file: ' + getRelativeToProjectRootPath(existingSwiftBridgingHeaderPath, projectRoot));
existingSwiftBridgingHeaderFileLines.push(swiftBridgingHeaderImport);
fs.writeFileSync(existingSwiftBridgingHeaderPath, existingSwiftBridgingHeaderFileLines.join('\n'), 'utf-8');
debug('file correctly fixed: ' + getRelativeToProjectRootPath(existingSwiftBridgingHeaderPath, projectRoot));
} else {
debug('file is correct: ' + getRelativeToProjectRootPath(existingSwiftBridgingHeaderPath, projectRoot));
}
hasSwiftBridgingHeaderPathXcodes.push(existingSwiftBridgingHeaderPath);
}
} else {
buildSettings.SWIFT_OBJC_BRIDGING_HEADER = swiftBridgingHeaderPathXcode;
buildSettingsChanged = true;
}
}
});
// Writing the file only if changed
if (buildSettingsChanged) {
fs.writeFileSync(xcodeProjectConfigPath, xcodeProject.writeSync(), 'utf-8');
debug('file correctly fixed: ' + getRelativeToProjectRootPath(xcodeProjectConfigPath, projectRoot));
} else {
debug('file is correct: ' + getRelativeToProjectRootPath(xcodeProjectConfigPath, projectRoot));
}
});
};