-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.yml
143 lines (135 loc) · 4.59 KB
/
utils.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
version: "3"
tasks:
# ===
# CHECKSUM UTILS
# ===
# @param {string} DATA_DIR The directory to compute the checksum for.
# @param {string} OUTPUT_FILE
# @param {[]string} [EXCLUDE_PATHS] A list of paths, relative to `DATA_DIR`, to exclude from the
# checksum.
compute-checksum:
desc: "Tries to compute a checksum for the given directory and output it to a file."
internal: true
silent: true
requires:
vars: ["DATA_DIR", "OUTPUT_FILE"]
cmds:
- >-
tar cf -
--directory "{{.DATA_DIR}}"
--group 0
--mtime "UTC 1970-01-01"
--numeric-owner
--owner 0
--sort name
{{- range .EXCLUDE_PATHS}}
--exclude="{{.}}"
{{- end}}
. 2> /dev/null
| md5sum > {{.OUTPUT_FILE}}
# Ignore errors so that dependent tasks don't fail
ignore_error: true
# @param {string} DATA_DIR The directory to validate the checksum for.
# @param {string} OUTPUT_FILE
# @param {[]string} [EXCLUDE_PATHS] A list of paths, relative to `DATA_DIR`, to exclude from the
# checksum.
validate-checksum:
desc: "Validates the checksum of the given directory matches the checksum in the given file, or
deletes the checksum file otherwise."
internal: true
silent: true
vars:
TMP_CHECKSUM_FILE: "{{.CHECKSUM_FILE}}.tmp"
requires:
vars: ["CHECKSUM_FILE", "DATA_DIR"]
cmds:
- task: "compute-checksum"
vars:
DATA_DIR: "{{.DATA_DIR}}"
EXCLUDE_PATHS:
ref: ".EXCLUDE_PATHS"
OUTPUT_FILE: "{{.TMP_CHECKSUM_FILE}}"
- defer: "rm -f '{{.TMP_CHECKSUM_FILE}}'"
# Check that the directory exists and the checksum matches; otherwise delete the checksum file
- >-
(
test -d "{{.DATA_DIR}}"
&& diff -q '{{.TMP_CHECKSUM_FILE}}' '{{.CHECKSUM_FILE}}' 2> /dev/null
) || rm -f '{{.CHECKSUM_FILE}}'
# ===
# STRING UTILS
# ===
replace-text:
desc: "Replaces some text in a file using sed."
internal: true
requires:
vars: ["FILE_PATH", "SED_EXP"]
cmds:
- |-
# NOTE:
# 1. We can't use `sed -i` since `-i` has different syntax on Linux and macOS
# 2. We can't use `--regexp` instead of `-E` since `--regexp` is not supported on macOS
src="{{.FILE_PATH}}"
dst="{{.FILE_PATH}}.tmp"
sed -E '{{.SED_EXP}}' "${src}" > "${dst}"
mv "${dst}" "${src}"
# ===
# VENV UTILS
# ===
create-venv:
desc: "Creates a Python venv using the given requirements file."
internal: true
label: "create-venv-{{.LABEL}}"
requires:
vars: ["LABEL", "OUTPUT_DIR", "REQUIREMENTS_FILE"]
cmds:
- "rm -rf '{{.OUTPUT_DIR}}'"
- "python3 -m venv '{{.OUTPUT_DIR}}'"
# Remove calls to `hash` from the venv activation script since Task uses `gosh` rather than
# `bash`.
# NOTE: Older versions of Python's venv would only call `hash` if they detected the running
# shell was one that had the command, but that's not the case in newer versions.
- task: "replace-text"
vars:
FILE_PATH: "{{.OUTPUT_DIR}}/bin/activate"
SED_EXP: >-
s/^([[:space:]]*)hash[[:space:]]+.*/\1true/g
- |-
. "{{.OUTPUT_DIR}}/bin/activate"
pip3 install --upgrade pip
pip3 install --upgrade -r "{{.REQUIREMENTS_FILE}}"
# ===
# C++ LINTING UTILS
# ===
# Runs clang-format on C++ files at the given paths.
#
# @param {string} FLAGS Any flags to pass to clang-format.
# @param {[]string} SRC_PATHS The paths on which to run clang-format.
# @param {string} VENV_DIR Python virtual environment where clang-format is installed.
clang-format:
internal: true
requires:
vars: ["FLAGS", "SRC_PATHS", "VENV_DIR"]
cmd: |-
. "{{.VENV_DIR}}/bin/activate"
find {{- range .SRC_PATHS}} "{{.}}" {{- end}} \
-type f \
\( -iname "*.cpp" -o -iname "*.h" -o -iname "*.hpp" \) \
-print0 | \
xargs -0 clang-format {{.FLAGS}} -Werror
# Runs clang-tidy on C++ files at the given paths.
#
# @param {string} FLAGS Any flags to pass to clang-tidy.
# @param {[]string} SRC_PATHS The paths on which to run clang-tidy.
# @param {string} VENV_DIR Python virtual environment where clang-tidy is installed.
clang-tidy:
internal: true
requires:
vars: ["FLAGS", "SRC_PATHS", "VENV_DIR"]
cmd: |-
. "{{.VENV_DIR}}/bin/activate"
find {{- range .SRC_PATHS}} "{{.}}" {{- end}} \
-type f \
\( -iname "*.cpp" -o -iname "*.h" -o -iname "*.hpp" \) \
-print0 | \
xargs -0 clang-tidy {{.FLAGS}}