Created
August 1, 2017 18:46
-
-
Save bdirito/aac1801901283c2265e23b1a5bafac1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env coffee | |
fs = require 'fs' | |
{parseString:parseXml, Builder:XmlBuilder} = require 'xml2js' | |
directoryExists = (path) -> | |
try | |
return fs.statSync(path).isDirectory() | |
catch e | |
return false | |
if directoryExists 'platforms/android' | |
console.log 'workarround for https://github.com/arnesson/cordova-plugin-firebase/issues/142' | |
# add google-service's client: client_info: mobilesdk_app_id | |
googleservicesjson = require '../../google-services.json' | |
STRINGS_XML = 'platforms/android/res/values/strings.xml' | |
stringsXml = fs.readFileSync STRINGS_XML, {encoding: 'utf8'} | |
parseXml stringsXml, (_fail, data) -> | |
if _fail then throw new Error _fail | |
setall = ( string={} ) -> | |
console.log string | |
string.$.name = 'google_app_id' | |
string.$.templateMergeStrategy = 'preserve' | |
string.$.translatable = false | |
# is this actually always 0? | |
string._ = "\"#{googleservicesjson.client[0].client_info.mobilesdk_app_id}\"" | |
return string | |
found = false | |
for string in data.resources.string | |
if string.$.name == 'google_app_id' | |
found = true | |
setall string # should be noop | |
break | |
if not found | |
data.resources.string.push setall() | |
builder = new XmlBuilder() | |
console.log data | |
fs.writeFileSync STRINGS_XML, builder.buildObject data | |
# java text | |
# add this if needed | |
# add [needed] after this text | |
addIfNeeded = (jText, addQ, addQAfter) -> | |
jTextLines = jText.split '\n' | |
for line in jTextLines | |
if line.trim() == addQ | |
return jTextLines.join '\n' | |
for [lineNo, line] from jTextLines.entries() | |
if line.trim() == addQAfter | |
jTextLines.splice lineNo+1, 0, addQ | |
return jTextLines.join '\n' | |
throw new Error "failed to find #{ addQAfter } in file" | |
MAINACTIVITY_JAVA = 'platforms/android/YOUR/PACKAGE/NAME/HERE/MainActivity.java' | |
mainactivityJava = fs.readFileSync MAINACTIVITY_JAVA, {encoding: 'utf8'} | |
jAdd = (add, addAfter) -> mainactivityJava = addIfNeeded mainactivityJava, add, addAfter | |
jAdd 'import com.google.firebase.FirebaseApp;', 'import org.apache.cordova.*;' | |
jAdd 'FirebaseApp.initializeApp(this);', 'super.onCreate(savedInstanceState);' | |
fs.writeFileSync MAINACTIVITY_JAVA, mainactivityJava |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment