|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# sync-relay.sh - Sync ORelay from deps/moxygen/moxygen/relay/ |
| 4 | +# |
| 5 | +# Usage: scripts/sync-relay.sh [--no-build] [--no-test] |
| 6 | +# |
| 7 | +# Transforms: |
| 8 | +# MoQRelay.h -> include/o_rly/ORelay.h |
| 9 | +# MoQRelay.cpp -> src/ORelay.cpp |
| 10 | +# test/MoQRelayTest.cpp -> tests/ORelayTest.cpp |
| 11 | +# |
| 12 | +# Name/namespace mappings applied: |
| 13 | +# class MoQRelay -> class ORelay |
| 14 | +# namespace moxygen -> namespace openmoq::o_rly |
| 15 | +# unqualified moxygen types in header get moxygen:: prefix |
| 16 | +# using namespace moxygen; added before namespace in .cpp |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 21 | +REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" |
| 22 | +MOXYGEN_RELAY="${REPO_ROOT}/deps/moxygen/moxygen/relay" |
| 23 | + |
| 24 | +NO_BUILD=false |
| 25 | +NO_TEST=false |
| 26 | +for arg in "$@"; do |
| 27 | + case "$arg" in |
| 28 | + --no-build) NO_BUILD=true ;; |
| 29 | + --no-test) NO_TEST=true ;; |
| 30 | + *) echo "Unknown argument: $arg" >&2; exit 1 ;; |
| 31 | + esac |
| 32 | +done |
| 33 | + |
| 34 | +TMP_DIR="$(mktemp -d)" |
| 35 | +trap 'rm -rf "${TMP_DIR}"' EXIT |
| 36 | + |
| 37 | +MOXYGEN_REV="$(git -C "${REPO_ROOT}/deps/moxygen" rev-parse --short HEAD 2>/dev/null || echo unknown)" |
| 38 | +echo "==> Syncing relay files from moxygen @ ${MOXYGEN_REV}" |
| 39 | + |
| 40 | +# ───────────────────────────────────────────────────────────────────────────── |
| 41 | +# Helper: replace Meta-only Apache 2.0 copyright with combined OpenMOQ header |
| 42 | +# ───────────────────────────────────────────────────────────────────────────── |
| 43 | +replace_copyright() { |
| 44 | + local file="$1" |
| 45 | + python3 - "$file" <<'PYEOF' |
| 46 | +import re, sys |
| 47 | +path = sys.argv[1] |
| 48 | +with open(path) as f: |
| 49 | + content = f.read() |
| 50 | +new_header = """\ |
| 51 | +/* |
| 52 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 53 | + * Originally from github.com/facebookexperimental/moxygen. |
| 54 | + * See deps/moxygen/LICENSE for the original license terms. |
| 55 | + * |
| 56 | + * Copyright (c) OpenMOQ contributors. |
| 57 | + */""" |
| 58 | +content = re.sub( |
| 59 | + r'/\*.*?This source code is licensed under.*?\*/', |
| 60 | + new_header, content, count=1, flags=re.DOTALL) |
| 61 | +with open(path, 'w') as f: |
| 62 | + f.write(content) |
| 63 | +PYEOF |
| 64 | +} |
| 65 | + |
| 66 | +# ───────────────────────────────────────────────────────────────────────────── |
| 67 | +# Helper: add moxygen:: qualifier to unqualified moxygen types |
| 68 | +# |
| 69 | +# Called only on the header, where we're now in namespace openmoq::o_rly and |
| 70 | +# moxygen types are no longer implicitly visible. Uses negative lookbehind so |
| 71 | +# already-qualified occurrences (e.g. moxygen::Publisher) are left alone. |
| 72 | +# ───────────────────────────────────────────────────────────────────────────── |
| 73 | +qualify_moxygen_types() { |
| 74 | + local file="$1" |
| 75 | + perl -pi -e ' |
| 76 | + next if /^#include/; |
| 77 | + s/(?<!moxygen::)\bPublisher\b/moxygen::Publisher/g; |
| 78 | + s/(?<!moxygen::)\bSubscriber\b/moxygen::Subscriber/g; |
| 79 | + s/(?<!moxygen::)\bMoQForwarder\b/moxygen::MoQForwarder/g; |
| 80 | + s/(?<!moxygen::)\bMoQSession\b/moxygen::MoQSession/g; |
| 81 | + s/(?<!moxygen::)\bMoQCache\b/moxygen::MoQCache/g; |
| 82 | + s/(?<!moxygen::)\bTrackNamespace\b/moxygen::TrackNamespace/g; |
| 83 | + s/(?<!moxygen::)\bTrackConsumer\b/moxygen::TrackConsumer/g; |
| 84 | + s/(?<!moxygen::)\bFetchConsumer\b/moxygen::FetchConsumer/g; |
| 85 | + s/(?<!moxygen::)\bFullTrackName\b/moxygen::FullTrackName/g; |
| 86 | + s/(?<!moxygen::)\bSubscribeRequest\b/moxygen::SubscribeRequest/g; |
| 87 | + s/(?<!moxygen::)\bSubscribeNamespaceOptions\b/moxygen::SubscribeNamespaceOptions/g; |
| 88 | + s/(?<!moxygen::)\bSubscribeNamespace\b/moxygen::SubscribeNamespace/g; |
| 89 | + s/(?<!moxygen::)\bPublishNamespace\b/moxygen::PublishNamespace/g; |
| 90 | + s/(?<!moxygen::)\bPublishRequest\b/moxygen::PublishRequest/g; |
| 91 | + s/(?<!moxygen::)\bGoaway\b/moxygen::Goaway/g; |
| 92 | + s/(?<!moxygen::)\bTrackStatus\b/moxygen::TrackStatus/g; |
| 93 | + s/(?<!moxygen::)\bRequestUpdate\b/moxygen::RequestUpdate/g; |
| 94 | + s/(?<!moxygen::)\bRequestErrorCode\b/moxygen::RequestErrorCode/g; |
| 95 | + s/(?<!moxygen::)\bRequestError\b/moxygen::RequestError/g; |
| 96 | + s/(?<!moxygen::)\bRequestID\b/moxygen::RequestID/g; |
| 97 | + s/(?<!moxygen::)\bFetch\b/moxygen::Fetch/g; |
| 98 | + s/(?<!moxygen::)\bkDefaultMaxCachedTracks\b/moxygen::kDefaultMaxCachedTracks/g; |
| 99 | + s/(?<!moxygen::)\bkDefaultMaxCachedGroupsPerTrack\b/moxygen::kDefaultMaxCachedGroupsPerTrack/g; |
| 100 | + ' "$file" |
| 101 | +} |
| 102 | + |
| 103 | +# ───────────────────────────────────────────────────────────────────────────── |
| 104 | +# 1. MoQRelay.h → include/o_rly/ORelay.h |
| 105 | +# ───────────────────────────────────────────────────────────────────────────── |
| 106 | +process_header() { |
| 107 | + local src="${MOXYGEN_RELAY}/MoQRelay.h" |
| 108 | + local dst="${REPO_ROOT}/include/o_rly/ORelay.h" |
| 109 | + local tmp="${TMP_DIR}/ORelay.h" |
| 110 | + |
| 111 | + echo " MoQRelay.h -> include/o_rly/ORelay.h" |
| 112 | + cp "$src" "$tmp" |
| 113 | + |
| 114 | + replace_copyright "$tmp" |
| 115 | + |
| 116 | + # Include style: "moxygen/relay/MoQRelay.h" -> <o_rly/ORelay.h>; other "..." -> <...> |
| 117 | + sed -i 's|#include "moxygen/relay/MoQRelay\.h"|#include <o_rly/ORelay.h>|g' "$tmp" |
| 118 | + sed -i 's|#include "\(moxygen/[^"]*\)"|#include <\1>|g' "$tmp" |
| 119 | + |
| 120 | + # Class rename |
| 121 | + sed -i 's/\bMoQRelay\b/ORelay/g' "$tmp" |
| 122 | + |
| 123 | + # Namespace |
| 124 | + sed -i 's/^namespace moxygen {$/namespace openmoq::o_rly {/' "$tmp" |
| 125 | + sed -i 's|^} // namespace moxygen$|} // namespace openmoq::o_rly|' "$tmp" |
| 126 | + |
| 127 | + # Qualify unqualified moxygen types (order matters: more specific before less specific) |
| 128 | + qualify_moxygen_types "$tmp" |
| 129 | + |
| 130 | + cp "$tmp" "$dst" |
| 131 | +} |
| 132 | + |
| 133 | +# ───────────────────────────────────────────────────────────────────────────── |
| 134 | +# 2. MoQRelay.cpp → src/ORelay.cpp |
| 135 | +# ───────────────────────────────────────────────────────────────────────────── |
| 136 | +process_source() { |
| 137 | + local src="${MOXYGEN_RELAY}/MoQRelay.cpp" |
| 138 | + local dst="${REPO_ROOT}/src/ORelay.cpp" |
| 139 | + local tmp="${TMP_DIR}/ORelay.cpp" |
| 140 | + |
| 141 | + echo " MoQRelay.cpp -> src/ORelay.cpp" |
| 142 | + cp "$src" "$tmp" |
| 143 | + |
| 144 | + replace_copyright "$tmp" |
| 145 | + |
| 146 | + # Include style: relay header -> ORelay.h; other "..." -> <...> |
| 147 | + sed -i 's|#include "moxygen/relay/MoQRelay\.h"|#include <o_rly/ORelay.h>|g' "$tmp" |
| 148 | + sed -i 's|#include "\(moxygen/[^"]*\)"|#include <\1>|g' "$tmp" |
| 149 | + |
| 150 | + # Class/method references |
| 151 | + sed -i 's/\bMoQRelay\b/ORelay/g' "$tmp" |
| 152 | + |
| 153 | + # Namespace: insert "using namespace moxygen;" before the namespace block |
| 154 | + # so types in the implementation don't need moxygen:: qualification |
| 155 | + sed -i 's/^namespace moxygen {$/using namespace moxygen;\n\nnamespace openmoq::o_rly {/' "$tmp" |
| 156 | + sed -i 's|^} // namespace moxygen$|} // namespace openmoq::o_rly|' "$tmp" |
| 157 | + |
| 158 | + cp "$tmp" "$dst" |
| 159 | +} |
| 160 | + |
| 161 | +# ───────────────────────────────────────────────────────────────────────────── |
| 162 | +# 3. test/MoQRelayTest.cpp → tests/ORelayTest.cpp |
| 163 | +# ───────────────────────────────────────────────────────────────────────────── |
| 164 | +process_test() { |
| 165 | + local src="${MOXYGEN_RELAY}/test/MoQRelayTest.cpp" |
| 166 | + local dst="${REPO_ROOT}/tests/ORelayTest.cpp" |
| 167 | + local tmp="${TMP_DIR}/ORelayTest.cpp" |
| 168 | + |
| 169 | + echo " test/MoQRelayTest.cpp -> tests/ORelayTest.cpp" |
| 170 | + cp "$src" "$tmp" |
| 171 | + |
| 172 | + replace_copyright "$tmp" |
| 173 | + |
| 174 | + # Replace MoQRelay include with ORelay |
| 175 | + sed -i 's|#include <moxygen/relay/MoQRelay\.h>|#include <o_rly/ORelay.h>|g' "$tmp" |
| 176 | + |
| 177 | + # Relay class under test |
| 178 | + sed -i 's/\bMoQRelay\b/ORelay/g' "$tmp" |
| 179 | + |
| 180 | + # Add openmoq::o_rly namespace (and moxygen for types used at file scope). |
| 181 | + # The test lives in namespace moxygen::test, but globals defined before that |
| 182 | + # namespace block need explicit usings. |
| 183 | + sed -i 's/^using namespace testing;$/using namespace testing;\nusing namespace moxygen;\nusing namespace openmoq::o_rly;/' "$tmp" |
| 184 | + |
| 185 | + cp "$tmp" "$dst" |
| 186 | +} |
| 187 | + |
| 188 | +# ───────────────────────────────────────────────────────────────────────────── |
| 189 | +# Run transformations |
| 190 | +# ───────────────────────────────────────────────────────────────────────────── |
| 191 | +echo "--> Transforming files" |
| 192 | +process_header |
| 193 | +process_source |
| 194 | +process_test |
| 195 | + |
| 196 | +# ───────────────────────────────────────────────────────────────────────────── |
| 197 | +# Format |
| 198 | +# ───────────────────────────────────────────────────────────────────────────── |
| 199 | +echo "--> Formatting" |
| 200 | +"${SCRIPT_DIR}/format.sh" |
| 201 | + |
| 202 | +# ───────────────────────────────────────────────────────────────────────────── |
| 203 | +# Build |
| 204 | +# ───────────────────────────────────────────────────────────────────────────── |
| 205 | +if ! ${NO_BUILD}; then |
| 206 | + echo "--> Building" |
| 207 | + "${SCRIPT_DIR}/build.sh" |
| 208 | +fi |
| 209 | + |
| 210 | +# ───────────────────────────────────────────────────────────────────────────── |
| 211 | +# Test |
| 212 | +# ───────────────────────────────────────────────────────────────────────────── |
| 213 | +if ! ${NO_TEST}; then |
| 214 | + echo "--> Testing" |
| 215 | + "${SCRIPT_DIR}/test.sh" |
| 216 | +fi |
| 217 | + |
| 218 | +echo "==> Done" |
0 commit comments