5
5
// This hook automates this:
6
6
// https://github.com/cordova-rtc/cordova-plugin-iosrtc/blob/master/docs/Building.md
7
7
8
- var
9
- fs = require ( "fs" ) ,
10
- path = require ( "path" ) ,
8
+ var fs = require ( 'fs' ) ,
9
+ path = require ( 'path' ) ,
11
10
xcode = require ( 'xcode' ) ,
12
11
xmlEntities = new ( require ( 'html-entities' ) . XmlEntities ) ( ) ,
13
-
14
12
DISABLE_IOSRTC_HOOK = process . env . DISABLE_IOSRTC_HOOK ? true : false ,
15
13
IPHONEOS_DEPLOYMENT_TARGET = process . env . IPHONEOS_DEPLOYMENT_TARGET || '10.2' ,
16
14
IPHONEOS_DEPLOYMENT_TARGET_XCODE = '"' + IPHONEOS_DEPLOYMENT_TARGET + '"' ,
21
19
ENABLE_BITCODE = 'NO' ,
22
20
ENABLE_BITCODE_XCODE = '"' + ENABLE_BITCODE + '"' ,
23
21
UNIFIED_BRIDGING_HEADER = 'Plugins/Unified-Bridging-Header.h' ,
24
- IOSRTC_BRIDGING_HEADER = " cordova-plugin-iosrtc-Bridging-Header.h" ,
22
+ IOSRTC_BRIDGING_HEADER = ' cordova-plugin-iosrtc-Bridging-Header.h' ,
25
23
BRIDGING_HEADER_END = '/Plugins/cordova-plugin-iosrtc/' + IOSRTC_BRIDGING_HEADER ,
26
24
TEST_UNIFIED_BRIDGING_HEADER = process . env . TEST_UNIFIED_BRIDGING_HEADER ? true : false ; // Set to true to test handling of existing swift bridging header
27
25
28
26
// Helpers
29
27
30
28
// Returns the project name
31
29
function getProjectName ( protoPath ) {
32
- var
33
- cordovaConfigPath = path . join ( protoPath , 'config.xml' ) ,
30
+ var cordovaConfigPath = path . join ( protoPath , 'config.xml' ) ,
34
31
content = fs . readFileSync ( cordovaConfigPath , 'utf-8' ) ;
35
32
36
- var name = / < n a m e > ( [ \S ] * ) < \/ n a m e > / mi . exec ( content ) [ 1 ] . trim ( ) ;
33
+ var name = / < n a m e > ( [ \S ] * ) < \/ n a m e > / im . exec ( content ) [ 1 ] . trim ( ) ;
37
34
38
35
return xmlEntities . decode ( name ) ;
39
36
}
40
37
41
38
// Drops the comments
42
39
var COMMENT_KEY = / _ c o m m e n t $ / ;
43
40
function nonComments ( obj ) {
44
- var
45
- keys = Object . keys ( obj ) ,
41
+ var keys = Object . keys ( obj ) ,
46
42
newObj = { } ,
47
43
i = 0 ;
48
44
@@ -60,11 +56,14 @@ function matchBuildSettingsValue(value, expectedValue) {
60
56
}
61
57
62
58
function hasBuildSettingsValue ( value , expectedValue ) {
63
- return value && ( matchBuildSettingsValue ( value , expectedValue ) || value . indexOf ( expectedValue ) !== - 1 ) ;
59
+ return (
60
+ value &&
61
+ ( matchBuildSettingsValue ( value , expectedValue ) || value . indexOf ( expectedValue ) !== - 1 )
62
+ ) ;
64
63
}
65
64
66
65
function convertToFloat ( value ) {
67
- return parseFloat ( value . replace ( / [ ^ \d . - ] / g, '' ) , 10 ) ;
66
+ return parseFloat ( value . replace ( / [ ^ \d . - ] / g, '' ) , 10 ) ;
68
67
}
69
68
70
69
function matchMinValue ( value , minValue ) {
@@ -73,13 +72,18 @@ function matchMinValue(value, minValue) {
73
72
}
74
73
75
74
function matchBuildSettingsMinValue ( value , expectedValue ) {
76
- return value && ( matchBuildSettingsValue ( value , expectedValue ) || matchMinValue ( value , expectedValue ) ) ;
75
+ return (
76
+ value &&
77
+ ( matchBuildSettingsValue ( value , expectedValue ) || matchMinValue ( value , expectedValue ) )
78
+ ) ;
77
79
}
78
80
79
81
function matchXcconfigPathValue ( swiftOptions , path , expectedValue ) {
80
- return swiftOptions . filter ( function ( swiftOption ) {
81
- return swiftOption === path + ' = ' + expectedValue ;
82
- } ) . length > 0 ;
82
+ return (
83
+ swiftOptions . filter ( function ( swiftOption ) {
84
+ return swiftOption === path + ' = ' + expectedValue ;
85
+ } ) . length > 0
86
+ ) ;
83
87
}
84
88
85
89
function getXcconfigPathValue ( swiftOptions , path ) {
@@ -103,7 +107,7 @@ function getRelativeToProjectRootPath(path, projectRoot) {
103
107
}
104
108
105
109
function readFileLines ( filePath ) {
106
- return fs . readFileSync ( filePath ) . toString ( ) . split ( "\n" ) ;
110
+ return fs . readFileSync ( filePath ) . toString ( ) . split ( '\n' ) ;
107
111
}
108
112
109
113
function debug ( msg ) {
@@ -118,7 +122,6 @@ function debugError(msg) {
118
122
// Starting here
119
123
120
124
module . exports = function ( context ) {
121
-
122
125
// This script has to be executed depending on the command line arguments, not
123
126
// on the hook execution cycle.
124
127
/*
@@ -132,8 +135,7 @@ module.exports = function (context) {
132
135
return ;
133
136
}
134
137
135
- var
136
- projectRoot = context . opts . projectRoot ,
138
+ var projectRoot = context . opts . projectRoot ,
137
139
projectName = getProjectName ( projectRoot ) ,
138
140
platformPath = path . join ( projectRoot , 'platforms' , 'ios' ) ,
139
141
platformProjectPath = path . join ( platformPath , projectName ) ,
@@ -147,7 +149,10 @@ module.exports = function (context) {
147
149
148
150
// Showing info about the tasks to do
149
151
debug ( 'cordova-plugin-iosrtc hook is checking issues in the generated project files:' ) ;
150
- debug ( '- Minimum "iOS Deployment Target" and "Deployment Target" to: ' + IPHONEOS_DEPLOYMENT_TARGET_XCODE ) ;
152
+ debug (
153
+ '- Minimum "iOS Deployment Target" and "Deployment Target" to: ' +
154
+ IPHONEOS_DEPLOYMENT_TARGET_XCODE
155
+ ) ;
151
156
debug ( '- "Runpath Search Paths" to: ' + RUNPATH_SEARCH_PATHS_XCODE ) ;
152
157
if ( TEST_UNIFIED_BRIDGING_HEADER ) {
153
158
debug ( '- "Objective-C Bridging Header" to: ' + UNIFIED_BRIDGING_HEADER ) ;
@@ -159,18 +164,25 @@ module.exports = function (context) {
159
164
160
165
// Checking if the project files are in the right place
161
166
if ( ! fs . existsSync ( xcodeProjectConfigPath ) ) {
162
- debugError ( 'an error occurred searching the project file at: "' + xcodeProjectConfigPath + '"' ) ;
167
+ debugError (
168
+ 'an error occurred searching the project file at: "' + xcodeProjectConfigPath + '"'
169
+ ) ;
163
170
164
171
return ;
165
172
}
166
- debug ( '".pbxproj" project file found: ' + getRelativeToProjectRootPath ( xcodeProjectConfigPath , projectRoot ) ) ;
173
+ debug (
174
+ '".pbxproj" project file found: ' +
175
+ getRelativeToProjectRootPath ( xcodeProjectConfigPath , projectRoot )
176
+ ) ;
167
177
168
178
if ( ! fs . existsSync ( xcconfigPath ) ) {
169
179
debugError ( 'an error occurred searching the project file at: "' + xcconfigPath + '"' ) ;
170
180
171
181
return ;
172
182
}
173
- debug ( '".xcconfig" project file found: ' + getRelativeToProjectRootPath ( xcconfigPath , projectRoot ) ) ;
183
+ debug (
184
+ '".xcconfig" project file found: ' + getRelativeToProjectRootPath ( xcconfigPath , projectRoot )
185
+ ) ;
174
186
175
187
xcodeProject = xcode . project ( xcodeProjectConfigPath ) ;
176
188
@@ -186,12 +198,24 @@ module.exports = function (context) {
186
198
currentSwiftOptions = readFileLines ( xcconfigPath ) ;
187
199
}
188
200
189
- if ( ! matchXcconfigPathValue ( currentSwiftOptions , 'LD_RUNPATH_SEARCH_PATHS' , RUNPATH_SEARCH_PATHS ) ) {
201
+ if (
202
+ ! matchXcconfigPathValue (
203
+ currentSwiftOptions ,
204
+ 'LD_RUNPATH_SEARCH_PATHS' ,
205
+ RUNPATH_SEARCH_PATHS
206
+ )
207
+ ) {
190
208
swiftOptions . push ( 'LD_RUNPATH_SEARCH_PATHS = ' + RUNPATH_SEARCH_PATHS ) ;
191
209
xcconfigPathChanged = true ;
192
210
}
193
211
194
- if ( ! matchXcconfigMinValue ( currentSwiftOptions , 'IPHONEOS_DEPLOYMENT_TARGET' , IPHONEOS_DEPLOYMENT_TARGET ) ) {
212
+ if (
213
+ ! matchXcconfigMinValue (
214
+ currentSwiftOptions ,
215
+ 'IPHONEOS_DEPLOYMENT_TARGET' ,
216
+ IPHONEOS_DEPLOYMENT_TARGET
217
+ )
218
+ ) {
195
219
swiftOptions . push ( 'IPHONEOS_DEPLOYMENT_TARGET = ' + IPHONEOS_DEPLOYMENT_TARGET ) ;
196
220
xcconfigPathChanged = true ;
197
221
}
@@ -210,13 +234,25 @@ module.exports = function (context) {
210
234
if ( TEST_UNIFIED_BRIDGING_HEADER ) {
211
235
existingSwiftBridgingHeaderPath = path . join ( platformProjectPath , UNIFIED_BRIDGING_HEADER ) ;
212
236
} else {
213
- if ( ! matchXcconfigPathValue ( currentSwiftOptions , 'SWIFT_OBJC_BRIDGING_HEADER' , swiftBridgingHeaderPath ) ) {
214
- var currentSwiftBridgingHeader = getXcconfigPathValue ( currentSwiftOptions , 'SWIFT_OBJC_BRIDGING_HEADER' ) . replace ( '$(PROJECT_DIR)/$(PROJECT_NAME)/' , '' ) ;
237
+ if (
238
+ ! matchXcconfigPathValue (
239
+ currentSwiftOptions ,
240
+ 'SWIFT_OBJC_BRIDGING_HEADER' ,
241
+ swiftBridgingHeaderPath
242
+ )
243
+ ) {
244
+ var currentSwiftBridgingHeader = getXcconfigPathValue (
245
+ currentSwiftOptions ,
246
+ 'SWIFT_OBJC_BRIDGING_HEADER'
247
+ ) . replace ( '$(PROJECT_DIR)/$(PROJECT_NAME)/' , '' ) ;
215
248
if ( ! existingSwiftBridgingHeaderPath ) {
216
249
swiftOptions . push ( 'SWIFT_OBJC_BRIDGING_HEADER = ' + swiftBridgingHeaderPath ) ;
217
250
xcconfigPathChanged = true ;
218
251
} else {
219
- existingSwiftBridgingHeaderPath = path . join ( platformProjectPath , currentSwiftBridgingHeader ) ;
252
+ existingSwiftBridgingHeaderPath = path . join (
253
+ platformProjectPath ,
254
+ currentSwiftBridgingHeader
255
+ ) ;
220
256
}
221
257
}
222
258
}
@@ -236,9 +272,11 @@ module.exports = function (context) {
236
272
237
273
// Parsing it
238
274
xcodeProject . parse ( function ( error ) {
239
-
240
275
if ( error ) {
241
- debugError ( 'an error occurred during the parsing of the project file: ' + xcodeProjectConfigPath ) ;
276
+ debugError (
277
+ 'an error occurred during the parsing of the project file: ' +
278
+ xcodeProjectConfigPath
279
+ ) ;
242
280
243
281
return ;
244
282
}
@@ -248,7 +286,6 @@ module.exports = function (context) {
248
286
var hasSwiftBridgingHeaderPathXcodes = [ ] ;
249
287
var configurations = nonComments ( xcodeProject . pbxXCBuildConfigurationSection ( ) ) ;
250
288
Object . keys ( configurations ) . forEach ( function ( config ) {
251
-
252
289
var configuration = configurations [ config ] ,
253
290
buildSettings = configuration . buildSettings ;
254
291
@@ -257,12 +294,22 @@ module.exports = function (context) {
257
294
return ;
258
295
}
259
296
260
- if ( ! hasBuildSettingsValue ( buildSettings . LD_RUNPATH_SEARCH_PATHS , RUNPATH_SEARCH_PATHS_XCODE ) ) {
297
+ if (
298
+ ! hasBuildSettingsValue (
299
+ buildSettings . LD_RUNPATH_SEARCH_PATHS ,
300
+ RUNPATH_SEARCH_PATHS_XCODE
301
+ )
302
+ ) {
261
303
buildSettings . LD_RUNPATH_SEARCH_PATHS = RUNPATH_SEARCH_PATHS_XCODE ;
262
304
buildSettingsChanged = true ;
263
305
}
264
306
265
- if ( ! matchBuildSettingsMinValue ( buildSettings . IPHONEOS_DEPLOYMENT_TARGET , IPHONEOS_DEPLOYMENT_TARGET_XCODE ) ) {
307
+ if (
308
+ ! matchBuildSettingsMinValue (
309
+ buildSettings . IPHONEOS_DEPLOYMENT_TARGET ,
310
+ IPHONEOS_DEPLOYMENT_TARGET_XCODE
311
+ )
312
+ ) {
266
313
buildSettings . IPHONEOS_DEPLOYMENT_TARGET = IPHONEOS_DEPLOYMENT_TARGET_XCODE ;
267
314
buildSettingsChanged = true ;
268
315
}
@@ -282,48 +329,87 @@ module.exports = function (context) {
282
329
xcodeProject . addHeaderFile ( UNIFIED_BRIDGING_HEADER ) ;
283
330
}
284
331
285
- if ( ! hasBuildSettingsValue ( buildSettings . SWIFT_OBJC_BRIDGING_HEADER , swiftBridgingHeaderPathXcode ) ) {
286
-
332
+ if (
333
+ ! hasBuildSettingsValue (
334
+ buildSettings . SWIFT_OBJC_BRIDGING_HEADER ,
335
+ swiftBridgingHeaderPathXcode
336
+ )
337
+ ) {
287
338
// Play nice with existing Swift Bridging Header value
288
339
if ( existingSwiftBridgingHeaderPath ) {
289
-
290
340
// Sync SWIFT_OBJC_BRIDGING_HEADER with existingSwiftBridgingHeaderPath if do not match
291
- var existingSwiftBridgingHeaderPathXcode = '"' + existingSwiftBridgingHeaderPath + '"' ;
292
- if ( buildSettings . SWIFT_OBJC_BRIDGING_HEADER !== existingSwiftBridgingHeaderPathXcode ) {
341
+ var existingSwiftBridgingHeaderPathXcode =
342
+ '"' + existingSwiftBridgingHeaderPath + '"' ;
343
+ if (
344
+ buildSettings . SWIFT_OBJC_BRIDGING_HEADER !==
345
+ existingSwiftBridgingHeaderPathXcode
346
+ ) {
293
347
buildSettings . SWIFT_OBJC_BRIDGING_HEADER = existingSwiftBridgingHeaderPathXcode ;
294
348
buildSettingsChanged = true ;
295
349
}
296
350
297
- if ( hasSwiftBridgingHeaderPathXcodes . indexOf ( existingSwiftBridgingHeaderPath ) === - 1 ) {
298
-
351
+ if (
352
+ hasSwiftBridgingHeaderPathXcodes . indexOf (
353
+ existingSwiftBridgingHeaderPath
354
+ ) === - 1
355
+ ) {
299
356
// Check if existing existingSwiftBridgingHeaderPath exists and get file lines
300
357
301
- debug ( 'checking file: ' + getRelativeToProjectRootPath ( existingSwiftBridgingHeaderPath , projectRoot ) ) ;
358
+ debug (
359
+ 'checking file: ' +
360
+ getRelativeToProjectRootPath (
361
+ existingSwiftBridgingHeaderPath ,
362
+ projectRoot
363
+ )
364
+ ) ;
302
365
303
366
var existingSwiftBridgingHeaderFileLines = [ ] ;
304
367
if ( fs . existsSync ( existingSwiftBridgingHeaderPath ) ) {
305
- existingSwiftBridgingHeaderFileLines = readFileLines ( existingSwiftBridgingHeaderPath ) ;
368
+ existingSwiftBridgingHeaderFileLines = readFileLines (
369
+ existingSwiftBridgingHeaderPath
370
+ ) ;
306
371
}
307
372
308
373
// Check if existing existingSwiftBridgingHeaderFileLines contains swiftBridgingHeaderPath
309
374
var swiftBridgingHeaderImport = '#import "' + IOSRTC_BRIDGING_HEADER + '"' ;
310
- var hasSwiftBridgingHeaderPathXcode = existingSwiftBridgingHeaderFileLines . filter ( function ( line ) {
311
- return line === swiftBridgingHeaderImport ;
312
- } ) . length > 0 ;
375
+ var hasSwiftBridgingHeaderPathXcode =
376
+ existingSwiftBridgingHeaderFileLines . filter ( function ( line ) {
377
+ return line === swiftBridgingHeaderImport ;
378
+ } ) . length > 0 ;
313
379
314
380
if ( ! hasSwiftBridgingHeaderPathXcode ) {
315
- debug ( 'updating existing swift bridging header file: ' + getRelativeToProjectRootPath ( existingSwiftBridgingHeaderPath , projectRoot ) ) ;
381
+ debug (
382
+ 'updating existing swift bridging header file: ' +
383
+ getRelativeToProjectRootPath (
384
+ existingSwiftBridgingHeaderPath ,
385
+ projectRoot
386
+ )
387
+ ) ;
316
388
existingSwiftBridgingHeaderFileLines . push ( swiftBridgingHeaderImport ) ;
317
- fs . writeFileSync ( existingSwiftBridgingHeaderPath , existingSwiftBridgingHeaderFileLines . join ( '\n' ) , 'utf-8' ) ;
318
- debug ( 'file correctly fixed: ' + getRelativeToProjectRootPath ( existingSwiftBridgingHeaderPath , projectRoot ) ) ;
389
+ fs . writeFileSync (
390
+ existingSwiftBridgingHeaderPath ,
391
+ existingSwiftBridgingHeaderFileLines . join ( '\n' ) ,
392
+ 'utf-8'
393
+ ) ;
394
+ debug (
395
+ 'file correctly fixed: ' +
396
+ getRelativeToProjectRootPath (
397
+ existingSwiftBridgingHeaderPath ,
398
+ projectRoot
399
+ )
400
+ ) ;
319
401
} else {
320
- debug ( 'file is correct: ' + getRelativeToProjectRootPath ( existingSwiftBridgingHeaderPath , projectRoot ) ) ;
402
+ debug (
403
+ 'file is correct: ' +
404
+ getRelativeToProjectRootPath (
405
+ existingSwiftBridgingHeaderPath ,
406
+ projectRoot
407
+ )
408
+ ) ;
321
409
}
322
410
323
411
hasSwiftBridgingHeaderPathXcodes . push ( existingSwiftBridgingHeaderPath ) ;
324
412
}
325
-
326
-
327
413
} else {
328
414
buildSettings . SWIFT_OBJC_BRIDGING_HEADER = swiftBridgingHeaderPathXcode ;
329
415
buildSettingsChanged = true ;
@@ -334,9 +420,15 @@ module.exports = function (context) {
334
420
// Writing the file only if changed
335
421
if ( buildSettingsChanged ) {
336
422
fs . writeFileSync ( xcodeProjectConfigPath , xcodeProject . writeSync ( ) , 'utf-8' ) ;
337
- debug ( 'file correctly fixed: ' + getRelativeToProjectRootPath ( xcodeProjectConfigPath , projectRoot ) ) ;
423
+ debug (
424
+ 'file correctly fixed: ' +
425
+ getRelativeToProjectRootPath ( xcodeProjectConfigPath , projectRoot )
426
+ ) ;
338
427
} else {
339
- debug ( 'file is correct: ' + getRelativeToProjectRootPath ( xcodeProjectConfigPath , projectRoot ) ) ;
428
+ debug (
429
+ 'file is correct: ' +
430
+ getRelativeToProjectRootPath ( xcodeProjectConfigPath , projectRoot )
431
+ ) ;
340
432
}
341
433
} ) ;
342
434
} ;
0 commit comments