/
Taskfile.yml
259 lines (255 loc) · 9.16 KB
/
Taskfile.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# SPDX-FileCopyrightText: Copyright 2024 Prasad Tengse
# SPDX-License-Identifier: MIT
#
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: "3"
vars:
# Go coverage data directory is root Taskfile's directory + '.gocover'.
GO_COVER_DIR: '{{ joinPath .ROOT_DIR ".gocover" }}'
tasks:
# -----------------------------------------------------------------
# Default Task. Shows List of available tasks.
#
# This intentionally lacks a desc field to hide it from help output.
# -----------------------------------------------------------------
default:
cmds:
- cmd: task --list
silent: true
# -----------------------------------------------------------------
# Creates a directory if not present.
# -----------------------------------------------------------------
internal:mkdir:
internal: true
requires:
vars:
- DIRECTORY
status:
- "{{ if .DIRECTORY }}test -d {{ .DIRECTORY|quote }}{{ end }}"
cmds:
# Do not use a long form flag --parents as it is not supported on macOS.
- cmd: mkdir -p {{.DIRECTORY|quote}}
platforms:
- linux
- darwin
- freebsd
- netbsd
- dragonfly
- openbsd
- cmd: powershell.exe -NonInteractive -NoProfile -NoLogo -Command 'New-Item -ItemType Directory -Force -Path "{{.DIRECTORY}}"'
platforms:
- windows
# -----------------------------------------------------------------
# Removes files with PATTERN in the given DIRECTORY.
# -----------------------------------------------------------------
internal:rm-file-glob:
internal: true
requires:
vars:
- DIRECTORY
- PATTERN
status:
- "{{ if .DIRECTORY }}! test -d {{ .DIRECTORY|quote }}{{ end }}"
cmds:
- cmd: rm -f {{ joinPath (.DIRECTORY | quote) .PATTERN }}
platforms:
- linux
- darwin
- freebsd
- netbsd
- dragonfly
- openbsd
- cmd: powershell.exe -NonInteractive -NoProfile -NoLogo -Command '(Remove-Item -Force -ErrorAction SilentlyContinue -Path "{{ joinPath .DIRECTORY .PATTERN }}")'
platforms:
- windows
# -----------------------------------------------------------------
# Removes an empty DIRECTORY.
# -----------------------------------------------------------------
internal:rmdir:
internal: true
requires:
vars:
- DIRECTORY
status:
- "{{ if .DIRECTORY }}! test -d {{ .DIRECTORY|quote }}{{ end }}"
cmds:
- cmd: rmdir {{ .DIRECTORY | quote }}
platforms:
- linux
- darwin
- freebsd
- netbsd
- dragonfly
- openbsd
- cmd: powershell.exe -NonInteractive -NoProfile -NoLogo -Command 'Remove-Item -Force -Path "{{ .DIRECTORY }}"'
platforms:
- windows
# -----------------------------------------------------------------
# Create .gocover directory to store coverage data
# -----------------------------------------------------------------
internal:go:create-coverage-dir:
internal: true
status:
- "{{ if .GO_COVER_DIR }}test -d {{ .GO_COVER_DIR|quote }}{{ end }}"
cmds:
- task: internal:mkdir
vars:
DIRECTORY: "{{ .GO_COVER_DIR }}"
# -----------------------------------------------------------------
# Cleanup coverage files before building/testing.
#
# This is required to avoid covtool errors when sources change.
# Thus, this task fingerprints the sources, and only executed
# when relevant source files change.
# Unlike clean-coverage task, this will not remove coverage directory.
#
# Test tasks should use internal:go:clean-coverage-files:pre-build
# as it avoids duplicate work by nesting two tasks. Otherwise,
# internal:rm-file-glob tasks will be executed multiple times on
# change to go sources or Taskfile.
# -----------------------------------------------------------------
internal:go:clean-coverage-files:
internal: true
status:
- "{{ if .GO_COVER_DIR }}! test -d {{ .GO_COVER_DIR|quote }}{{ end }}"
cmds:
- task: internal:rm-file-glob
vars:
DIRECTORY: "{{ .GO_COVER_DIR }}"
PATTERN: "{{ .ITEM }}"
for:
- "covcounters.*"
- "covmeta.*"
- "coverage.*"
internal:go:clean-coverage-files:pre-build:
internal: true
method: checksum
sources:
- "**/*.go"
- "Taskfile.yml"
cmds:
- task: internal:go:clean-coverage-files
# -----------------------------------------------------------------
# Run go test on package(s) specified by GO_TEST_PKG.
# -----------------------------------------------------------------
internal:go:test:
internal: true
requires:
vars:
- GO_TEST_PKG
preconditions:
- sh: >-
{{- if .GO_COVER_DIR }}
{{- if not (osIsAbs .GO_COVER_DIR) }}
false
{{- end }}
{{- end }}
msg: "GO_COVER_DIR({{.GO_COVER_DIR}}) must be an absolute path."
cmds:
# Cleanup coverage files if sources have changed.
- task: internal:go:clean-coverage-files:pre-build
# Create .gocover directory to store coverage data.
- task: internal:go:create-coverage-dir
# Run go test, optionally with coverage directory specified.
- cmd: go test {{ .GO_TEST_FLAG_VERBOSE }} -cover -timeout {{ default "3m" .GO_TEST_TIMEOUT }} {{ .GO_TEST_PKG }} {{ .GO_TEST_FLAG_COVERDIR }} {{.CLI_ARGS}}
vars:
# GO_TEST_FLAG_COVERDIR will be set if GO_COVER_DIR is already set and is
# an absolute path. This is enforced as --test.coverdir does not deal with relative
# paths when testing multiple packages or when running trampolines.
# See - https://github.com/golang/go/issues/51430#issuecomment-1344711300
GO_TEST_FLAG_COVERDIR: >-
{{- if .GO_COVER_DIR }}
{{- if osIsAbs .GO_COVER_DIR }}
{{- printf `--test.gocoverdir "%s"` .GO_COVER_DIR }}
{{- end }}
{{- end }}
# If user already specified -v/-v=true flag, skip checking variables for debug flags.
# Otherwise, check if RUNNER_DEBUG or DEBUG is set to truthy value and set GO_TEST_FLAG_VERBOSE
# to -v.
#
# Output of this variable MUST be a single line. i.e no newlines.
GO_TEST_FLAG_VERBOSE: >-
{{- if not (mustRegexMatch "--?v=?(true|false)?" .CLI_ARGS) }}
{{- if eq .RUNNER_DEBUG "1" }}
{{- printf "-v" }}
{{- else if .DEBUG }}
{{- if or (eq .DEBUG "1") (eq (lower .DEBUG) "yes") (eq (lower .DEBUG) "true") }}
{{- printf "-v" }}
{{- end }}
{{- end }}
{{- end }}
# -----------------------------------------------------------------
# Run all configured linters.
# -----------------------------------------------------------------
lint:
desc: "Run all configured linters"
summary: |
Run all configured linters on the project.
prefix: "lint"
preconditions:
- sh: command -v golangci-lint
msg: Missing tool 'golangci-lint'.
aliases:
- "go:lint"
- "golangci-lint"
cmd: golangci-lint run {{.CLI_ARGS}}
# -----------------------------------------------------------------
# Test all packages with coverage.
# -----------------------------------------------------------------
test:
desc: "Test all packages"
summary: |-
Runs Go test on all supported packages.
This by default will create .gocover directory in Root Taskfile's
directory to store coverage data which may be written by the test
itself or via test trampolines.
Runtime Info:
OS : {{ default "NA" OS }}
GO_COVER_DIR : {{ default "NA" .GO_COVER_DIR }}
aliases:
- "go:test"
cmds:
- task: internal:go:test
vars:
GO_TEST_PKG: "./..."
# -----------------------------------------------------------------
# Cleanup generated data, cache and build artifacts
# -----------------------------------------------------------------
clean:
desc: "Clean cache, build artifacts etc."
aliases:
- "go:clean"
cmds:
- task: internal:go:clean-coverage-files
- task: internal:rmdir
vars:
DIRECTORY: '{{ .GO_COVER_DIR }}'
- task: internal:rm-file-glob
vars:
DIRECTORY: '{{ joinPath .ROOT_DIR "dist" }}'
PATTERN: "*.json"
- task: internal:rm-file-glob
vars:
DIRECTORY: '{{ joinPath .ROOT_DIR "dist" }}'
PATTERN: "*.yml"
- task: internal:rm-file-glob
vars:
DIRECTORY: '{{ joinPath .ROOT_DIR "dist" }}'
PATTERN: "*.yaml"
- task: internal:rmdir
vars:
DIRECTORY: '{{ joinPath .ROOT_DIR "dist" }}'
- task: internal:rm-file-glob
vars:
DIRECTORY: '{{ joinPath .ROOT_DIR "example" }}'
PATTERN: "example"
- task: internal:rm-file-glob
vars:
DIRECTORY: '{{ joinPath .ROOT_DIR "bin" }}'
PATTERN: "example"
- task: internal:rmdir
vars:
DIRECTORY: "{{ .ITEM }}"
for:
- '{{ .GO_COVER_DIR }}'
- '{{ joinPath .ROOT_DIR "bin" }}'