1
- const path = require ( "path" ) ;
1
+ const { join , isAbsolute , resolve } = require ( "path" ) ;
2
2
const fs = require ( "fs" ) ;
3
3
const shelljs = require ( "shelljs" ) ;
4
4
const os = require ( "os" ) ;
@@ -11,18 +11,16 @@ const { getPackageJson } = require("../../projectHelpers");
11
11
const MIN_ANDROID_RUNTIME_VERSION = "3.0.0" ;
12
12
const VALID_ANDROID_RUNTIME_TAGS = Object . freeze ( [ "next" , "rc" ] ) ;
13
13
14
+ const resolveRelativePath = ( path ) => {
15
+ if ( path )
16
+ return isAbsolute ( path ) ? path : resolve ( process . cwd ( ) , path ) ;
17
+ return null ;
18
+ } ;
19
+
14
20
function ProjectSnapshotGenerator ( options ) {
15
21
this . options = options = options || { } ;
16
22
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 ( ) ;
26
24
27
25
console . log ( "Project root: " + options . projectRoot ) ;
28
26
console . log ( "Snapshots build directory: " + this . getBuildPath ( ) ) ;
@@ -32,57 +30,57 @@ function ProjectSnapshotGenerator (options) {
32
30
module . exports = ProjectSnapshotGenerator ;
33
31
34
32
ProjectSnapshotGenerator . calculateBuildPath = function ( projectRoot ) {
35
- return path . join ( projectRoot , "platforms/android/snapshot-build/build" ) ;
33
+ return join ( projectRoot , "platforms/android/snapshot-build/build" ) ;
36
34
}
37
35
38
36
ProjectSnapshotGenerator . prototype . getBuildPath = function ( ) {
39
37
return ProjectSnapshotGenerator . calculateBuildPath ( this . options . projectRoot ) ;
40
38
}
41
39
42
40
ProjectSnapshotGenerator . cleanSnapshotArtefacts = function ( projectRoot ) {
43
- const platformPath = path . join ( projectRoot , "platforms/android" ) ;
41
+ const platformPath = join ( projectRoot , "platforms/android" ) ;
44
42
45
43
// 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" ) ) ;
47
45
48
46
// 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 ) ) ;
50
48
}
51
49
52
50
ProjectSnapshotGenerator . installSnapshotArtefacts = function ( projectRoot ) {
53
51
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 ) ;
57
55
58
56
// 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" ) ) ;
60
58
61
59
// Copy include.gradle to the specified destination in the platforms folder
62
60
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" ) ) ;
64
62
65
63
// 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" ) ) ;
68
66
}
69
67
70
- if ( shelljs . test ( "-e" , path . join ( buildPath , "ndk-build/libs" ) ) ) {
68
+ if ( shelljs . test ( "-e" , join ( buildPath , "ndk-build/libs" ) ) ) {
71
69
// 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" ) ;
73
71
74
72
// Copy the libs to the specified destination in the platforms folder
75
73
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 ) ;
77
75
}
78
76
else {
79
77
// 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" ) ;
83
81
84
82
// 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" ) ) ;
86
84
87
85
/*
88
86
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) {
98
96
}
99
97
100
98
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" ) ;
102
100
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" ) ;
104
102
}
105
103
106
104
const zip = new require ( "adm-zip" ) ( nativescriptLibraryPath ) ;
@@ -122,7 +120,7 @@ ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() {
122
120
const currentRuntimeVersion = this . getAndroidRuntimeVersion ( ) ;
123
121
124
122
if ( ! currentRuntimeVersion ||
125
- ! fs . existsSync ( path . join ( this . options . projectRoot , "platforms/android" ) ) ) {
123
+ ! fs . existsSync ( join ( this . options . projectRoot , "platforms/android" ) ) ) {
126
124
127
125
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\"." ) ;
128
126
}
@@ -155,7 +153,7 @@ ProjectSnapshotGenerator.prototype.generate = function(generationOptions) {
155
153
shelljs . mkdir ( "-p" , this . getBuildPath ( ) ) ;
156
154
157
155
// 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" ) ;
159
157
if ( generationOptions . tnsJavaClassesPath ) {
160
158
if ( generationOptions . tnsJavaClassesPath != tnsJavaClassesDestination ) {
161
159
shelljs . cp ( generationOptions . tnsJavaClassesPath , tnsJavaClassesDestination ) ;
@@ -165,21 +163,16 @@ ProjectSnapshotGenerator.prototype.generate = function(generationOptions) {
165
163
this . generateTnsJavaClassesFile ( { output : tnsJavaClassesDestination , options : generationOptions . tnsJavaClassesOptions } ) ;
166
164
}
167
165
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" ) ;
174
167
const androidNdkPath = generationOptions . androidNdkPath || process . env . ANDROID_NDK_HOME ;
175
168
176
169
console . log ( "Snapshot tools path: " + snapshotToolsPath ) ;
177
170
178
171
// Generate snapshots
179
172
const generator = new SnapshotGenerator ( { buildPath : this . getBuildPath ( ) } ) ;
180
173
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" ) ,
183
176
targetArchs : generationOptions . targetArchs || [ "arm" , "arm64" , "ia32" ] ,
184
177
v8Version : generationOptions . v8Version || this . getV8Version ( ) ,
185
178
preprocessedInputFile : generationOptions . preprocessedInputFile ,
0 commit comments