-
Notifications
You must be signed in to change notification settings - Fork 129
/
Makefile
304 lines (275 loc) · 9.41 KB
/
Makefile
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Copyright 2023 SLSA Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
SHELL := /bin/bash
OUTPUT_FORMAT ?= $(shell if [ "${GITHUB_ACTIONS}" == "true" ]; then echo "github"; else echo ""; fi)
.PHONY: help
help: ## Shows all targets and help from the Makefile (this message).
@echo "slsa-github-generator Makefile"
@echo "Usage: make [COMMAND]"
@echo ""
@grep --no-filename -E '^([/a-z.A-Z0-9_%-]+:.*?|)##' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = "(:.*?|)## ?"}; { \
if (length($$1) > 0) { \
printf " \033[36m%-20s\033[0m %s\n", $$1, $$2; \
} else { \
if (length($$2) > 0) { \
printf "%s\n", $$2; \
} \
} \
}'
node_modules/.installed: package.json package-lock.json
npm ci
touch node_modules/.installed
## Testing
#####################################################################
.PHONY: unit-test
unit-test: go-test ts-test ## Runs all unit tests.
.PHONY: go-test
go-test: ## Run Go unit tests.
@ # NOTE: go test builds packages even if there are no tests.
@set -e;\
go mod vendor; \
extraargs=""; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
extraargs="-v"; \
fi; \
go test -mod=vendor $$extraeargs ./...
.PHONY: ts-test
ts-test: ## Run TypeScript tests.
@# Run unit tests for all TS actions where tests are found.
@set -e;\
PATHS=$$( \
find .github/actions/ actions/ \
-name __tests__ -type d \
-not -path '*/node_modules/*' \
-not -iwholename '*/third_party/*' | xargs dirname \
); \
for path in $$PATHS; do \
make -C $$path unit-test; \
done
## Tools
#####################################################################
.PHONY: format
format: yaml-format md-format ts-format go-format markdown-toc shfmt autogen ## Runs all code formatters.
.PHONY: yaml-format
yaml-format: node_modules/.installed ## Runs code formatter for YAML files.
@set -e;\
yml_files=$$( \
git ls-files \
'*.yaml' '**/*.yaml' \
'*.yml' '**/*.yml' \
':!:third_party/*' ':!:third_party/**/*' \
); \
for path in $$yml_files; do \
./node_modules/.bin/prettier --write $$path; \
done;
.PHONY: md-format
md-format: node_modules/.installed ## Runs code formatter for Markdown files.
@set -e;\
md_files=$$( \
git ls-files \
'*.md' '**/*.md' \
':!:third_party/*' ':!:third_party/**/*' \
); \
for path in $$md_files; do \
./node_modules/.bin/prettier --write $$path; \
done;
.PHONY: ts-format
ts-format: ## Runs code formatter for TypeScript files.
@set -e;\
actions_paths=$$( \
find .github/actions/ actions/ \
-name package.json -type f \
-not -path '*/node_modules/*' \
-not -iwholename '*/third_party/*' | xargs dirname \
); \
for path in $$actions_paths; do \
make -C $$path format; \
done
.PHONY: go-format
go-format: ## Runs code formatter for Go files.
@set -e;\
go_files=$$( \
git ls-files \
'*.go' '**/*.go' \
':!:third_party/*' ':!:third_party/**/*' \
); \
for path in $$go_files; do \
gofumpt -w $$path; \
done;
COPYRIGHT ?= "SLSA Authors"
LICENSE ?= apache
.PHONY: autogen
autogen: ## Runs autogen on code files.
@set -euo pipefail; \
code_files=$$( \
git ls-files \
'*.go' '**/*.go' \
'*.ts' '**/*.ts' \
'*.sh' '**/*.sh' \
'*.yaml' '**/*.yaml' \
'*.yml' '**/*.yml' \
'Makefile' \
':!:third_party/*' ':!:third_party/**/*' \
); \
git_root="$$(git rev-parse --show-toplevel)"; \
for filename in $${code_files}; do \
if ! ( head "$${filename}" | grep -iL $(COPYRIGHT) > /dev/null ); then \
echo $${filename}; \
cd $$(dirname "$${filename}"); \
"$${git_root}/third_party/autogen/autogen.sh" -i --no-code --no-tlc -c $(COPYRIGHT) -l $(LICENSE) $$(basename "$${filename}"); \
cd - > /dev/null; \
fi; \
done
.PHONY: markdown-toc
markdown-toc: node_modules/.installed ## Runs markdown-toc on markdown files.
@# NOTE: Do not include issue templates since they contain Front Matter.
@# markdown-toc will update Front Matter even if there is no TOC in the file.
@# See: https://github.com/jonschlinkert/markdown-toc/issues/151
@set -euo pipefail; \
md_files=$$( \
git ls-files \
'*.md' '**/*.md' \
':!:.github/ISSUE_TEMPLATE/*.md' \
':!:third_party/*' ':!:third_party/**/*' \
); \
for filename in $${md_files}; do \
npm run markdown-toc "$${filename}"; \
done;
.PHONY: shfmt
shfmt: ## Runs the shfmt formatter.
@set -e;\
sh_files=$$( \
find . -type f \
-not -iwholename '*/.git/*' \
-not -iwholename '*/vendor/*' \
-not -iwholename '*/node_modules/*' \
-not -iwholename '*/third_party/*' \
-exec bash -c 'file "$$1" | cut -d':' -f2 | grep --quiet shell' _ {} \; -print \
); \
for filename in $${sh_files}; do \
shfmt -w -i 2 "$${filename}"; \
done;
## Linters
#####################################################################
.PHONY: lint
lint: markdownlint golangci-lint shellcheck eslint yamllint actionlint renovate-config-validator ## Run all linters.
.PHONY: actionlint
actionlint: ## Runs the actionlint linter.
@# NOTE: We need to ignore config files used in tests.
@set -e;\
files=$$( \
find .github/workflows/ -type f \
\( \
-name '*.yaml' -o \
-name '*.yml' \
\) \
-not -iwholename '*/configs-*/*' \
); \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
actionlint -format '{{range $$err := .}}::error file={{$$err.Filepath}},line={{$$err.Line}},col={{$$err.Column}}::{{$$err.Message}}%0A```%0A{{replace $$err.Snippet "\\n" "%0A"}}%0A```\n{{end}}' -ignore 'SC2016:' $${files}; \
else \
actionlint $${files}; \
fi
.PHONY: markdownlint
markdownlint: node_modules/.installed ## Runs the markdownlint linter.
@set -e;\
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
exit_code=0; \
while IFS="" read -r p && [ -n "$$p" ]; do \
file=$$(echo "$$p" | jq -c -r '.fileName // empty'); \
line=$$(echo "$$p" | jq -c -r '.lineNumber // empty'); \
endline=$${line}; \
message=$$(echo "$$p" | jq -c -r '.ruleNames[0] + "/" + .ruleNames[1] + " " + .ruleDescription + " [Detail: \"" + .errorDetail + "\", Context: \"" + .errorContext + "\"]"'); \
exit_code=1; \
echo "::error file=$${file},line=$${line},endLine=$${endline}::$${message}"; \
done <<< "$$(./node_modules/.bin/markdownlint --dot --json . 2>&1 | jq -c '.[]')"; \
exit "$${exit_code}"; \
else \
npm run markdownlint; \
fi
.PHONY: golangci-lint
golangci-lint: ## Runs the golangci-lint linter.
@set -e;\
extraargs=""; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
extraargs="--out-format github-actions"; \
fi; \
golangci-lint run -c .golangci.yml ./... $$extraargs
SHELLCHECK_ARGS = --severity=style --external-sources
.PHONY: shellcheck
shellcheck: ## Runs the shellcheck linter.
@set -e;\
files=$$( \
find . -type f \
-not -iwholename '*/.git/*' \
-not -iwholename '*/vendor/*' \
-not -iwholename '*/node_modules/*' \
-not -iwholename '*/third_party/*' \
-exec bash -c 'file "$$1" | cut -d':' -f2 | grep --quiet shell' _ {} \; -print \
); \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
exit_code=0; \
while IFS="" read -r p && [ -n "$$p" ]; do \
level=$$(echo "$$p" | jq -c '.level // empty' | tr -d '"'); \
file=$$(echo "$$p" | jq -c '.file // empty' | tr -d '"'); \
line=$$(echo "$$p" | jq -c '.line // empty' | tr -d '"'); \
endline=$$(echo "$$p" | jq -c '.endLine // empty' | tr -d '"'); \
col=$$(echo "$$p" | jq -c '.column // empty' | tr -d '"'); \
endcol=$$(echo "$$p" | jq -c '.endColumn // empty' | tr -d '"'); \
message=$$(echo "$$p" | jq -c '.message // empty' | tr -d '"'); \
exit_code=1; \
case $$level in \
"info") \
echo "::notice file=$${file},line=$${line},endLine=$${endline},col=$${col},endColumn=$${endcol}::$${message}"; \
;; \
"warning") \
echo "::warning file=$${file},line=$${line},endLine=$${endline},col=$${col},endColumn=$${endcol}::$${message}"; \
;; \
"error") \
echo "::error file=$${file},line=$${line},endLine=$${endline},col=$${col},endColumn=$${endcol}::$${message}"; \
;; \
esac; \
done <<< "$$(echo -n "$$files" | xargs shellcheck -f json $(SHELLCHECK_ARGS) | jq -c '.[]')"; \
exit "$${exit_code}"; \
else \
echo -n "$$files" | xargs shellcheck $(SHELLCHECK_ARGS); \
fi
.PHONY: eslint
eslint: ## Runs the eslint linter.
@set -e;\
PATHS=$$( \
find .github/actions/ actions/ \
-name package.json -type f \
-not -path '*/node_modules/*' \
-not -path '*/third_party/*' | xargs dirname); \
for path in $$PATHS; do \
make -C $$path lint; \
done
.PHONY: yamllint
yamllint: ## Runs the yamllint linter.
@set -e;\
extraargs=""; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
extraargs="-f github"; \
fi; \
yamllint --strict -c .yamllint.yaml . $$extraargs
.PHONY: renovate-config-validator
renovate-config-validator: node_modules/.installed ## Runs renovate-config-validator
@npm run renovate-config-validator
## Maintenance
#####################################################################
.PHONY: clean
clean: ## Delete temporary files.
rm -rf node_modules