Skip to content

Commit

Permalink
bake.bash: 重构,消除bake.cmd的summary选项,只保留desc
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed Mar 28, 2024
1 parent 6d2d43e commit b387077
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
14 changes: 7 additions & 7 deletions bake
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
SCRIPT_FILE="$(basename "$SCRIPT_PATH")"

# include common script
source "$SCRIPT_DIR/vendor/bake.bash"
source "$SCRIPT_DIR/bake.bash"

##########################################
# app cmd script
# 应用的命令脚本
##########################################

bake.cmd --cmd _root \
--usage "./$SCRIPT_FILE [cmd] [opts] [args...]" \
--summary "flutter-note build cli." \
--help "$( cat <<-EOF
--desc "$( cat <<-EOF
___ _ _ _ _ _ _
| __|| | _ _ | |_ | |_ ___ _ _ | \| | ___ | |_ ___
Expand All @@ -36,6 +34,9 @@ bake.cmd --cmd _root \
flutter-note build tools.
https://github.com/chen56/note
Usage:
./$SCRIPT_FILE [cmd] [opts] [args...]
Examples:
./${SCRIPT_FILE} # same as './${SCRIPT_FILE} -h'
./${SCRIPT_FILE} -h # show all commands help
Expand Down Expand Up @@ -79,10 +80,9 @@ install() {
run git lfs install
}

bake.cmd --cmd p --summary "<mono> project set" --usage "Usage: ./$SCRIPT_FILE p <sub_cmd>"
#p(){ echo "use './$SCRIPT_FILE p -h' see sub commands, there are all projects "; }
bake.cmd --cmd p --desc "<mono> project set, Usage: ./$SCRIPT_FILE p <sub_cmd>"

bake.cmd --cmd p.all --summary "<mono> run cmd on all mono project" --usage "Usage: ./$SCRIPT_FILE all [any command]"
bake.cmd --cmd p.all --desc "<mono> run cmd on all mono project Usage: ./$SCRIPT_FILE all [any command]"
p.all() {
# 目录中有"pubspec.yaml"的,认为是flutter项目
# for project in $( find . -name pubspec.yaml | sed s/pubspec.yaml$//g ) ; do
Expand Down
44 changes: 10 additions & 34 deletions bake.bash
Original file line number Diff line number Diff line change
Expand Up @@ -413,32 +413,16 @@ bake._show_cmd_help() {

eval "$(bake.parse "${FUNCNAME[0]}" "$@")"

local usage
usage="${_data["${cmd}/usage"]}"
if [[ "$usage" != "" ]]; then
echo -e "\nUsage: $usage "
else
echo -e "\nUsage: ./$BAKE_FILE $(bake._str_split "$cmd" "." | tr "\n" " ") [options] [args...]"
fi

echo

if [[ "$cmd" == "_root" ]] ;then
echo "Currently running: $(bake._pwd)/$BAKE_FILE $@"
echo "Running:【: $(bake._pwd)/$BAKE_FILE $@"
else
echo "Currently running: $(bake._pwd)/$BAKE_FILE $cmd $@"
echo "Running:【$(bake._pwd)/$BAKE_FILE $cmd $@"
fi

echo

if [[ "${_data["${cmd}/description"]}" != "" ]]; then
echo "Description: ${_data["${cmd}/description"]}"
elif [[ "${_data[${cmd} / summary]}" != "" ]]; then
echo "Description: ${_data["${cmd}/summary"]}"
else
echo "no description"
fi

echo "${_data["${cmd}/desc"]}"
echo

echo "Available Options:"
Expand Down Expand Up @@ -482,11 +466,10 @@ Available Commands:"
local subCmdPath="$cmd/$subCmd"
[[ "$cmd" == "_root" ]] && subCmdPath="$subCmd"

local summary
summary="${_data["$subCmdPath/summary"]}"
summary="$(echo -e "$summary")" # backslash escapes interpretation
local desc="${_data["$subCmdPath/desc"]}"
desc="$(echo -e "$desc" | head -n 1 )" # backslash escapes interpretation

printf " %-$((maxLengthOfCmd))s ${summary}\n" "${subCmd}"
printf " %-$((maxLengthOfCmd))s ${desc}\n" "${subCmd}"
done
}

Expand Down Expand Up @@ -628,17 +611,13 @@ bake.parse() {
# bake.cmd (public api)
# 注册一个命令的帮助信息
# Examples:
# bake.cmd --cmd build --summary "build project" --usage "Usage: ./$SCRIPT_FILE build [options]"
# bake.cmd --cmd build --desc "build project"
# 尤其是可以配置_root命令以定制根命令的帮助信息,比如:
# bake.cmd --cmd _root \
# --usage "./$SCRIPT_FILE [cmd] [opts] [args...]" \
# --summary "flutter-note cli." \
# --description ".... your root cmd help "
# --desc "flutter-note cli."
# 这样就可以用'./your_script -h' 查看根帮助了
bake.opt --cmd "bake.cmd" --name "cmd" --type string --optHelp "cmd, function name"
bake.opt --cmd "bake.cmd" --name "usage" --type string --optHelp "usage"
bake.opt --cmd "bake.cmd" --name "summary" --type string --optHelp "summary help, short, show on cmd list"
bake.opt --cmd "bake.cmd" --name "description" --type string --optHelp "description, long help ,show on cmd help page"
bake.opt --cmd "bake.cmd" --name "desc" --type string --optHelp "cmd desc, show in help"
bake.cmd() {
# 模版代码,放到每个需要使用option的函数中,然后就可以使用option了
eval "$(bake.parse "${FUNCNAME[0]}" "$@")"
Expand All @@ -647,10 +626,7 @@ bake.cmd() {
echo "error: bake.cmd [--cmd] required " >&2
return 1
fi
# bake.parse "${FUNCNAME[0]}" "$@" >&2
_data["$cmd/usage"]="$usage"
_data["$cmd/summary"]="$summary"
_data["$cmd/description"]="$description"
_data["$cmd/desc"]="$desc"
}


Expand Down

0 comments on commit b387077

Please sign in to comment.