File discord-wbundled-electron.spec of Package discord-wbundled-electron

#
# spec file for package discord-wbundled-electron
#
# Copyright (c) 2025 SUSE LLC and contributors
# Copyright (c) 2023 Björn Bidar
#
# 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/
#


%bcond openasar 0

# Require bash for extglob in install section.
%global _buildshell /bin/bash
%global __requires_exclude ^(libffmpeg|libnode).*
%global __provides_exclude ^(libffmpeg|libnode).*

%define _channel_suffix canary
%define _exename Discord%{?_channel_suffix:%{lua: return (string.gsub(rpm.expand('%{_channel_suffix}'), "^%l", string.upper))}}
%define _friendlychannel Stable
%define _name discord
%global _upstream_version 0.0.786

Name:           %{_name}-wbundled-electron
Version:        %{_upstream_version}+git1
Release:        0
Summary:        Voice and Text Chat for Gamers
License:        NonFree
Group:          Productivity/Networking/Instant Messenger
URL:            https://discordapp.com/
#!RemoteAsset
Source0:        https://dl%{?_channel_suffix:-%{_channel_suffix}}.discordapp.net/apps/linux/%{_upstream_version}/%{_name}%{?_channel_suffix:-%{_channel_suffix}}-%{_upstream_version}.tar.gz
#Source0:        https://discordapp.com/api/download?platform=linux&format=tar.gz
Source2:        discord-symbolic.svg
Source3:        https://raw.githubusercontent.com/flathub/com.discordapp.Discord/master/com.discordapp.Discord.appdata.xml
Source4:        https://github.com/GooseMod/OpenAsar/releases/download/nightly/app.asar
Source99:       PERMISSION
BuildRequires:  asar
BuildRequires:  desktop-file-utils
BuildRequires:  fdupes
BuildRequires:  hicolor-icon-theme
BuildRequires:  sed
BuildRequires:  unzip
Requires:       electron
Conflicts:      discord

# Discord seems to contain no native code
BuildArch:      noarch

%description
Discord is a voice and text chat for gamers. The Text chat supports
inline images and videos. Voice chat includes a jitter buffer,
automatic gain control, noise suppression, echo cancellation.
Server-to-client communications are encrypted.

%prep
%setup -q -n Discord%{?_channel_suffix:%{lua: return (string.gsub(rpm.expand('%{_channel_suffix}'), "^%l", string.upper))}}

# remove bundled copy of electron
rm %{_exename}
rm chrome-sandbox \
   chrome_crashpad_handler
rm chrome_100_percent.pak
rm chrome_200_percent.pak
rm resources.pak
rm icudtl.dat
rm *.so*
rm snapshot_blob.bin
rm v8_context_snapshot.bin
rm -rf locales
rm -rf swiftshader
rm vk_swiftshader_icd.json

#remove unused scripts
rm postinst.sh

asar e resources/app.asar resources/app
rm resources/app.asar

sed -i 's[XXXPKGNAMEXXX[%{_name}[g' resources/app/app_bootstrap/autoStart/linux.js

# Patch js code to use system electron. Thanks to Arch Linux for patches.
sed -i "s|process.resourcesPath|'%{_datadir}/%{_name}'|" resources/app/app_bootstrap/buildInfo.js
sed -Ei "s|resourcesPath = _path.+;|resourcesPath = '%{_datadir}/%{_name}';|"  resources/app/common/paths.js
sed -i "s|exeDir,|'/usr/share/pixmaps',|" resources/app/app_bootstrap/autoStart/linux.js

# Patch window class to display correct icon
# jq --compact-output --arg NAME "%%{_name}" '.name=$NAME' resources/app/package.json | sponge resources/app/package.json
# sed -i "s/StartupWMClass=discord/StartupWMClass=%%{_name}/" %%{_name}.desktop

#Remove development garbage
cd %{_builddir}/%{_exename}/resources/app

# Remove any hidden files
find -name .\* -exec rm -rfv {} \; || true

# Unwanted files which where not caught by find
%define unwanted_files \\\
        node_modules/ajv/scripts/ \\\
        node_modules/sshpk/bin \\\
        node_modules/uuid/bin \\\
        node_modules/dashdash/etc/dashdash.bash_completion.in \\\
        node_modules/mkdirp/bin/cmd.js \\\
        node_modules/rimraf/bin.js

rm -rfv %{unwanted_files}

find -type d -name \*.bin -print0|xargs -r0 rm -rfv {} \;

#Fix file mode
find . -type f -exec chmod 644 {} \;

%autopatch -p1

%build
cd %{_builddir}

#We disable packing of asar, it is counterproductive on linux (makes dupe hardlinking impossible)
asar p %{_builddir}/%{_exename}/resources/app %{_builddir}/%{_exename}/app.asar --unpack '*' --unpack-dir '*'
rm -rf %{_builddir}/%{_exename}/resources/app

mv %{_builddir}/%{_exename}/resources/* %{_builddir}/%{_exename}

%if %{with openasar}
# Override Discord's app.asar with OpenAsar as it works with > Electron19
cp %{SOURCE4} %{_builddir}/%{_exename}/app.asar
%endif

#Create starter script.
echo "#!/bin/sh" > %{_builddir}/%{_name}
echo "ELECTRON_ENABLE_STACK_DUMPING=true exec electron %{_datadir}/%{_name}/app.asar \"\$@\"" >> %{_builddir}/%{_name}

%install
install -pDm 755 %{_builddir}/%{_name} %{buildroot}%{_bindir}/%{_name}

cd %{_builddir}/%{_exename}
install -Dm644 discord.png %{buildroot}%{_datadir}/icons/hicolor/256x256/apps/%{_name}.png
%if 0%{?_channel_suffix:1}
mv %{_name}%{?_channel_suffix:-%{_channel_suffix}}.desktop \
   %{_name}.desktop
%endif
desktop-file-install \
  --dir=%{buildroot}%{_datadir}/applications \
  --set-key=Exec \
  --set-value=%{_bindir}/%{_name} \
  %{_name}.desktop

install -Dm644 %{SOURCE2} %{buildroot}%{_datadir}/icons/hicolor/symbolic/apps/%{_name}-symbolic.svg
install -Dm644 %{SOURCE3} %{buildroot}%{_datadir}/metainfo/com.discordapp.Discord.appdata.xml

# install share
mkdir -p %{buildroot}%{_datadir}/%{_name}
shopt -s extglob
cp -r !(discord*|*.so) %{buildroot}%{_datadir}/%{_name}
shopt -u extglob

# fix missing icon in some environments
ln -sf %{_datadir}/icons/hicolor/256x256/apps/%{_name}.png %{buildroot}%{_datadir}/%{_name}

%fdupes %{buildroot}%{_prefix}

%files
%defattr(-,root,root)
%{_bindir}/%{_name}
%{_datadir}/applications/%{_name}.desktop
%{_datadir}/icons/hicolor/256x256/apps/%{_name}.png
%{_datadir}/icons/hicolor/symbolic/apps/%{_name}-symbolic.svg
%{_datadir}/metainfo/com.discordapp.Discord.appdata.xml
%{_datadir}/%{_name}

%changelog
openSUSE Build Service is sponsored by