Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 488730d

Browse files
author
Dimitar Tachev
authoredOct 2, 2019
Merge pull request #1055 from NativeScript/tachev/snapshot-on-windows
feat: support V8 snapshots on Windows
2 parents 2af9547 + 3a687c0 commit 488730d

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed
 

‎lib/before-checkForChanges.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
const { shouldSnapshot, isWinOS } = require("./utils");
2+
const semver = require("semver");
3+
14
module.exports = function ($staticConfig, hookArgs) {
2-
const majorVersionMatch = ($staticConfig.version || '').match(/^(\d+)\./);
3-
const majorVersion = majorVersionMatch && majorVersionMatch[1] && +majorVersionMatch[1];
5+
const cliVersion = semver.parse($staticConfig.version);
6+
const majorVersion = cliVersion && cliVersion.major;
7+
const minorVersion = cliVersion && cliVersion.minor;
8+
49
if (majorVersion && majorVersion < 6) {
510
// check if we are using the bundle workflow or the legacy one.
611
const isUsingBundleWorkflow = hookArgs &&
@@ -12,5 +17,17 @@ module.exports = function ($staticConfig, hookArgs) {
1217
const packageJsonData = require("../package.json")
1318
throw new Error(`The current version of ${packageJsonData.name} (${packageJsonData.version}) is not compatible with the used CLI: ${$staticConfig.version}. Please upgrade your NativeScript CLI version (npm i -g nativescript).`);
1419
}
20+
} else {
21+
const env = hookArgs.prepareData.env || {};
22+
const shouldSnapshotOptions = {
23+
platform: hookArgs.prepareData.platform,
24+
release: hookArgs.prepareData.release
25+
};
26+
27+
const shouldSnapshotOnWin = env.snapshot && shouldSnapshot(shouldSnapshotOptions) && isWinOS();
28+
const isCLIWithoutWinSnapshotsSupport = majorVersion && majorVersion == 6 && minorVersion && minorVersion < 2;
29+
if (shouldSnapshotOnWin && isCLIWithoutWinSnapshotsSupport) {
30+
throw new Error(`In order to generate Snapshots on Windows, please upgrade your NativeScript CLI version (npm i -g nativescript).`);
31+
}
1532
}
1633
}

‎lib/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ const { isAndroid } = require("../projectHelpers");
33

44
function shouldSnapshot(config) {
55
const platformSupportsSnapshot = isAndroid(config.platform);
6-
const osSupportsSnapshot = os.type() !== "Windows_NT";
76

8-
return config.release && platformSupportsSnapshot && osSupportsSnapshot;
7+
return config.release && platformSupportsSnapshot;
98
}
109

1110
function convertToUnixPath(relativePath) {
1211
return relativePath.replace(/\\/g, "/");
1312
}
1413

14+
function isWinOS() {
15+
return os.type() === "Windows_NT";
16+
}
17+
1518
module.exports = {
1619
shouldSnapshot,
17-
convertToUnixPath
20+
convertToUnixPath,
21+
isWinOS
1822
};

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { dirname, isAbsolute, join, resolve } = require("path");
1+
const { dirname, isAbsolute, join, resolve, sep } = require("path");
22
const { existsSync, readFileSync, writeFileSync } = require("fs");
33

44
const shelljs = require("shelljs");
@@ -102,22 +102,15 @@ ProjectSnapshotGenerator.installSnapshotArtefacts = function (projectRoot) {
102102

103103
// Copy the libs to the specified destination in the platforms folder
104104
shelljs.mkdir("-p", libsDestinationPath);
105-
shelljs.cp("-R", join(buildPath, "ndk-build/libs") + "/", libsDestinationPath);
105+
shelljs.cp("-R", join(buildPath, "ndk-build/libs") + sep, libsDestinationPath);
106106
} else {
107107
// useLibs = false
108108
const blobsSrcPath = join(buildPath, "snapshots/blobs");
109109
const blobsDestinationPath = resolve(appPath, "../snapshots");
110110
const appPackageJsonPath = join(appPath, "package.json");
111111

112112
// Copy the blobs in the prepared app folder
113-
shelljs.cp("-R", blobsSrcPath + "/", resolve(appPath, "../snapshots"));
114-
115-
/*
116-
Rename TNSSnapshot.blob files to snapshot.blob files. The xxd tool uses the file name for the name of the static array.
117-
This is why the *.blob files are initially named TNSSnapshot.blob.
118-
After the xxd step, they must be renamed to snapshot.blob, because this is the filename that the Android runtime is looking for.
119-
*/
120-
shelljs.exec("find " + blobsDestinationPath + " -name '*.blob' -execdir mv {} snapshot.blob ';'");
113+
shelljs.cp("-R", blobsSrcPath + sep, resolve(appPath, "../snapshots"));
121114

122115
// Update the package.json file
123116
const appPackageJson = shelljs.test("-e", appPackageJsonPath) ? JSON.parse(readFileSync(appPackageJsonPath, 'utf8')) : {};

‎snapshot/android/snapshot-generator.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ SnapshotGenerator.prototype.generate = function (options) {
244244
}
245245

246246
SnapshotGenerator.prototype.getSnapshotToolCommand = function (snapshotToolPath, inputFilePath, outputPath, toolParams) {
247-
return `${snapshotToolPath} ${inputFilePath} --startup_blob ${join(outputPath, `${SNAPSHOT_BLOB_NAME}.blob`)} ${toolParams}`;
247+
return `${snapshotToolPath} ${inputFilePath} --startup_blob ${outputPath} ${toolParams}`;
248248
}
249249

250250
SnapshotGenerator.prototype.getXxdCommand = function (srcOutputDir, xxdLocation) {
251251
// https://github.com/NativeScript/docker-images/tree/master/v8-snapshot/bin
252-
return `${xxdLocation || ""}xxd -i ${SNAPSHOT_BLOB_NAME}.blob > ${join(srcOutputDir, `${SNAPSHOT_BLOB_NAME}.c`)}`;
252+
return `${xxdLocation || ""}xxd -i ${SNAPSHOT_BLOB_NAME}.blob > ${srcOutputDir}`;
253253
}
254254

255255
SnapshotGenerator.prototype.getPathInDocker = function (mappedLocalDir, mappedDockerDir, targetPath) {
@@ -309,11 +309,12 @@ SnapshotGenerator.prototype.buildCSource = function (androidArch, blobInputDir,
309309
if (snapshotInDocker) {
310310
const blobsInputInDocker = `/blobs/${androidArch}`
311311
const srcOutputDirInDocker = `/dist/src/${androidArch}`;
312-
const buildCSourceCommand = this.getXxdCommand(srcOutputDirInDocker, "/bin/");
312+
const outputPathInDocker = this.getPathInDocker(srcOutputDir, srcOutputDirInDocker, join(srcOutputDir, `${SNAPSHOT_BLOB_NAME}.c`));
313+
const buildCSourceCommand = this.getXxdCommand(outputPathInDocker, "/bin/");
313314
command = `docker run --rm -v "${blobInputDir}:${blobsInputInDocker}" -v "${srcOutputDir}:${srcOutputDirInDocker}" ${SNAPSHOTS_DOCKER_IMAGE} /bin/sh -c "cd ${blobsInputInDocker} && ${buildCSourceCommand}"`;
314315
}
315316
else {
316-
command = this.getXxdCommand(srcOutputDir);
317+
command = this.getXxdCommand(join(srcOutputDir, `${SNAPSHOT_BLOB_NAME}.c`));
317318
}
318319
shellJsExecuteInDir(blobInputDir, function () {
319320
shelljs.exec(command);
@@ -345,11 +346,11 @@ SnapshotGenerator.prototype.runMksnapshotTool = function (tool, mksnapshotParams
345346
const toolPathInAppDir = this.copySnapshotTool(snapshotToolsPath, toolPath, toolsTempFolder);
346347
const toolPathInDocker = this.getPathInDocker(inputFileDir, appDirInDocker, toolPathInAppDir);
347348
const inputFilePathInDocker = this.getPathInDocker(inputFileDir, appDirInDocker, inputFile);
348-
const outputPathInDocker = this.getPathInDocker(blobOutputDir, blobOutputDirInDocker, blobOutputDir);
349+
const outputPathInDocker = this.getPathInDocker(blobOutputDir, blobOutputDirInDocker, join(blobOutputDir, `${SNAPSHOT_BLOB_NAME}.blob`));
349350
const toolCommandInDocker = this.getSnapshotToolCommand(toolPathInDocker, inputFilePathInDocker, outputPathInDocker, toolParams);
350351
command = `docker run --rm -v "${inputFileDir}:${appDirInDocker}" -v "${blobOutputDir}:${blobOutputDirInDocker}" ${SNAPSHOTS_DOCKER_IMAGE} /bin/sh -c "${toolCommandInDocker}"`;
351352
} else {
352-
command = this.getSnapshotToolCommand(toolPath, inputFile, blobOutputDir, toolParams);
353+
command = this.getSnapshotToolCommand(toolPath, inputFile, join(blobOutputDir, `${SNAPSHOT_BLOB_NAME}.blob`), toolParams);
353354
}
354355

355356
// Generate .blob file
@@ -370,6 +371,12 @@ SnapshotGenerator.prototype.runMksnapshotTool = function (tool, mksnapshotParams
370371
if (buildCSource) {
371372
this.buildCSource(androidArch, blobOutputDir, snapshotInDocker)
372373
}
374+
375+
/*
376+
Rename TNSSnapshot.blob files to snapshot.blob files. The xxd tool uses the file name for the name of the static array.
377+
This is why the *.blob files are initially named TNSSnapshot.blob.
378+
After the xxd step, they must be renamed to snapshot.blob, because this is the filename that the Android runtime is looking for.
379+
*/
380+
shelljs.mv(join(blobOutputDir, `${SNAPSHOT_BLOB_NAME}.blob`), join(blobOutputDir, `snapshot.blob`));
373381
});
374382
}
375-

0 commit comments

Comments
 (0)
This repository has been archived.