From 6990a0125da75c907c828949edc0ba18cab34e2b Mon Sep 17 00:00:00 2001 From: Chen Peng Date: Sat, 30 Mar 2024 11:21:16 +0800 Subject: [PATCH] update bake --- bake | 14 ++++----- spaces/shell/core/note.bash | 54 +++++++++++++++++++++++++++++++++++ spaces/shell/note.common.bash | 31 ++++++++++++++++++++ vendor/bake.bash | 38 +++++++++++++++--------- 4 files changed, 115 insertions(+), 22 deletions(-) create mode 100755 spaces/shell/core/note.bash create mode 100755 spaces/shell/note.common.bash diff --git a/bake b/bake index 5b026bab..eec89170 100755 --- a/bake +++ b/bake @@ -1,16 +1,16 @@ #!/usr/bin/env bash # On Mac OS, readlink -f doesn't work, so use._real_path get the real path of the file -_real_path() ( - cd "$(dirname "$1")" +_real_path() { + cd "$(dirname "$1")" || exit file="$PWD/$(basename "$1")" while [[ -L "$file" ]]; do file="$(readlink "$file")" - cd -P "$(dirname "$file")" + cd -P "$(dirname "$file")" || exit file="$PWD/$(basename "$file")" done echo "$file" -) +} SCRIPT_PATH="$(_real_path "${BASH_SOURCE[0]}")" SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" SCRIPT_FILE="$(basename "$SCRIPT_PATH")" @@ -86,6 +86,7 @@ run() { # flutter_note flutter create --platforms=macos . install() { + _install_bake run git lfs install } @@ -138,11 +139,6 @@ build() { p.flutter_note flutter build web -v --release --tree-shake-icons preview.web() { run p.flutter_note deno run --allow-env --allow-read --allow-sys --allow-net npm:http-server ./build/web --port 8000 -g --brotli; } preview.macos() ( run p.flutter_note open build/macos/Build/Products/Release/flutter_note.app ; ) -# http-server 不支持base href设置,所以单独build,并设置base-href为"/",而github-pages的base-href必须是repository名 -# build "$@" -# npx http-server ./flutter_note/build/web --port 8000 -web.serve() { run p.flutter_note deno run --allow-env --allow-read --allow-sys --allow-net npm:http-server ./build/web --port 8000 -g --brotli ;} - clean() { p.all flutter clean; rm -rf build;} gen.all() { gen.notes; } # gen.mate;不再用了 diff --git a/spaces/shell/core/note.bash b/spaces/shell/core/note.bash new file mode 100755 index 00000000..50ead164 --- /dev/null +++ b/spaces/shell/core/note.bash @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +set -o errtrace # -E trap inherited in sub script +set -o errexit # -e +set -o functrace # -T If set, any trap on DEBUG and RETURN are inherited by shell functions +set -o pipefail # default pipeline status==last command status, If set, status=any command fail + +# 模版代码,获取文件真实路径 +# On Mac OS, readlink -f doesn't work, so use._real_path get the real path of the file +_real_path() { + cd "$(dirname "$1")" || exit + file="$PWD/$(basename "$1")" + while [[ -L "$file" ]]; do + file="$(readlink "$file")" + cd -P "$(dirname "$file")" || exit + file="$PWD/$(basename "$file")" + done + echo "$file" +} + +SCRIPT_PATH="$(_real_path "${BASH_SOURCE[0]}")" +SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" +#SCRIPT_FILE="$(basename "$SCRIPT_PATH")" + +source "$SCRIPT_DIR/../note.common.bash" + +print.markdown <<'MARKDOWN_END' +# bash 核心 - 命令行、字符串、分割符、转义 + +了解字符串,字符串分割,转义,是bash的核心 + +## 转义字符串 + +MARKDOWN_END + + +( + # 了解字符串,字符串分割,转义,是bash的核心 + unescaped_string="Hello + World" + # 使用printf命令,"%b"参数可以正确处理转义字符 + printf -v escaped_string "%q" "$unescaped_string" + + echo "escaped_string: $escaped_string" +) + + +( + # 了解字符串,字符串分割,转义,是bash的核心 + escaped_string="Hello\nWorld" + # 使用printf命令,"%b"参数可以正确处理转义字符 + printf -v unescaped_string "%b" "$escaped_string" + + echo "unescaped_string: $unescaped_string" +) diff --git a/spaces/shell/note.common.bash b/spaces/shell/note.common.bash new file mode 100755 index 00000000..0bf71eea --- /dev/null +++ b/spaces/shell/note.common.bash @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -o errtrace # -E trap inherited in sub script +set -o errexit # -e +set -o functrace # -T If set, any trap on DEBUG and RETURN are inherited by shell functions +set -o pipefail # default pipeline status==last command status, If set, status=any command fail + +# 模版代码,获取文件真实路径 +# On Mac OS, readlink -f doesn't work, so use._real_path get the real path of the file +_real_path() { + cd "$(dirname "$1")" || exit + file="$PWD/$(basename "$1")" + while [[ -L "$file" ]]; do + file="$(readlink "$file")" + cd -P "$(dirname "$file")" || exit + file="$PWD/$(basename "$file")" + done + echo "$file" +} + +SCRIPT_PATH="$(_real_path "${BASH_SOURCE[0]}")" +SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" +SCRIPT_FILE="$(basename "$SCRIPT_PATH")" + +print.markdown(){ + # IFS= 表示将IFS清空,也就是将内部字段分隔符设置为空字符串,这样一来,在执行 read 命令时, + # 将整行文本视为一个单独的字段(即使行中有多个空格、制表符或换行符)。 + # -r 参数告诉 read 命令不进行反斜杠转义(backslash escaping),即不对行中的反斜杠进行特殊处理。 + while IFS= read -r line; do + echo "$line" + done +} diff --git a/vendor/bake.bash b/vendor/bake.bash index 32cb9866..2e0c383e 100644 --- a/vendor/bake.bash +++ b/vendor/bake.bash @@ -8,13 +8,10 @@ set -o pipefail # default pipeline status==last command status, If set, status= _bake_version=v0.3.20240327 # v0.2.20230528 - It can run normally on macos -# todo -# 1. 当前 无法判断错误命令:./bake no_this_cmd ,因为不知道这是否是此命令的参数, -# 需要设置设一个简单的规则:只有叶子命令才能正常执行,这样非叶子命令就不需要有参数 -# 2. 当前 无法判断错误options:./bake --no_this_opt ,同上 -# 3. 类似flutter run [no-]pub 反向选项 -# # bake == (bash)ake == 去Make的bash tool +# +# https://github.com/chen56/bake +# # bake 是个简单的命令行工具,以替代Makefile的子命令功能 # make工具的主要特点是处理文件依赖进行增量编译,但flutter、golang、java、js项目的build工具 # 太厉害了,这几年唯一还在用Makefile的理由就是他的子命令机制: "make build"、 @@ -55,6 +52,12 @@ _bake_version=v0.3.20240327 # 范例可以看实际案例: # - https://github.com/chen56/note/blob/main/bake # - https://github.com/chen56/younpc/blob/main/bake +# todo +# 1. 当前 无法判断错误命令:./bake no_this_cmd ,因为不知道这是否是此命令的参数, +# 需要设置设一个简单的规则:只有叶子命令才能正常执行,这样非叶子命令就不需要有参数 +# 2. 当前 无法判断错误options:./bake --no_this_opt ,同上 +# 3. 类似flutter run [no-]pub 反向选项 +# # check bake dependencies @@ -126,7 +129,6 @@ bake._throw(){ return 1 } bake._error() { - if [[ "${_LOG_LEVELS[@]:0}" != *"$LOG"* ]]; then return 0; fi bake._log "$@" } bake._info() { @@ -140,7 +142,6 @@ bake._debug() { bake._log(){ local level; level=$1 - if [[ "${_LOG_LEVELS[@]:2}" != *"$LOG"* ]]; then return 0; fi echo -e "$level $(date "+%F %T") $(bake._pwd)\$ ${FUNCNAME[1]}() : $*" >&2 } @@ -168,13 +169,24 @@ TYPE_CMD="type:cmd" # bake._path_dirname a/b/c a/b => c bake._str_cutLeft() { printf "${1#$2}"; } -# Usage: bake.str.split [delimiter:default /] +# 分割字符串 +# Usage: bake._str_split [delimiter:default /] +# Example: bake._str_split "a/b/c" "/" +# => $'a\nb\nc' # 会把字符串分割为以换行符间隔的字符串 bake._str_split() { - local str=$1 delimiter=${2:-/} - # # use <() process-substitution - # # don't use <<< "" its add newline + #${delimiter:-DEFAULT} unset 或null 都给默认值 + # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html + local str="$1" + local delimiter="${2:-/}" + + # use <() process-substitution + # or here string <<< "" its add newline local arr - readarray -t arr < <(printf '%s' "${str//$delimiter/$'\n'}") + # https://helpmanual.io/builtin/readarray/ + # -d The first character of delim is used to terminate each input line, rather than newline. + # -t Remove a trailing delim (default newline) from each line read. + readarray -t -d "$delimiter" arr <<< "${str}" + # same as: for i in "${arr[@]}"; do echo "$i" done printf '%s\n' "${arr[@]}" }