From fc767f7ef197f31d1d431cebeac0ecabff5b8e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Thu, 12 May 2022 20:11:59 -0700 Subject: [PATCH] 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 --- scripts/{ => hermes}/bump-hermes-version.js | 32 +---- scripts/hermes/hermes-utils.js | 145 ++++++++++++++++++++ 2 files changed, 146 insertions(+), 31 deletions(-) rename scripts/{ => hermes}/bump-hermes-version.js (57%) create mode 100644 scripts/hermes/hermes-utils.js diff --git a/scripts/bump-hermes-version.js b/scripts/hermes/bump-hermes-version.js similarity index 57% rename from scripts/bump-hermes-version.js rename to scripts/hermes/bump-hermes-version.js index 67ed4f2be35..2b4e81e810b 100644 --- a/scripts/bump-hermes-version.js +++ b/scripts/hermes/bump-hermes-version.js @@ -16,11 +16,7 @@ const {exit} = require('shelljs'); const yargs = require('yargs'); const inquirer = require('inquirer'); -const fs = require('fs'); -const path = require('path'); - -const HERMES_TAG_FILE_DIR = 'sdks'; -const HERMES_TAG_FILE_PATH = `${HERMES_TAG_FILE_DIR}/.hermesversion`; +const {setHermesTag} = require('./hermes-utils'); let argv = yargs.option('t', { alias: 'tag', @@ -29,32 +25,6 @@ let argv = yargs.option('t', { required: true, }).argv; -function readHermesTag() { - if (fs.existsSync(path)) { - const data = fs.readFileSync(HERMES_TAG_FILE_PATH, { - encoding: 'utf8', - flag: 'r', - }); - return data.trim(); - } else { - return ''; - } -} - -function setHermesTag(hermesTag) { - if (readHermesTag() === hermesTag) { - // No need to update. - return; - } - - if (!fs.existsSync(HERMES_TAG_FILE_DIR)) { - fs.mkdirSync(HERMES_TAG_FILE_DIR, {recursive: true}); - } - - fs.writeFileSync(HERMES_TAG_FILE_PATH, hermesTag.trim()); - console.log('Hermes tag has been updated. Please commit your changes.'); -} - async function main() { const hermesTag = argv.tag; const {confirmHermesTag} = await inquirer.prompt({ diff --git a/scripts/hermes/hermes-utils.js b/scripts/hermes/hermes-utils.js new file mode 100644 index 00000000000..5729a3ec83a --- /dev/null +++ b/scripts/hermes/hermes-utils.js @@ -0,0 +1,145 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const {echo, exec, exit} = require('shelljs'); + +const SDKS_DIR = path.normalize(path.join(__dirname, '..', '..', 'sdks')); +const HERMES_DIR = path.join(SDKS_DIR, 'hermes'); +const HERMES_TAG_FILE_PATH = path.join(SDKS_DIR, '.hermesversion'); +const HERMES_TARBALL_BASE_URL = 'https://github.com/facebook/hermes/tarball/'; +const HERMES_TARBALL_DOWNLOAD_DIR = path.join(SDKS_DIR, 'download'); + +function prepareFileSystem() { + if (!fs.existsSync(SDKS_DIR)) { + fs.mkdirSync(SDKS_DIR, {recursive: true}); + } + + if (!fs.existsSync(HERMES_DIR)) { + fs.mkdirSync(HERMES_DIR, {recursive: true}); + } + + if (!fs.existsSync(HERMES_TARBALL_DOWNLOAD_DIR)) { + fs.mkdirSync(HERMES_TARBALL_DOWNLOAD_DIR, {recursive: true}); + } +} + +function readHermesTag() { + if (fs.existsSync(HERMES_TAG_FILE_PATH)) { + const data = fs.readFileSync(HERMES_TAG_FILE_PATH, { + encoding: 'utf8', + flag: 'r', + }); + return data.trim(); + } else { + return 'main'; + } +} + +function setHermesTag(hermesTag) { + if (readHermesTag() === hermesTag) { + // No need to update. + return; + } + + prepareFileSystem(); + + fs.writeFileSync(HERMES_TAG_FILE_PATH, hermesTag.trim()); + console.log('Hermes tag has been updated. Please commit your changes.'); +} + +function getHermesTagSHA(hermesTag) { + return exec( + `git ls-remote https://github.com/facebook/hermes ${hermesTag} | cut -f 1`, + {silent: true}, + ).trim(); +} + +function getHermesTarballDownloadPath(hermesTag) { + const hermesTagSHA = getHermesTagSHA(hermesTag); + return `${HERMES_TARBALL_DOWNLOAD_DIR}/hermes-${hermesTagSHA}.tgz`; +} + +function downloadHermesTarball() { + const hermesTag = readHermesTag(); + const hermesTagSHA = getHermesTagSHA(hermesTag); + const hermesTarballDownloadPath = getHermesTarballDownloadPath(hermesTag); + let hermesTarballUrl = HERMES_TARBALL_BASE_URL + hermesTag; + + prepareFileSystem(); + + if (fs.existsSync(hermesTarballDownloadPath)) { + return; + } + + echo(`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`); + if (exec(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`).code) { + echo('[Hermes] Failed to download Hermes tarball.'); + exit(1); + return; + } +} + +function expandHermesTarball() { + const hermesTag = readHermesTag(); + const hermesTagSHA = getHermesTagSHA(hermesTag); + const hermesTarballDownloadPath = getHermesTarballDownloadPath(hermesTag); + + prepareFileSystem(); + + if (!fs.existsSync(hermesTarballDownloadPath)) { + echo( + `[Hermes] Failed to expand Hermes tarball, no file found at ${hermesTarballDownloadPath}.`, + ); + exit(1); + return; + } + + echo(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`); + if ( + exec( + `tar -zxf ${hermesTarballDownloadPath} --strip-components=1 --directory ${HERMES_DIR}`, + ).code + ) { + echo('[Hermes] Failed to expand Hermes tarball.'); + exit(1); + return; + } +} + +function copyBuildScripts() { + fs.copyFileSync( + `${SDKS_DIR}/hermes-engine/hermes-engine.podspec`, + `${HERMES_DIR}/hermes-engine.podspec`, + ); + fs.copyFileSync( + `${SDKS_DIR}/hermes-engine/utils/build-apple-framework.sh`, + `${HERMES_DIR}/utils/build-apple-framework.sh`, + ); + fs.copyFileSync( + `${SDKS_DIR}/hermes-engine/utils/build-ios-framework.sh`, + `${HERMES_DIR}/utils/build-ios-framework.sh`, + ); + fs.copyFileSync( + `${SDKS_DIR}/hermes-engine/utils/build-mac-framework.sh`, + `${HERMES_DIR}/utils/build-mac-framework.sh`, + ); +} + +module.exports = { + copyBuildScripts, + downloadHermesTarball, + expandHermesTarball, + getHermesTagSHA, + readHermesTag, + setHermesTag, +};