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
·138 lines (105 loc) · 4.11 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
#!/usr/bin/env node
'use strict';
// This hook automates this:
// https://github.com/eface2face/cordova-plugin-iosrtc/blob/master/docs/Building.md
var
fs = require("fs"),
path = require("path"),
xcode = require('xcode'),
BUILD_VERSION = '9.2',
BUILD_VERSION_XCODE = '"' + BUILD_VERSION + '"',
RUNPATH_SEARCH_PATHS = '@executable_path/Frameworks',
RUNPATH_SEARCH_PATHS_XCODE = '"' + RUNPATH_SEARCH_PATHS + '"',
ENABLE_BITCODE = 'NO',
ENABLE_BITCODE_XCODE = '"' + ENABLE_BITCODE + '"',
BRIDGING_HEADER_END = '/Plugins/cordova-plugin-iosrtc/cordova-plugin-iosrtc-Bridging-Header.h',
COMMENT_KEY = /_comment$/;
// Helpers
// Returns the project name
function getProjectName(protoPath) {
var
cordovaConfigPath = path.join(protoPath, 'config.xml'),
content = fs.readFileSync(cordovaConfigPath, 'utf-8');
return /<name>([\s\S]*)<\/name>/mi.exec(content)[1].trim();
}
// Drops the comments
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;
}
// Starting here
module.exports = function (context) {
var
projectRoot = context.opts.projectRoot,
projectName = getProjectName(projectRoot),
xcconfigPath = path.join(projectRoot, '/platforms/ios/cordova/build.xcconfig'),
xcodeProjectName = projectName + '.xcodeproj',
xcodeProjectPath = path.join(projectRoot, 'platforms', 'ios', xcodeProjectName, 'project.pbxproj'),
swiftBridgingHead = projectName + BRIDGING_HEADER_END,
swiftBridgingHeadXcode = '"' + swiftBridgingHead + '"',
swiftOptions = [''], // <-- begin to file appending AFTER initial newline
xcodeProject;
// Checking if the project files are in the right place
if (!fs.existsSync(xcodeProjectPath)) {
debugerror('an error occurred searching the project file at: "' + xcodeProjectPath + '"');
return;
}
debug('".pbxproj" project file found: ' + xcodeProjectPath);
if (!fs.existsSync(xcconfigPath)) {
debugerror('an error occurred searching the project file at: "' + xcconfigPath + '"');
return;
}
debug('".xcconfig" project file found: ' + xcconfigPath);
xcodeProject = xcode.project(xcodeProjectPath);
// Showing info about the tasks to do
debug('fixing issues in the generated project files:');
debug('- "iOS Deployment Target" and "Deployment Target" to: ' + BUILD_VERSION_XCODE);
debug('- "Runpath Search Paths" to: ' + RUNPATH_SEARCH_PATHS_XCODE);
debug('- "Objective-C Bridging Header" to: ' + swiftBridgingHeadXcode);
debug('- "ENABLE_BITCODE" set to: ' + ENABLE_BITCODE_XCODE);
// Massaging the files
// "build.xcconfig"
swiftOptions.push('LD_RUNPATH_SEARCH_PATHS = ' + RUNPATH_SEARCH_PATHS);
swiftOptions.push('SWIFT_OBJC_BRIDGING_HEADER = ' + swiftBridgingHead);
swiftOptions.push('IPHONEOS_DEPLOYMENT_TARGET = ' + BUILD_VERSION);
swiftOptions.push('ENABLE_BITCODE = ' + ENABLE_BITCODE);
// NOTE: Not needed
// swiftOptions.push('EMBEDDED_CONTENT_CONTAINS_SWIFT = YES');
fs.appendFileSync(xcconfigPath, swiftOptions.join('\n'));
debug('file correctly fixed: ' + xcconfigPath);
// "project.pbxproj"
// Parsing it
xcodeProject.parse(function (error) {
var configurations, buildSettings;
if (error) {
debugerror('an error occurred during the parsing of the project file');
return;
}
configurations = nonComments(xcodeProject.pbxXCBuildConfigurationSection());
// Adding or changing the parameters we need
Object.keys(configurations).forEach(function (config) {
buildSettings = configurations[config].buildSettings;
buildSettings.LD_RUNPATH_SEARCH_PATHS = RUNPATH_SEARCH_PATHS_XCODE;
buildSettings.SWIFT_OBJC_BRIDGING_HEADER = swiftBridgingHeadXcode;
buildSettings.IPHONEOS_DEPLOYMENT_TARGET = BUILD_VERSION_XCODE;
buildSettings.ENABLE_BITCODE = ENABLE_BITCODE_XCODE;
});
// Writing the file again
fs.writeFileSync(xcodeProjectPath, xcodeProject.writeSync(), 'utf-8');
debug('file correctly fixed: ' + xcodeProjectPath);
});
};
function debug(msg) {
console.log('iosrtc-swift-support.js [INFO] ' + msg);
}
function debugerror(msg) {
console.error('iosrtc-swift-support.js [ERROR] ' + msg);
}