Last active
January 30, 2022 03:20
-
-
Save nazfox/838c5050b12fa444a343c0b5556fc902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# -------------------------------------------------- | |
# Generate help messages from Makefile | |
# -------------------------------------------------- | |
# | |
# Example Makefile | |
# | |
# ``` | |
# ## This is no displayed, this is just a comment | |
# | |
# ## This will be displayed as a help message | |
# ## And this too | |
# help: | |
# @makefile-usage.sh $(MAKEFILE_LIST) | |
# ``` | |
# | |
# And the help message is... | |
# | |
# ``` | |
# $ make help | |
# Usage: | |
# make [target] | |
# | |
# Targets: | |
# help This will be displayed as a help message | |
# And this too | |
# ``` | |
makefile=$1 | |
target_max_char=$2 | |
if [ -z "$target_max_char" ]; then | |
target_max_char=20 | |
fi | |
white_spaces=$(printf '%'$target_max_char's') | |
echo 'Usage:' | |
echo ' make [target]' | |
echo '' | |
echo 'Targets:' | |
awk ' | |
{ \ | |
if ($0 ~ /^#!/) { \ | |
# Collect help messages \ | |
if (helpMessage) { \ | |
helpMessage = helpMessage "\n'"$white_spaces"' "; \ | |
} \ | |
helpMessage = helpMessage substr($0, 3); \ | |
} else if ($0 ~ /^[a-zA-Z\-_0-9]+:/) { \ | |
# Display a help message when we find a task \ | |
colonPos = index($0, ":") - 1; \ | |
target = substr($0, 0, colonPos); \ | |
if (helpMessage) { \ | |
printf " %-'"$target_max_char"'s %s\n", target, helpMessage; \ | |
helpMessage = ""; \ | |
} \ | |
} else { \ | |
# Empty the help message \ | |
helpMessage = ""; \ | |
} \ | |
}' $makefile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment