Newer
Older
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
/**
Helper module to work with config.xml file.
We will use it to inject plugin-specific options.
*/
var path = require('path');
var xmlHelper = require('./xmlHelper.js');
var cordovaContext;
var projectRoot;
var platforms;
module.exports = {
writeOptions: writeOptions
};
// region Public API
/**
* Inject options into config.xml files of each platform.
*
* @param {Object} context - cordova context instance
* @param {Object} options - plugin options to inject
*/
function writeOptions(context, options) {
setup(context);
injectOptions(options);
}
// endregion
// region Private API
/**
* Initialize module.
*
* @param {Object} cordovaContext - cordova context instance
*/
function setup(context) {
cordovaContext = context;
platforms = context.opts.platforms;
projectRoot = context.opts.projectRoot;
}
/**
* Get name of the current project.
*
* @param {Object} ctx - cordova context instance
* @param {String} projectRoot - current root of the project
*
* @return {String} name of the project
*/
function getProjectName(ctx, projectRoot) {
var cordova_util = ctx.requireCordovaModule('cordova-lib/src/cordova/util');
var xml = cordova_util.projectConfig(projectRoot);
var ConfigParser;
// If we are running Cordova 5.4 or abova - use parser from cordova-common.
// Otherwise - from cordova-lib.
try {
ConfigParser = ctx.requireCordovaModule('cordova-common/src/ConfigParser/ConfigParser');
} catch (e) {
ConfigParser = ctx.requireCordovaModule('cordova-lib/src/configparser/ConfigParser')
}
return new ConfigParser(xml).name();
}
/**
* Get path to config.xml inside iOS project.
*
* @return {String} absolute path to config.xml file
*/
function pathToIosConfigXml() {
var projectName = getProjectName(cordovaContext, projectRoot);
return path.join(projectRoot, 'platforms', 'ios', projectName, 'config.xml');
}
/**
* Get path to config.xml inside Android project.
*
* @return {String} absolute path to config.xml file
*/
function pathToAndroidConfigXml() {
return path.join(projectRoot, 'platforms', 'android', 'res', 'xml', 'config.xml');
}
/**
* Get path to platform-specific config.xml file.
*
* @param {String} platform - for what platform we need config.xml
* @return {String} absolute path to config.xml
*/
function getPlatformSpecificConfigXml(platform) {
var configFilePath = null;
switch (platform) {
case 'ios':
{
configFilePath = pathToIosConfigXml();
break;
}
case 'android':
{
configFilePath = pathToAndroidConfigXml();
break;
}
}
return configFilePath;
}
/**
* Write provided options into config.xml file for each platform.
*
* @param {Object} options - plugin options
*/
function injectOptions(options) {
platforms.forEach(function(platform) {
var configXmlFilePath = getPlatformSpecificConfigXml(platform);
if (configXmlFilePath == null) {
return;
}
// read data from config.xml
var configData = xmlHelper.readXmlAsJson(configXmlFilePath);
if (configData == null) {
console.warn('Configuration file ' + configXmlFilePath + ' not found');
return;
}
// inject new options
var chcpXmlConfig = {};
for (var preferenceName in options) {
injectPreference(chcpXmlConfig, preferenceName, options[preferenceName]);
}
// write them back to config.xml
configData.widget['chcp'] = [];
configData.widget.chcp.push(chcpXmlConfig);
xmlHelper.writeJsonAsXml(configData, configXmlFilePath);
});
}
/**
* Inject preference into xml.
*
* @param {Object} xml - current xml preferences for the plugin
* @param {String} preferenceName - preference name
* @param {Object} preferenceAttributes - preference attributes
*/
function injectPreference(xml, preferenceName, preferenceAttributes) {
xml[preferenceName] = [{
'$': preferenceAttributes
}];
}
// endregion