forked from obi1kenobi/cargo-semver-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_new_lint.sh
executable file
·131 lines (117 loc) · 3.39 KB
/
make_new_lint.sh
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
#!/usr/bin/env bash
# check for bash using maximum compatibility sh syntax
if [ -z "$BASH_VERSION" ]; then
>&2 printf 'This script must be run using the bash shell.\n'
exit 1
fi
# Fail on first error, on undefined variables, and on failures in pipelines.
set -euo pipefail
TOPLEVEL="$(git rev-parse --show-toplevel)"
LINTS_DIR="$TOPLEVEL/src/lints"
TEST_CRATES_DIR="$TOPLEVEL/test_crates"
TEST_OUTPUTS_DIR="$TOPLEVEL/test_outputs"
SRC_QUERY_FILE="$TOPLEVEL/src/query.rs"
# Make the script cwd-independent by always moving to the repo root first.
cd "$TOPLEVEL"
# What should the lint be called?
set +u
NEW_LINT_NAME="$1"
if [[ "$NEW_LINT_NAME" == "" || "$NEW_LINT_NAME" == "--help" ]]; then
echo "Specify the name of the lint to add: make_new_lint.sh <LINT_NAME>"
exit 1
fi
set -u
# Make the lint file.
LINT_FILENAME="$LINTS_DIR/$NEW_LINT_NAME.ron"
echo -n "Creating lint definition file ${LINT_FILENAME#"$TOPLEVEL/"} ..."
if [[ -f "$LINT_FILENAME" ]]; then
echo ' already exists.'
else
cat <<EOF >"$LINT_FILENAME"
SemverQuery(
id: "$NEW_LINT_NAME",
human_readable_name: "TODO",
description: "TODO",
required_update: Major, // TODO
lint_level: Deny, // TODO
reference_link: None, // TODO
query: r#"
{
CrateDiff {
# TODO
}
}"#,
arguments: {
// TODO
},
error_message: "TODO",
per_result_error_template: Some("TODO"),
// TODO: see https://github.com/obi1kenobi/cargo-semver-checks/blob/main/CONTRIBUTING.md#adding-a-witness
// for information about this field.
witness: None,
)
EOF
echo ' done!'
fi
# Make the test crates.
NEW_LINT_TEST_CRATES_DIR="$TEST_CRATES_DIR/$NEW_LINT_NAME"
./scripts/make_new_test_crate.sh "$NEW_LINT_NAME"
# Add the test outputs file.
NEW_TEST_OUTPUT_FILE="$TEST_OUTPUTS_DIR/query_execution/$NEW_LINT_NAME.snap"
echo -n "Creating test outputs file ${NEW_TEST_OUTPUT_FILE#"$TOPLEVEL/"} ..."
if [[ -f "$NEW_TEST_OUTPUT_FILE" ]]; then
echo ' already exists.'
else
cat <<EOF >"$NEW_TEST_OUTPUT_FILE"
---
source: src/query.rs
expression: "&query_execution_results"
---
{
"./test_crates/$NEW_LINT_NAME/": [
// TODO
]
}
EOF
echo ' done!'
fi
# Add the new lint to the `add_lints!()` macro.
echo -n "Registering lint in src/query.rs ..."
# Requirements on $SRC_QUERY_FILE:
# - one lint per line, with leading indent (a nonzero amount of whitespace)
# - lint lines must be sorted
# - lints begin with a line that begins with add_lints!(
# - lints end with a line that begins with )
if updated_text=$(awk -v lint="$NEW_LINT_NAME" '
BEGIN {
lint = "" lint
}
searching {
current_lint = "" $2
if (lint == current_lint) {
searching = 0
} else if ($0 ~ /^\)/ || lint < current_lint) {
printf(" %s,\n", lint)
inserted = 1
searching = 0
}
}
{ print }
/^add_lints!\(/ {
searching = 1
FS = "[,[:space:]]+"
}
END {
exit !inserted
}
' "$SRC_QUERY_FILE"); then
printf '%s\n' "$updated_text" > "$SRC_QUERY_FILE"
printf ' done!\n'
else
printf ' already exists.\n'
fi
echo ''
echo 'Lint created successfully! Remember to:'
echo "- implement the lint in ${LINT_FILENAME#"$TOPLEVEL/"}"
echo "- populate the test crates in ${NEW_LINT_TEST_CRATES_DIR#"$TOPLEVEL/"}"
echo "- add the expected test outputs in ${NEW_TEST_OUTPUT_FILE#"$TOPLEVEL/"}"