1@echo off
2setlocal
3
4set _EXITCODE=0
5
6REM If no target is provided, default to test.
7if [%1]==[] goto test
8
9set _TARGETS=bin,bootstrap,dev,generate,test,testacc,testrace,vet
10set _EXTERNAL_TOOLS=github.com/mitchellh/gox,github.com/kardianos/govendor
11
12REM Run target.
13for %%a in (%_TARGETS%) do (if x%1==x%%a goto %%a)
14goto usage
15
16REM bin generates the releasable binaries for Vault
17:bin
18	call :generate
19	call .\scripts\windows\build.bat "%CD%"
20	goto :eof
21
22REM bootstrap downloads required build tools
23:bootstrap
24    for %%t in (%_EXTERNAL_TOOLS%) do (go get -u -v %%t)
25	goto :eof
26
27REM dev creates binaries for testing Vault locally. These are put
28REM into ./bin/ as well as %GOPATH%/bin
29:dev
30	call :generate
31	call .\scripts\windows\build.bat "%CD%" VAULT_DEV
32	goto :eof
33
34REM generate runs `go generate` to build the dynamically generated
35REM source files.
36:generate
37	for /F "usebackq" %%f in (`go list ./... ^| findstr /v vendor`) do @go generate %%f
38	goto :eof
39
40REM test runs the unit tests and vets the code.
41:test
42	call :testsetup
43	go test %_TEST% %TESTARGS% -timeout=30s -parallel=4
44	call :setMaxExitCode %ERRORLEVEL%
45	echo.
46	goto vet
47
48REM testacc runs acceptance tests.
49:testacc
50	call :testsetup
51	if x%_TEST% == x./... goto testacc_fail
52	if x%_TEST% == x.\... goto testacc_fail
53	set VAULT_ACC=1
54	go test %_TEST% -v %TESTARGS% -timeout 45m
55	exit /b %ERRORLEVEL%
56:testacc_fail
57	echo ERROR: Set %%TEST%% to a specific package.
58	exit /b 1
59
60REM testrace runs the race checker.
61:testrace
62	call :testsetup
63	go test -race %_TEST% %TESTARGS%
64	exit /b %ERRORLEVEL%
65
66REM testsetup calls `go generate` and defines the variables VAULT_ACC
67REM and _TEST. VAULT_ACC is always cleared. _TEST defaults to the value
68REM of the TEST environment variable, provided that TEST is defined,
69REM otherwise _TEST it is set to "./...".
70:testsetup
71	call :generate
72	set VAULT_ACC=
73	set _TEST=./...
74	if defined TEST set _TEST=%TEST%
75	goto :eof
76
77REM vet runs the Go source code static analysis tool `vet` to find
78REM any common errors.
79:vet
80	set _VETARGS=-asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr
81	if defined VETARGS set _VETARGS=%VETARGS%
82
83	go tool vet 2>nul
84	if %ERRORLEVEL% equ 3 go get golang.org/x/tools/cmd/vet
85
86	set _vetExitCode=0
87	set _VAULT_PKG_DIRS=%TEMP%\vault-pkg-dirs.txt
88
89	go list -f {{.Dir}} ./... | findstr /v vendor >"%_VAULT_PKG_DIRS%"
90	REM Skip the first row, which is the main vault package (.*github.com/hashicorp/vault$)
91	for /f "delims= skip=1" %%d in ("%_VAULT_PKG_DIRS%") do (
92		go tool vet %_VETARGS% "%%d"
93		if ERRORLEVEL 1 set _vetExitCode=1
94		call :setMaxExitCode %_vetExitCode%
95	)
96	del /f "%_VAULT_PKG_DIRS%" 2>NUL
97	if %_vetExitCode% equ 0 exit /b %_EXITCODE%
98	echo.
99	echo Vet found suspicious constructs. Please check the reported constructs
100	echo and fix them if necessary before submitting the code for reviewal.
101	exit /b %_EXITCODE%
102
103:setMaxExitCode
104	if %1 gtr %_EXITCODE% set _EXITCODE=%1
105	goto :eof
106
107:usage
108	echo usage: make [target]
109	echo.
110	echo target is in {%_TARGETS%}.
111	echo target defaults to test if none is provided.
112	exit /b 2
113	goto :eof
114