Skip to content

Commit 3412f06

Browse files
hramosfacebook-github-bot
authored andcommitted
Hermes: Consolidate Hermes build scripts into scripts/hermes/hermes-utils.js
Summary: Currently, the tasks for downloading, expanding, and preparing Hermes to be built for Apple targets are split across the Circle CI and CocoaPods configs. This diff sets out to consolidate these tasks into a single hermes-utils.js script that can be reused across CI and build systems. The release script that is used to assign the Hermes tag for a given React Native version has been moved into `scripts/hermes`. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D36329356 fbshipit-source-id: 0222070adf201fa533b607a183471396d45c6caf
1 parent 309d705 commit 3412f06

File tree

2 files changed

+146
-31
lines changed

2 files changed

+146
-31
lines changed

scripts/bump-hermes-version.js renamed to scripts/hermes/bump-hermes-version.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
const {exit} = require('shelljs');
1818
const yargs = require('yargs');
1919
const inquirer = require('inquirer');
20-
const fs = require('fs');
21-
const path = require('path');
22-
23-
const HERMES_TAG_FILE_DIR = 'sdks';
24-
const HERMES_TAG_FILE_PATH = `${HERMES_TAG_FILE_DIR}/.hermesversion`;
20+
const {setHermesTag} = require('./hermes-utils');
2521

2622
let argv = yargs.option('t', {
2723
alias: 'tag',
@@ -30,32 +26,6 @@ let argv = yargs.option('t', {
3026
required: true,
3127
}).argv;
3228

33-
function readHermesTag() {
34-
if (fs.existsSync(path)) {
35-
const data = fs.readFileSync(HERMES_TAG_FILE_PATH, {
36-
encoding: 'utf8',
37-
flag: 'r',
38-
});
39-
return data.trim();
40-
} else {
41-
return '';
42-
}
43-
}
44-
45-
function setHermesTag(hermesTag) {
46-
if (readHermesTag() === hermesTag) {
47-
// No need to update.
48-
return;
49-
}
50-
51-
if (!fs.existsSync(HERMES_TAG_FILE_DIR)) {
52-
fs.mkdirSync(HERMES_TAG_FILE_DIR, {recursive: true});
53-
}
54-
55-
fs.writeFileSync(HERMES_TAG_FILE_PATH, hermesTag.trim());
56-
console.log('Hermes tag has been updated. Please commit your changes.');
57-
}
58-
5929
async function main() {
6030
const hermesTag = argv.tag;
6131
const {confirmHermesTag} = await inquirer.prompt({

scripts/hermes/hermes-utils.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
'use strict';
11+
12+
const fs = require('fs');
13+
const path = require('path');
14+
const {echo, exec, exit} = require('shelljs');
15+
16+
const SDKS_DIR = path.normalize(path.join(__dirname, '..', '..', 'sdks'));
17+
const HERMES_DIR = path.join(SDKS_DIR, 'hermes');
18+
const HERMES_TAG_FILE_PATH = path.join(SDKS_DIR, '.hermesversion');
19+
const HERMES_TARBALL_BASE_URL = 'https://github.com/facebook/hermes/tarball/';
20+
const HERMES_TARBALL_DOWNLOAD_DIR = path.join(SDKS_DIR, 'download');
21+
22+
function prepareFileSystem() {
23+
if (!fs.existsSync(SDKS_DIR)) {
24+
fs.mkdirSync(SDKS_DIR, {recursive: true});
25+
}
26+
27+
if (!fs.existsSync(HERMES_DIR)) {
28+
fs.mkdirSync(HERMES_DIR, {recursive: true});
29+
}
30+
31+
if (!fs.existsSync(HERMES_TARBALL_DOWNLOAD_DIR)) {
32+
fs.mkdirSync(HERMES_TARBALL_DOWNLOAD_DIR, {recursive: true});
33+
}
34+
}
35+
36+
function readHermesTag() {
37+
if (fs.existsSync(HERMES_TAG_FILE_PATH)) {
38+
const data = fs.readFileSync(HERMES_TAG_FILE_PATH, {
39+
encoding: 'utf8',
40+
flag: 'r',
41+
});
42+
return data.trim();
43+
} else {
44+
return 'main';
45+
}
46+
}
47+
48+
function setHermesTag(hermesTag) {
49+
if (readHermesTag() === hermesTag) {
50+
// No need to update.
51+
return;
52+
}
53+
54+
prepareFileSystem();
55+
56+
fs.writeFileSync(HERMES_TAG_FILE_PATH, hermesTag.trim());
57+
console.log('Hermes tag has been updated. Please commit your changes.');
58+
}
59+
60+
function getHermesTagSHA(hermesTag) {
61+
return exec(
62+
`git ls-remote https://github.com/facebook/hermes ${hermesTag} | cut -f 1`,
63+
{silent: true},
64+
).trim();
65+
}
66+
67+
function getHermesTarballDownloadPath(hermesTag) {
68+
const hermesTagSHA = getHermesTagSHA(hermesTag);
69+
return `${HERMES_TARBALL_DOWNLOAD_DIR}/hermes-${hermesTagSHA}.tgz`;
70+
}
71+
72+
function downloadHermesTarball() {
73+
const hermesTag = readHermesTag();
74+
const hermesTagSHA = getHermesTagSHA(hermesTag);
75+
const hermesTarballDownloadPath = getHermesTarballDownloadPath(hermesTag);
76+
let hermesTarballUrl = HERMES_TARBALL_BASE_URL + hermesTag;
77+
78+
prepareFileSystem();
79+
80+
if (fs.existsSync(hermesTarballDownloadPath)) {
81+
return;
82+
}
83+
84+
echo(`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`);
85+
if (exec(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`).code) {
86+
echo('[Hermes] Failed to download Hermes tarball.');
87+
exit(1);
88+
return;
89+
}
90+
}
91+
92+
function expandHermesTarball() {
93+
const hermesTag = readHermesTag();
94+
const hermesTagSHA = getHermesTagSHA(hermesTag);
95+
const hermesTarballDownloadPath = getHermesTarballDownloadPath(hermesTag);
96+
97+
prepareFileSystem();
98+
99+
if (!fs.existsSync(hermesTarballDownloadPath)) {
100+
echo(
101+
`[Hermes] Failed to expand Hermes tarball, no file found at ${hermesTarballDownloadPath}.`,
102+
);
103+
exit(1);
104+
return;
105+
}
106+
107+
echo(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`);
108+
if (
109+
exec(
110+
`tar -zxf ${hermesTarballDownloadPath} --strip-components=1 --directory ${HERMES_DIR}`,
111+
).code
112+
) {
113+
echo('[Hermes] Failed to expand Hermes tarball.');
114+
exit(1);
115+
return;
116+
}
117+
}
118+
119+
function copyBuildScripts() {
120+
fs.copyFileSync(
121+
`${SDKS_DIR}/hermes-engine/hermes-engine.podspec`,
122+
`${HERMES_DIR}/hermes-engine.podspec`,
123+
);
124+
fs.copyFileSync(
125+
`${SDKS_DIR}/hermes-engine/utils/build-apple-framework.sh`,
126+
`${HERMES_DIR}/utils/build-apple-framework.sh`,
127+
);
128+
fs.copyFileSync(
129+
`${SDKS_DIR}/hermes-engine/utils/build-ios-framework.sh`,
130+
`${HERMES_DIR}/utils/build-ios-framework.sh`,
131+
);
132+
fs.copyFileSync(
133+
`${SDKS_DIR}/hermes-engine/utils/build-mac-framework.sh`,
134+
`${HERMES_DIR}/utils/build-mac-framework.sh`,
135+
);
136+
}
137+
138+
module.exports = {
139+
copyBuildScripts,
140+
downloadHermesTarball,
141+
expandHermesTarball,
142+
getHermesTagSHA,
143+
readHermesTag,
144+
setHermesTag,
145+
};

0 commit comments

Comments
 (0)