11cb0ef41Sopenharmony_ci#!/bin/sh
21cb0ef41Sopenharmony_ciset -e
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci# Shell script to update llhttp in the source tree to specific version
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciBASE_DIR=$(cd "$(dirname "$0")/../.." && pwd)
71cb0ef41Sopenharmony_ciDEPS_DIR="${BASE_DIR}/deps"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci[ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node"
101cb0ef41Sopenharmony_ci[ -x "$NODE" ] || NODE=$(command -v node)
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci# shellcheck disable=SC1091
131cb0ef41Sopenharmony_ci. "$BASE_DIR/tools/dep_updaters/utils.sh"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciNEW_VERSION="$("$NODE" --input-type=module <<'EOF'
161cb0ef41Sopenharmony_ciconst res = await fetch('https://api.github.com/repos/nodejs/llhttp/releases/latest');
171cb0ef41Sopenharmony_ciif (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res });
181cb0ef41Sopenharmony_ciconst { tag_name } = await res.json();
191cb0ef41Sopenharmony_ciconsole.log(tag_name.replace('release/v', ''));
201cb0ef41Sopenharmony_ciEOF
211cb0ef41Sopenharmony_ci)"
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciCURRENT_MAJOR_VERSION=$(grep "#define LLHTTP_VERSION_MAJOR" ./deps/llhttp/include/llhttp.h | sed -n "s/^.*MAJOR \(.*\)/\1/p")
241cb0ef41Sopenharmony_ciCURRENT_MINOR_VERSION=$(grep "#define LLHTTP_VERSION_MINOR" ./deps/llhttp/include/llhttp.h | sed -n "s/^.*MINOR \(.*\)/\1/p")
251cb0ef41Sopenharmony_ciCURRENT_PATCH_VERSION=$(grep "#define LLHTTP_VERSION_PATCH" ./deps/llhttp/include/llhttp.h | sed -n "s/^.*PATCH \(.*\)/\1/p")
261cb0ef41Sopenharmony_ciCURRENT_VERSION="$CURRENT_MAJOR_VERSION.$CURRENT_MINOR_VERSION.$CURRENT_PATCH_VERSION"
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciecho "Comparing $NEW_VERSION with $CURRENT_VERSION"
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciif [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then
311cb0ef41Sopenharmony_ci  echo "Skipped because llhttp is on the latest version."
321cb0ef41Sopenharmony_ci  exit 0
331cb0ef41Sopenharmony_cifi
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cicleanup () {
361cb0ef41Sopenharmony_ci  EXIT_CODE=$?
371cb0ef41Sopenharmony_ci  [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE"
381cb0ef41Sopenharmony_ci  exit $EXIT_CODE
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciecho "Making temporary workspace ..."
421cb0ef41Sopenharmony_ciWORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')
431cb0ef41Sopenharmony_citrap cleanup INT TERM EXIT
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cicd "$WORKSPACE"
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciif echo "$NEW_VERSION" | grep -qs "/" ; then # Download a release
481cb0ef41Sopenharmony_ci  REPO="git@github.com:$NEW_VERSION.git"
491cb0ef41Sopenharmony_ci  BRANCH=$2
501cb0ef41Sopenharmony_ci  [ -z "$BRANCH" ] && BRANCH=main
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  echo "Cloning llhttp source archive $REPO ..."
531cb0ef41Sopenharmony_ci  git clone "$REPO" llhttp
541cb0ef41Sopenharmony_ci  cd llhttp
551cb0ef41Sopenharmony_ci  echo "Checking out branch $BRANCH ..."
561cb0ef41Sopenharmony_ci  git checkout "$BRANCH"
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  echo "Building llhttp ..."
591cb0ef41Sopenharmony_ci  npm install
601cb0ef41Sopenharmony_ci  make release
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  echo "Copying llhttp release ..."
631cb0ef41Sopenharmony_ci  rm -rf "$DEPS_DIR/llhttp"
641cb0ef41Sopenharmony_ci  cp -a release "$DEPS_DIR/llhttp"
651cb0ef41Sopenharmony_cielse
661cb0ef41Sopenharmony_ci  echo "Download llhttp release $NEW_VERSION ..."
671cb0ef41Sopenharmony_ci  LLHTTP_TARBALL="llhttp-v$NEW_VERSION.tar.gz"
681cb0ef41Sopenharmony_ci  curl -sL -o "$LLHTTP_TARBALL" "https://github.com/nodejs/llhttp/archive/refs/tags/release/v$NEW_VERSION.tar.gz"
691cb0ef41Sopenharmony_ci  gzip -dc "$LLHTTP_TARBALL" | tar xf -
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  echo "Copying llhttp release ..."
721cb0ef41Sopenharmony_ci  rm -rf "$DEPS_DIR/llhttp"
731cb0ef41Sopenharmony_ci  cp -a "llhttp-release-v$NEW_VERSION" "$DEPS_DIR/llhttp"
741cb0ef41Sopenharmony_cifi
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciecho ""
771cb0ef41Sopenharmony_ciecho "All done!"
781cb0ef41Sopenharmony_ciecho ""
791cb0ef41Sopenharmony_ciecho "Please git add llhttp, commit the new version:"
801cb0ef41Sopenharmony_ciecho ""
811cb0ef41Sopenharmony_ciecho "$ git add -A deps/llhttp"
821cb0ef41Sopenharmony_ciecho "$ git commit -m \"deps: update llhttp to $NEW_VERSION\""
831cb0ef41Sopenharmony_ciecho ""
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci# The last line of the script should always print the new version,
861cb0ef41Sopenharmony_ci# as we need to add it to $GITHUB_ENV variable.
871cb0ef41Sopenharmony_ciecho "NEW_VERSION=$NEW_VERSION"
88