Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 15e5f06

Browse files
committedSep 16, 2020
(Hooks) Generalise the extraction of plugin variables into utilities.js
1 parent 5118b1d commit 15e5f06

File tree

2 files changed

+100
-80
lines changed

2 files changed

+100
-80
lines changed
 

‎scripts/after_prepare.js

+44-78
Original file line numberDiff line numberDiff line change
@@ -11,95 +11,61 @@ var fs = require('fs');
1111
var path = require("path");
1212
var Utilities = require("./lib/utilities");
1313

14-
var appName = Utilities.getAppName();
14+
var appName;
1515
var pluginVariables = {};
1616

1717
var IOS_DIR = 'platforms/ios';
1818
var ANDROID_DIR = 'platforms/android';
19-
var PLUGIN_ID = 'cordova-plugin-firebasex';
20-
21-
var PLATFORM = {
22-
IOS: {
23-
dest: IOS_DIR + '/' + appName + '/Resources/GoogleService-Info.plist',
24-
src: [
25-
'GoogleService-Info.plist',
26-
IOS_DIR + '/www/GoogleService-Info.plist',
27-
'www/GoogleService-Info.plist'
28-
],
29-
appPlist: IOS_DIR + '/' + appName + '/'+appName+'-Info.plist',
30-
entitlementsDebugPlist: IOS_DIR + '/' + appName + '/Entitlements-Debug.plist',
31-
entitlementsReleasePlist: IOS_DIR + '/' + appName + '/Entitlements-Release.plist',
32-
},
33-
ANDROID: {
34-
dest: ANDROID_DIR + '/app/google-services.json',
35-
src: [
36-
'google-services.json',
37-
ANDROID_DIR + '/assets/www/google-services.json',
38-
'www/google-services.json',
39-
ANDROID_DIR + '/app/src/main/google-services.json'
40-
],
41-
colorsXml:{
42-
src: './plugins/' + Utilities.getPluginId() +'/src/android/colors.xml',
43-
target: ANDROID_DIR + '/app/src/main/res/values/colors.xml'
19+
var PLUGIN_ID;
20+
21+
var PLATFORM;
22+
23+
var setupEnv = function(){
24+
appName = Utilities.getAppName();
25+
PLUGIN_ID = Utilities.getPluginId();
26+
PLATFORM = {
27+
IOS: {
28+
dest: IOS_DIR + '/' + appName + '/Resources/GoogleService-Info.plist',
29+
src: [
30+
'GoogleService-Info.plist',
31+
IOS_DIR + '/www/GoogleService-Info.plist',
32+
'www/GoogleService-Info.plist'
33+
],
34+
appPlist: IOS_DIR + '/' + appName + '/' + appName + '-Info.plist',
35+
entitlementsDebugPlist: IOS_DIR + '/' + appName + '/Entitlements-Debug.plist',
36+
entitlementsReleasePlist: IOS_DIR + '/' + appName + '/Entitlements-Release.plist',
37+
},
38+
ANDROID: {
39+
dest: ANDROID_DIR + '/app/google-services.json',
40+
src: [
41+
'google-services.json',
42+
ANDROID_DIR + '/assets/www/google-services.json',
43+
'www/google-services.json',
44+
ANDROID_DIR + '/app/src/main/google-services.json'
45+
],
46+
colorsXml: {
47+
src: './plugins/' + Utilities.getPluginId() + '/src/android/colors.xml',
48+
target: ANDROID_DIR + '/app/src/main/res/values/colors.xml'
49+
}
4450
}
45-
}
46-
};
51+
};
52+
}
4753

54+
module.exports = function(context){
55+
//get platform from the context supplied by cordova
56+
var platforms = context.opts.platforms;
57+
Utilities.setContext(context);
58+
setupEnv();
4859

49-
var parsePluginVariables = function(){
50-
// Parse plugin.xml
51-
var plugin = Utilities.parsePluginXml();
52-
var prefs = [];
53-
if(plugin.plugin.preference){
54-
prefs = prefs.concat(plugin.plugin.preference);
55-
}
56-
plugin.plugin.platform.forEach(function(platform){
57-
if(platform.preference){
58-
prefs = prefs.concat(platform.preference);
59-
}
60-
});
61-
prefs.forEach(function(pref){
62-
if (pref._attributes){
63-
pluginVariables[pref._attributes.name] = pref._attributes.default;
64-
}
65-
});
66-
67-
// Parse config.xml
68-
var config = Utilities.parseConfigXml();
69-
(config.widget.plugin ? [].concat(config.widget.plugin) : []).forEach(function(plugin){
70-
(plugin.variable ? [].concat(plugin.variable) : []).forEach(function(variable){
71-
if((plugin._attributes.name === PLUGIN_ID || plugin._attributes.id === PLUGIN_ID) && variable._attributes.name && variable._attributes.value){
72-
pluginVariables[variable._attributes.name] = variable._attributes.value;
73-
}
74-
});
75-
});
76-
77-
// Parse package.json
78-
var packageJSON = Utilities.parsePackageJson();
79-
if(packageJSON.cordova && packageJSON.cordova.plugins){
80-
for(const pluginId in packageJSON.cordova.plugins){
81-
if(pluginId === PLUGIN_ID){
82-
for(const varName in packageJSON.cordova.plugins[pluginId]){
83-
var varValue = packageJSON.cordova.plugins[pluginId][varName];
84-
pluginVariables[varName] = varValue;
85-
}
86-
}
87-
}
88-
}
60+
pluginVariables = Utilities.parsePluginVariables();
8961

9062
// set platform key path from plugin variable
91-
if (pluginVariables.ANDROID_FIREBASE_CONFIG_FILEPATH) PLATFORM.ANDROID.src = [pluginVariables.ANDROID_FIREBASE_CONFIG_FILEPATH];
92-
if (pluginVariables.IOS_FIREBASE_CONFIG_FILEPATH) PLATFORM.IOS.src = [pluginVariables.IOS_FIREBASE_CONFIG_FILEPATH];
93-
};
94-
95-
module.exports = function (context) {
63+
if(pluginVariables.ANDROID_FIREBASE_CONFIG_FILEPATH) PLATFORM.ANDROID.src = [pluginVariables.ANDROID_FIREBASE_CONFIG_FILEPATH];
64+
if(pluginVariables.IOS_FIREBASE_CONFIG_FILEPATH) PLATFORM.IOS.src = [pluginVariables.IOS_FIREBASE_CONFIG_FILEPATH];
9665

97-
//get platform from the context supplied by cordova
98-
var platforms = context.opts.platforms;
99-
parsePluginVariables();
10066

10167
// Copy key files to their platform specific folders
102-
if (platforms.indexOf('android') !== -1 && Utilities.directoryExists(ANDROID_DIR)) {
68+
if(platforms.indexOf('android') !== -1 && Utilities.directoryExists(ANDROID_DIR)){
10369
Utilities.log('Preparing Firebase on Android');
10470
Utilities.copyKey(PLATFORM.ANDROID);
10571

@@ -152,7 +118,7 @@ module.exports = function (context) {
152118
}
153119
}
154120

155-
if (platforms.indexOf('ios') !== -1 && Utilities.directoryExists(IOS_DIR)){
121+
if(platforms.indexOf('ios') !== -1 && Utilities.directoryExists(IOS_DIR)){
156122
Utilities.log('Preparing Firebase on iOS');
157123
Utilities.copyKey(PLATFORM.IOS);
158124

‎scripts/lib/utilities.js

+56-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var fs = require('fs');
55
var path = require("path");
66
var parser = require('xml-js');
77

8-
var _configXml, _pluginXml;
8+
var _configXml, _pluginXml, _context, _pluginVariables;
99

1010
var Utilities = {};
1111

@@ -21,6 +21,10 @@ fs.ensureDirSync = function(dir){
2121
}
2222
};
2323

24+
Utilities.setContext = function(context){
25+
_context = context;
26+
};
27+
2428
Utilities.parsePackageJson = function(){
2529
return JSON.parse(fs.readFileSync(path.resolve('./package.json')));
2630
};
@@ -59,7 +63,57 @@ Utilities.getAppName = function(){
5963
* The ID of the plugin; this should match the ID in plugin.xml.
6064
*/
6165
Utilities.getPluginId = function(){
62-
return "cordova-plugin-firebasex";
66+
// if(!_context) throw "Cannot retrieve plugin ID as hook context is not set";
67+
return _context.opts.plugin.id;
68+
};
69+
70+
Utilities.parsePluginVariables = function(){
71+
if(_pluginVariables) return _pluginVariables;
72+
73+
var pluginVariables = {};
74+
75+
// Parse plugin.xml
76+
var plugin = Utilities.parsePluginXml();
77+
var prefs = [];
78+
if(plugin.plugin.preference){
79+
prefs = prefs.concat(plugin.plugin.preference);
80+
}
81+
plugin.plugin.platform.forEach(function(platform){
82+
if(platform.preference){
83+
prefs = prefs.concat(platform.preference);
84+
}
85+
});
86+
prefs.forEach(function(pref){
87+
if (pref._attributes){
88+
pluginVariables[pref._attributes.name] = pref._attributes.default;
89+
}
90+
});
91+
92+
// Parse config.xml
93+
var config = Utilities.parseConfigXml();
94+
(config.widget.plugin ? [].concat(config.widget.plugin) : []).forEach(function(plugin){
95+
(plugin.variable ? [].concat(plugin.variable) : []).forEach(function(variable){
96+
if((plugin._attributes.name === Utilities.getPluginId() || plugin._attributes.id === Utilities.getPluginId()) && variable._attributes.name && variable._attributes.value){
97+
pluginVariables[variable._attributes.name] = variable._attributes.value;
98+
}
99+
});
100+
});
101+
102+
// Parse package.json
103+
var packageJSON = Utilities.parsePackageJson();
104+
if(packageJSON.cordova && packageJSON.cordova.plugins){
105+
for(const pluginId in packageJSON.cordova.plugins){
106+
if(pluginId === Utilities.getPluginId()){
107+
for(const varName in packageJSON.cordova.plugins[pluginId]){
108+
var varValue = packageJSON.cordova.plugins[pluginId][varName];
109+
pluginVariables[varName] = varValue;
110+
}
111+
}
112+
}
113+
}
114+
115+
_pluginVariables = pluginVariables;
116+
return pluginVariables;
63117
};
64118

65119
Utilities.copyKey = function(platform){

0 commit comments

Comments
 (0)
Please sign in to comment.