Skip to content

Instantly share code, notes, and snippets.

@bdirito
Created August 1, 2017 18:46
Show Gist options
  • Save bdirito/8b2370f6cf3243153e7e457fe1ae5c4a to your computer and use it in GitHub Desktop.
Save bdirito/8b2370f6cf3243153e7e457fe1ae5c4a to your computer and use it in GitHub Desktop.
#!/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