File prepare_vendor.sh of Package signal-desktop

#!/bin/bash -ux
# shellcheck disable=2181

#dnf install asar binutils cargo-vendor-filterer curl diffutils moreutils sed tar wget xz zstd

SIGNAL_URL=$(rpmspec -P -Delectron_req\ %nil ./*.spec | grep Source0 | sed -e 's/Source0:[ ]*//g')
SIGNAL_TARBALL=$(basename "$SIGNAL_URL")
SIGNAL_PKGVERSION=$(rpmspec -P -Delectron_req\ %nil ./*.spec | grep ^Version | sed -e 's/Version:[ ]*//g')
SIGNAL_PKGNAME="Signal-Desktop"
SIGNAL_PKGDIR="$(pwd)"
SIGNAL_TMPDIR=$(mktemp --tmpdir -d signal-desktop-XXXXXXXX)
SIGNAL_PATH="$SIGNAL_TMPDIR/$SIGNAL_PKGNAME-$SIGNAL_PKGVERSION"

echo "URL:     $SIGNAL_URL"
echo "TARBALL: $SIGNAL_TARBALL"
echo "NAME:    $SIGNAL_PKGNAME"
echo "VERSION: $SIGNAL_PKGVERSION"
echo "PATH:    $SIGNAL_PATH"

cleanup_tmpdir()
{
    popd 2> /dev/null || exit 1
    rm -rf "$SIGNAL_TMPDIR"
}
trap cleanup_tmpdir SIGINT

cleanup_and_exit()
{
    cleanup_tmpdir
    if test "$1" = 0 -o -z "$1"; then
        exit 0
    else
        exit "$1"
    fi
}

if [ ! -w "$SIGNAL_TARBALL" ]; then
    wget "$SIGNAL_URL"
fi

tar -xf "$SIGNAL_TARBALL" -C "$SIGNAL_TMPDIR"

pushd "$SIGNAL_PATH" || cleanup_and_exit 1



export ELECTRON_SKIP_BINARY_DOWNLOAD=1

export RUSTC_BOOTSTRAP=1 # needed to build signal-client

export CARGO_NET_GIT_FETCH_WITH_CLI=true #otherwise cargo fails with warning: spurious network error (3 tries remaining): error reading from the zlib stream; class=Zlib (5)


export SIGNAL_ENV=production


echo ">>>>>> Patch package.json"
cp -p package.json package.json.orig

echo ">>>>>> Fix node version requirement (package.json)"
sed -i 's#"node": "#&>=#' package.json



echo ">>>>>> Remove HEIF/HEIC support (package.json)"
sed -i '/"heic-convert": .*/d' package.json
sed -i '/".*heicConverter.bundle.js".*/d' package.json

diff -u package.json.orig package.json



echo ">>>>>> Install npm modules"
npm ci  --verbose --ignore-scripts

ret=$?
if [ $ret -ne 0 ]; then
    echo "ERROR: yarn install failed"
    cleanup_and_exit 1
fi

echo ">>>>>> Apply patches from upstream"
npx --offline patch-package
ret=$?
if [ $ret -ne 0 ]; then
    echo "ERROR: patch failed"
    cleanup_and_exit 1
fi

echo -n ">>>>>> Export PATH="
export PATH="$PATH:$SIGNAL_PATH/node_modules/.bin"
echo "$PATH"

echo ">>>>>> Download sqlcipher vendor vendor for better-sqlite3"
pushd node_modules/@signalapp/better-sqlite3/deps || cleanup_and_exit 1
node download.js
if [ $? -ne 0 ]; then
    echo "ERROR: download.js failed for sqlite3"
    cleanup_and_exit 1
fi
mkdir -pv sqlite3
cd sqlite3
tar -zxvvf ../sqlcipher.tar.gz
rm -rvf OpenSSL*
rm -rvf openssl-include
rm -v ../sqlcipher.tar.gz

echo ">>>>>> Download signal_tokenizer"
rm -rvf signal-tokenizer
FTS5_VERSION=$(<../download.js grep '^const TOKENIZER_VERSION' | sed 's/^.* = '\''//' | sed 's/'\'';$//')
curl -L https://github.com/signalapp/Signal-FTS5-Extension/archive/refs/tags/v${FTS5_VERSION}.tar.gz | tar -zxvvf -
mv -v Signal-FTS5-Extension-* signal-tokenizer
cd signal-tokenizer
mkdir -pv .cargo 
cargo vendor-filterer --platform='*-unknown-linux-gnu' --platform='*-unknown-linux-gnueabihf' --all-features > .cargo/config
if [ $? -ne 0 ]; then
    echo 'ERROR: cargo vendor failed'
    cleanup_and_exit 1
fi




popd || cleanup_and_exit 1




echo ">>>>>> Download node vendor for sticker-creator"
pushd sticker-creator || cleanup_and_exit 1
npm ci  --verbose --ignore-scripts
if [ $? -ne 0 ]; then
    echo "ERROR: yarn install failed for sticker-creator"
    cleanup_and_exit 1
fi
popd


echo ">>>>>> Clean up release files"
rm -rf release
rm -rf dist


echo ">>>>>> Cleanup object dirs"
find . -type d -name "*.o.d" -print0 | xargs -0 rm -rvf
find . -type d -name "__pycache__" -print0 | xargs -0 rm -rvf

echo ">>>>>> Cleanup object files"
find . -name "*.node" -print -delete
find . -name "*.jar" -print -delete
find . -name "*.dll" -print -delete
find . -name "*.exe" -print -delete
find . -name "*.dylib" -print -delete
find . -name "*.so" -print -delete
find . -name "*.o" -print -delete
find . -name "*.a" -print -delete
find . -name "*.wasm" -print -delete

echo '>>>>>> Remove vendored binaries'
#We use sponge to avoid a race condition between find and rm
find . -type f| sponge |\
    xargs -P"$(nproc)" -- sh -c 'file -S "$@" | grep -v '\'': .*script'\'' | grep '\'': .*executable'\'' | tee /dev/stderr | sed '\''s/: .*//'\'' | xargs rm -fv'


# Missing source file — https://github.com/signalapp/Signal-Desktop/issues/6814
export XZ_OPT="-T$(nproc) -vv"
#wget https://updates.signal.org/desktop/apt/pool/s/signal-desktop/signal-desktop_${SIGNAL_PKGVERSION}_amd64.deb
#ar -p signal-desktop_${SIGNAL_PKGVERSION}_amd64.deb data.tar.xz | tar -Jxvv ./opt/Signal/resources/app.asar
#asar ef opt/Signal/resources/app.asar build/dns-fallback.json
#mv -v dns-fallback.json build/

#echo '>>>>>> Hardlink duplicate files to reduce extraction time'
#Disabled this — it fails to link sometimes causing tarball nondeterminism
#/usr/lib/rpm/fdupes_wrapper node_modules

# Remove empty directories
echo ">>>>>> Remove empty directories"
find . -type d -empty -print -delete

echo ">>>>>> Package vendor files"
rm -f "${SIGNAL_PKGDIR}/vendor.tar.zst"
ZSTD_CLEVEL=19 ZSTD_NBTHREADS=$(nproc) tar --zstd --sort=name -vvScf "${SIGNAL_PKGDIR}/vendor.tar.zst" {sticker-creator/,}node_modules
if [ $? -ne 0 ]; then
    cleanup_and_exit 1
fi
echo "vendor $(du -sh "${SIGNAL_PKGDIR}/vendor.tar.zst")"


cleanup_and_exit 0
openSUSE Build Service is sponsored by