Skip to content

Commit

Permalink
Fix parsing of config.xml to extract app name. Fixes #139.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpa99c committed Nov 7, 2019
1 parent d3932c0 commit 71c896f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 85 deletions.
171 changes: 95 additions & 76 deletions scripts/after_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,86 @@ var parser = new (require('xml2js')).Parser();

var utilities = require("./lib/utilities");

var config = fs.readFileSync('config.xml').toString();
var name = utilities.getValue(config, 'name');
if (name.includes('&')) {
name = name.replace(/&/g, '&');
}
var configJson;
var appName;
var pluginVariables = {};

var IOS_DIR = 'platforms/ios';
var ANDROID_DIR = 'platforms/android';
var PLUGIN_ID = 'cordova-plugin-firebasex';

var PLATFORM = {
IOS: {
dest: IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
src: [
'GoogleService-Info.plist',
IOS_DIR + '/www/GoogleService-Info.plist',
'www/GoogleService-Info.plist'
],
appPlist: IOS_DIR + '/' + name + '/'+name+'-Info.plist',
},
ANDROID: {
dest: ANDROID_DIR + '/app/google-services.json',
src: [
'google-services.json',
ANDROID_DIR + '/assets/www/google-services.json',
'www/google-services.json',
ANDROID_DIR + '/app/src/main/google-services.json'
],
}
var PLATFORM;

var resolveAppName = function(config){
appName = config.widget.name.toString().trim();
if (appName.includes('&')) {
appName = appName.replace(/&/g, '&');
}
PLATFORM = {
IOS: {
dest: IOS_DIR + '/' + appName + '/Resources/GoogleService-Info.plist',
src: [
'GoogleService-Info.plist',
IOS_DIR + '/www/GoogleService-Info.plist',
'www/GoogleService-Info.plist'
],
appPlist: IOS_DIR + '/' + appName + '/'+appName+'-Info.plist',
},
ANDROID: {
dest: ANDROID_DIR + '/app/google-services.json',
src: [
'google-services.json',
ANDROID_DIR + '/assets/www/google-services.json',
'www/google-services.json',
ANDROID_DIR + '/app/src/main/google-services.json'
],
}
};
};

var parsePluginVariables = function(){
const deferred = Q.defer();
var parseConfigXml = function () {
parser.parseString(config, function (err, data) {
(data.widget.plugin || []).forEach(function (plugin) {
(plugin.variable || []).forEach(function (variable) {
if((plugin.$.name === PLUGIN_ID || plugin.$.id === PLUGIN_ID) && variable.$.name && variable.$.value){
pluginVariables[variable.$.name] = variable.$.value;
}
var parseConfigXml = function () {
const deferred = Q.defer();
if(configJson){
deferred.resolve(configJson);
}else{
var configXml = fs.readFileSync('config.xml').toString();
parser.parseString(configXml, function (err, _config) {
configJson = _config;
deferred.resolve(_config);
});
});
deferred.resolve();
});
}
return deferred.promise;
};
};

var parsePackageJson = function(){
return JSON.parse(fs.readFileSync('./package.json'));
};

var parsePackageJson = function(){
var parsePluginVariables = function(){
const deferred = Q.defer();
var packageJSON = JSON.parse(fs.readFileSync('./package.json'));
if(packageJSON.cordova && packageJSON.cordova.plugins){
for(const pluginId in packageJSON.cordova.plugins){
if(pluginId === PLUGIN_ID){
for(const varName in packageJSON.cordova.plugins[pluginId]){
var varValue = packageJSON.cordova.plugins[pluginId][varName];
pluginVariables[varName] = varValue;
}
parseConfigXml().then(function(config){
(config.widget.plugin || []).forEach(function(plugin){
(plugin.variable || []).forEach(function(variable){
if((plugin.$.name === PLUGIN_ID || plugin.$.id === PLUGIN_ID) && variable.$.name && variable.$.value){
pluginVariables[variable.$.name] = variable.$.value;
}
});
});

var packageJSON = parsePackageJson();
if(packageJSON.cordova && packageJSON.cordova.plugins){
for(const pluginId in packageJSON.cordova.plugins){
if(pluginId === PLUGIN_ID){
for(const varName in packageJSON.cordova.plugins[pluginId]){
var varValue = packageJSON.cordova.plugins[pluginId][varName];
pluginVariables[varName] = varValue;
}
}
}
}
}
}
deferred.resolve();
deferred.resolve();
});
return deferred.promise;
};

return parseConfigXml().then(parsePackageJson);
};

module.exports = function (context) {
Expand All @@ -88,34 +102,39 @@ module.exports = function (context) {
//get platform from the context supplied by cordova
var platforms = context.opts.platforms;

// Copy key files to their platform specific folders
if (platforms.indexOf('android') !== -1 && utilities.directoryExists(ANDROID_DIR)) {
console.log('Preparing Firebase on Android');
utilities.copyKey(PLATFORM.ANDROID);
}

if (platforms.indexOf('ios') !== -1 && utilities.directoryExists(IOS_DIR)) {
console.log('Preparing Firebase on iOS');
utilities.copyKey(PLATFORM.IOS);

var helper = require("./ios/helper");
helper.getXcodeProjectPath(function(xcodeProjectPath){
helper.ensureRunpathSearchPath(context, xcodeProjectPath);
});
parseConfigXml().then(function(config){
resolveAppName(config);

parsePluginVariables().then(function(){
if(pluginVariables['IOS_STRIP_DEBUG'] && pluginVariables['IOS_STRIP_DEBUG'] === 'true'){
helper.stripDebugSymbols();
// Copy key files to their platform specific folders
if (platforms.indexOf('android') !== -1 && utilities.directoryExists(ANDROID_DIR)) {
console.log('Preparing Firebase on Android');
utilities.copyKey(PLATFORM.ANDROID);
}
helper.applyPluginVarsToPlists(PLATFORM.IOS.dest, PLATFORM.IOS.appPlist, pluginVariables);

deferred.resolve();
}).catch(error => {
deferred.reject(error);
});
}else{
deferred.resolve();
}
if (platforms.indexOf('ios') !== -1 && utilities.directoryExists(IOS_DIR)) {
console.log('Preparing Firebase on iOS');
utilities.copyKey(PLATFORM.IOS);

var helper = require("./ios/helper");
helper.getXcodeProjectPath(function(xcodeProjectPath){
helper.ensureRunpathSearchPath(context, xcodeProjectPath);
});

parsePluginVariables().then(function(){
if(pluginVariables['IOS_STRIP_DEBUG'] && pluginVariables['IOS_STRIP_DEBUG'] === 'true'){
helper.stripDebugSymbols();
}
helper.applyPluginVarsToPlists(PLATFORM.IOS.dest, PLATFORM.IOS.appPlist, pluginVariables);

deferred.resolve();
}).catch(error => {
deferred.reject(error);
});
}else{
deferred.resolve();
}
});

return deferred.promise;
};
9 changes: 0 additions & 9 deletions scripts/lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ module.exports = {
}
},

getValue: function (config, name) {
var value = config.match(new RegExp('<' + name + '(.*?)>(.*?)</' + name + '>', 'i'));
if (value && value[2]) {
return value[2]
} else {
return null
}
},

fileExists: function (path) {
try {
return fs.statSync(path).isFile();
Expand Down

0 comments on commit 71c896f

Please sign in to comment.