Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 24778ab

Browse files
Refactoring
1 parent 20d496a commit 24778ab

File tree

4 files changed

+130
-124
lines changed

4 files changed

+130
-124
lines changed

‎plugins/NativeScriptSnapshotPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ exports.NativeScriptSnapshotPlugin = (function() {
7171
options: options.tnsJavaClassesOptions
7272
});
7373

74-
// Run the snapshot tool when the packing is done
74+
// Generate snapshots
7575
compiler.plugin("after-emit", function(compilation, callback) {
7676
debugger;
7777
const chunkToSnapshot = compilation.chunks.find(chunk => chunk.name == options.chunk);

‎snapshot/android/project-snapshot-generator.js‎

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require("path");
1+
const { join, isAbsolute, resolve } = require("path");
22
const fs = require("fs");
33
const shelljs = require("shelljs");
44
const os = require("os");
@@ -11,18 +11,16 @@ const { getPackageJson } = require("../../projectHelpers");
1111
const MIN_ANDROID_RUNTIME_VERSION = "3.0.0";
1212
const VALID_ANDROID_RUNTIME_TAGS = Object.freeze(["next", "rc"]);
1313

14+
const resolveRelativePath = (path) => {
15+
if (path)
16+
return isAbsolute(path) ? path : resolve(process.cwd(), path);
17+
return null;
18+
};
19+
1420
function ProjectSnapshotGenerator (options) {
1521
this.options = options = options || {};
1622

17-
options.projectRoot = options.projectRoot ?
18-
(path.isAbsolute(options.projectRoot) ?
19-
options.projectRoot :
20-
path.resolve(process.cwd(), options.projectRoot)) :
21-
process.cwd();
22-
23-
if (!options.projectRoot) {
24-
throw new Error("The project root is not specified.");
25-
}
23+
options.projectRoot = resolveRelativePath(options.projectRoot) || process.cwd();
2624

2725
console.log("Project root: " + options.projectRoot);
2826
console.log("Snapshots build directory: " + this.getBuildPath());
@@ -32,57 +30,57 @@ function ProjectSnapshotGenerator (options) {
3230
module.exports = ProjectSnapshotGenerator;
3331

3432
ProjectSnapshotGenerator.calculateBuildPath = function(projectRoot) {
35-
return path.join(projectRoot, "platforms/android/snapshot-build/build");
33+
return join(projectRoot, "platforms/android/snapshot-build/build");
3634
}
3735

3836
ProjectSnapshotGenerator.prototype.getBuildPath = function() {
3937
return ProjectSnapshotGenerator.calculateBuildPath(this.options.projectRoot);
4038
}
4139

4240
ProjectSnapshotGenerator.cleanSnapshotArtefacts = function(projectRoot) {
43-
const platformPath = path.join(projectRoot, "platforms/android");
41+
const platformPath = join(projectRoot, "platforms/android");
4442

4543
// Remove blob files from prepared folder
46-
shelljs.rm("-rf", path.join(platformPath, "src/main/assets/snapshots"));
44+
shelljs.rm("-rf", join(platformPath, "src/main/assets/snapshots"));
4745

4846
// Remove prepared include.gradle configurations
49-
shelljs.rm("-rf", path.join(platformPath, "configurations/", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE));
47+
shelljs.rm("-rf", join(platformPath, "configurations/", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE));
5048
}
5149

5250
ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) {
5351
const buildPath = ProjectSnapshotGenerator.calculateBuildPath(projectRoot);
54-
const platformPath = path.join(projectRoot, "platforms/android");
55-
const assetsPath = path.join(platformPath, "src/main/assets");
56-
const configDestinationPath = path.join(platformPath, "configurations", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE);
52+
const platformPath = join(projectRoot, "platforms/android");
53+
const assetsPath = join(platformPath, "src/main/assets");
54+
const configDestinationPath = join(platformPath, "configurations", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE);
5755

5856
// Remove build folder to make sure that the apk will be fully rebuild
59-
shelljs.rm("-rf", path.join(platformPath, "build"));
57+
shelljs.rm("-rf", join(platformPath, "build"));
6058

6159
// Copy include.gradle to the specified destination in the platforms folder
6260
shelljs.mkdir("-p", configDestinationPath);
63-
shelljs.cp(path.join(buildPath, "include.gradle"), path.join(configDestinationPath, "include.gradle"));
61+
shelljs.cp(join(buildPath, "include.gradle"), join(configDestinationPath, "include.gradle"));
6462

6563
// Copy tns-java-classes.js
66-
if (shelljs.test("-e", path.join(buildPath, "tns-java-classes.js"))) {
67-
shelljs.cp(path.join(buildPath, "tns-java-classes.js"), path.join(assetsPath, "app/tns-java-classes.js"));
64+
if (shelljs.test("-e", join(buildPath, "tns-java-classes.js"))) {
65+
shelljs.cp(join(buildPath, "tns-java-classes.js"), join(assetsPath, "app/tns-java-classes.js"));
6866
}
6967

70-
if (shelljs.test("-e", path.join(buildPath, "ndk-build/libs"))) {
68+
if (shelljs.test("-e", join(buildPath, "ndk-build/libs"))) {
7169
// useLibs = true
72-
const libsDestinationPath = path.join(platformPath, "src", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE, "jniLibs");
70+
const libsDestinationPath = join(platformPath, "src", SnapshotGenerator.SNAPSHOT_PACKAGE_NANE, "jniLibs");
7371

7472
// Copy the libs to the specified destination in the platforms folder
7573
shelljs.mkdir("-p", libsDestinationPath);
76-
shelljs.cp("-R", path.join(buildPath, "ndk-build/libs") + "/", libsDestinationPath);
74+
shelljs.cp("-R", join(buildPath, "ndk-build/libs") + "/", libsDestinationPath);
7775
}
7876
else {
7977
// useLibs = false
80-
const blobsSrcPath = path.join(buildPath, "snapshots/blobs");
81-
const blobsDestinationPath = path.join(assetsPath, "snapshots");
82-
const appPackageJsonPath = path.join(assetsPath, "app/package.json");
78+
const blobsSrcPath = join(buildPath, "snapshots/blobs");
79+
const blobsDestinationPath = join(assetsPath, "snapshots");
80+
const appPackageJsonPath = join(assetsPath, "app/package.json");
8381

8482
// Copy the blobs in the prepared app folder
85-
shelljs.cp("-R", blobsSrcPath + "/", path.join(assetsPath, "snapshots"));
83+
shelljs.cp("-R", blobsSrcPath + "/", join(assetsPath, "snapshots"));
8684

8785
/*
8886
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.
@@ -98,9 +96,9 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) {
9896
}
9997

10098
ProjectSnapshotGenerator.prototype.getV8Version = function() {
101-
const nativescriptLibraryPath = path.join(this.options.projectRoot, "platforms/android/libs/runtime-libs/nativescript-regular.aar");
99+
const nativescriptLibraryPath = join(this.options.projectRoot, "platforms/android/libs/runtime-libs/nativescript-regular.aar");
102100
if (!fs.existsSync(nativescriptLibraryPath)) {
103-
nativescriptLibraryPath = path.join(options.projectRoot, "platforms/android/libs/runtime-libs/nativescript.aar");
101+
nativescriptLibraryPath = join(options.projectRoot, "platforms/android/libs/runtime-libs/nativescript.aar");
104102
}
105103

106104
const zip = new require("adm-zip")(nativescriptLibraryPath);
@@ -122,7 +120,7 @@ ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() {
122120
const currentRuntimeVersion = this.getAndroidRuntimeVersion();
123121

124122
if (!currentRuntimeVersion ||
125-
!fs.existsSync(path.join(this.options.projectRoot, "platforms/android"))) {
123+
!fs.existsSync(join(this.options.projectRoot, "platforms/android"))) {
126124

127125
throw new Error("In order to generate a V8 snapshot you must have the \"android\" platform installed - to do so please run \"tns platform add android\".");
128126
}
@@ -155,7 +153,7 @@ ProjectSnapshotGenerator.prototype.generate = function(generationOptions) {
155153
shelljs.mkdir("-p", this.getBuildPath());
156154

157155
// Generate tns-java-classes.js if needed
158-
const tnsJavaClassesDestination = path.join(this.getBuildPath(), "tns-java-classes.js");
156+
const tnsJavaClassesDestination = join(this.getBuildPath(), "tns-java-classes.js");
159157
if (generationOptions.tnsJavaClassesPath) {
160158
if (generationOptions.tnsJavaClassesPath != tnsJavaClassesDestination) {
161159
shelljs.cp(generationOptions.tnsJavaClassesPath, tnsJavaClassesDestination);
@@ -165,21 +163,16 @@ ProjectSnapshotGenerator.prototype.generate = function(generationOptions) {
165163
this.generateTnsJavaClassesFile({ output: tnsJavaClassesDestination, options: generationOptions.tnsJavaClassesOptions });
166164
}
167165

168-
var snapshotToolsPath = generationOptions.snapshotToolsPath ?
169-
(path.isAbsolute(generationOptions.snapshotToolsPath) ?
170-
generationOptions.snapshotToolsPath :
171-
path.resolve(process.cwd(), generationOptions.snapshotToolsPath)) :
172-
path.join(os.tmpdir(), "snapshot-tools");
173-
166+
const snapshotToolsPath = resolveRelativePath(generationOptions.snapshotToolsPath) || join(os.tmpdir(), "snapshot-tools");
174167
const androidNdkPath = generationOptions.androidNdkPath || process.env.ANDROID_NDK_HOME;
175168

176169
console.log("Snapshot tools path: " + snapshotToolsPath);
177170

178171
// Generate snapshots
179172
const generator = new SnapshotGenerator({ buildPath: this.getBuildPath() });
180173
return generator.generate({
181-
snapshotToolsPath: snapshotToolsPath,
182-
inputFile: generationOptions.inputFile || path.join(this.options.projectRoot, "__snapshot.js"),
174+
snapshotToolsPath,
175+
inputFile: generationOptions.inputFile || join(this.options.projectRoot, "__snapshot.js"),
183176
targetArchs: generationOptions.targetArchs || ["arm", "arm64", "ia32"],
184177
v8Version: generationOptions.v8Version || this.getV8Version(),
185178
preprocessedInputFile: generationOptions.preprocessedInputFile,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /