From 5fceb121c99b82ec9e5cfc4ffb22976da0e70113 Mon Sep 17 00:00:00 2001 From: Bazaah Date: Fri, 9 Dec 2022 16:25:32 +0000 Subject: [PATCH] tool/make2doc: script for creating help docs for Makefiles This will become important in a few commits ;) --- tool/make2doc.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 tool/make2doc.sh diff --git a/tool/make2doc.sh b/tool/make2doc.sh new file mode 100755 index 0000000..f80e15b --- /dev/null +++ b/tool/make2doc.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +RULE_COLOR="$(tput setaf 6)" +SECTION_COLOR="$(tput setaf 3)" +VARIABLE_COLOR="$(tput setaf 2)" +VALUE_COLOR="$(tput setaf 1)" +CLEAR_STYLE="$(tput sgr0)" +TARGET_STYLED_HELP_NAME="${RULE_COLOR}TARGET${CLEAR_STYLE}" +ARGUMENTS_HELP_NAME="${VARIABLE_COLOR}ARGUMENT${CLEAR_STYLE}=${VALUE_COLOR}VALUE${CLEAR_STYLE}" + +echo "Usage: make [$TARGET_STYLED_HELP_NAME [$TARGET_STYLED_HELP_NAME ...]] [$ARGUMENTS_HELP_NAME [$ARGUMENTS_HELP_NAME ...]]" +echo "${SECTION_COLOR}Targets:${CLEAR_STYLE}" +echo " ${RULE_COLOR}help${CLEAR_STYLE}" +echo " Get help for commands in this folder" +echo "" + +TARGET_REGEX="^[a-zA-Z0-9%_\/%-][a-zA-Z0-9%_\/%.-]+:" +SECTION_REGEX="^##\s*@section\s*(.*)$" +DOCBLOCK_REGEX="^##\s*(.*)$" +PARAM_REGEX="@param\s+([a-zA-Z_]+)(=([^\s]+))?\s*(.*$)?" + +COMMENT="" +PARAMS="" +PARAMS_DOC="" +cat $FILE | while read line +do + # do something with $line here + if [[ ! -z $line ]] + then + if [[ $line =~ $SECTION_REGEX ]] + then + SECTION_NAME=$(echo $line | sed -e "s/^##\s*@section\s*\(.*\)$/\1/g") + echo "$SECTION_COLOR$SECTION_NAME$CLEAR_STYLE:" + elif [[ $line =~ $TARGET_REGEX ]] + then + # if there is no comment for this target, we don't display it in the docs to keep private targets hidden + if [[ ! -z $COMMENT ]] + then + TARGET=$(echo $line | sed -e "s/^\([a-zA-Z0-9%_\/%\.-]\+\):.*/\1/g") + echo " $RULE_COLOR$TARGET$CLEAR_STYLE $PARAMS" + echo -e "$COMMENT" + if [[ ! -z $PARAMS_DOC ]] + then + echo " Params:" + echo -e "$PARAMS_DOC" + fi + fi + COMMENT="" + PARAMS="" + PARAMS_DOC="" + elif [[ $line =~ $PARAM_REGEX ]] + then + PARAM=$(echo $line | sed -e "s/##\s*@param\s\+\([a-zA-Z_]\+\)\(=\([^[:space:]]\+\)\)\?\s*\(.*\)\?$/${VARIABLE_COLOR}\1${CLEAR_STYLE}=${VALUE_COLOR}\3${CLEAR_STYLE}/g") + PARAM_DOC=$(echo $line | sed -e "s/##\s*@param\s\+\([a-zA-Z_]\+\)\(=\([^[:space:]]\+\)\)\?\s*\(.*\)\?$/- \1 (ex: \3) \4/g") + PARAMS="${PARAMS}${PARAM} " + PARAMS_DOC="${PARAMS_DOC} ${PARAM_DOC}\n" + elif [[ $line =~ $DOCBLOCK_REGEX ]] + then + # echo "doc : $line" + # echo $line | sed -e "s/^##\s*\(.*\)$/\1/g" + LINE_CLEANED=$(echo $line | sed -e "s/^##\s*\(.*\)$/\1/g") + COMMENT="${COMMENT} $LINE_CLEANED\n" + fi + fi +done