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