Skip to content

Commit

Permalink
Merge pull request #16 from rosen-vladimirov/vladimirov/prepare-hooks…
Browse files Browse the repository at this point in the history
…-6.0

fix: prepare hooks for NativeScript 6.0 release
  • Loading branch information
EddyVerbruggen authored Jun 26, 2019
2 parents e3155dd + 0059fb6 commit 4082d8e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
25 changes: 13 additions & 12 deletions src/scripts/android/appsync-android.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
}
Expand All @@ -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();
});
}
};
27 changes: 14 additions & 13 deletions src/scripts/ios/appsync-ios.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -17,20 +18,20 @@ function patchUIApplicationMain(iosProjectFolder) {
if (tnsAppSyncFileDestContents.indexOf("TNSAppSync") === -1) {
// let's first inject a header we need
replaceInFile(
appSyncFileDest,
'#include <NativeScript/NativeScript.h>',
'#include <NativeScript/NativeScript.h>\n#include <AppSync/TNSAppSync.h>'
appSyncFileDest,
'#include <NativeScript/NativeScript.h>',
'#include <NativeScript/NativeScript.h>\n#include <AppSync/TNSAppSync.h>'
);

// 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);
}
}
Expand All @@ -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();
});
}
};
33 changes: 33 additions & 0 deletions src/scripts/prepare-hooks-helper.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4082d8e

Please sign in to comment.