# Contributor: Hoang Nguyen <folliekazetani@protonmail.com>
# Contributor: Fraser Waters <frassle@gmail.com>
maintainer="Hoang Nguyen <folliekazetani@protonmail.com>"
pkgname=pulumi
pkgver=3.170.0
pkgrel=0
pkgdesc="Infrastructure as Code SDK"
url="https://pulumi.com/"
# Tests show that pulumi's plugin system doesn't work on any other platforms
arch="x86_64 aarch64"
license="Apache-2.0"
makedepends="go"
checkdepends="tzdata"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	$pkgname-language-go:_go
	$pkgname-language-nodejs:_nodejs
	$pkgname-language-python:_python
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/pulumi/pulumi/archive/v$pkgver.tar.gz"
options="net" # download Go modules

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"
export PULUMI_SKIP_UPDATE_CHECK=true

build() {
	local _goldflags="-X github.com/pulumi/pulumi/sdk/v${pkgver%%.*}/go/common/version.Version=v$pkgver"
	mkdir -p "$builddir"/bin

	cd "$builddir"/sdk/go
	go generate ./...
	go build -C pulumi-language-go -v \
		-o "$builddir"/bin/pulumi-language-go \
		-ldflags "$_goldflags"

	for lang in python nodejs; do
		cd "$builddir"/sdk/$lang/cmd/pulumi-language-$lang
		go build -v \
			-o "$builddir"/bin/pulumi-language-$lang \
			-ldflags "$_goldflags"
	done

	cd "$builddir"/pkg
	go build -v \
		-o "$builddir"/bin/pulumi \
		-ldflags "$_goldflags" \
		./cmd/pulumi

	cd "$builddir"
	for shell in bash fish zsh; do
		./bin/pulumi gen-completion $shell > pulumi.$shell
	done
}

check() {
	cd "$builddir"/pkg

	# TestGenerateProgram/empty-list-property executes `go mod edit` command in the test directory
	export GOFLAGS="$GOFLAGS -modcacherw"

	# PULUMI_ACCEPT=true is required for TestProgressEvents test
	# PULUMI_DISABLE_AUTOMATIC_PLUGIN_ACQUISITION is set to work around GitHub API's rate limit (ref: https://github.com/pulumi/pulumi/issues/15900)
	# TestProjectNameOverrides, TestProjectNameDefaults, TestPulumiNewOrgTemplate tests depend on pulumi-language-yaml
	# TestPulumiNewSetsTemplateTag test requires PULUMI_ACCESS_TOKEN to be set for running `pulumi new` without prompting
	# TestAzureCloudManager, TestAzureKeyVault* tests require Azure CLI installed
	# TestGoModEdits hardcode pulumi/ as the root path to search for sdk/go.mod file.
	# shellcheck disable=2046
	PULUMI_ACCEPT=true \
		PULUMI_DISABLE_AUTOMATIC_PLUGIN_ACQUISITION=true \
		go test \
		-skip 'TestPulumiNewSetsTemplateTag/python|TestGoModEdits|TestProjectName*|TestAzure*|TestPulumiNewOrgTemplate' \
		$(go list ./... | grep -v -E 'github.com/pulumi/pulumi/pkg/v3/codegen/(dotnet|nodejs|python)')
}

package() {
	install -Dm755 bin/pulumi -t "$pkgdir"/usr/bin/

	install -Dm644 pulumi.bash \
		"$pkgdir"/usr/share/bash-completion/completions/pulumi
	install -Dm644 pulumi.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/pulumi.fish
	install -Dm644 pulumi.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_pulumi
}

_go() {
	pkgdesc="$pkgdesc (Go language provider)"

	install -Dm755 "$builddir"/bin/pulumi-language-go -t "$subpkgdir"/usr/bin/
}

_nodejs() {
	pkgdesc="$pkgdesc (NodeJS language provider)"
	depends="cmd:node"

	install -Dm755 "$builddir"/bin/pulumi-language-nodejs -t "$subpkgdir"/usr/bin/
	install -Dm755 "$builddir"/sdk/nodejs/dist/pulumi-analyzer-policy \
		-t "$subpkgdir"/usr/bin/
	install -Dm755 "$builddir"/sdk/nodejs/dist/pulumi-resource-pulumi-nodejs \
		-t "$subpkgdir"/usr/bin/
}

_python() {
	pkgdesc="$pkgdesc (Python language provider)"
	depends="python3"

	install -Dm755 "$builddir"/bin/pulumi-language-python -t "$subpkgdir"/usr/bin/
	install -Dm755 "$builddir"/sdk/python/cmd/pulumi-language-python-exec -t "$subpkgdir"/usr/bin/
	install -Dm755 "$builddir"/sdk/python/dist/pulumi-analyzer-policy-python \
		-t "$subpkgdir"/usr/bin/
	install -Dm755 "$builddir"/sdk/python/dist/pulumi-resource-pulumi-python \
		-t "$subpkgdir"/usr/bin/
}

sha512sums="
07883e8585e20eb4fd9697beb5731d2a75b09bf72de3e2be3787e8ade01ea8e674435a5bf885cf6bc37782dbd6d0457a1db598f4203cc6647291cabf427dce2a  pulumi-3.170.0.tar.gz
"
