From 3b27bfc2a23150166917c22e1ced504636733aba Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Sun, 20 Nov 2016 21:32:37 +0100 Subject: [PATCH] Some automations for Android #198 --- scripts/installer.js | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/scripts/installer.js b/scripts/installer.js index fb5df002..26d6ab6d 100755 --- a/scripts/installer.js +++ b/scripts/installer.js @@ -138,6 +138,7 @@ function promptQuestionsResult(result) { if(usingAndroid) { writeGradleFile(result); writeGoogleServiceCopyHook(); + writeGoogleServiceGradleHook(); } console.log('Firebase post install completed. To re-run this script, navigate to the root directory of `nativescript-plugin-firebase` in your `node_modules` folder and run: `npm run config`.'); } @@ -315,6 +316,56 @@ module.exports = function() { } } +function writeGoogleServiceGradleHook() { + console.log("Install firebase-build-gradle hook."); + try { + var scriptContent = +` +var path = require("path"); +var fs = require("fs"); + +module.exports = function() { + + console.log("Configure firebase"); + var buildGradlePath = path.join(__dirname, "..", "..", "platforms", "android", "build.gradle"); + if (fs.existsSync(buildGradlePath)) { + var buildGradleContent = fs.readFileSync(buildGradlePath).toString(); + + if (buildGradleContent.indexOf('classpath "com.google.gms:google-services:3.0.0"') != -1) { + return; + } + + var search = -1; + + search = buildGradleContent.indexOf("repositories", 0); + if (search == -1) { + return; + } + + search = buildGradleContent.indexOf("dependencies", search); + if (search == -1) { + return; + } + + search = buildGradleContent.indexOf("}", search); + if (search == -1) { + return; + } + + buildGradleContent = buildGradleContent.substr(0, search - 1) + ' classpath "com.google.gms:google-services:3.0.0"\\n ' + buildGradleContent.substr(search - 1); + + fs.writeFileSync(buildGradlePath, buildGradleContent); + } +}; +`; + var scriptPath = path.join(appRoot, "hooks", "after-prepare", "firebase-build-gradle.js"); + fs.writeFileSync(scriptPath, scriptContent); + } catch(e) { + console.log("Failed to install firebase-build-gradle hook."); + console.log(e); + } +} + /** * Determines if the answer validates as selected *