# File generated by cdo from CONTRIBUTING.md; DO NOT EDIT.

SHELL=bash
.SHELLFLAGS=-e -o pipefail -c

.PHONY: __help__
__help__:
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Targets:'
	@echo '  all      Run all'
	@echo '  build    Build custom k6 with extension'
	@echo '  clean    Clean the working directory'
	@echo '  cli      Build the k6-web-dashboard CLI'
	@echo '  coverage View the test coverage report'
	@echo '  doc      Update documentation'
	@echo '  exif     Update image metadata'
	@echo '  format   Format the go source codes'
	@echo '  gen      Generate code'
	@echo '  lint     Run the linter'
	@echo '  makefile Generate the Makefile'
	@echo '  replay   Replay test from recorded JSON file'
	@echo '  run      Run test script'
	@echo '  security Run security and vulnerability checks'
	@echo '  test     Run the tests'
	@echo '  testdata Record test results for testing'

# Run all
.PHONY: all
all: clean lint security test build cli doc makefile

# Build custom k6 with extension
.PHONY: build
build: 
	@(\
		xk6 build --with github.com/grafana/xk6-dashboard=.;\
	)

# Clean the working directory
.PHONY: clean
clean: 
	@(\
		rm -rf ./k6 ./coverage.txt ./build ./dashboard/assets/node_modules ./bun.lockb;\
	)

# Build the k6-web-dashboard CLI
.PHONY: cli
cli: 
	@(\
		goreleaser build --snapshot --clean --single-target;\
	)

# View the test coverage report
.PHONY: coverage
coverage: test
	@(\
		go tool cover -html=coverage.txt;\
	)

# Update documentation
.PHONY: doc
doc: 
	@(\
		mdcode update;\
		# See tools/docsme/main.txt;\
		# go run -tags docsme ./tools/docsme -r cli -o cmd/k6-web-dashboard/README.md;\
	)

# Update image metadata
.PHONY: exif
exif: 
	@(\
		exiftool -all= -overwrite_original -ext png screenshot;\
		exiftool -ext png -overwrite_original -XMP:Subject="k6 dashboard xk6" -Title="k6 dashboard screenshot" -Description="Screenshot of xk6-dashboard extension that enables creating web based metrics dashboard for k6." -Author="Raintank, Inc. dba Grafana Labs" screenshot;\
		exiftool -all= -overwrite_original -ext png .github;\
		exiftool -ext png -overwrite_original -XMP:Subject+="k6 dashboard xk6" -Title="k6 dashboard screenshot" -Description="Screenshot of xk6-dashboard extension that enables creating web based metrics dashboard for k6." -Author="Raintank, Inc. dba Grafana Labs" .github;\
		exiftool -all= -overwrite_original -ext pdf screenshot;\
		exiftool -ext pdf -overwrite_original -Subject="k6 dashboard report" -Title="k6 dashboard report" -Description="Example report of xk6-dashboard extension that enables creating web based metrics dashboard for k6." -Author="Raintank, Inc. dba Grafana Labs" screenshot;\
	)

# Format the go source codes
.PHONY: format
format: 
	@(\
		go fmt ./...;\
	)

# Generate code
.PHONY: gen
gen: 
	@(\
		go generate ./...;\
		yarn --silent --cwd dashboard/assets install;\
		yarn --silent --cwd dashboard/assets build;\
	)

# Run the linter
.PHONY: lint
lint: 
	@(\
		golangci-lint run ./...;\
	)

# Generate the Makefile
.PHONY: makefile
makefile: 
	@(\
		cdo --makefile Makefile;\
	)

# Replay test from recorded JSON file
.PHONY: replay
replay: 
	@(\
		SLUG=$${1//\//-};\
		RECORD="build/$${SLUG}-record.ndjson.gz";\
		go run ./cmd/k6-web-dashboard replay $$RECORD;\
	)

# Run test script
.PHONY: run
run: 
	@(\
		SLUG=$${1//\//-};\
		REPORT="build/$${SLUG}-report.html";\
		RECORD="build/$${SLUG}-record.ndjson.gz";\
		RESULT="build/$${SLUG}-result.json.gz";\
		xk6 run -- $${1:-} \;\
		--quiet --no-summary --no-usage-report \;\
		--out "dashboard=export=$${REPORT}&record=$${RECORD}" \;\
		--out json="$${RESULT}";\
	)

# Run security and vulnerability checks
.PHONY: security
security: 
	@(\
		gosec ./...;\
		govulncheck ./...;\
	)

# Run the tests
.PHONY: test
test: 
	@(\
		go test -count 1 -p 4 -race -coverprofile=coverage.txt -timeout 60s ./...;\
	)

# Record test results for testing
.PHONY: testdata
testdata: 
	@(\
		JSON=dashboard/testdata/result.json;\
		JSON_GZ="$${JSON}.gz";\
		NDJSON=dashboard/testdata/result.ndjson;\
		NDJSON_GZ="$${NDJSON}.gz";\
		xk6 run -- scripts/test.js \;\
		--quiet --no-summary --no-usage-report \;\
		--out json=$$JSON --out json=$$JSON_GZ \;\
		--out "dashboard=port=-1&period=2s&record=$${NDJSON}" \;\
		--out "dashboard=port=-1&period=2s&record=$${NDJSON_GZ}" \;\
	)

