File bitwarden.spec of Package bitwarden

# spec file for package bitwarden
#
# Copyright (c) 2021 Bruno Pitrus.
# Based on the Arch Linux PKGBUILD (c) 2017 prozum
#

%global flavor @BUILD_FLAVOR@%{nil}
%if "%{flavor}" == "cli"
%define psuffix -cli
%define friendly_flavor console
%endif
%if "%{flavor}" == ""
%define psuffix %{nil}
%define friendly_flavor GUI
%endif

Name:		bitwarden%{psuffix}
Version:	2022.6.0
Release:	0
Summary:	A secure and free password manager for all of your devices
License:	GPL-3.0-only
URL:		https://github.com/bitwarden/clients


%define nodejs_version 16

Source0:	https://github.com/bitwarden/clients/archive/web-v%{version}.tar.gz

Source2:	bitwarden.sh
Source3:	bitwarden.desktop


%include %_sourcedir/node_modules.spec.inc

#PATCH-FIX-OPENSUSE
Patch0:	remove-unnecessary-deps.patch
#PATCH-FIX-OPENSUSE
Patch1:	fix-desktop-file.patch
#PATCH-FIX-OPENSUSE
Patch3: do-not-install-font-privately.patch

BuildRequires: fdupes
BuildRequires: local-npm-registry
BuildRequires: jq
BuildRequires: moreutils

#tools we use explicitly
%if 0%{?fedora_version}
BuildRequires: npm nodejs-devel
%define _ttfontsdir %{_datadir}/fonts/truetype
%else
BuildRequires: npm%{nodejs_version} nodejs%{nodejs_version}-devel
%endif
#Tools used by npm
BuildRequires: gcc-c++ pkgconfig(libsecret-1) 


%if "%{flavor}" == "cli"
BuildRequires: nodejs-packaging
%endif


%if "%{flavor}" == ""



BuildRequires: nodejs-electron-devel


BuildRequires: fdupes fontpackages-devel hicolor-icon-theme sed


%define ELECTRON_MAJOR_VERSION %(sh -c '<%{_libdir}/electron/version cut -d. -f1')
%define ELECTRON_MAJOR_VERSION_PLUS_1 %(sh -c 'echo $((%{ELECTRON_MAJOR_VERSION} + 1))')
Requires: ((nodejs-electron%{?_isa} >= %ELECTRON_MAJOR_VERSION) with (nodejs-electron%{?_isa} < %ELECTRON_MAJOR_VERSION_PLUS_1))
Requires: google-opensans-fonts

%endif

%description
Bitwarden is a free and open-source password management service that stores sensitive information such as website credentials in an encrypted vault.  Bitwarden offers a cloud-hosted service as well as the ability to deploy the solution on-premises. This package provides the %friendly_flavor client.

%prep
rm -rf NodeCache NPM
mkdir -v NodeCache
%setup -n clients-web-v%{version}

#Remove nonfree code
rm -rf bitwarden_license

#Remove bundled open sans
cp -v /dev/null libs/angular/src/scss/webfonts.css
rm -rvf libs/angular/src/scss/webfonts

#Patches from Arch
%patch0 -p1
%patch1 -p1


#Do not install font privately
%patch3 -p1
mv -v libs/angular/src/scss/bwicons/fonts/bwi-font.woff %{_builddir}
rm -rvf libs/angular/src/scss/bwicons/fonts

#Patch dependency which is download from github to go through registry
jq -c '.dependencies.duo_web_sdk="2.7.0"' < package.json | sponge package.json

#Old package lock causes npm to fail with a confusing error message
#rm -v package-lock.json libs/{angular,common,electron}/package-lock.json

mkdir -v %{_builddir}/NPM
cp -lv %{_sourcedir}/*.tgz %{_builddir}/NPM
cp -lv %{_sourcedir}/package-lock.json %{_builddir}/NPM

#Patch broken dependencies which specify "latest" version of their sub-dependencies
cd %{_builddir}/NPM

tar -zxvf @nodert-win10-rs4-windows.security.credentials.ui-0.4.4.tgz
rm -v @nodert-win10-rs4-windows.security.credentials.ui-0.4.4.tgz
cd package
jq -cj '.dependencies.nan="*"' package.json | sponge package.json
cd ..
tar -zcvf @nodert-win10-rs4-windows.security.credentials.ui-0.4.4.tgz package
rm -rvf package

#Remove prebuild-install which is incorrectly specified as runtime dependency and brings a lot of garbage
#Compare https://github.com/microsoft/vscode/issues/143395
tar -zxvf keytar-7.9.0.tgz
rm -v keytar-7.9.0.tgz
cd package
jq -cj 'del(.dependencies."prebuild-install")' package.json | sponge package.json
cd ..
tar -zcvf keytar-7.8.0.tgz package
rm -rvf package

tar -zxvf forcefocus-1.1.0.tgz
rm -v forcefocus-1.1.0.tgz
cd package
jq -cj 'del(.dependencies."prebuild-install")' package.json | sponge package.json
cd ..
tar -zcvf forcefocus-1.1.0.tgz package
rm -rvf package

%build
%if 0%{?fedora_version}
NODE_HEADERS=/usr/include/node
%else
NODE_HEADERS=/usr/include/node%{nodejs_version}
%endif
export NODE_VERSION=%{nodejs_version}
export ELECTRON_SKIP_BINARY_DOWNLOAD=1
%set_build_flags
export npm_config_cache=%{_builddir}/NodeCache

cd %{_builddir}/clients-web-v%{version}
rm -v package-lock.json

export CPATH=${NODE_HEADERS}
local-npm-registry %{_builddir}/NPM install --nodedir=${NODE_HEADERS}


%if "%{flavor}" == "cli"
cd apps/cli

npm run build:prod
%endif

%if "%{flavor}" == ""


cd apps/desktop

npm run build
npm run clean:dist

export CPATH=/usr/include/electron
cd build
local-npm-registry %{_builddir}/NPM install --nodedir=/usr/include/electron
%endif

%install
cd %{_builddir}/clients-web-v%{version}/apps/desktop
mkdir -pv %{buildroot}%{_libdir}
cp -plvr build %{buildroot}%{_libdir}/%{name}
for i in 16 32 64 128 256 512 1024
do
install -pvDm644 resources/icons/${i}x${i}.png "%{buildroot}%{_datadir}/icons/hicolor/${i}x${i}/apps/%{name}.png"
done
install -pvDm755 "%{_sourcedir}/%{name}.sh" "%{buildroot}%{_bindir}/bitwarden-desktop"
sed -i "s,XXXLIBDIRXXX,%{_libdir},g" "%{buildroot}%{_bindir}/bitwarden-desktop"
install -pvDm644 "%{_sourcedir}"/%{name}.desktop -t "%{buildroot}%{_datadir}"/applications
install -pvDm644 %{_builddir}/bwi-font.woff -t%{buildroot}%{_ttfontsdir}

%fdupes %{buildroot}%{_prefix}

#Remove development garbage
cd %{buildroot}%{_libdir}/%{name}
#JS debug symbols (unusable)
find -name '*.map' -type f -print -delete
#Source code
find -name '*.c' -type f -print -delete
find -name '*.cpp' -type f -print -delete
find -name '*.h' -type f -print -delete
find -name '*.gyp' -type f -print -delete
find -name '*.gypi' -type f -print -delete
find -name '*.ts' -type f -print -delete
find -name src -print0 |xargs -r0 -- rm -rvf --
find -name Makefile -type f -print -delete
find -name 'Pipfile*' -type f -print -delete
find -name '*.patch' -type f -print -delete
#Temporary build files
find -name '.deps' -print0 |xargs -r0 -- rm -rvf --
find -name 'obj.target' -print0 |xargs -r0 -- rm -rvf --
find -name '*package-lock.json' -type f -print -delete
find -name '*.mk' -type f -print -delete
find -name '*.Makefile' -type f -print -delete

#Documentation
find -name '*.md' -type f -print -delete
find -name doc -print0 |xargs -r0 -- rm -rvf --
find -name test -print0 |xargs -r0 -- rm -rvf --
#Compile-time-only dependencies
find -name nan -print0 |xargs -r0 -- rm -rvf --
find -name node-addon-api -print0 |xargs -r0 -- rm -rvf --
#Other trash
find -name '*.yml' -type f -print -delete
find -name '.npmignore' -type f -print -delete


#Fix file mode
find . -type f -exec chmod 644 {} \;
find . -name '*.node' -exec chmod 755 {} \;

%if 0%{?suse_version}
%reconfigure_fonts_scriptlets
%endif

%files
%defattr(-,root,root)
%{_bindir}/bitwarden-desktop
%{_libdir}/%{name}
%{_datadir}/applications/%{name}.desktop
%{_datadir}/icons/hicolor/1024x1024
%{_datadir}/icons/hicolor/128x128/apps/bitwarden.png
%{_datadir}/icons/hicolor/16x16/apps/bitwarden.png
%{_datadir}/icons/hicolor/256x256/apps/bitwarden.png
%{_datadir}/icons/hicolor/32x32/apps/bitwarden.png
%{_datadir}/icons/hicolor/512x512/apps/bitwarden.png
%{_datadir}/icons/hicolor/64x64/apps/bitwarden.png
%{_ttfontsdir}/

%changelog
openSUSE Build Service is sponsored by