From 0059fb65ad768222933250be6488d1cf991df4fe Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Tue, 25 Jun 2019 17:04:54 +0300 Subject: [PATCH] fix: prepare hooks for NativeScript 6.0 release In NativeScript 6.0 release hookArgs are changed, so this requires changes in the plugin. Apply required changes and make them compatible with CLI 5.4.x (and older) and 6.x.x versions. --- src/scripts/android/appsync-android.js | 25 +++++++++---------- src/scripts/ios/appsync-ios.js | 27 +++++++++++---------- src/scripts/prepare-hooks-helper.js | 33 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 src/scripts/prepare-hooks-helper.js diff --git a/src/scripts/android/appsync-android.js b/src/scripts/android/appsync-android.js index ae2e731..ac1d97b 100644 --- a/src/scripts/android/appsync-android.js +++ b/src/scripts/android/appsync-android.js @@ -1,5 +1,6 @@ const fs = require('fs'), - path = require('path'); + path = require('path'), + prepareHooksHelper = require("../prepare-hooks-helper"); // patch NativeScriptApplication.java so it calls TNSAppSync (which is included in the bundled .aar file) function patchNativeScriptApplication(androidProjectFolder) { @@ -13,18 +14,18 @@ function patchNativeScriptApplication(androidProjectFolder) { // patch NativeScriptApplication so TNSAppSync.activatePackage it's only called once in the app lifecycle const tnsAppFile = path.join(nsPackage, "NativeScriptApplication.java"); replaceInFile( - tnsAppFile, - 'super.onCreate();', - // adding a space so we don't do this more than once - 'super.onCreate() ;\n\t\t\t\tTNSAppSync.activatePackage(this);'); + tnsAppFile, + 'super.onCreate();', + // adding a space so we don't do this more than once + 'super.onCreate() ;\n\t\t\t\tTNSAppSync.activatePackage(this);'); - } catch(e) { + } catch (e) { console.log("AppSync Android hook error: " + e); } } function replaceInFile(someFile, what, by) { - fs.readFile(someFile, 'utf8', function (err,data) { + fs.readFile(someFile, 'utf8', function (err, data) { if (err) { return console.log(err); } @@ -36,11 +37,11 @@ function replaceInFile(someFile, what, by) { }); } -module.exports = function (logger, platformsData, projectData, hookArgs) { - const androidProjectFolder = path.join(projectData.platformsDir, "android"); +module.exports = function ($injector, hookArgs) { + const platform = prepareHooksHelper.getPlatformFromPrepareHookArgs(hookArgs); - return new Promise(function (resolve, reject) { + if (platform === 'android') { + const androidProjectFolder = prepareHooksHelper.getNativeProjectDir($injector, platform, hookArgs); patchNativeScriptApplication(androidProjectFolder); - resolve(); - }); + } }; diff --git a/src/scripts/ios/appsync-ios.js b/src/scripts/ios/appsync-ios.js index d9ac56e..b4d0351 100644 --- a/src/scripts/ios/appsync-ios.js +++ b/src/scripts/ios/appsync-ios.js @@ -1,5 +1,6 @@ const fs = require('fs'), - path = require('path'); + path = require('path'), + prepareHooksHelper = require("../prepare-hooks-helper"); // inject some code into main.m function patchUIApplicationMain(iosProjectFolder) { @@ -17,20 +18,20 @@ function patchUIApplicationMain(iosProjectFolder) { if (tnsAppSyncFileDestContents.indexOf("TNSAppSync") === -1) { // let's first inject a header we need replaceInFile( - appSyncFileDest, - '#include ', - '#include \n#include ' + appSyncFileDest, + '#include ', + '#include \n#include ' ); // now inject the function call that determines the correct application path (either default or appsync'ed) replaceInFile( - appSyncFileDest, - 'applicationPath = [NSBundle mainBundle].bundlePath;', - 'applicationPath = [TNSAppSync applicationPathWithDefault:[NSBundle mainBundle].bundlePath];' + appSyncFileDest, + 'applicationPath = [NSBundle mainBundle].bundlePath;', + 'applicationPath = [TNSAppSync applicationPathWithDefault:[NSBundle mainBundle].bundlePath];' ); } - } catch(e) { + } catch (e) { console.log("AppSync iOS hook error: " + e); } } @@ -41,11 +42,11 @@ function replaceInFile(theFile, what, by) { fs.writeFileSync(theFile, result, 'utf8'); } -module.exports = function (logger, platformsData, projectData, hookArgs) { - const iosProjectFolder = path.join(projectData.platformsDir, "ios"); +module.exports = function ($injector, hookArgs) { + const platform = prepareHooksHelper.getPlatformFromPrepareHookArgs(hookArgs); - return new Promise(function (resolve, reject) { + if (platform === 'ios') { + const iosProjectFolder = prepareHooksHelper.getNativeProjectDir($injector, platform, hookArgs); patchUIApplicationMain(iosProjectFolder); - resolve(); - }); + } }; \ No newline at end of file diff --git a/src/scripts/prepare-hooks-helper.js b/src/scripts/prepare-hooks-helper.js new file mode 100644 index 0000000..f5c3c32 --- /dev/null +++ b/src/scripts/prepare-hooks-helper.js @@ -0,0 +1,33 @@ +function getProjectData ($injector, hookArgs) { + if (hookArgs && hookArgs.projectData) { + // CLI 5.4.x or older + return hookArgs.projectData; + } + + // CLI 6.0.0 and later + const projectDir = hookArgs && hookArgs.prepareData && hookArgs.prepareData.projectDir; + const $projectDataService = $injector.resolve('projectDataService') + const projectData = $projectDataService.getProjectData(projectDir); + return projectData; +} + +module.exports.getPlatformFromPrepareHookArgs = function (hookArgs) { + const platform = (hookArgs && (hookArgs.platform || (hookArgs.prepareData && hookArgs.prepareData.platform)) || '').toLowerCase(); + return platform; +} + +module.exports.getNativeProjectDir = function ($injector, platform, hookArgs) { + let service = null; + try { + // CLI 6.0.0 and later + service = $injector.resolve('platformsDataService'); + } catch (err) { + // CLI 5.4.x and below: + service = $injector.resolve('platformsData'); + } + + const projectData = getProjectData($injector, hookArgs); + const platformData = service.getPlatformData(platform, projectData); + + return platformData.projectRoot; +} \ No newline at end of file