forked from saghul/cordova-plugin-iosrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iosrtc-swift-support.js
executable file
·116 lines (90 loc) · 3.77 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
#!/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 = '8.2',
BUILD_VERSION_XCODE = '"' + BUILD_VERSION + '"',
RUNPATH_SEARCH_PATHS = '@executable_path/Frameworks',
RUNPATH_SEARCH_PATHS_XCODE = '"' + RUNPATH_SEARCH_PATHS + '"',
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)) {
console.error('ERROR: An error occured searching the project file at: "' + xcodeProjectPath + '"');
return;
}
console.log('".pbxproj" project file found: ' + xcodeProjectPath);
if (!fs.existsSync(xcconfigPath)) {
console.error('ERROR: An error occured searching the project file at: "' + xcconfigPath + '"');
return;
}
console.log('".xcconfig" project file found: ' + xcconfigPath);
xcodeProject = xcode.project(xcodeProjectPath);
// Showing info about the tasks to do
console.log('Fixing issues in the generated project files:');
console.log('- "iOS Deployment Target" and "Deployment Target" to: ' + BUILD_VERSION_XCODE);
console.log('- "Runpath Search Paths" to: ' + RUNPATH_SEARCH_PATHS_XCODE);
console.log('- "Objective-C Bridging Header" to: ' + swiftBridgingHeadXcode);
// 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);
// NOTE: Not needed
// swiftOptions.push('EMBEDDED_CONTENT_CONTAINS_SWIFT = YES');
fs.appendFileSync(xcconfigPath, swiftOptions.join('\n'));
console.log('File correctly fixed: ' + xcconfigPath);
// "project.pbxproj"
// Parsing it
xcodeProject.parse(function (error) {
var configurations, buildSettings;
if (error) {
console.error('ERROR: An error occured during the parse of the project file');
} else {
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;
});
// Writing the file again
fs.writeFileSync(xcodeProjectPath, xcodeProject.writeSync(), 'utf-8');
console.log('File correctly fixed: ' + xcodeProjectPath);
}
});
};