Skip to content

Commit

Permalink
CDN programatically replacing the Github source when cocoapods minimu…
Browse files Browse the repository at this point in the history
…m version is met.
  • Loading branch information
andrehtissot committed Apr 2, 2020
1 parent 94d0d45 commit 2da28e2
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 90 deletions.
2 changes: 1 addition & 1 deletion cordova-plugin-fcm-with-dependecy-updated.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "cordova-plugin-fcm-with-dependecy-updated"
spec.version = "4.5.2"
spec.version = "4.5.3"
spec.summary = "Google FCM Push Notifications Cordova Plugin"

# This description is used to generate tags and improve search results.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.5.2",
"version": "4.5.3",
"name": "cordova-plugin-fcm-with-dependecy-updated",
"cordova_name": "Cordova FCM Push Plugin",
"description": "Google Firebase Cloud Messaging Cordova Push Plugin fork with dependecy updated",
Expand Down
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
specific language governing permissions and limitations
under the License.
-->
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-fcm-with-dependecy-updated" version="4.5.2">
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-fcm-with-dependecy-updated" version="4.5.3">
<name>Cordova FCM Push Plugin</name>
<description>Google Firebase Cloud Messaging Cordova Push Plugin fork with dependecy updated</description>
<license>MIT</license>
Expand Down Expand Up @@ -120,4 +120,5 @@

<!-- COPY FCM CONFIG FILES TO PLATFORM BEFORE COMPILING -->
<hook src="scripts/copy_google_service_files.js" type="after_prepare" />
<hook src="scripts/cdnfy_podfile.js" type="before_plugin_install" />
</plugin>
104 changes: 104 additions & 0 deletions scripts/cdnfy_podfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env node
'use strict';

var child_process = require('child_process');
var helpers = require('./helpers');
var fs = require('fs');

var IOS_PLATFORM_PATH = 'platforms/ios';
var PODFILE_PATH = 'platforms/ios/Podfile';
var PLUGIN_DEFINITION_PATH = 'plugins/cordova-plugin-fcm-with-dependecy-updated/plugin.xml';

function isSupportedByCocoapods(callback) {
// Verifies if installed version of cocoapods supports the cdn repo
// 1.7.2
var major = '1';
var minor = '7';
var patch = '2';
child_process.exec('pod --version', (err, stdout) => {
if (err) {
callback(false);
}
var currentVersion = stdout.replace(/[^\.\d]/, '').split('.');
if (currentVersion[0] < major) {
callback(false);
}
if (currentVersion[0] === major) {
if (currentVersion[1] < minor) {
callback(false);
}
if (currentVersion[1] === minor && currentVersion[2] < patch) {
callback(false);
}
}
callback(true);
});
}

function isPluginFileAvailable() {
if (helpers.fileExists(PLUGIN_DEFINITION_PATH)) {
return true;
} else {
helpers.logWarning('Plugin.xml not found as ' + PLUGIN_DEFINITION_PATH);
return false;
}
}

function isIOSPlatformInstalled() {
return helpers.directoryExists(IOS_PLATFORM_PATH);
}

function replaceGithubHostedByCDN() {
if (isIOSPlatformInstalled()) {
replaceContentInFile(
PODFILE_PATH,
'Podfile',
"source 'https://github.com/CocoaPods/Specs.git'",
"source 'https://cdn.cocoapods.org/'"
);
}
replaceContentInFile(
PLUGIN_DEFINITION_PATH,
'Plugin.xml',
'<source url="https://github.com/CocoaPods/Specs.git"/>',
'<source url="https://cdn.cocoapods.org"/>'
);
}

function replaceContentInFile(filePath, fileName, from, to) {
var currentContent;
try {
currentContent = fs.readFileSync(filePath).toString();
} catch (e) {
helpers.logWarning('Failed to read ' + fileName);
return;
}
if (currentContent === '') {
helpers.logWarning(fileName + ' found is completely empty');
return;
}
var newContent = currentContent.replace(from, to);
if (newContent === currentContent) {
return;
}
try {
fs.writeFileSync(filePath, newContent);
} catch (e) {
helpers.logWarning('Failed to write enhanced ' + fileName);
return;
}
}

function main() {
isSupportedByCocoapods(function (isVersionSupported) {
if (!isVersionSupported) {
return;
}
if (!isPluginFileAvailable()) {
return;
}
replaceGithubHostedByCDN();
});
}

main();
12 changes: 2 additions & 10 deletions scripts/copy_google_service_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ function copyGoogleServiceFile(platform) {
});
}

function directoryExists(path) {
try {
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
}

function ensureFileDirExistance(filePath) {
var dirPath = filePath.substring(0, filePath.lastIndexOf('/'));
ensureDirExistance(dirPath);
Expand All @@ -48,9 +40,9 @@ function ensureDirExistance(dirPath) {
}
}

if (directoryExists(PLATFORM.ANDROID.dir)) {
if (helpers.directoryExists(PLATFORM.ANDROID.dir)) {
copyGoogleServiceFile(PLATFORM.ANDROID);
}
if (directoryExists(PLATFORM.IOS.dir)) {
if (helpers.directoryExists(PLATFORM.IOS.dir)) {
copyGoogleServiceFile(PLATFORM.IOS);
}
14 changes: 11 additions & 3 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

var fs = require('fs');

function fileExists(path) {
exports.fileExists = function (path) {
try {
return fs.statSync(path).isFile();
} catch (e) {
return false;
}
}
};

exports.directoryExists = function (path) {
try {
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
};

exports.findExistingFilePath = function (paths) {
for (var i = paths.length - 1; i > -1; i--) {
if (fileExists(paths[i])) {
if (exports.fileExists(paths[i])) {
return paths[i];
}
}
Expand Down
74 changes: 0 additions & 74 deletions scripts/insert_app_id_key_to_strings.js

This file was deleted.

0 comments on commit 2da28e2

Please sign in to comment.