#!/usr/bin/make -f

CORE = abaplint/packages/core
BUNDLE = abap-astgen/dist/abapgen.js

%:
	dh $@

# Upstream runs abapgen on Bun and ships a Bun standalone binary. Here tsc
# compiles @abaplint/core as upstream does, then esbuild bundles abapgen
# with it and the fast-xml-parser components into one CommonJS file.
# json5 and vscode-languageserver-types stay external and come from
# Debian. --keep-names: the JSON "type" field is an abaplint class name.
override_dh_auto_build:
	tsc -p debian/tsconfig.abaplint.json
	cd $(CORE) && node scripts/version.js
	esbuild abap-astgen/src/parse-abap.ts --bundle --platform=node --format=cjs \
		--keep-names --outfile=$(BUNDLE) --log-level=warning \
		--external:json5 --external:vscode-languageserver-types \
		--alias:@abaplint/core=./$(CORE) \
		--alias:fast-xml-parser=./fast-xml-parser/src/fxp.js \
		--alias:fast-xml-builder=./fast-xml-builder \
		--alias:path-expression-matcher=./path-expression-matcher \
		--alias:strnum=./strnum \
		--alias:anynum=./anynum \
		--alias:xml-naming=./xml-naming \
		--alias:is-unsafe=./is-unsafe \
		--alias:@nodable/entities=./nodable-entities/Entity

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	debian/tests/upstream/run debian/.tests $(BUNDLE)
endif

override_dh_auto_install:
	install -D -m 644 $(BUNDLE) debian/node-joernio-abapgen/usr/share/node-joernio-abapgen/abapgen.js
	install -D -m 755 debian/abapgen.sh debian/node-joernio-abapgen/usr/bin/abapgen

override_dh_auto_clean:
	rm -rf abap-astgen/dist $(CORE)/build debian/.tests
