# Template file for 'vscode'
pkgname=vscode
version=1.76.2
revision=1
_electronver=19.0.8
_npmver=8.6.0
hostmakedepends="pkg-config python3 nodejs yarn tar git ripgrep"
makedepends="libxkbfile-devel libsecret-devel libxml2-devel ncurses-devel electron19"
depends="libXtst ncurses nss dejavu-fonts-ttf xdg-utils ripgrep electron19"
short_desc="Microsoft Code for Linux"
maintainer="shizonic <realtiaz@gmail.com>, Alex Lohr <alex.lohr@logmein.com>"
license="MIT"
homepage="https://code.visualstudio.com/"
distfiles="https://github.com/microsoft/vscode/archive/refs/tags/${version}.tar.gz"
checksum=f68b35c96f85a30e5f69e5119a183113433c2a47c08a2d639329e4c01cf0d7f4
nocross=yes # x64 build does not cut it, it contains native code

if [ "$XBPS_TARGET_WORDSIZE" = "32" ]; then
	broken="FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory"
fi

# avoid downloading electron binaries for target platform; we use
# the system electron, and downloading does not work for all archs
export ELECTRON_SKIP_BINARY_DOWNLOAD=1

do_configure() {
	# Use yarn to install dependencies
	echo "" > build/npm/preinstall.js

	# redirect telemetry urls to 0.0.0.0
	# src: vscodium/undo_telemetry.sh
	_TELEMETRY_URLS="(dc\.services\.visualstudio\.com)|(vortex\.data\.microsoft\.com)|(mobile\.events\.data\.microsoft\.com)"
	_REPLACEMENT="s/$_TELEMETRY_URLS/0\.0\.0\.0/g"
	grep -rl --exclude-dir=.git -E $_TELEMETRY_URLS | xargs sed -i -E $_REPLACEMENT

	# set used electron version
	vsed -e "s/^target .*/target \"${_electronver}\"/" -i .yarnrc

	# @vscode/ripgrep attempts to install an own version of ripgrep and provides a path
	# we want to use our own version instead, so all we need is the fixed path (/bin/rg)
	# remove @vscode/ripgrep from all package.json files it is in:
	_PACKAGE_PATTERN="\"@vscode.ripgrep\":"
	grep -rl --exclude-dir=.git -E $_PACKAGE_PATTERN | xargs sed -i -E "/${_PACKAGE_PATTERN}/d"
	# replace path import with fixed path:
	_IMPORT_PATTERN="import.*from '@vscode.ripgrep';"
	_IMPORT_REPLACEMENT="s/${_IMPORT_PATTERN}/const rgPath = '\/bin\/rg';/"
	grep -rl --exclude-dir=.git -E "$_IMPORT_PATTERN" | xargs sed -i -E "$_IMPORT_REPLACEMENT"
}

do_build() {
	# Update to a more recent npm version to fix some issues
	npm install -g npm@${_npmver}

	# The default memory limit may be too low for current versions of node
	# to successfully build vscode.  This sets it to 4GB, but
	# change this number if it still doesn't work for your system.
	_mem_limit="--max_old_space_size=4095"

	export NODE_OPTIONS="${_mem_limit}"
	/usr/lib/node_modules/npm/bin/node-gyp-bin/node-gyp install \
	--target=$_electronver \
	--tarball=/usr/include/electron${_electronver%%.*}/node_headers.tar.gz

	# ignore nodejs version checks, we can not pass it as an argument to
	# yarn install because it doesn't inherit properly
	echo "ignore-engines true" >> .yarnrc

	yarn install --frozen-lockfile --arch=x64

	export CFLAGS="$CFLAGS -I/usr/include/node"
	yarn run gulp vscode-linux-x64-min
}

do_install() {
	vmkdir usr/lib/code-oss
	vcopy ../VSCode-linux-x64/resources usr/lib/code-oss
	vcopy ../VSCode-linux-x64/bin usr/lib/code-oss
	vmkdir usr/bin
	ln -sf /usr/lib/code-oss/bin/code-oss ${DESTDIR}/usr/bin/
	vmkdir usr/share/applications
	ln -sf /usr/lib/code-oss/resources/app/resources/linux/code.desktop ${DESTDIR}/usr/share/applications/code-oss.desktop
	vmkdir usr/share/pixmaps
	ln -sf /usr/lib/code-oss/resources/app/resources/linux/code.png ${DESTDIR}/usr/share/pixmaps/code-oss.png
	vsed \
	-e "s|ELECTRON=.*|ELECTRON=/usr/lib/electron${_electronver%%.*}/electron|g" \
	-e 's|"$CLI"|"$CLI" --app="${VSCODE_PATH}/resources/app"|g' \
	-i "$DESTDIR"/usr/lib/code-oss/bin/code-oss
	vlicense LICENSE.txt
	chmod -R o-w ${DESTDIR}/usr/lib/code-oss/resources/app/
}
