# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=powershell
pkgver=7.5.2
pkgrel=0
pkgdesc="A cross-platform automation and configuration tool/framework"
arch="x86_64 aarch64 armv7" # blocked by dotnet runtime
url="https://github.com/PowerShell/PowerShell"
license="MIT"
depends="
	dotnet9-runtime
	libpsl-native
	"
makedepends="
	dotnet9-sdk
	git
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/PowerShell/PowerShell/archive/v$pkgver.tar.gz
	add-missing-nuget-src.patch
	dependency-gatherer.targets
	fix-filesystem-test.patch
	"
builddir="$srcdir/PowerShell-$pkgver"
# if true, then within pipeline environment, in which case send logs there
# to be scooped
if [ -d "$APORTSDIR/logs" ]; then
	_logdir="$APORTSDIR"/logs
else
	_logdir="$srcdir"/logs
fi

# mono-flavored runtime does not support crossgen2
case $CARCH in
	armhf|s390x|ppc64le) _use_crossgen2=false;;
esac

# change nuget cache
export NUGET_PACKAGES="$srcdir"/nuget_cache

# secfixes:
#   7.3.10-r0:
#     - CVE-2023-36013

prepare() {
	default_prepare

	# build expects to be in git directory
	git init
	git config user.name "example"
	git config user.email "example@example.com"
	git add -f ./*
	git commit -m 'Initial'
	git tag -a "v$pkgver" -m "Initial"
	git remote add origin "$url"
	rm "global.json"
	mkdir -p "$_logdir"

	# Replicating build.psm1:Start-PSBuild()
	## Restore-PSPackage()
	dotnet restore --locked-mode -p:PublishReadyToRun=$_use_crossgen2 --use-current-runtime src/powershell-unix
	dotnet restore --locked-mode src/TypeCatalogGen
	dotnet restore --locked-mode src/ResGen
	dotnet restore --locked-mode src/Modules
	dotnet restore --locked-mode src/Microsoft.PowerShell.GlobalTool.Shim
}

build() {
	msg "Building $pkgname"

	## Start-ResGen(): Generate resource binding C# files
	(
		cd "$builddir"/src/ResGen
		dotnet run --no-restore
	)

	## Start-TypeGen(): Setup the build target to gather dependency information
	cp "$srcdir/dependency-gatherer.targets" \
		"src/Microsoft.PowerShell.SDK/obj/Microsoft.PowerShell.SDK.csproj.TypeCatalog.targets"

	dotnet msbuild \
		src/Microsoft.PowerShell.SDK \
		-t:_GetDependencies \
		-p:DesignTimeBuild=true \
		-p:_DependencyFile="$builddir"/src/TypeCatalogGen/powershell.inc \
		-nologo

	dotnet run \
		--no-restore \
		--project src/TypeCatalogGen \
		src/System.Management.Automation/CoreCLR/CorePsTypeCatalog.cs \
		"$builddir"/src/TypeCatalogGen/powershell.inc

	## Build powershell core
	dotnet publish --configuration Linux "src/powershell-unix/" \
		--output lib \
		--no-self-contained \
		--no-restore \
		--framework "net9.0" \
		--use-current-runtime \
		-p:PublishReadyToRun=$_use_crossgen2 \
		/bl:"$_logdir"/powershell-build.binlog \
		/consoleLoggerParameters:ShowTimestamp

	## Publish reference assemblies
	grep 'Microsoft.NETCore.App' "$builddir"/src/TypeCatalogGen/powershell.inc | sed 's/;//' | while read -r assembly; do
		install -Dm755 -t lib/ref "$assembly"
	done

	# powershell doesn't build all standard libraries,
	# emulate upstream build behavior
	# see https://github.com/PowerShell/PowerShell/issues/20143
	(
		cd "$NUGET_PACKAGES"
		_missing_libs="
			PackageManagement PowerShellGet Microsoft.PowerShell.Archive
			PSReadLine ThreadJob
		"
		for lib in $_missing_libs; do
			local pkg="$(echo $lib | tr '[:upper:]' '[:lower:]' )"
			cp -ar "$(find $pkg/* -maxdepth 0 | tail -n 1)" "$builddir"/lib/Modules/$lib
		done
	)
}

check() {
	msg "Checking $pkgname"
	cd "$builddir"/test/xUnit
	dotnet test \
		-c Release \
		--runtime "$(dotnet --info | awk '$1=="RID:"{print $2}')" \
		-p:PublishReadyToRun=$_use_crossgen2 \
		/bl:"$_logdir"/powershell-check.binlog \
		/consoleLoggerParameters:ShowTimestamp
}

package() {
	# directory creation
	install -dm 755 \
		"$pkgdir"/usr/lib \
		"$pkgdir"/usr/bin

	# libary copy
	cp -ar "$builddir"/lib "$pkgdir"/usr/lib/$pkgname

	# already provided by 'libpsl-native' aport
	rm -f "$pkgdir"/usr/lib/$pkgname/libpsl-native.so

	# does not build for linux-musl, thus points to libc.so rather than musl
	# see https://github.com/dotnet/runtime/issues/63187
	rm -f "$pkgdir"/usr/lib/$pkgname/libSystem.IO.Ports.Native.so

	# binary link
	ln -s "/usr/lib/$pkgname/pwsh" "$pkgdir"/usr/bin/pwsh
}

sha512sums="
f4df096f5078a78c055da2ec27d8b5f7e4321fdca5ba1de73a9ca9ef89a8e9063a0fbb3e78cdbf8be39c4a8cbb06bac77c52e8be076d59e112b8b176c4bddfe0  powershell-7.5.2.tar.gz
b03ea346d7f4e57b7c2dd8d797ed24a142f511f92ccd876ad9a4b581c8786c404bffef8c7e85ec00ef0e3aff3d6faf96972461e895451de127939771b32e391b  add-missing-nuget-src.patch
5a70efe247cdae8ed5c66702909137e0727cf5e25aca54983891fc17716a56081825f9a6e57c56f8fa8aa5615a159f81c68ca501d42e2ce085c073be633b3025  dependency-gatherer.targets
0e7550d8b702eeb9ebbd42100b32cbf7a0be676c5922cd6ee03ecdd3ba11d20d42a468f73ee428c6c92c8b7b18f9b022591a368191c2a358034ba4d7e78d2b84  fix-filesystem-test.patch
"
