11cb0ef41Sopenharmony_ci#!/bin/sh
21cb0ef41Sopenharmony_ciset -e
31cb0ef41Sopenharmony_ci# Shell script to update base64 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/aklomp/base64/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_VERSION=$(grep "base64 LANGUAGES C VERSION" ./deps/base64/base64/CMakeLists.txt | sed -n "s/^.*VERSION \(.*\))/\1/p")
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciecho "Comparing $NEW_VERSION with $CURRENT_VERSION"
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciif [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then
271cb0ef41Sopenharmony_ci  echo "Skipped because base64 is on the latest version."
281cb0ef41Sopenharmony_ci  exit 0
291cb0ef41Sopenharmony_cifi
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciecho "Making temporary workspace"
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciWORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')
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_citrap cleanup INT TERM EXIT
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cicd "$WORKSPACE"
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciBASE64_TARBALL="base64-v$NEW_VERSION.tar.gz"
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciecho "Fetching base64 source archive"
481cb0ef41Sopenharmony_cicurl -sL -o "$BASE64_TARBALL" "https://api.github.com/repos/aklomp/base64/tarball/v$NEW_VERSION"
491cb0ef41Sopenharmony_cilog_and_verify_sha256sum "base64" "$BASE64_TARBALL"
501cb0ef41Sopenharmony_cigzip -dc "$BASE64_TARBALL" | tar xf -
511cb0ef41Sopenharmony_cirm "$BASE64_TARBALL"
521cb0ef41Sopenharmony_cimv aklomp-base64-* base64
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciecho "Replacing existing base64"
551cb0ef41Sopenharmony_cirm -rf "$DEPS_DIR/base64/base64"
561cb0ef41Sopenharmony_cimv "$WORKSPACE/base64" "$DEPS_DIR/base64/"
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci# Build configuration is handled by `deps/base64/base64.gyp`, but since `config.h` has to be present for the build
591cb0ef41Sopenharmony_ci# to work, we create it and leave it empty.
601cb0ef41Sopenharmony_ciecho "// Intentionally empty" > "$DEPS_DIR/base64/base64/lib/config.h"
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci# Clear out .gitignore, otherwise config.h is ignored. That's dangerous when
631cb0ef41Sopenharmony_ci# people check in our tarballs into source control and run `git clean`.
641cb0ef41Sopenharmony_ciecho "# Intentionally empty" > "$DEPS_DIR/base64/base64/.gitignore"
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciecho "All done!"
671cb0ef41Sopenharmony_ciecho ""
681cb0ef41Sopenharmony_ciecho "Please git add base64/base64, commit the new version:"
691cb0ef41Sopenharmony_ciecho ""
701cb0ef41Sopenharmony_ciecho "$ git add -A deps/base64/base64 src/base64_version.h"
711cb0ef41Sopenharmony_ciecho "$ git commit -m \"deps: update base64 to $NEW_VERSION\""
721cb0ef41Sopenharmony_ciecho ""
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci# update the base64_version.h
751cb0ef41Sopenharmony_cicat > "$BASE_DIR/src/base64_version.h" << EOL
761cb0ef41Sopenharmony_ci// This is an auto generated file, please do not edit.
771cb0ef41Sopenharmony_ci// Refer to tools/dep_updaters/update-base64.sh
781cb0ef41Sopenharmony_ci#ifndef SRC_BASE64_VERSION_H_
791cb0ef41Sopenharmony_ci#define SRC_BASE64_VERSION_H_
801cb0ef41Sopenharmony_ci#define BASE64_VERSION "$NEW_VERSION"
811cb0ef41Sopenharmony_ci#endif  // SRC_BASE64_VERSION_H_
821cb0ef41Sopenharmony_ciEOL
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci# The last line of the script should always print the new version,
851cb0ef41Sopenharmony_ci# as we need to add it to $GITHUB_ENV variable.
861cb0ef41Sopenharmony_ciecho "NEW_VERSION=$NEW_VERSION"
87