File code.spec of Package code

# spec file for package code
# Copyright (c) 2022 Bruno Pitrus.
# Based on the Arch Linux PKGBUILD (c) Filipe Laíns, Michael Hansen, Francisco Magalhães

%global flavor @BUILD_FLAVOR@%{nil}

# Do not check for npm requires or provides in app.asar
%if "%{flavor}" == ""
%global __provides_exclude ^npm(.*)|^nodejs(.*)
%global __requires_exclude ^npm(.*)|^nodejs(.*)|^/usr/lib

%if "%{flavor}" == "languageservers"
%global __requires_exclude ^npm(.*)

%global onig_req_version 1.7.0

%if "%{flavor}" == ""
Name:		code
License:	 Apache-2.0 and (Apache-2.0 or MPL-2.0) and BSD-3-Clause and CC-BY-4.0 and ISC and MIT and MPL-2.0 and Unicode-DFS-2016 and W3C-20150513
Summary:	Visual Studio Code. Code editing. Redefined.
Group:      Development/Tools/IDE
%if "%{flavor}" == "languageservers"
Name: vscode-languageservers
License: MIT
Summary: Language servers for CSS, JSON and Markdown
Group:      Development/Tools/Other
Version:	1.90.0
Release:	0

%if "%{flavor}" == ""
#x86 electron requires SSE2
%ifarch %ix86
ExclusiveArch:  i586 i686
BuildArch:      i686
%{expand:%%global optflags %(echo "%optflags") -march=pentium4 -mtune=generic}

%if "%{flavor}" == "languageservers"
BuildArch: noarch

#Needed for devtools jsmaps to work
%define git_commit 89de5a8d4d6205e5b11647eb6a74844ca23d2573

#created by
Source1: vendor.tar.zst
Source3: code-oss.js

# openSUSE-specific fixes
Patch1:	product_json.patch
Patch3: remove-git.patch
Patch4: remove-marketplace-extensions.patch
Patch5: do-not-download-electron.patch
Patch6: no-asar.patch
Patch7: install-font-systemwide.patch
Patch13: product.json-reproducible-date.patch
%ifarch %ix86 %arm
Patch15: do-not-gulp-all-memory.patch
Source15: do-not-gulp-all-memory.patch
Patch16: do-not-check-for-yarn.patch
#Remove prebuild-install which is incorrectly specified as runtime dependency and brings a lot of garbage
Patch17: Remove-build-time-dependencies.patch
Patch18: vscode-l10n-dev-use-tree-sitter.patch
Patch20: no-comments-or-sourcemaps.patch
Patch21: webpack-no-source-maps.patch

# patches to use system libs
Patch1005: ripgrep.patch
Patch1006: Use-system-spdlog.patch
Patch1007: sqlite-remove-gcc-symbol-versions.patch
Patch1008: system-esbuild.patch

# patches fixing interaction with third-party software
Patch2000: Fix-restart-opening-electron-default-app.patch
Patch2009: fix-exe-path.patch
Patch2012: webpack-openssl-3-0.patch
Patch2013: extensions-promise-rejected.patch
Patch2014: remove-deprecated-AccessorSignatures.patch
Patch2015: nan_callbacks_12_inl-node21.patch

# patches that should be submitted upstream verbatim or near-verbatim
Patch3001: asar-deterministic.patch
Patch3002: electron29-nan-SetAccessor.patch

# openSUSE added features that upstream does not want

# patches to remove upstream hostility (DRM etc.)
Patch5000: esbuild_version.patch
Patch5001: extensions-esbuild_version.patch
Patch5014: allow-proposed-extension-apis.patch
Patch5016: extensions-disable-mangler.patch

#tools we use explicitly
%if "%{flavor}" == ""
BuildRequires: increase-file-max
%if 0%{?fedora_version}
BuildRequires: yarnpkg
%define _ttfontsdir %{_datadir}/fonts/truetype
BuildRequires: yarn
BuildRequires: fontpackages-devel ImageMagick jq sed
BuildRequires: nodejs-electron-devel >= 27
#Tools used by yarn
BuildRequires: pkgconfig(libsecret-1) pkgconfig(sqlite3) pkgconfig(x11) pkgconfig(xkbfile) spdlog-devel
BuildRequires: pkgconfig(krb5)
# there are breaking API changes between esbuild 0.16 and 0.17
BuildRequires: esbuild >= 0.17

# for tree sitter
BuildRequires: nodejs-devel

BuildRequires: nodejs-vscode-oniguruma-devel = %onig_req_version
Requires:      nodejs-vscode-oniguruma = %onig_req_version
Requires:      nodejs-electron%{_isa} >= 27
Requires: /usr/bin/rg


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

Enhances: kate-plugins

BuildRequires: fdupes
BuildRequires: nodejs-packaging
%if 0%{?fedora} >= 37
BuildRequires: nodejs-npm
BuildRequires: npm

BuildRequires: zstd

%if "%{flavor}" == ""
Visual Studio Code is an IDE developed by Microsoft that combines the simplicity of a code editor with what developers need for the core edit-build-debug cycle.
This is an open-source release of Code built from the official code-oss repository.

%if "%{flavor}" == "languageservers"
These are the standalone versions of the language servers included in Visual Studio Code.
They are usable in any editor implementing the LSP client protocol, such as Kate.
This package includes the CSS, JSON and Markdown servers.

%package -n vscode-html-languageserver
Summary: Language server for HTML
Group:      Development/Tools/Other
Requires: npm(typescript)
Enhances: kate-plugins

%description -n vscode-html-languageserver
This is a standalone version of the HTML language server included in Visual Studio Code.
It is usable in any editor implementing the LSP client protocol, such as Kate.
ulimit -Ha

%autosetup -p1 -n vscode-%{version} -a 1

rm -v node_modules/@vscode/ripgrep/lib/{download,postinstall}.js

# see remove-build-time-dependencies.patch and ripgrep.patch
rm -rvf node_modules/@vscode/sqlite3/node_modules/{tar,minipass}
rm -rvf node_modules/@vscode/ripgrep/node_modules/{agent-base,https-proxy-agent,debug,yauzl}
rm -rvf node_modules/@vscode/windows-mutex/{LICENSE,node_modules/bindings}

#fails to build, only used in @vscode/proxy-ageng and guarded by `process.platform === 'win32'`
rm -rvf {,remote/}node_modules/@vscode/windows-ca-certs

%if "%{flavor}" == ""
#Sanity check that we've declared the correct version in header
test $(jq -cj '.version' node_modules/vscode-oniguruma/package.json) = %{onig_req_version}

find -name vscode-oniguruma -exec sh -c 'rm -rf {} && ln -sv %{nodejs_sitelib}/vscode-oniguruma {}' \; ||true

sed -i 's#XXXLIBEXECDIRXXX#%{_libexecdir}#g' src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts src/vs/code/node/cli.ts
sed -i 's#XXXLIBDIRXXX#%{_libdir}#g' resources/linux/bin/

%if "%{flavor}" == ""
#Remove postinstall script
jq -cj 'del(.scripts.postinstall)' package.json > tmp
mv -v tmp package.json

# Install font systemwide
mv -v src/vs/base/browser/ui/codicons/codicon/codicon.ttf %{_builddir}/
mv -v extensions/theme-seti/icons/seti.woff %{_builddir}/
rm -v extensions/simple-browser/node_modules/vscode-codicons/dist/codicon.ttf

#Set up yarn
sed -i 's/^target .*/target "'%{SYSTEM_ELECTRON_VERSION}'"/' .yarnrc
echo 'yarn-offline-mirror "%{_builddir}/vendor"' >> .yarnrc
echo 'nodedir %{_includedir}/electron' >> .yarnrc
echo 'sqlite /usr' >> .yarnrc

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

mkdir %{_builddir}/path
#some scripts have #1/usr/bin/env python
ln -sv %{_bindir}/python3 %{_builddir}/path/python



#work around bsc#1216691
export SOURCE_DATE_EPOCH="$(stat --printf=%Y LICENSE.txt)"

export CFLAGS="%{optflags} -fpic -fno-semantic-interposition -fvisibility=hidden"
export CXXFLAGS="%{optflags} -fpic -fno-semantic-interposition -fvisibility=hidden"
export LDFLAGS="%{?build_ldflags}"
export MAKEFLAGS="%{_smp_mflags}"
export AR=gcc-ar
export NM=gcc-nm
export RANLIB=gcc-ranlib
%if "%{flavor}" == ""
export YARN_CACHE_FOLDER=%{_builddir}/Yarn
export ESBUILD_BINARY_PATH=/usr/bin/esbuild
#see  build/lib/getVersion.js
export BUILD_SOURCEVERSION=%{git_commit}


cd %{_builddir}/vscode-%{version}

# Build native deps using electron instead of node because the system node may be too new
find -name node_modules -print0 > modules_list
<modules_list sed -z '/node_modules\/.*\/node_modules/d' | sed -z 's/node_modules$//' | PATH="%{_builddir}/path:$PATH" xargs -0  -I{} -n1 -t -- sh -c ' cd {} && %electron_rebuild --sqlite=/usr || exit 255'

#spurious makefiles remaining, should not get published
rm -rvf node_modules/node-pty/node-addon-api

#This module is windows only, an empty DLL is produced which fails ‰check
rm -rvf node_modules/@vscode/deviceid/build

# see vscode-l10n-dev-use-tree-sitter.patch
cp -prvT {build/,}node_modules/tree-sitter
cp -prvT {build/,}node_modules/tree-sitter-typescript

# Rebuild this for node, this module does not work in electron so we cannot run gulp in electron
pushd node_modules/tree-sitter
rm -rf build
%if 0%{?suse_version}
NODEJS_DEFAULT_VER=$(echo %nodejs_version|sed 's/\..*//')
CFLAGS="${CFLAGS} -I%{_includedir}/node${NODEJS_DEFAULT_VER}" \
CXXFLAGS="${CXXFLAGS} -I%{_includedir}/node${NODEJS_DEFAULT_VER}" \
npm rebuild  --verbose --foreground-scripts \

pushd node_modules/tree-sitter-typescript
rm -rf build
CFLAGS="${CFLAGS} -I%{_includedir}/node${NODEJS_DEFAULT_VER}" \
CXXFLAGS="${CXXFLAGS} -I%{_includedir}/node${NODEJS_DEFAULT_VER}" \
npm rebuild  --verbose --foreground-scripts \


#Despite the task name, this is actually arch independent.
yarn gulp vscode-linux-x64-min
yarn gulp vscode-linux-x64-prepare-rpm

#Prepare multiple size icons
cd resources/win32
[ $(identify code.ico | wc -l) = 4 ] #Sanity check
convert code.ico -strip extracted.png
identify extracted-0.png | grep -F 16x16
identify extracted-1.png | grep -F 32x32
identify extracted-2.png | grep -F 128x128
identify extracted-3.png | grep -F 256x256

identify code_70x70.png | grep -F 70x70
identify code_150x150.png | grep -F 150x150

cd ../linux
identify code.png | grep -F 1024x1024


%if "%{flavor}" == "languageservers"
  for i in 'css' 'html' 'json' 'markdown'; do
    pushd extensions/$i-language-features/server
    npm rebuild  --verbose --foreground-scripts

  for i in 'css' 'html' 'json' 'markdown'; do
    pushd extensions/$i-language-features/server
    #ignore errors
    PATH="%{_builddir}/vscode-%{version}/node_modules/.bin:$PATH" tsc || true



%if "%{flavor}" == ""
cp -alr .build/linux/rpm/x86_64/rpmbuild/BUILD/usr %{buildroot}

#Native code does not go in /usr/share
mkdir -pv %{buildroot}%{_libdir}
mv -v %{buildroot}%{_datadir}/code %{buildroot}%{_libdir}
sed -i 's[/usr/share/code/code[code[g' %{buildroot}%{_datadir}/applications/*.desktop
chmod 644 %{buildroot}%{_datadir}/applications/*.desktop

install -pvDm644 %{_builddir}/codicon.ttf -t%{buildroot}%{_ttfontsdir}
install -pvDm644 %{_builddir}/seti.woff -t%{buildroot}%{_ttfontsdir}

install -pvDm755 .build/linux/rpm/x86_64/rpmbuild/BUILD/usr/share/code/bin/code -t %{buildroot}%{_bindir}
install -pvDm755 %{_sourcedir}/code-oss.js -t %{buildroot}%{_libexecdir}

sed -i 's[XXXLIBDIRXXX[%{_libdir}[g' %{buildroot}%{_bindir}/code %{buildroot}%{_libexecdir}/code-oss.js

cd %{buildroot}%{_libdir}/code
rm -rvf bin
rm -rvf resources/completions

#HACK: Sort a json that gets generated non-deterministically
jq -cjS . out/nls.metadata.json >tmp
mv -v tmp out/nls.metadata.json

#Remove development garbage
find -name '*~' -type f -delete
find -name '' -print0 |xargs -r0 -- rm -rvf
rm -rvf 'node_modules.asar.unpacked/@vscode/ripgrep/bin'
rm -rvf 'node_modules.asar.unpacked/@vscode/sqlite3/tools'

find . -type d -empty -print -delete

#link wasm module which is compiled elsewhere
rm -rvf 'node_modules.asar.unpacked/vscode-oniguruma'
ln -sv %{nodejs_sitelib}/vscode-oniguruma node_modules.asar.unpacked/

#Fix file mode
chmod -v 644 out/vs/workbench/contrib/terminal/browser/media/*.sh #They are snippets, not programs, and don't have a shebang
chmod -R og-w .
cd node_modules.asar.unpacked
find . -type f -exec chmod 644 {} \;
find . -name '*.node' -exec chmod -v 755 {} \; #for RPM dependency resolution

%fdupes %{buildroot}%{_libdir}

#Install icons
rm -frv %{buildroot}%{_datadir}/pixmaps
mkdir -pv %{buildroot}%{_datadir}/icons/hicolor/1024x1024/apps
mv -v %{buildroot}%{_libdir}/code/resources/linux/code.png %{buildroot}%{_datadir}/icons/hicolor/1024x1024/apps/code-oss.png
ln -srvf %{buildroot}%{_datadir}/icons/hicolor/1024x1024/apps/code-oss.png %{buildroot}%{_libdir}/code/resources/linux/code.png 

cd %{_builddir}/vscode-%{version}/resources/win32
install -pvDm644 code_70x70.png %{buildroot}%{_datadir}/icons/hicolor/70x70/apps/code-oss.png
install -pvDm644 code_150x150.png %{buildroot}%{_datadir}/icons/hicolor/150x150/apps/code-oss.png
install -pvDm644 extracted-0.png %{buildroot}%{_datadir}/icons/hicolor/16x16/apps/code-oss.png
install -pvDm644 extracted-1.png %{buildroot}%{_datadir}/icons/hicolor/32x32/apps/code-oss.png
install -pvDm644 extracted-2.png %{buildroot}%{_datadir}/icons/hicolor/128x128/apps/code-oss.png
install -pvDm644 extracted-3.png %{buildroot}%{_datadir}/icons/hicolor/256x256/apps/code-oss.png

%if 0%{?suse_version}



%if "%{flavor}" == "languageservers"
mkdir -pv %{buildroot}%{_bindir}
for i in 'css' 'html' 'json' 'markdown' ; do
      printf '%%s\n' '#!/usr/bin/node' "require('%{nodejs_sitelib}/vscode-${i}-languageserver')" >%{buildroot}%{_bindir}/vscode-${i}-languageserver
 pushd extensions/$i-language-features/server
 rm -rf node_modules/@types
 mkdir -pv %{buildroot}%{nodejs_sitelib}/vscode-${i}-languageserver
 cp -alrv node_modules out package.json -t %{buildroot}%{nodejs_sitelib}/vscode-${i}-languageserver
mkdir -pv "%buildroot/%_docdir/%name"
pushd extensions/json-language-features/server
cp -lvT "%buildroot/%_docdir/%name"/
pushd extensions/markdown-language-features/server
cp -lvT "%buildroot/%_docdir/%name"/

for i in 'css' 'html' 'json' 'markdown' ; do
sed -i '1s/env //' %{buildroot}%{nodejs_sitelib}/vscode-${i}-languageserver/node_modules/vscode-languageserver/bin/installServerIntoExtension
sed -i '1s/env //' %{buildroot}%{nodejs_sitelib}/vscode-markdown-languageserver/node_modules/he/bin/he

chmod -v 755 %{buildroot}%{_bindir}/*

pushd %{buildroot}%{nodejs_sitelib}
find -name '*.md' -type f -print -delete
find -name '*.map' -type f -print -delete
find -name '*.ts' -type f -print -delete
find -name tsconfig.json -type f -print -delete
find -name '.yarn*' -type f -print -delete
find -name .github -print0 |xargs -r0 -- rm -rvf --
find -name '.lsifrc*' -type f -print -delete
find -name .editorconfig -type f -print -delete
find -name '.eslint*' -type f -print -delete
find -name '.prettier*' -type f -print -delete

find . -type d -empty -print -delete

mkdir -pv %{buildroot}%{nodejs_sitelib}/vscode-html-languageserver/lib
cp -lv  extensions/html-language-features/server/lib/*.d.ts -t %{buildroot}%{nodejs_sitelib}/vscode-html-languageserver/lib

%fdupes %{buildroot}%{nodejs_sitelib}

%if "%{flavor}" == ""
%if "%{flavor}" == "languageservers"
%dir %{nodejs_sitelib}
%files -n vscode-html-languageserver
%dir %{nodejs_sitelib}

openSUSE Build Service is sponsored by