Skip to content

Commit

Permalink
Rework hook scripts:
Browse files Browse the repository at this point in the history
- to be fully synchronous to eliminate race conditions (remove q dependency)
- use xml-js (instead of xml2js) to convert XML>JSON and JSON>XML
- [Android] handle existing colors.xml. Resolves #132.
  • Loading branch information
dpa99c committed Nov 8, 2019
1 parent c4ade00 commit bda54cb
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 181 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
"test:android": "bash ./test/test-default.sh 9.0.0 android 8.1.0 && bash ./test/test-with-3-plugins.sh 9.0.0 android 8.1.0 cordova-plugin-request-location-accuracy cordova-android-play-services-gradle-release cordova-android-firebase-gradle-release"
},
"dependencies": {
"plist": "^3.0.1",
"xcode": "^2.0.0",
"xml2js": "^0.4.16",
"q": "^1.5.1",
"plist": "^3.0.1"
"xml-js": "^1.6.11"
}
}
8 changes: 2 additions & 6 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<preference name="FIREBASE_CRASHLYTICS_COLLECTION_ENABLED" default="true" />

<platform name="android">
<preference name="ANDROID_ICON_ACCENT" default="#FF00FFFF" />

<js-module name="FirebasePlugin" src="www/firebase.js">
<clobbers target="FirebasePlugin" />
</js-module>
Expand Down Expand Up @@ -56,12 +58,6 @@
<source-file src="src/android/FirebasePluginMessageReceiverManager.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/JavaScriptException.java" target-dir="src/org/apache/cordova/firebase"/>

<source-file src="src/android/colors.xml" target-dir="res/values" />
<preference name="ANDROID_ICON_ACCENT" default="#FF00FFFF" />
<config-file target="res/values/colors.xml" parent="/resources">
<color name="accent">$ANDROID_ICON_ACCENT</color>
</config-file>

<framework src="src/android/build.gradle" custom="true" type="gradleReference" />

<!-- Default versions for Gradle dependencies -->
Expand Down
203 changes: 100 additions & 103 deletions scripts/after_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,133 +8,130 @@
* Credits: https://github.com/arnesson.
*/
var fs = require('fs');
var path = require('path');
var Q = require('q');
var parser = new (require('xml2js')).Parser();
var Utilities = require("./lib/utilities");

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

var configJson;
var appName;
var appName = Utilities.getAppName();
var pluginVariables = {};

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

var PLATFORM;

var resolveAppName = function(config){
appName = config.widget.name.toString().trim();
if (appName.includes('&amp;')) {
appName = appName.replace(/&amp;/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 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'
],
colorsXml:{
src: './plugins/' + Utilities.getPluginId() +'/src/android/colors.xml',
target: ANDROID_DIR + '/app/src/main/res/values/colors.xml'
}
};
};

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);
});
}
return deferred.promise;
};

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

var parsePluginVariables = function(){
const deferred = Q.defer();
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 config = Utilities.parseConfigXml();
(config.widget.plugin || []).forEach(function(plugin){
(plugin.variable || []).forEach(function(variable){
if((plugin._attributes.name === PLUGIN_ID || plugin._attributes.id === PLUGIN_ID) && variable._attributes.name && variable._attributes.value){
pluginVariables[variable._attributes.name] = variable._attributes.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;
}
var packageJSON = Utilities.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();
});
return deferred.promise;
}
};

module.exports = function (context) {
const deferred = Q.defer();

//get platform from the context supplied by cordova
var platforms = context.opts.platforms;
parsePluginVariables();

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

parseConfigXml().then(function(config){
resolveAppName(config);

// 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);
});

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;
if(!fs.existsSync(PLATFORM.ANDROID.colorsXml.target)){
fs.copyFileSync(PLATFORM.ANDROID.colorsXml.src, PLATFORM.ANDROID.colorsXml.target);
}

const $colorsXml = Utilities.parseXmlFileToJson(PLATFORM.ANDROID.colorsXml.target, {compact: true});
var accentColor = pluginVariables.ANDROID_ICON_ACCENT,
$resources = $colorsXml.resources,
existingAccent = false,
writeChanges = false;

if($resources.color){
var $colors = $resources.color.length ? $resources.color : [$resources.color];
$colors.forEach(function($color){
if($color._attributes.name === 'accent'){
existingAccent = true;
if($color._text !== accentColor){
$color._text = accentColor;
writeChanges = true;
}
}
});
}else{
$resources.color = {};
}

if(!existingAccent){
var $accentColor = {
_attributes: {
name: 'accent'
},
_text: accentColor
};
if($resources.color.length){
$resources.color.push($accentColor)
}else{
$resources.color = $accentColor;
}
writeChanges = true;
}

if(writeChanges){
Utilities.writeJsonToXmlFile($colorsXml, PLATFORM.ANDROID.colorsXml.target);
Utilities.log('Updated colors.xml with accent color');
}
}else if (platforms.indexOf('ios') !== -1 && Utilities.directoryExists(IOS_DIR)){
Utilities.log('Preparing Firebase on iOS');
Utilities.copyKey(PLATFORM.IOS);

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

if(pluginVariables['IOS_STRIP_DEBUG'] && pluginVariables['IOS_STRIP_DEBUG'] === 'true'){
helper.stripDebugSymbols();
}
helper.applyPluginVarsToPlists(PLATFORM.IOS.dest, PLATFORM.IOS.appPlist, pluginVariables);
}
};
9 changes: 4 additions & 5 deletions scripts/ios/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ module.exports = {
/**
* Used to get the path to the XCode project's .pbxproj file.
*/
getXcodeProjectPath: function (cb) {
utilities.getAppName(function(appName){
cb(path.join("platforms", "ios", appName + ".xcodeproj", "project.pbxproj"));
});
getXcodeProjectPath: function () {
var appName = utilities.getAppName();
return path.join("platforms", "ios", appName + ".xcodeproj", "project.pbxproj");
},

/**
Expand Down Expand Up @@ -204,7 +203,7 @@ module.exports = {
}
if(pluginVariables['SETUP_RECAPTCHA_VERIFICATION'] === 'true'){
var reversedClientId = googlePlist['REVERSED_CLIENT_ID'];

if(!appPlist['CFBundleURLTypes']) appPlist['CFBundleURLTypes'] = [];
var entry, i;
for(i=0; i<appPlist['CFBundleURLTypes'].length; i++){
Expand Down
Loading

0 comments on commit bda54cb

Please sign in to comment.