|
| 1 | +var path = require('path'); |
| 2 | +var fs = require('fs'); |
| 3 | +var os = require('os'); |
| 4 | + |
| 5 | +var PLUGIN_VERSION = '+'; |
| 6 | +var _log = console.log.bind(console); |
| 7 | + |
| 8 | +function targetsAndroid (projectDir) { |
| 9 | + var pkg = require(path.join(projectDir, 'package.json')); |
| 10 | + if (!pkg.nativescript) { |
| 11 | + throw new Error('Not a NativeScript project'); |
| 12 | + } |
| 13 | + |
| 14 | + return ('tns-android' in pkg.nativescript); |
| 15 | +} |
| 16 | + |
| 17 | +function buildGradleExists (platformsDir) { |
| 18 | + return fs.existsSync(_getBuildGradlePath(platformsDir)); |
| 19 | +} |
| 20 | + |
| 21 | +function checkForGoogleServicesJson (projectDir, resourcesDir) { |
| 22 | + var androidIsTargeted = targetsAndroid(projectDir); |
| 23 | + var resourcesPath = path.join(resourcesDir, 'Android', 'google-services.json'); |
| 24 | + |
| 25 | + if (androidIsTargeted && !fs.existsSync(resourcesPath)) { |
| 26 | + _log('|!| google-services.json appears to be missing. Please make sure it is present in the "app/App_Resources/Android" folder, in order to use FCM push notifications |!|'); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function addOnPluginInstall (platformsDir) { |
| 31 | + var path = _getBuildGradlePath(platformsDir); |
| 32 | + if (buildGradleExists(platformsDir)) { |
| 33 | + addIfNecessary(platformsDir); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function addIfNecessary (platformsDir) { |
| 38 | + _amendBuildGradle(platformsDir, function (pluginImported, pluginApplied, fileContents) { |
| 39 | + var newContents = fileContents; |
| 40 | + if (!pluginImported) { |
| 41 | + newContents = _addPluginImport(newContents); |
| 42 | + } |
| 43 | + |
| 44 | + if (!pluginApplied) { |
| 45 | + newContents = _addPluginApplication(newContents); |
| 46 | + } |
| 47 | + return newContents; |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +function removeIfPresent (platformsDir) { |
| 52 | + _amendBuildGradle(platformsDir, function (pluginImported, pluginApplied, fileContents) { |
| 53 | + var newFileContents = fileContents; |
| 54 | + if (pluginImported) { |
| 55 | + newFileContents = _removePluginImport(newFileContents); |
| 56 | + } |
| 57 | + |
| 58 | + if (pluginApplied) { |
| 59 | + newFileContents = _removePluginApplication(newFileContents); |
| 60 | + } |
| 61 | + return newFileContents; |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +function setLogger (logFunc) { |
| 66 | + _log = logFunc; |
| 67 | +} |
| 68 | + |
| 69 | +// ============= private |
| 70 | + |
| 71 | +var _quotesRegExp = '["\']'; |
| 72 | +var _versionRegExp = '[^\'"]+'; |
| 73 | +var _pluginImportName = 'com.google.gms:google-services'; |
| 74 | +var _pluginApplicationName = 'com.google.gms.google-services'; |
| 75 | + |
| 76 | +function _amendBuildGradle (platformsDir, applyAmendment) { |
| 77 | + var path = _getBuildGradlePath(platformsDir); |
| 78 | + |
| 79 | + if (!buildGradleExists(platformsDir)) { |
| 80 | + return _log('build.gradle file not found'); |
| 81 | + } |
| 82 | + |
| 83 | + var fileContents = fs.readFileSync(path, 'utf8'); |
| 84 | + var pluginImported = _checkForImport(fileContents); |
| 85 | + var pluginApplied = _checkForApplication(fileContents); |
| 86 | + var newContents = applyAmendment(pluginImported, pluginApplied, fileContents); |
| 87 | + |
| 88 | + fs.writeFileSync(path, newContents, 'utf8'); |
| 89 | +} |
| 90 | + |
| 91 | +function _removePluginImport (buildGradleContents) { |
| 92 | + var regExpStr = '\\s*classpath +' + _formNamePartOfRegExp(true) + '{0,0}:' + _versionRegExp + _quotesRegExp; |
| 93 | + var regExp = new RegExp(regExpStr, 'i'); |
| 94 | + return buildGradleContents.replace(regExp, ''); |
| 95 | +} |
| 96 | + |
| 97 | +function _removePluginApplication (buildGradleContents) { |
| 98 | + var regExpStr = '\\s*apply plugin: +' + _formNamePartOfRegExp(false); |
| 99 | + var regExp = new RegExp(regExpStr, 'i'); |
| 100 | + return buildGradleContents.replace(regExp, ''); |
| 101 | +} |
| 102 | + |
| 103 | +function _addPluginImport (buildGradleContents) { |
| 104 | + var androidGradle = 'com.android.tools.build:gradle'; |
| 105 | + var insertBeforeDoubleQuotes = 'classpath "' + androidGradle; |
| 106 | + var insertBeforeSingleQoutes = 'classpath \'' + androidGradle; |
| 107 | + |
| 108 | + var ind = buildGradleContents.indexOf(insertBeforeDoubleQuotes); |
| 109 | + if (ind === -1) { |
| 110 | + ind = buildGradleContents.indexOf(insertBeforeSingleQoutes); |
| 111 | + } |
| 112 | + |
| 113 | + if (ind === -1) { |
| 114 | + _log('build.gradle has unexpected contents -- please check it manually'); |
| 115 | + return buildGradleContents; |
| 116 | + } |
| 117 | + |
| 118 | + var result = buildGradleContents.substring(0, ind); |
| 119 | + result += 'classpath "' + _pluginImportName + ':' + PLUGIN_VERSION + '"' + os.EOL; |
| 120 | + result += '\t\t' + insertBeforeDoubleQuotes; |
| 121 | + result += buildGradleContents.substring(ind + ('classpath "' + androidGradle).length); |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +function _addPluginApplication (buildGradleContents) { |
| 126 | + buildGradleContents += os.EOL + 'apply plugin: "' + _pluginApplicationName + '"' + os.EOL; |
| 127 | + return buildGradleContents; |
| 128 | +} |
| 129 | + |
| 130 | +function _formNamePartOfRegExp (useImportName) { |
| 131 | + var name = useImportName ? _pluginImportName : _pluginApplicationName; |
| 132 | + return _quotesRegExp + name + _quotesRegExp; |
| 133 | +} |
| 134 | + |
| 135 | +function _checkForImport (buildGradleContents) { |
| 136 | + var re = new RegExp('classpath +' + _formNamePartOfRegExp(true) + '{0,0}', 'i'); |
| 137 | + return re.test(buildGradleContents); |
| 138 | +} |
| 139 | + |
| 140 | +function _checkForApplication (buildGradleContents) { |
| 141 | + var re = new RegExp('apply plugin: +' + _formNamePartOfRegExp(false), 'i'); |
| 142 | + return re.test(buildGradleContents); |
| 143 | +} |
| 144 | + |
| 145 | +function _getBuildGradlePath (platformsDir) { |
| 146 | + return path.join(platformsDir, 'android', 'build.gradle'); |
| 147 | +} |
| 148 | + |
| 149 | +// ============= end private |
| 150 | + |
| 151 | +module.exports = { |
| 152 | + removeIfPresent: removeIfPresent, |
| 153 | + setLogger: setLogger, |
| 154 | + addIfNecessary: addIfNecessary, |
| 155 | + targetsAndroid: targetsAndroid, |
| 156 | + buildGradleExists: buildGradleExists, |
| 157 | + addOnPluginInstall: addOnPluginInstall, |
| 158 | + checkForGoogleServicesJson: checkForGoogleServicesJson |
| 159 | +}; |
0 commit comments