Skip to content

Commit 0059fb6

Browse files
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.
1 parent e3155dd commit 0059fb6

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs'),
2-
path = require('path');
2+
path = require('path'),
3+
prepareHooksHelper = require("../prepare-hooks-helper");
34

45
// patch NativeScriptApplication.java so it calls TNSAppSync (which is included in the bundled .aar file)
56
function patchNativeScriptApplication(androidProjectFolder) {
@@ -13,18 +14,18 @@ function patchNativeScriptApplication(androidProjectFolder) {
1314
// patch NativeScriptApplication so TNSAppSync.activatePackage it's only called once in the app lifecycle
1415
const tnsAppFile = path.join(nsPackage, "NativeScriptApplication.java");
1516
replaceInFile(
16-
tnsAppFile,
17-
'super.onCreate();',
18-
// adding a space so we don't do this more than once
19-
'super.onCreate() ;\n\t\t\t\tTNSAppSync.activatePackage(this);');
17+
tnsAppFile,
18+
'super.onCreate();',
19+
// adding a space so we don't do this more than once
20+
'super.onCreate() ;\n\t\t\t\tTNSAppSync.activatePackage(this);');
2021

21-
} catch(e) {
22+
} catch (e) {
2223
console.log("AppSync Android hook error: " + e);
2324
}
2425
}
2526

2627
function replaceInFile(someFile, what, by) {
27-
fs.readFile(someFile, 'utf8', function (err,data) {
28+
fs.readFile(someFile, 'utf8', function (err, data) {
2829
if (err) {
2930
return console.log(err);
3031
}
@@ -36,11 +37,11 @@ function replaceInFile(someFile, what, by) {
3637
});
3738
}
3839

39-
module.exports = function (logger, platformsData, projectData, hookArgs) {
40-
const androidProjectFolder = path.join(projectData.platformsDir, "android");
40+
module.exports = function ($injector, hookArgs) {
41+
const platform = prepareHooksHelper.getPlatformFromPrepareHookArgs(hookArgs);
4142

42-
return new Promise(function (resolve, reject) {
43+
if (platform === 'android') {
44+
const androidProjectFolder = prepareHooksHelper.getNativeProjectDir($injector, platform, hookArgs);
4345
patchNativeScriptApplication(androidProjectFolder);
44-
resolve();
45-
});
46+
}
4647
};

src/scripts/ios/appsync-ios.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs'),
2-
path = require('path');
2+
path = require('path'),
3+
prepareHooksHelper = require("../prepare-hooks-helper");
34

45
// inject some code into main.m
56
function patchUIApplicationMain(iosProjectFolder) {
@@ -17,20 +18,20 @@ function patchUIApplicationMain(iosProjectFolder) {
1718
if (tnsAppSyncFileDestContents.indexOf("TNSAppSync") === -1) {
1819
// let's first inject a header we need
1920
replaceInFile(
20-
appSyncFileDest,
21-
'#include <NativeScript/NativeScript.h>',
22-
'#include <NativeScript/NativeScript.h>\n#include <AppSync/TNSAppSync.h>'
21+
appSyncFileDest,
22+
'#include <NativeScript/NativeScript.h>',
23+
'#include <NativeScript/NativeScript.h>\n#include <AppSync/TNSAppSync.h>'
2324
);
2425

2526
// now inject the function call that determines the correct application path (either default or appsync'ed)
2627
replaceInFile(
27-
appSyncFileDest,
28-
'applicationPath = [NSBundle mainBundle].bundlePath;',
29-
'applicationPath = [TNSAppSync applicationPathWithDefault:[NSBundle mainBundle].bundlePath];'
28+
appSyncFileDest,
29+
'applicationPath = [NSBundle mainBundle].bundlePath;',
30+
'applicationPath = [TNSAppSync applicationPathWithDefault:[NSBundle mainBundle].bundlePath];'
3031
);
3132
}
3233

33-
} catch(e) {
34+
} catch (e) {
3435
console.log("AppSync iOS hook error: " + e);
3536
}
3637
}
@@ -41,11 +42,11 @@ function replaceInFile(theFile, what, by) {
4142
fs.writeFileSync(theFile, result, 'utf8');
4243
}
4344

44-
module.exports = function (logger, platformsData, projectData, hookArgs) {
45-
const iosProjectFolder = path.join(projectData.platformsDir, "ios");
45+
module.exports = function ($injector, hookArgs) {
46+
const platform = prepareHooksHelper.getPlatformFromPrepareHookArgs(hookArgs);
4647

47-
return new Promise(function (resolve, reject) {
48+
if (platform === 'ios') {
49+
const iosProjectFolder = prepareHooksHelper.getNativeProjectDir($injector, platform, hookArgs);
4850
patchUIApplicationMain(iosProjectFolder);
49-
resolve();
50-
});
51+
}
5152
};

src/scripts/prepare-hooks-helper.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function getProjectData ($injector, hookArgs) {
2+
if (hookArgs && hookArgs.projectData) {
3+
// CLI 5.4.x or older
4+
return hookArgs.projectData;
5+
}
6+
7+
// CLI 6.0.0 and later
8+
const projectDir = hookArgs && hookArgs.prepareData && hookArgs.prepareData.projectDir;
9+
const $projectDataService = $injector.resolve('projectDataService')
10+
const projectData = $projectDataService.getProjectData(projectDir);
11+
return projectData;
12+
}
13+
14+
module.exports.getPlatformFromPrepareHookArgs = function (hookArgs) {
15+
const platform = (hookArgs && (hookArgs.platform || (hookArgs.prepareData && hookArgs.prepareData.platform)) || '').toLowerCase();
16+
return platform;
17+
}
18+
19+
module.exports.getNativeProjectDir = function ($injector, platform, hookArgs) {
20+
let service = null;
21+
try {
22+
// CLI 6.0.0 and later
23+
service = $injector.resolve('platformsDataService');
24+
} catch (err) {
25+
// CLI 5.4.x and below:
26+
service = $injector.resolve('platformsData');
27+
}
28+
29+
const projectData = getProjectData($injector, hookArgs);
30+
const platformData = service.getPlatformData(platform, projectData);
31+
32+
return platformData.projectRoot;
33+
}

0 commit comments

Comments
 (0)