|
6 | 6 | require 'net/http' |
7 | 7 | require 'rexml/document' |
8 | 8 |
|
| 9 | +# This function abort the build if the `HERMES_ENGINE_TARBALL_PATH` ENV variable is set with an invalid path |
| 10 | +def abort_if_invalid_tarball_provided!() |
| 11 | + if ENV.has_key?('HERMES_ENGINE_TARBALL_PATH') && !File.exist?(ENV['HERMES_ENGINE_TARBALL_PATH']) |
| 12 | + abort "[Hermes] HERMES_ENGINE_TARBALL_PATH is set, but points to a non-existing file: \"#{ENV['HERMES_ENGINE_TARBALL_PATH']}\"\nIf you don't want to use tarball, run `unset HERMES_ENGINE_TARBALL_PATH`" |
| 13 | + end |
| 14 | +end |
| 15 | + |
| 16 | +# It computes the right value for the hermes-engine.podspec's source. |
| 17 | +# - To use a specific tarball, install the dependencies with: |
| 18 | +# `HERMES_ENGINE_TARBALL_PATH=<path_to_tarball> bundle exec pod install` |
| 19 | +# - To force a build from source, install the dependencies with: |
| 20 | +# `BUILD_FROM_SOURCE=true bundle exec pod install` |
| 21 | +# If none of the two are provided, Cocoapods will check whether there is a tarball for the current version |
| 22 | +# (either release or nightly). If not, it will fall back building from source (the latest commit on main). |
| 23 | +# |
| 24 | +# Parameters: |
| 25 | +# - build_from_source: boolean to force a build from source. |
| 26 | +# - hermestag_file: path to the hermes tag file. |
| 27 | +# - git: uri to the hermes repository |
| 28 | +# - version: current version of the pod |
| 29 | +# - build_type: build type of the hermes engine. It can be `:release` or `:debug` |
| 30 | +# - react_native_path: path to react native |
| 31 | +# |
| 32 | +# Returns: a properly configured source object |
| 33 | +def compute_hermes_source(build_from_source, hermestag_file, git, version, build_type, react_native_path) |
| 34 | + source = {} |
| 35 | + |
| 36 | + if ENV.has_key?('HERMES_ENGINE_TARBALL_PATH') |
| 37 | + use_tarball(source) |
| 38 | + elsif build_from_source |
| 39 | + if File.exists?(hermestag_file) |
| 40 | + build_from_tagfile(source, git, hermestag_file) |
| 41 | + else |
| 42 | + build_hermes_from_source(source, git) |
| 43 | + end |
| 44 | + elsif hermes_artifact_exists(release_tarball_url(version, build_type)) |
| 45 | + use_release_tarball(source, version, build_type) |
| 46 | + elsif hermes_artifact_exists(nightly_tarball_url(version).gsub("\\", "")) |
| 47 | + use_nightly_tarball(source, react_native_path, version) |
| 48 | + else |
| 49 | + build_hermes_from_source(source, git) |
| 50 | + end |
| 51 | + |
| 52 | + return source |
| 53 | +end |
| 54 | + |
| 55 | +def use_tarball(source) |
| 56 | + tarball_path = ENV['HERMES_ENGINE_TARBALL_PATH'] |
| 57 | + putsIfPodPresent("[Hermes] Using pre-built Hermes binaries from local path: #{tarball_path}") |
| 58 | + source[:http] = "file://#{tarball_path}" |
| 59 | +end |
| 60 | + |
| 61 | +def build_from_tagfile(source, git, hermestag_file) |
| 62 | + hermestag = File.read(hermestag_file).strip |
| 63 | + putsIfPodPresent("[Hermes] Building Hermes from source from tag #{hermestag}...") |
| 64 | + source[:git] = git |
| 65 | + source[:tag] = hermestag |
| 66 | +end |
| 67 | + |
| 68 | +def use_release_tarball(source, version, build_type) |
| 69 | + # Sample url from Maven: |
| 70 | + # https://repo1.maven.org/maven2/com/facebook/react/react-native-artifacts/0.71.0/react-native-artifacts-0.71.0-hermes-ios-debug.tar.gz |
| 71 | + putsIfPodPresent('[Hermes] Using the release tarball from Maven Central', 'info') |
| 72 | + source[:http] = release_tarball_url(version, build_type) |
| 73 | +end |
| 74 | + |
| 75 | +def release_tarball_url(version, build_type) |
| 76 | + return "https://repo1.maven.org/maven2/com/facebook/react/react-native-artifacts/#{version}/react-native-artifacts-#{version}-hermes-ios-#{build_type.to_s}.tar.gz" |
| 77 | +end |
| 78 | + |
| 79 | +def use_nightly_tarball(source, react_native_path, version) |
| 80 | + putsIfPodPresent('[Hermes] Nightly version, download pre-built for Hermes') |
| 81 | + destination_path = download_nightly_hermes(react_native_path, version) |
| 82 | + # set tarball as hermes engine |
| 83 | + source[:http] = "file://#{destination_path}" |
| 84 | +end |
| 85 | + |
| 86 | +def putsIfPodPresent(message, level = 'warning') |
| 87 | + unless Object.const_defined?("Pod::UI") |
| 88 | + return |
| 89 | + end |
| 90 | + |
| 91 | + case level |
| 92 | + when 'info' |
| 93 | + Pod::UI.puts message.green |
| 94 | + when 'error' |
| 95 | + Pod::UI.puts message.red |
| 96 | + else |
| 97 | + Pod::UI.puts message.yellow |
| 98 | + end |
| 99 | +end |
| 100 | + |
9 | 101 | # This function downloads the nightly prebuilt version of Hermes based on the passed version |
10 | 102 | # and save it in the node_module/react_native/sdks/downloads folder |
11 | 103 | # It then returns the path to the hermes tarball |
|
15 | 107 | # - version: the version of React Native that requires the Hermes tarball |
16 | 108 | # Returns: the path to the downloaded Hermes tarball |
17 | 109 | def download_nightly_hermes(react_native_path, version) |
18 | | - params = "r=snapshots\&g=com.facebook.react\&a=react-native-artifacts\&c=hermes-ios-debug\&e=tar.gz\&v=#{version}-SNAPSHOT" |
19 | | - tarball_url = "http://oss.sonatype.org/service/local/artifact/maven/redirect\?#{params}" |
| 110 | + tarball_url = nightly_tarball_url(version) |
20 | 111 |
|
21 | 112 | destination_folder = "#{react_native_path}/sdks/downloads" |
22 | 113 | destination_path = "#{destination_folder}/hermes-ios.tar.gz" |
23 | 114 |
|
24 | 115 | `mkdir -p "#{destination_folder}" && curl "#{tarball_url}" -Lo "#{destination_path}"` |
25 | 116 | return destination_path |
26 | 117 | end |
| 118 | + |
| 119 | +def nightly_tarball_url(version) |
| 120 | + params = "r=snapshots\&g=com.facebook.react\&a=react-native-artifacts\&c=hermes-ios-debug\&e=tar.gz\&v=#{version}-SNAPSHOT" |
| 121 | + return "http://oss.sonatype.org/service/local/artifact/maven/redirect\?#{params}" |
| 122 | +end |
| 123 | + |
| 124 | +def build_hermes_from_source(source, git) |
| 125 | + putsIfPodPresent('[Hermes] Installing hermes-engine may take slightly longer, building Hermes compiler from source...') |
| 126 | + source[:git] = git |
| 127 | + source[:commit] = `git ls-remote https://github.com/facebook/hermes main | cut -f 1`.strip |
| 128 | +end |
| 129 | + |
| 130 | +# This function checks that Hermes artifact exists. |
| 131 | +# As of now it should check it on the Maven repo. |
| 132 | +# |
| 133 | +# Parameters |
| 134 | +# - version: the version of React Native |
| 135 | +# - build_type: debug or release |
| 136 | +def hermes_artifact_exists(tarball_url) |
| 137 | + # -L is used to follow redirects, useful for the nightlies |
| 138 | + # I also needed to wrap the url in quotes to avoid escaping & and ?. |
| 139 | + return (`curl -o /dev/null --silent -Iw '%{http_code}' -L "#{tarball_url}"` == "200") |
| 140 | +end |
0 commit comments