File element-desktop.spec of Package element-desktop

#
# spec file for package element-desktop
#
# Copyright (c) 2022 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


Name:           element-desktop
Group:		Productivity/Networking/Talk/Clients
Version:        1.11.67
Release:        0
Summary:        A glossy Matrix collaboration client - desktop
License:        Apache-2.0
URL:            https://github.com/vector-im/element-desktop
Source0:        https://github.com/vector-im/element-desktop/archive/v%{version}.tar.gz#/element-desktop-%{version}.tar.gz
Source1:        https://github.com/vector-im/element-web/archive/v%{version}.tar.gz#/element-web-%{version}.tar.gz
Source2:	io.element.Element.desktop
##
# Crap begins here.  This is a tarball of all the node packages required
# as a cache for yarn -offline.
#
# Node pkgs is primarily node source, but it does contain multiple arch
# binaries in app-builder-bin and 7zip-bin.
#
# To get this, simply set .yarnrc to the one below, run the build of
# element-web and element-desktop manually and tar up the created
# node-pkgs directory
##
Source3:	node-pkgs.tar.gz
##
# This is the offline yarnrc
##
Source4:	yarnrc
##
# The crates used to build the native version .. very dependent on
# cargo version.  To get this set CARGO_HOME appropriately, run the
# element-desktop build and tar up the CARGO_HOME directory.
##
Source5:	cargo.tar.gz
##
# The prebuilt electron binaries.  If you think you want to build them
# yourself, think again ... it took me a couple of days with a fast build
# machine last time.
#
# The hash is simply
# echo -n "`dirname https://github.com/electron/electron/releases/download/v13.5.2/electron-v13.5.2-linux-x64.zip`"|sha256sum
##
%define		electronver	29.4.0
%define		electronhash	0a240d372c27dba441dcf349503f94e3d1b9a8e111ad571e9152e81d50bd722c
Source7:	https://github.com/electron/electron/releases/download/v%{electronver}/electron-v%{electronver}-linux-x64.zip
#Source8:	https://github.com/electron/electron/releases/download/v%{electronver}/SHASUMS256.txt
##
# FIXME: get rid of this.
#  fpm is only used to build the dpkg we don't want anyway
##
#%define		fpm	fpm-1.9.3-2.3.1-linux-x86_64
#Source9:	https://github.com/electron-userland/electron-builder-binaries/releases/download/%{fpm}/%{fpm}.7z
##
# Yet another copy of node devel headers, but these are specific to the
# electron build
##
Source10:	https://electronjs.org/headers/v%{electronver}/node-v%{electronver}-headers.tar.gz
##
# Differential node additions ... makes it easier to do incremental releases.
# Typically new releases add newer versions of node package, so simply pack
# up the new versions in this tar file and the build will automatically select
# them (saves uploading the entire node-pkgs.tar.gz again
##
Source11:	node-pkgs-additional.tar.gz
##
# Sentry CLI
# note that hash is the first 6 digits of the md5sum of the download url
#
# node: problem with x86_64 in download url which is transformed to x86-64
#
# echo -n %Source12 | md5sum | cut -b 1-6
##
#Source12:	https://downloads.sentry-cdn.com/sentry-cli/2.21.2/sentry-cli-Linux-x86_64
#%define		sentryhash 1fdea4
##
# The node lock tells yarn how to build the npm cache and what dependencies
# to use, so element-desktop's node.lock has to be updated for electron
# matrix-seshat and keytar
##
Source13:	element-desktop-yarn.lock

%if %{defined fedora}
BuildRequires:  npm
%endif
BuildRequires:  nodejs >= 18.0.0
BuildRequires:  nodejs-devel >= 18.0.0
BuildRequires:  python3
BuildRequires:  cargo >= 1.60.0
BuildRequires:  yarn
BuildRequires:	sqlcipher-devel
BuildRequires:  libsecret-devel
BuildRequires:  gcc-c++
BuildRequires:	git
#BuildRequires:  p7zip
BuildRequires:	hicolor-icon-theme
BuildRequires:	fdupes

%define debug_package %{nil}

%description
A glossy Matrix collaboration client - desktop

This is a packaging of the element-web client into an electron application
that allows it to interact with linux desktop notifications and the secrets
storage area.

%prep
%setup -T -c
pwd
tar xf %{SOURCE0}
tar xf %{SOURCE1}
tar xf %{SOURCE3}
# plus additional
tar xf %{SOURCE11}
cp %{SOURCE4} element-web-%{version}/.yarnrc
cp %{SOURCE4} element-desktop-%{version}/.yarnrc
cp %{SOURCE13} element-desktop-%{version}/yarn.lock
tar xpf %{SOURCE5}
##
# Hack to make sentry-cli binary available in the cache
##
#mkdir -p node-cache/sentry-cli
#install -m 0755 %{SOURCE12} node-cache/sentry-cli/%{sentryhash}-sentry-cli-Linux-x86-64
##
# This is really ugly, but the electron pieces go in the XDG cache
##
mkdir -p cache/electron/%{electronhash}
cp %{SOURCE7} cache/electron/%{electronhash}/
##
# "why twice?" you ask ... because it's electron.  yarn install requires
# the first, yarn build the second
##
cp %{SOURCE7} cache/electron/
#cp %{SOURCE8} cache/electron/%{electronhash}/
#mkdir -p cache/electron-builder/fpm/%{fpm}
#cd cache/electron-builder/fpm/%{fpm}
#7za x %{SOURCE9}
#cd ../../../..

##
# This is an awful hack, but it seems to be hardcoded in elecron-gyp
##
rm -fr $HOME/.electron-gyp
mkdir -p $HOME/.electron-gyp
cd $HOME/.electron-gyp
tar xf %{SOURCE10}
mv node_headers %{electronver}/
echo "11" > %{electronver}/installVersion

%build
export CARGO_HOME=`pwd`/cargo
export XDG_CACHE_HOME=`pwd`/cache
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
##
# increase the gc size to prevent node from crashing
##
export NODE_OPTIONS=--max-old-space-size=8192
##
# Another hack to fix the electron build
##
export NODE_TLS_REJECT_UNAUTHORIZED=0
##
# From rust 1.70, the sparse protocol became the default
# meaning that our cargo.tar.gz, which is build with the git
# protocol becomes invalid.  Fix by pushing cargo back to
# using the git protocol
##
export CARGO_REGISTRIES_CRATES_IO_PROTOCOL=git
cd element-web-%{version}

##
# hack to stop build-jitsi from getting an unresolved promise
# in the minimum version download (only needed for node > 14)
##
yarn --offline install
> scripts/build-jitsi.js
> scripts/build-jitsi.ts
yarn build
##
# make sure the webapp has a config.json
##
install -D -m644 -p config.sample.json webapp/config.json
cd ../element-desktop-%{version}
##
# Hack up electron version.  Without this it will use
# electron v17.0.0 which is rather buggy
##
sed -i 's/"electron": "^29.0.0"/"electron": "%{electronver}"/' package.json
# Get rid of the need for fpm
sed -i 's/"tar.gz", "deb"/"dir"/' electron-builder.ts
##
# --frozen-lockfile is required to prevent yarn strippint the apparently
# unnecessary dependencies in the lockfile for matrix-seshat and keytar
##
yarn --offline --frozen-lockfile install
##
# We need the native versions of these but can't allow yarn run hak
# which is build:native to run because it will try to download stuff
# so we do this to hack replacements.  Unfortunately, this is going to
# use cache files rather than the yarn cache which is why the duplication
#
# The values here must match exactly those in hakDependencies of package.json
##
# hack so keytar can find node-gyp the command
##
PATH=$PATH:"`pwd`/node_modules/.bin"
##
# shim the include files otherwise node-gyp will try to download them
##
nodever=`node -v|sed s/^v//`
# double interpolation problem in backslash expansion
nodemaj=`echo $nodever|sed s/\\\\..*$//`
%if %{defined fedora}
nodemaj=
%endif
echo strict-ssl=false > ${HOME}/.npmrc
mkdir -p ../cache/node-gyp/${nodever}/include
ln -s /usr/include/node${nodemaj} ../cache/node-gyp/${nodever}/include/node
echo "11" > ../cache/node-gyp/${nodever}/installVersion
yarn add --offline --frozen-lockfile keytar@^7.9.0
##
# for matrix-seshat <= 2.3.3 yarn add did a native build, but for
# 3.0.0+ we have to fiddle the package.json file to get that to happen
##
tar -C .. -x -z -f ../node-pkgs/matrix-seshat-3.0.1.tgz
sed -i 's/devDependencies/dependencies/' ../package/package.json
sed -i '/"scripts":/a    "install": "yarn build",' ../package/package.json
yarn add --offline --frozen-lockfile file:../package
#
yarn --offline build
##
# pack the web application into an asar
##
./node_modules/.bin/asar pack ../element-web-%{version}/webapp dist/linux-unpacked/resources/webapp.asar
##
# remove the unpacked remnants (FIXME: the asar won't unpack if we do this)
# the problem seems to be around the contents of the matrix-seshat module
# so remove everything apart from that.
##
#rm -fr dist/linux-unpacked/resources/app.asar.unpacked
cd dist/linux-unpacked/resources/app.asar.unpacked/node_modules
for f in *; do
    if [ "$f" != "matrix-seshat" -a "$f" != "keytar" ]; then
	rm -fr "$f";
    else
	rm -fr "$f"/native/target
	rm -fr "$f"/native/src
    fi
done

%install
install -d "%{buildroot}/%{_prefix}/lib/element"
install -d "%{buildroot}/%{_bindir}"

# Install the app content, replace the webapp with a symlink to the system package
cp -r element-desktop-%{version}/dist/linux-unpacked/* "%{buildroot}%{_prefix}/lib/element/"

ln -s "%{_prefix}/lib/element/element-desktop" "%{buildroot}/%{_bindir}/element-desktop"

# Config file
install -Dm644 element-desktop-%{version}/element.io/release/config.json -t "%{buildroot}%{_sysconfdir}/element"

# Extras
install -Dm644 %{SOURCE2} -t "%{buildroot}%{_datadir}/applications/"

# Icons
install -Dm644 element-web-%{version}/res/themes/element/img/logos/element-logo.svg "%{buildroot}%{_datadir}/icons/hicolor/scalable/apps/io.element.Element.svg"
for i in 16 24 48 64 96 128 256 512; do
	install -Dm644 element-desktop-%{version}/build/icons/${i}x${i}.png "%{buildroot}%{_datadir}/icons/hicolor/${i}x${i}/apps/io.element.Element.png"
done

%fdupes %{buildroot}/%{_datadir}
%fdupes %{buildroot}/%{_prefix}/lib

%files
%license element-desktop-%{version}/LICENSE
%{_bindir}/%{name}
%{_prefix}/lib/element/
%{_sysconfdir}/element/
%{_datadir}/applications/io.element.Element.desktop
%{_datadir}/icons/hicolor/scalable/apps/io.element.Element.svg
%{_datadir}/icons/hicolor/*/apps/io.element.Element.png

%changelog
openSUSE Build Service is sponsored by