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 f474e30

Browse files
authored
refactor: fetch v8Versions map from external url (#242)
Https request to the v8-versions.json file from the android-runtime repo is used to determine the used v8 version.
1 parent 210648f commit f474e30

File tree

7 files changed

+123
-121
lines changed

7 files changed

+123
-121
lines changed

‎bin/ns-bundle‎

100644100755
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
const { spawn } = require("child_process");
44
const { resolve: pathResolve, join } = require("path");
55
const { existsSync } = require("fs");
6+
7+
const semver = require("semver");
8+
69
const { getPackageJson } = require("../projectHelpers");
7-
const { isVersionGte } = require("../utils");
810

911
const PROJECT_DIR = pathResolve(__dirname, "../../../");
1012
const packageJson = getPackageJson(PROJECT_DIR);
@@ -79,7 +81,8 @@ function cleanBuildArtefacts(platform) {
7981
getTnsVersion().then(version => {
8082
// the android build artefacts should be cleaned manually
8183
// for nativescript-cli v3.0.1 and below or if using uglify
82-
if (!isVersionGte(version, "3.0.1") || shouldUglify()) {
84+
85+
if (!semver.gte(version, "3.0.1") || shouldUglify()) {
8386
gradlewClean().then(resolve).catch(throwError);
8487
} else {
8588
return resolve();
@@ -114,7 +117,7 @@ function getTnsVersion() {
114117
return new Promise((resolve, reject) => {
115118
const childProcess = spawn("tns", ["--version"], { shell: true });
116119

117-
childProcess.stdout.on("data", resolve);
120+
childProcess.stdout.on("data", version=>resolve(version.toString()));
118121

119122
childProcess.on("close", code => {
120123
if (code) {
@@ -127,12 +130,6 @@ function getTnsVersion() {
127130
});
128131
}
129132

130-
function versionToNumber(version) {
131-
const VERSION_MATCHER = /(\d+)\.(\d+)\.(\d+)/;
132-
133-
return Number(VERSION_MATCHER.exec(version).splice(1).join(""));
134-
}
135-
136133
// Clear platform/**/app folder contents
137134
function cleanApp(platform) {
138135
return new Promise((resolve, reject) => {

‎index.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const path = require("path");
22
const { existsSync } = require("fs");
33

44
const { getPackageJson, isAngular } = require("./projectHelpers");
5-
const { sanitize } = require("./utils");
65

76
const PROJECT_DIR = path.dirname(path.dirname(__dirname));
87
const APP_DIR = path.join(PROJECT_DIR, "app");
@@ -38,6 +37,11 @@ exports.getAppPath = platform => {
3837
}
3938
};
4039

40+
const sanitize = name => name
41+
.split("")
42+
.filter(char => /[a-zA-Z0-9]/.test(char))
43+
.join("");
44+
4145
function getPackageJsonEntry() {
4246
const packageJsonSource = getPackageJson(APP_DIR);
4347
const entry = packageJsonSource.main;

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"generate-android-snapshot": "./bin/generate-android-snapshot"
2727
},
2828
"dependencies": {
29+
"semver": "^5.4.1",
2930
"shelljs": "^0.6.0"
3031
},
3132
"devDependencies": {}

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

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
const { join, isAbsolute, resolve } = require("path");
22
const fs = require("fs");
3-
const shelljs = require("shelljs");
43
const os = require("os");
54

5+
const shelljs = require("shelljs");
6+
const semver = require("semver");
7+
68
const SnapshotGenerator = require("./snapshot-generator");
79
const TnsJavaClassesGenerator = require("./tns-java-classes-generator");
8-
const { isVersionGte } = require("../../utils");
10+
const { getJsonFile } = require("./utils");
911
const { getPackageJson } = require("../../projectHelpers");
1012

1113
const MIN_ANDROID_RUNTIME_VERSION = "3.0.0";
1214
const VALID_ANDROID_RUNTIME_TAGS = Object.freeze(["next", "rc"]);
15+
const V8_VERSIONS_URL = "https://raw.githubusercontent.com/NativeScript/android-runtime/master/v8-versions.json";
1316

1417
const resolveRelativePath = (path) => {
1518
if (path)
@@ -95,23 +98,23 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) {
9598
}
9699
}
97100

98-
ProjectSnapshotGenerator.prototype.getV8Version = function() {
99-
construntimeVersion=this.getAndroidRuntimeVersion();
100-
101-
if (!runtimeVersion) {
102-
return;
103-
}elseif(
104-
VALID_ANDROID_RUNTIME_TAGS.includes(runtimeVersion)||
105-
isVersionGte(runtimeVersion,"3.2.0")
106-
){
107-
return"5.9.211";
108-
}elseif(isVersionGte(runtimeVersion,"3.1.0")){
109-
return"5.5.372";
110-
}elseif(isVersionGte(runtimeVersion,"2.4.0")){
111-
return"5.2.361";
112-
}elseif(isVersionGte(runtimeVersion,"2.0.0")){
113-
return"4.7.80";
114-
}
101+
ProjectSnapshotGenerator.prototype.getV8Version = function(generationOptions) {
102+
returnnewPromise((resolve,reject)=>{
103+
constmaybeV8Version=generationOptions.v8Version;
104+
if (maybeV8Version) {
105+
returnresolve(maybeV8Version);
106+
}
107+
108+
getJsonFile(V8_VERSIONS_URL).then(v8VersionsMap=>{
109+
construntimeVersion=this.getAndroidRuntimeVersion().replace(/-.*/,"");
110+
111+
construntimeRange=Object.keys(v8VersionsMap)
112+
.find(range=>semver.satisfies(runtimeVersion,range));
113+
constv8Version=v8VersionsMap[runtimeRange];
114+
115+
returnresolve(v8Version);
116+
}).catch(reject);
117+
});
115118
}
116119

117120
ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() {
@@ -124,7 +127,7 @@ ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() {
124127
}
125128

126129
if (!VALID_ANDROID_RUNTIME_TAGS.includes(currentRuntimeVersion) &&
127-
!isVersionGte(currentRuntimeVersion, MIN_ANDROID_RUNTIME_VERSION)) {
130+
!semver.gte(currentRuntimeVersion, MIN_ANDROID_RUNTIME_VERSION)) {
128131

129132
throw new Error("In order to support heap snapshots, you must have at least tns-android@" + MIN_ANDROID_RUNTIME_VERSION +
130133
" installed. Current Android Runtime version is: " + currentRuntimeVersion + ".");
@@ -178,23 +181,29 @@ ProjectSnapshotGenerator.prototype.generate = function(generationOptions) {
178181

179182
// Generate snapshots
180183
const generator = new SnapshotGenerator({ buildPath: this.getBuildPath() });
181-
return generator.generate({
182-
snapshotToolsPath,
183-
inputFile: generationOptions.inputFile || join(this.options.projectRoot, "__snapshot.js"),
184-
targetArchs: generationOptions.targetArchs || ["arm", "arm64", "ia32"],
185-
v8Version: generationOptions.v8Version || this.getV8Version(),
186-
preprocessedInputFile: generationOptions.preprocessedInputFile,
187-
useLibs: generationOptions.useLibs || false,
188-
androidNdkPath
189-
}).then(() => {
190-
console.log("Snapshots build finished succesfully!");
191-
192-
if (generationOptions.install) {
193-
ProjectSnapshotGenerator.cleanSnapshotArtefacts(this.options.projectRoot);
194-
ProjectSnapshotGenerator.installSnapshotArtefacts(this.options.projectRoot);
195-
console.log(generationOptions.useLibs ?
196-
"Snapshot is included in the app as dynamically linked library (.so file)." :
197-
"Snapshot is included in the app as binary .blob file. The more space-efficient option is to embed it in a dynamically linked library (.so file).");
198-
}
184+
185+
return this.getV8Version(generationOptions).then(v8Version => {
186+
return generator.generate({
187+
snapshotToolsPath,
188+
inputFile: generationOptions.inputFile || join(this.options.projectRoot, "__snapshot.js"),
189+
targetArchs: generationOptions.targetArchs || ["arm", "arm64", "ia32"],
190+
v8Version: generationOptions.v8Version || this.getV8Version(),
191+
preprocessedInputFile: generationOptions.preprocessedInputFile,
192+
useLibs: generationOptions.useLibs || false,
193+
androidNdkPath
194+
}).then(() => {
195+
console.log("Snapshots build finished succesfully!");
196+
197+
if (generationOptions.install) {
198+
ProjectSnapshotGenerator.cleanSnapshotArtefacts(this.options.projectRoot);
199+
ProjectSnapshotGenerator.installSnapshotArtefacts(this.options.projectRoot);
200+
console.log(generationOptions.useLibs ?
201+
"Snapshot is included in the app as dynamically linked library (.so file)." :
202+
"Snapshot is included in the app as binary .blob file. The more space-efficient option is to embed it in a dynamically linked library (.so file).");
203+
}
204+
});
205+
}).catch(error => {
206+
throw new Error(`Cannot find suitable v8 version! Original error: ${error.message || error}`);
199207
});
200208
}
209+

‎snapshot/android/snapshot-generator.js‎

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const fs = require("fs");
2-
const shelljs = require("shelljs");
32
const { join, dirname } = require("path");
43
const os = require("os");
54
const child_process = require("child_process");
6-
const https = require("https");
5+
6+
const shelljs = require("shelljs");
7+
8+
const { downloadFile } = require("./utils");
79

810
const NDK_BUILD_SEED_PATH = join(__dirname, "snapshot-generator-tools/ndk-build");
911
const BUNDLE_PREAMBLE_PATH = join(__dirname, "snapshot-generator-tools/bundle-preamble.js");
@@ -61,45 +63,10 @@ SnapshotGenerator.prototype.downloadMksnapshotTool = function(snapshotToolsPath,
6163
return snapshotToolsDownloads[mksnapshotToolPath];
6264

6365
const downloadUrl = MKSNAPSHOT_TOOLS_DOWNLOAD_ROOT_URL + mksnapshotToolRelativePath;
64-
snapshotToolsDownloads[mksnapshotToolPath] = this.downloadExecFile(downloadUrl, mksnapshotToolPath);
66+
snapshotToolsDownloads[mksnapshotToolPath] = downloadFile(downloadUrl, mksnapshotToolPath);
6567
return snapshotToolsDownloads[mksnapshotToolPath];
6668
}
6769

68-
SnapshotGenerator.prototype.downloadExecFile = function(url, destinationFilePath) {
69-
return new Promise((resolve, reject) => {
70-
const request = https.get(url, (response) => {
71-
switch (response.statusCode) {
72-
case 200:
73-
shelljs.mkdir("-p", dirname(destinationFilePath));
74-
const file = fs.createWriteStream(destinationFilePath);
75-
file.on('error', function (error) {
76-
return reject(error);
77-
});
78-
file.on("finish", function() {
79-
file.close();
80-
fs.chmodSync(destinationFilePath, 0755);
81-
return resolve(destinationFilePath);
82-
});
83-
response.pipe(file);
84-
break;
85-
case 301:
86-
case 302:
87-
case 303:
88-
const redirectUrl = response.headers.location;
89-
return this.downloadExecFile(redirectUrl, destinationFilePath);
90-
default:
91-
return reject(new Error("Unable to download file at " + url + ". Status code: " + response.statusCode));
92-
}
93-
});
94-
95-
request.end();
96-
97-
request.on('error', function(err) {
98-
return reject(err);
99-
});
100-
});
101-
}
102-
10370
SnapshotGenerator.prototype.convertToAndroidArchName = function(archName) {
10471
switch (archName) {
10572
case "arm": return "armeabi-v7a";
@@ -205,4 +172,4 @@ SnapshotGenerator.prototype.generate = function(options) {
205172
}
206173
return this.buildPath;
207174
});
208-
}
175+
}

‎snapshot/android/utils.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const https = require("https");
2+
3+
const shelljs = require("shelljs");
4+
5+
const downloadFile = (url, destinationFilePath) =>
6+
new Promise((resolve, reject) => {
7+
const request = https.get(url, response => {
8+
switch (response.statusCode) {
9+
case 200:
10+
shelljs.mkdir("-p", dirname(destinationFilePath));
11+
const file = fs.createWriteStream(destinationFilePath);
12+
file.on('error', function (error) {
13+
return reject(error);
14+
});
15+
file.on("finish", function() {
16+
file.close();
17+
fs.chmodSync(destinationFilePath, 0755);
18+
return resolve(destinationFilePath);
19+
});
20+
response.pipe(file);
21+
break;
22+
case 301:
23+
case 302:
24+
case 303:
25+
const redirectUrl = response.headers.location;
26+
return this.downloadExecFile(redirectUrl, destinationFilePath);
27+
default:
28+
return reject(new Error("Unable to download file at " + url + ". Status code: " + response.statusCode));
29+
}
30+
});
31+
32+
request.end();
33+
34+
request.on('error', function(err) {
35+
return reject(err);
36+
});
37+
});
38+
39+
const getJsonFile = url =>
40+
new Promise((resolve, reject) => {
41+
https.get(url, res => {
42+
let body = "";
43+
res.on("data", chunk => {
44+
body += chunk;
45+
})
46+
47+
res.on("end", () => {
48+
const data = JSON.parse(body);
49+
return resolve(data);
50+
});
51+
}).on("error", reject);
52+
});
53+
54+
module.exports = {
55+
downloadFile,
56+
getJsonFile,
57+
};
58+

‎utils.js‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
(0)

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