# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=powershell
pkgver=7.4.5
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="
	dotnet8-runtime
	libpsl-native
	"
makedepends="
	dotnet8-sdk
	git
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/PowerShell/PowerShell/archive/v$pkgver.tar.gz
	rollback-system-manage-automation-psversioninfo.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 ./*
	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 build-server shutdown
	dotnet publish --configuration Linux "src/powershell-unix/" \
		--output lib \
		--no-self-contained \
		--no-restore \
		--framework "net8.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
}

check() {
	msg "Checking $pkgname"
	cd "$builddir"/test/xUnit
	dotnet build-server shutdown
	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="
1fed4f7367560e26e84d0451e988938e8d4ce66868588ea3b935431eaeb944d3032247a8d68d657c1f4017952e18b5f43f54e7975dd53e61e21e4f5e17ab3989  powershell-7.4.5.tar.gz
bac5ac577fd8e6bb8ba83b5c537269b635619ecde2dfa3b31de754865aab8f4fd004bcb972f08f3218ecbcb7ef33c7fb8489370f7f0aaa0bae2e88e3ec736625  rollback-system-manage-automation-psversioninfo.patch
5a70efe247cdae8ed5c66702909137e0727cf5e25aca54983891fc17716a56081825f9a6e57c56f8fa8aa5615a159f81c68ca501d42e2ce085c073be633b3025  dependency-gatherer.targets
0e7550d8b702eeb9ebbd42100b32cbf7a0be676c5922cd6ee03ecdd3ba11d20d42a468f73ee428c6c92c8b7b18f9b022591a368191c2a358034ba4d7e78d2b84  fix-filesystem-test.patch
"
