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 5079bd9

Browse files
committed
refactor: fetch v8Versions map from external url
1 parent b76ab7f commit 5079bd9

File tree

4 files changed

+116
-83
lines changed

4 files changed

+116
-83
lines changed

‎bin/ns-bundle‎

100644100755
Lines changed: 8 additions & 10 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);
@@ -45,7 +47,7 @@ execute(options);
4547
function execute(options) {
4648
const platform = options.platform;
4749
let commands = [
48-
() => runTns("prepare", platform)
50+
// () => runTns("prepare", platform)
4951
];
5052

5153
if (options.bundle) {
@@ -79,7 +81,9 @@ 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+
// version = version.replace(/-(.*)/, "");
86+
if (!semver.gte(version, "3.0.1") || shouldUglify()) {
8387
gradlewClean().then(resolve).catch(throwError);
8488
} else {
8589
return resolve();
@@ -114,7 +118,7 @@ function getTnsVersion() {
114118
return new Promise((resolve, reject) => {
115119
const childProcess = spawn("tns", ["--version"], { shell: true });
116120

117-
childProcess.stdout.on("data", resolve);
121+
childProcess.stdout.on("data", version=>resolve(String(version)));
118122

119123
childProcess.on("close", code => {
120124
if (code) {
@@ -127,12 +131,6 @@ function getTnsVersion() {
127131
});
128132
}
129133

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

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

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const semver = require("semver");
77

88
const SnapshotGenerator = require("./snapshot-generator");
99
const TnsJavaClassesGenerator = require("./tns-java-classes-generator");
10+
const { getJsonFile } = require("./utils");
1011
const { getPackageJson } = require("../../projectHelpers");
1112

1213
const MIN_ANDROID_RUNTIME_VERSION = "3.0.0";
1314
const VALID_ANDROID_RUNTIME_TAGS = Object.freeze(["next", "rc"]);
15+
const V8_VERSIONS_URL = "https://api.myjson.com/bins/b3s3l";
1416

1517
const resolveRelativePath = (path) => {
1618
if (path)
@@ -96,22 +98,24 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function(projectRoot) {
9698
}
9799
}
98100

99-
ProjectSnapshotGenerator.prototype.getV8Version = function() {
100-
const runtimeVersion = this.getAndroidRuntimeVersion();
101-
102-
if (!runtimeVersion) {
103-
return;
104-
} else if (
105-
VALID_ANDROID_RUNTIME_TAGS.includes(runtimeVersion) ||
106-
107-
semver.gte(runtimeVersion, "3.1.0")
108-
) {
109-
return "5.5.372";
110-
} else if (semver.gte(runtimeVersion, "2.4.0")) {
111-
return "5.2.361";
112-
} else if (semver.gte(runtimeVersion, "2.0.0")) {
113-
return "4.7.80";
114-
}
101+
ProjectSnapshotGenerator.prototype.getV8Version = function(generationOptions) {
102+
return new Promise((resolve, reject) => {
103+
const maybeV8Version = generationOptions.v8Version;
104+
if (maybeV8Version) {
105+
return resolve(maybeV8Version);
106+
}
107+
108+
getJsonFile(V8_VERSIONS_URL).then(v8VersionsMap => {
109+
const runtimeVersion = this.getAndroidRuntimeVersion().replace(/-.*/, "");
110+
111+
const runtimeRange = Object.keys(v8VersionsMap).find(range => {
112+
return semver.satisfies(runtimeVersion, range)
113+
});
114+
115+
const v8Version = v8VersionsMap[runtimeRange];
116+
return resolve(v8Version);
117+
}).catch(reject);
118+
});
115119
}
116120

117121
ProjectSnapshotGenerator.prototype.validateAndroidRuntimeVersion = function() {
@@ -178,23 +182,29 @@ ProjectSnapshotGenerator.prototype.generate = function(generationOptions) {
178182

179183
// Generate snapshots
180184
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-
}
185+
186+
return this.getV8Version(generationOptions).then(v8Version => {
187+
return generator.generate({
188+
snapshotToolsPath,
189+
inputFile: generationOptions.inputFile || join(this.options.projectRoot, "__snapshot.js"),
190+
targetArchs: generationOptions.targetArchs || ["arm", "arm64", "ia32"],
191+
v8Version: generationOptions.v8Version || this.getV8Version(),
192+
preprocessedInputFile: generationOptions.preprocessedInputFile,
193+
useLibs: generationOptions.useLibs || false,
194+
androidNdkPath
195+
}).then(() => {
196+
console.log("Snapshots build finished succesfully!");
197+
198+
if (generationOptions.install) {
199+
ProjectSnapshotGenerator.cleanSnapshotArtefacts(this.options.projectRoot);
200+
ProjectSnapshotGenerator.installSnapshotArtefacts(this.options.projectRoot);
201+
console.log(generationOptions.useLibs ?
202+
"Snapshot is included in the app as dynamically linked library (.so file)." :
203+
"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).");
204+
}
205+
});
206+
}).catch(error => {
207+
throw new Error("Cannot find suitable v8 version!");
199208
});
200209
}
210+

‎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+

0 commit comments

Comments
(0)

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