@@ -11,7 +11,13 @@ const {
11
11
createDirectory,
12
12
getJsonFile,
13
13
} = require ( "./utils" ) ;
14
- const { getPackageJson } = require ( "../../projectHelpers" ) ;
14
+ const {
15
+ getPackageJson,
16
+ getAndroidRuntimeVersion,
17
+ getAndroidProjectPath,
18
+ resolveAndroidAppPath,
19
+ resolveAndroidConfigurationsPath,
20
+ } = require ( "../../projectHelpers" ) ;
15
21
16
22
const MIN_ANDROID_RUNTIME_VERSION = "3.0.0" ;
17
23
const VALID_ANDROID_RUNTIME_TAGS = Object . freeze ( [ "next" , "rc" ] ) ;
@@ -25,10 +31,9 @@ const resolveRelativePath = (path) => {
25
31
return null ;
26
32
} ;
27
33
28
- function ProjectSnapshotGenerator ( options ) {
34
+ function ProjectSnapshotGenerator ( options ) {
29
35
this . options = options = options || { } ;
30
-
31
- options . projectRoot = resolveRelativePath ( options . projectRoot ) || process . cwd ( ) ;
36
+ options . projectRoot = resolveRelativePath ( options . projectRoot ) || process . cwd ( ) ;
32
37
33
38
console . log ( "Project root: " + options . projectRoot ) ;
34
39
console . log ( "Snapshots build directory: " + this . getBuildPath ( ) ) ;
@@ -37,29 +42,45 @@ function ProjectSnapshotGenerator (options) {
37
42
}
38
43
module . exports = ProjectSnapshotGenerator ;
39
44
40
- ProjectSnapshotGenerator . calculateBuildPath = function ( projectRoot ) {
41
- return join ( projectRoot , "platforms/android/snapshot-build/build" ) ;
45
+ ProjectSnapshotGenerator . calculateBuildPath = function ( projectRoot ) {
46
+ return join (
47
+ ProjectSnapshotGenerator . calculateProjectPath ( projectRoot ) ,
48
+ "snapshot-build" ,
49
+ "build"
50
+ ) ;
42
51
}
43
52
44
- ProjectSnapshotGenerator . prototype . getBuildPath = function ( ) {
53
+ ProjectSnapshotGenerator . prototype . getBuildPath = function ( ) {
45
54
return ProjectSnapshotGenerator . calculateBuildPath ( this . options . projectRoot ) ;
46
55
}
47
56
48
- ProjectSnapshotGenerator . cleanSnapshotArtefacts = function ( projectRoot ) {
49
- const platformPath = join ( projectRoot , "platforms/android" ) ;
57
+ ProjectSnapshotGenerator . calculateProjectPath = function ( projectRoot ) {
58
+ const projectPath = getAndroidProjectPath ( { projectRoot} ) ;
59
+ return join ( projectRoot , projectPath ) ;
60
+ }
61
+
62
+ ProjectSnapshotGenerator . prototype . getProjectPath = function ( ) {
63
+ return ProjectSnapshotGenerator . calculateProjectPath ( this . options . projectRoot ) ;
64
+ }
65
+
66
+ ProjectSnapshotGenerator . cleanSnapshotArtefacts = function ( projectRoot ) {
67
+ const platformPath = ProjectSnapshotGenerator . calculateProjectPath ( projectRoot ) ;
50
68
51
69
// Remove blob files from prepared folder
52
70
shelljs . rm ( "-rf" , join ( platformPath , "src/main/assets/snapshots" ) ) ;
53
71
54
72
// Remove prepared include.gradle configurations
55
- shelljs . rm ( "-rf" , join ( platformPath , "configurations/" , SnapshotGenerator . SNAPSHOT_PACKAGE_NANE ) ) ;
73
+ const configurationsPath = resolveAndroidConfigurationsPath ( projectRoot ) ;
74
+ shelljs . rm ( "-rf" , join ( configurationsPath , SnapshotGenerator . SNAPSHOT_PACKAGE_NANE ) ) ;
56
75
}
57
76
58
- ProjectSnapshotGenerator . installSnapshotArtefacts = function ( projectRoot ) {
77
+ ProjectSnapshotGenerator . installSnapshotArtefacts = function ( projectRoot ) {
59
78
const buildPath = ProjectSnapshotGenerator . calculateBuildPath ( projectRoot ) ;
60
- const platformPath = join ( projectRoot , "platforms/android" ) ;
61
- const assetsPath = join ( platformPath , "src/main/assets" ) ;
62
- const configDestinationPath = join ( platformPath , "configurations" , SnapshotGenerator . SNAPSHOT_PACKAGE_NANE ) ;
79
+ const platformPath = ProjectSnapshotGenerator . calculateProjectPath ( projectRoot ) ;
80
+
81
+ const appPath = resolveAndroidAppPath ( projectRoot ) ;
82
+ const configurationsPath = resolveAndroidConfigurationsPath ( projectRoot ) ;
83
+ const configDestinationPath = join ( configurationsPath , SnapshotGenerator . SNAPSHOT_PACKAGE_NANE ) ;
63
84
64
85
// Remove build folder to make sure that the apk will be fully rebuild
65
86
shelljs . rm ( "-rf" , join ( platformPath , "build" ) ) ;
@@ -70,7 +91,7 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) {
70
91
71
92
// Copy tns-java-classes.js
72
93
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" ) ) ;
94
+ shelljs . cp ( join ( buildPath , "tns-java-classes.js" ) , join ( appPath , "tns-java-classes.js" ) ) ;
74
95
}
75
96
76
97
if ( shelljs . test ( "-e" , join ( buildPath , "ndk-build/libs" ) ) ) {
@@ -84,11 +105,11 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) {
84
105
else {
85
106
// useLibs = false
86
107
const blobsSrcPath = join ( buildPath , "snapshots/blobs" ) ;
87
- const blobsDestinationPath = join ( assetsPath , "snapshots" ) ;
88
- const appPackageJsonPath = join ( assetsPath , "app/ package.json" ) ;
108
+ const blobsDestinationPath = resolve ( appPath , "../ snapshots" ) ;
109
+ const appPackageJsonPath = join ( appPath , "package.json" ) ;
89
110
90
111
// Copy the blobs in the prepared app folder
91
- shelljs . cp ( "-R" , blobsSrcPath + "/" , join ( assetsPath , "snapshots" ) ) ;
112
+ shelljs . cp ( "-R" , blobsSrcPath + "/" , resolve ( appPath , "../ snapshots" ) ) ;
92
113
93
114
/*
94
115
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.
@@ -120,9 +141,8 @@ const fetchV8VersionsFile = () =>
120
141
} ) ;
121
142
122
143
const findV8Version = ( runtimeVersion , v8VersionsMap ) => {
123
- const runtimeReleaseVersion = runtimeVersion . replace ( / - .* / , "" ) ;
124
144
const runtimeRange = Object . keys ( v8VersionsMap )
125
- . find ( range => semver . satisfies ( runtimeReleaseVersion , range ) ) ;
145
+ . find ( range => semver . satisfies ( runtimeVersion , range ) ) ;
126
146
127
147
return v8VersionsMap [ runtimeRange ] ;
128
148
}
@@ -139,16 +159,16 @@ const getV8VersionsMap = runtimeVersion =>
139
159
}
140
160
} ) ;
141
161
142
- ProjectSnapshotGenerator . prototype . getV8Version = function ( generationOptions ) {
162
+ ProjectSnapshotGenerator . prototype . getV8Version = function ( generationOptions ) {
143
163
return new Promise ( ( resolve , reject ) => {
144
164
const maybeV8Version = generationOptions . v8Version ;
145
165
if ( maybeV8Version ) {
146
166
return resolve ( maybeV8Version ) ;
147
167
}
148
168
149
- const runtimeVersion = this . getAndroidRuntimeVersion ( ) ;
169
+ const runtimeVersion = getAndroidRuntimeVersion ( this . options . projectRoot ) ;
150
170
getV8VersionsMap ( runtimeVersion )
151
- . then ( ( { versionsMap, latest} ) => {
171
+ . then ( ( { versionsMap, latest } ) => {
152
172
const v8Version = findV8Version ( runtimeVersion , versionsMap ) ;
153
173
154
174
if ( ! v8Version && ! latest ) {
@@ -165,12 +185,10 @@ ProjectSnapshotGenerator.prototype.getV8Version = function(generationOptions) {
165
185
} ) ;
166
186
}
167
187
168
- ProjectSnapshotGenerator . prototype . validateAndroidRuntimeVersion = function ( ) {
169
- const currentRuntimeVersion = this . getAndroidRuntimeVersion ( ) ;
170
-
171
- if ( ! currentRuntimeVersion ||
172
- ! existsSync ( join ( this . options . projectRoot , "platforms/android" ) ) ) {
188
+ ProjectSnapshotGenerator . prototype . validateAndroidRuntimeVersion = function ( ) {
189
+ const currentRuntimeVersion = getAndroidRuntimeVersion ( this . options . projectRoot ) ;
173
190
191
+ if ( ! currentRuntimeVersion || ! this . getProjectPath ( ) ) {
174
192
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\"." ) ;
175
193
}
176
194
@@ -182,17 +200,7 @@ ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() {
182
200
}
183
201
}
184
202
185
- ProjectSnapshotGenerator . prototype . getAndroidRuntimeVersion = function ( ) {
186
- try {
187
- const projectPackageJSON = getPackageJson ( this . options . projectRoot ) ;
188
-
189
- return projectPackageJSON [ "nativescript" ] [ "tns-android" ] [ "version" ] ;
190
- } catch ( e ) {
191
- return null ;
192
- }
193
- }
194
-
195
- ProjectSnapshotGenerator . prototype . generateTnsJavaClassesFile = function ( generationOptions ) {
203
+ ProjectSnapshotGenerator . prototype . generateTnsJavaClassesFile = function ( generationOptions ) {
196
204
const tnsJavaClassesGenerator = new TnsJavaClassesGenerator ( ) ;
197
205
return tnsJavaClassesGenerator . generate ( {
198
206
projectRoot : this . options . projectRoot ,
@@ -201,7 +209,7 @@ ProjectSnapshotGenerator.prototype.generateTnsJavaClassesFile = function(generat
201
209
} ) ;
202
210
}
203
211
204
- ProjectSnapshotGenerator . prototype . generate = function ( generationOptions ) {
212
+ ProjectSnapshotGenerator . prototype . generate = function ( generationOptions ) {
205
213
generationOptions = generationOptions || { } ;
206
214
207
215
console . log ( "Running snapshot generation with the following arguments: " ) ;
0 commit comments