11cb0ef41Sopenharmony_ci#!/bin/sh
21cb0ef41Sopenharmony_ciset -e
31cb0ef41Sopenharmony_ci# Shell script to update uvwasi in the source tree to a specific version
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciBASE_DIR=$(cd "$(dirname "$0")/../.." && pwd)
61cb0ef41Sopenharmony_ciDEPS_DIR="$BASE_DIR/deps"
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci[ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node"
91cb0ef41Sopenharmony_ci[ -x "$NODE" ] || NODE=$(command -v node)
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci# shellcheck disable=SC1091
121cb0ef41Sopenharmony_ci. "$BASE_DIR/tools/dep_updaters/utils.sh"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciNEW_VERSION="$("$NODE" --input-type=module <<'EOF'
151cb0ef41Sopenharmony_ciconst res = await fetch('https://api.github.com/repos/nodejs/uvwasi/releases/latest');
161cb0ef41Sopenharmony_ciif (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res });
171cb0ef41Sopenharmony_ciconst { tag_name } = await res.json();
181cb0ef41Sopenharmony_ciconsole.log(tag_name.replace('v', ''));
191cb0ef41Sopenharmony_ciEOF
201cb0ef41Sopenharmony_ci)"
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciCURRENT_MAJOR_VERSION=$(grep "#define UVWASI_VERSION_MAJOR" "$DEPS_DIR/uvwasi/include/uvwasi.h" | sed -n "s/^.*MAJOR \(.*\)/\1/p")
231cb0ef41Sopenharmony_ciCURRENT_MINOR_VERSION=$(grep "#define UVWASI_VERSION_MINOR" "$DEPS_DIR/uvwasi/include/uvwasi.h" | sed -n "s/^.*MINOR \(.*\)/\1/p")
241cb0ef41Sopenharmony_ciCURRENT_PATCH_VERSION=$(grep "#define UVWASI_VERSION_PATCH" "$DEPS_DIR/uvwasi/include/uvwasi.h" | sed -n "s/^.*PATCH \(.*\)/\1/p")
251cb0ef41Sopenharmony_ciCURRENT_VERSION="$CURRENT_MAJOR_VERSION.$CURRENT_MINOR_VERSION.$CURRENT_PATCH_VERSION"
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciecho "Comparing $NEW_VERSION with $CURRENT_VERSION"
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciif [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then
301cb0ef41Sopenharmony_ci  echo "Skipped because uvwasi is on the latest version."
311cb0ef41Sopenharmony_ci  exit 0
321cb0ef41Sopenharmony_cifi
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciecho "Making temporary workspace"
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciWORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')
371cb0ef41Sopenharmony_ciecho "$WORKSPACE"
381cb0ef41Sopenharmony_cicleanup () {
391cb0ef41Sopenharmony_ci  EXIT_CODE=$?
401cb0ef41Sopenharmony_ci  [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE"
411cb0ef41Sopenharmony_ci  exit $EXIT_CODE
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_citrap cleanup INT TERM EXIT
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciUVWASI_ZIP="uvwasi-$NEW_VERSION"
471cb0ef41Sopenharmony_cicd "$WORKSPACE"
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciecho "Fetching UVWASI source archive..."
501cb0ef41Sopenharmony_cicurl -sL -o "$UVWASI_ZIP.zip" "https://github.com/nodejs/uvwasi/archive/refs/tags/v$NEW_VERSION.zip"
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cilog_and_verify_sha256sum "uvwasi" "$UVWASI_ZIP.zip"
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciecho "Moving existing GYP build file"
551cb0ef41Sopenharmony_cimv "$DEPS_DIR/uvwasi/"*.gyp "$WORKSPACE/"
561cb0ef41Sopenharmony_cirm -rf "$DEPS_DIR/uvwasi/"
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciecho "Unzipping..."
591cb0ef41Sopenharmony_ciunzip "$UVWASI_ZIP.zip" -d "$DEPS_DIR/uvwasi/"
601cb0ef41Sopenharmony_cirm "$UVWASI_ZIP.zip"
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cimv "$WORKSPACE/"*.gyp "$DEPS_DIR/uvwasi/"
631cb0ef41Sopenharmony_cicd "$DEPS_DIR/uvwasi/"
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciecho "Copying new files to deps folder"
661cb0ef41Sopenharmony_cicp -r "$UVWASI_ZIP/include" "$DEPS_DIR/uvwasi/"
671cb0ef41Sopenharmony_cicp -r "$UVWASI_ZIP/src" "$DEPS_DIR/uvwasi/"
681cb0ef41Sopenharmony_cicp "$UVWASI_ZIP/LICENSE" "$DEPS_DIR/uvwasi/"
691cb0ef41Sopenharmony_cirm -rf "$UVWASI_ZIP"
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciecho "All done!"
721cb0ef41Sopenharmony_ciecho ""
731cb0ef41Sopenharmony_ciecho "Please git add uvwasi, commit the new version:"
741cb0ef41Sopenharmony_ciecho ""
751cb0ef41Sopenharmony_ciecho "$ git add -A deps/uvwasi"
761cb0ef41Sopenharmony_ciecho "$ git commit -m \"deps: update uvwasi to $NEW_VERSION\""
771cb0ef41Sopenharmony_ciecho ""
781cb0ef41Sopenharmony_ciecho "Make sure to update the deps/uvwasi/uvwasi.gyp if any significant changes have occurred upstream"
791cb0ef41Sopenharmony_ciecho ""
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci# The last line of the script should always print the new version,
821cb0ef41Sopenharmony_ci# as we need to add it to $GITHUB_ENV variable.
831cb0ef41Sopenharmony_ciecho "NEW_VERSION=$NEW_VERSION"
84