Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 110221d

Browse files
Plamen5kovvchimev
authored and
vchimev
committed
fix snapshots
1 parent 34e0d50 commit 110221d

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

index.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const path = require("path");
22
const { existsSync } = require("fs");
3-
const semver = require("semver");
43

5-
const { getPackageJson, getProjectDir, isAngular, getAndroidRuntimeVersion } = require("./projectHelpers");
4+
const { getPackageJson, getProjectDir, isAngular, resolveAndroidAppPath } = require("./projectHelpers");
65

76
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
87
const APP_DIR = path.join(PROJECT_DIR, "app");
@@ -32,16 +31,7 @@ exports.getAppPath = platform => {
3231

3332
return `platforms/ios/${sanitizedName}/app`;
3433
} else if (/android/i.test(platform)) {
35-
const androidPackageVersion = getAndroidRuntimeVersion(PROJECT_DIR);
36-
const RESOURCES_PATH = "src/main/assets/app";
37-
const PROJECT_ROOT_PATH = "platforms/android";
38-
39-
const normalizedPlatformVersion = `${semver.major(androidPackageVersion)}.${semver.minor(androidPackageVersion)}.0`;
40-
const appPath = semver.lt(normalizedPlatformVersion, "3.4.0") ?
41-
path.join(PROJECT_ROOT_PATH, RESOURCES_PATH) :
42-
path.join(PROJECT_ROOT_PATH, "app", RESOURCES_PATH);
43-
44-
return path.join(PROJECT_DIR, appPath);
34+
return resolveAndroidAppPath(PROJECT_DIR);
4535
} else {
4636
throw new Error(`Invalid platform: ${platform}`);
4737
}

plugins/NativeScriptSnapshotPlugin.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { resolve, join } = require("path");
22
const { closeSync, openSync } = require("fs");
33

44
const ProjectSnapshotGenerator = require("../snapshot/android/project-snapshot-generator");
5+
const { resolveAndroidAppPath } = require("../projectHelpers");
56

67
exports.NativeScriptSnapshotPlugin = (function() {
78
function NativeScriptSnapshotPlugin(options) {
@@ -43,16 +44,16 @@ exports.NativeScriptSnapshotPlugin = (function() {
4344

4445
console.log(`\n Snapshotting bundle at ${inputFile}`);
4546

46-
const preparedAppRootPath = join(options.projectRoot, "platforms/android/src/main/assets");
47-
const preprocessedInputFile = join(preparedAppRootPath, "app/_embedded_script_.js");
47+
const preparedAppRootPath = resolveAndroidAppPath(this.options.projectRoot);
48+
const preprocessedInputFile = join(preparedAppRootPath, "_embedded_script_.js");
4849

4950
return ProjectSnapshotGenerator.prototype.generate.call(this, {
5051
inputFile,
5152
preprocessedInputFile,
5253
targetArchs: options.targetArchs,
5354
useLibs: options.useLibs,
5455
androidNdkPath: options.androidNdkPath,
55-
tnsJavaClassesPath: join(preparedAppRootPath, "app/tns-java-classes.js")
56+
tnsJavaClassesPath: join(preparedAppRootPath, "tns-java-classes.js")
5657
}).then(() => {
5758
// Make the original file empty
5859
if (inputFile !== preprocessedInputFile) {

projectHelpers.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require("path");
22
const fs = require("fs");
3+
const semver = require("semver");
34

45
const isTypeScript = ({ projectDir, packageJson } = {}) => {
56
packageJson = packageJson || getPackageJson(projectDir);
@@ -63,6 +64,19 @@ const getProjectDir = ({ nestingLvl } = { nestingLvl: 0 }) => {
6364
.reduce(dir => path.dirname(dir), __dirname);
6465
};
6566

67+
const resolveAndroidAppPath = (projectDir) => {
68+
const androidPackageVersion = getAndroidRuntimeVersion(projectDir);
69+
const RESOURCES_PATH = "src/main/assets/app";
70+
const PROJECT_ROOT_PATH = "platforms/android";
71+
72+
const normalizedPlatformVersion = `${semver.major(androidPackageVersion)}.${semver.minor(androidPackageVersion)}.0`;
73+
const appPath = semver.lt(normalizedPlatformVersion, "3.4.0") ?
74+
path.join(PROJECT_ROOT_PATH, RESOURCES_PATH) :
75+
path.join(PROJECT_ROOT_PATH, "app", RESOURCES_PATH);
76+
77+
return path.join(projectDir, appPath);
78+
};
79+
6680
const getPackageJsonPath = projectDir => path.resolve(projectDir, "package.json");
6781

6882
module.exports = {
@@ -72,4 +86,5 @@ module.exports = {
7286
getPackageJson,
7387
getProjectDir,
7488
getAndroidRuntimeVersion,
89+
resolveAndroidAppPath,
7590
};

snapshot/android/project-snapshot-generator.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const semver = require("semver");
66

77
const SnapshotGenerator = require("./snapshot-generator");
88
const TnsJavaClassesGenerator = require("./tns-java-classes-generator");
9+
const { resolveAndroidAppPath } = require("../../projectHelpers");
10+
911
const {
1012
CONSTANTS,
1113
createDirectory,
@@ -27,7 +29,6 @@ const resolveRelativePath = (path) => {
2729

2830
function ProjectSnapshotGenerator(options) {
2931
this.options = options = options || {};
30-
3132
options.projectRoot = resolveRelativePath(options.projectRoot) || process.cwd();
3233

3334
console.log("Project root: " + options.projectRoot);
@@ -58,7 +59,8 @@ ProjectSnapshotGenerator.cleanSnapshotArtefacts = function (projectRoot) {
5859
ProjectSnapshotGenerator.installSnapshotArtefacts = function (projectRoot) {
5960
const buildPath = ProjectSnapshotGenerator.calculateBuildPath(projectRoot);
6061
const platformPath = join(projectRoot, "platforms/android");
61-
const assetsPath = join(platformPath, "src/main/assets");
62+
63+
const appPath = resolveAndroidAppPath(projectRoot);
6264
const configDestinationPath = join(platformPath, "configurations", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE);
6365

6466
// Remove build folder to make sure that the apk will be fully rebuild
@@ -70,7 +72,7 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function (projectRoot) {
7072

7173
// Copy tns-java-classes.js
7274
if (shelljs.test("-e", join(buildPath, "tns-java-classes.js"))) {
73-
shelljs.cp(join(buildPath, "tns-java-classes.js"), join(assetsPath, "app/tns-java-classes.js"));
75+
shelljs.cp(join(buildPath, "tns-java-classes.js"), join(appPath, "tns-java-classes.js"));
7476
}
7577

7678
if (shelljs.test("-e", join(buildPath, "ndk-build/libs"))) {
@@ -84,11 +86,11 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function (projectRoot) {
8486
else {
8587
// useLibs = false
8688
const blobsSrcPath = join(buildPath, "snapshots/blobs");
87-
const blobsDestinationPath = join(assetsPath, "snapshots");
88-
const appPackageJsonPath = join(assetsPath, "app/package.json");
89+
const blobsDestinationPath = resolve(appPath, "../snapshots");
90+
const appPackageJsonPath = join(appPath, "package.json");
8991

9092
// Copy the blobs in the prepared app folder
91-
shelljs.cp("-R", blobsSrcPath + "/", join(assetsPath, "snapshots"));
93+
shelljs.cp("-R", blobsSrcPath + "/", resolve(appPath, "../snapshots"));
9294

9395
/*
9496
Rename TNSSnapshot.blob files to snapshot.blob files. The xxd tool uses the file name for the name of the static array. This is why the *.blob files are initially named TNSSnapshot.blob. After the xxd step, they must be renamed to snapshot.blob, because this is the filename that the Android runtime is looking for.
@@ -146,7 +148,7 @@ ProjectSnapshotGenerator.prototype.getV8Version = function (generationOptions) {
146148
return resolve(maybeV8Version);
147149
}
148150

149-
const runtimeVersion = this.getAndroidRuntimeVersion(this.options.projectRoot);
151+
const runtimeVersion = getAndroidRuntimeVersion(this.options.projectRoot);
150152
getV8VersionsMap(runtimeVersion)
151153
.then(({ versionsMap, latest }) => {
152154
const v8Version = findV8Version(runtimeVersion, versionsMap);

0 commit comments

Comments
 (0)