Skip to content

Instantly share code, notes, and snippets.

@apainintheneck
Created April 23, 2023 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apainintheneck/432e2d89c9e086de91dd6e5cbe47edf7 to your computer and use it in GitHub Desktop.
Save apainintheneck/432e2d89c9e086de91dd6e5cbe47edf7 to your computer and use it in GitHub Desktop.
Script to search for caveats functions that don't work with the new Homebrew JSON API.
#!/usr/bin/env fish
# This probably should work in any shell as long as `brew` is in your path.
cd (brew --repo homebrew/core)
awk '
is_caveats(), is_end() {
# save caveats function text
caveats_buffer[++caveats_idx] = $0
if (is_multiline_string()) next
if (!has_branching_logic) {
has_branching_logic = is_on_x_block() || is_if_or_unless()
}
if (is_end()) {
if (has_branching_logic) {
# print buffer
printf("[%s]\n", FILENAME)
for (i = 1; i <= caveats_idx; ++i)
print caveats_buffer[i]
}
# clean up
caveats_idx = 0
has_branching_logic = 0
}
}
function is_multiline_string() {
if (MULTILINE_STRING) {
if (NF == 1 && $1 == "EOS") {
MULTILINE_STRING = 0
}
return 1
}
if ($NF == "<<~EOS") {
MULTILINE_STRING = 1
return 1
}
return 0
}
function is_on_x_block() {
return $1 ~ /^on_/ && $NF == "do"
}
function is_if_or_unless( i) {
for (i = 1; i <= NF; ++i) {
if ($i == "if" || $i == "unless")
return 1
}
return 0
}
function is_caveats() {
if ($1 == "def" && $2 == "caveats") {
END_INDENT = get_indent()
return 1
} else {
return 0
}
}
function is_end() {
return NF == 1 && $1 == "end" && get_indent() == END_INDENT
}
function get_indent() {
match($0, /^ */)
return RLENGTH
}' (find . -name '*.rb')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment