Skip to content

Commit

Permalink
把bake.bash搬来mono项目好了
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed Apr 18, 2024
1 parent c255007 commit 1e92cb1
Show file tree
Hide file tree
Showing 11 changed files with 3,874 additions and 58 deletions.
108 changes: 52 additions & 56 deletions bake
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,15 @@ SCRIPT_FILE="$(basename "$SCRIPT_PATH")"
# 进入脚本所在目录,这样上下文就是本项目了
cd "$SCRIPT_DIR" || exit 200

bake.upgrade(){
mkdir -p "$SCRIPT_DIR/vendor"
echo "bake -> bake.upgrade ▶︎【curl -o $SCRIPT_DIR/bake.bash https://github.com/chen56/bake/raw/main/bake.bash】"
curl -L -o "$SCRIPT_DIR/vendor/bake.bash" https://github.com/chen56/bake/raw/main/bake.bash ;
}
if ! [[ -f "$SCRIPT_DIR/vendor/bake.bash" ]]; then
bake.upgrade
fi

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

##########################################
# 应用的命令脚本 , 公共函数和全局变量
##########################################

declare -A _pkgs=(
["bake"]="$SCRIPT_DIR"
["bake"]="$SCRIPT_DIR/packages/you_bake"
["learn_dart"]="$SCRIPT_DIR/notes/learn_dart"
["you"]="$SCRIPT_DIR"
["note_dart"]="$SCRIPT_DIR/packages/note_dart"
Expand All @@ -35,20 +26,6 @@ declare -A _pkgs=(
["shell"]="$SCRIPT_DIR/notes/shell"
)

bake.cmd --cmd pkgs --desc "<mono> mono project manage: ./$SCRIPT_FILE pkgs <ls|run>"
ls(){
for pkg in ${!_pkgs[*]} ; do
echo "$pkg:${_pkgs[$pkg]}"
done
}

bake.cmd --cmd pkgs.run --desc "<mono> run cmd on all pkg,Usage: ./$SCRIPT_FILE pkgs run [any cmd]"
run(){
for pkg in ${!_pkgs[*]} ; do
# 子shell内执行,防止环境感染
( cd "${_pkgs[$pkg]}" || exit 200 ; _run "$@" ; )
done
}

# 运行所用项目的某子命令
# Usage: _run_all_package <subcmd>
Expand Down Expand Up @@ -103,37 +80,6 @@ _func_exists(){
# 应用的命令脚本
##########################################

bake.cmd --cmd root --desc "$( cat <<-EOF
___ _ _ _ _ _ _
| __|| | _ _ | |_ | |_ ___ _ _ | \| | ___ | |_ ___
| _| | || || || _|| _|/ -_)| '_| | . |/ _ \| _|/ -_)
|_| |_| \_._| \__| \__|\___||_| |_|\_|\___/ \__|\___|
flutter-note build tools.
https://github.com/chen56/note
PWD: $PWD
Usage:
./bake [cmd] [opts] [args...]
Examples:
./bake # same as './bake -h'
./bake -h # show all commands help
./bake -h --debug # show all commands help , include internal function
./bake flutter dev # == cd notes/flutter_web && flutter run --device-id macos
./bake test # test all projects
./bake build # defalut build == flutter build web --web-renderer html
./bake preview # defalut preview == start server at web build
./bake test # test all projects
./bake pkgs ls # show all mono project
./bake pkgs run pwd # run "pwd" on all mono project dir
./bake pkgs run flutter pub get # run "flutter pub get" on all mono project dir
EOF
)"

# 根项目,主要是bin/辅助工具等
you.run() { _run "$@";}
Expand All @@ -143,6 +89,10 @@ you.upgrade() ( you.run dart pub upgrade ;)
you.test() ( you.run dart test;)
you.build() ( you.run dart compile exe bin/notecli.dart ;)

# bash common script lib
bake.run() { _run "$@";}
bake.test() ( packages/you_bake/test.bash test;)

note_dart.run() ( cd packages/note_dart || return 200 && _run "$@")
note_dart.install() ( note_dart.run flutter pub get)
note_dart.clean() ( note_dart.run flutter clean; rm -rf build;)
Expand Down Expand Up @@ -180,6 +130,17 @@ upgrade() { _run_all_package upgrade;}
clean() { _run_all_package clean; }
test() { _run_all_package test; }
gen() { _run_all_package gen; }
ls(){
for pkg in ${!_pkgs[*]} ; do
echo "$pkg:${_pkgs[$pkg]}"
done
}
run(){
for pkg in ${!_pkgs[*]} ; do
# 子shell内执行,防止环境感染
( cd "${_pkgs[$pkg]}" || exit 200 ; _run "$@" ; )
done
}

# github 发布时使用,参考[.github/workflows/*.yaml]
docker.build() (
Expand All @@ -203,5 +164,40 @@ info() {
####################################################
# app entry script & _root cmd
####################################################
bake.cmd --cmd ls --desc "<mono> mono project manage: ./$SCRIPT_FILE pkgs <ls|run>"
bake.cmd --cmd run --desc "<mono> run cmd on all pkg,Usage: ./$SCRIPT_FILE pkgs run [any cmd]"

bake.cmd --cmd root --desc "$( cat <<-EOF
___ _ _ _ _ _ _
| __|| | _ _ | |_ | |_ ___ _ _ | \| | ___ | |_ ___
| _| | || || || _|| _|/ -_)| '_| | . |/ _ \| _|/ -_)
|_| |_| \_._| \__| \__|\___||_| |_|\_|\___/ \__|\___|
https://github.com/chen56/you/flutter_web
PWD: $PWD
Usage:
./bake [cmd] [opts] [args...]
Examples:
./bake # same as './bake -h'
./bake -h # show all commands help
./bake -h --debug # show all commands help , include internal function
./bake flutter dev # == cd notes/flutter_web && flutter run --device-id macos
./bake test # test all projects
./bake build # defalut build == flutter build web --web-renderer html
./bake preview # defalut preview == start server at web build
./bake test # test all projects
./bake pkgs ls # show all mono project
./bake pkgs run pwd # run "pwd" on all mono project dir
./bake pkgs run flutter pub get # run "flutter pub get" on all mono project dir
EOF
)"

bake.go "$@"

4 changes: 2 additions & 2 deletions notes/shell/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ SCRIPT_FILE="$(basename "$SCRIPT_PATH")"

_install_bake(){
mkdir -p "$SCRIPT_DIR/vendor"
echo "$SCRIPT_PATH -> _install_bake ▶︎【curl -o $SCRIPT_DIR/bake.bash https://github.com/chen56/bake/raw/main/bake.bash】"
curl -L -o "$SCRIPT_DIR/vendor/bake.bash" https://github.com/chen56/bake/raw/main/bake.bash ;
echo "$SCRIPT_PATH -> _install_bake ▶︎【curl -o $SCRIPT_DIR/bake.bash https://github.com/chen56/you/raw/main/packages/you_bake/bake.bash】"
curl -L -o "$SCRIPT_DIR/vendor/bake.bash" https://github.com/chen56/you/raw/main/packages/you_bake/bake.bash ;
}
if ! [[ -f "$SCRIPT_DIR/vendor/bake.bash" ]]; then
_install_bake
Expand Down
117 changes: 117 additions & 0 deletions packages/you_bake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# bake


```bash

# bake == (bash)ake == 去Make的bash tool
#
# https://github.com/chen56/you/packages/you_bake
#
# bake 是个简单的命令行工具,以替代Makefile的子命令功能
# make工具的主要特点是处理文件依赖进行增量编译,但flutter、golang、java、js项目的build工具
# 太厉害了,这几年唯一还在用Makefile的理由就是他的子命令机制: "make build"、
# "make run", 可以方便的自定义单一入口的父子命令,但Makefile本身的语法套路也很复杂,
# 很多批处理还是要靠bash, 这就尴尬了,工具太多,麻烦!本脚本尝试彻底摆脱使用Makefile。
# 经尝试,代码很少啊 ,核心代码几百行啊,父子命令二三百行左右,option解析二三百行左右,功能足够了:
#
# bake命令规则:
# 1. 函数即命令,所有bake内的函数均可以在脚本外运行:
# ./bake [all function] # bake内的所有函数均可以在脚本外直接运行
# ./bake info # 比如这个内部函数, 看bake内部变量,调试脚本用
# ./bake test # 你如果定义过test()函数,就可以这样运行
# 2. 带"."的函数,形成父子命令:
# web.build(){ echo "build web app"; }
# web.test(){ echo "build web app"; }
# 可以这样调用
# ./bake web -h # 运行子命令,或看帮助
# ./bake web.build -h # 上面等同
# ./bake web test -h # 运行子命令,或看帮助
# ./bake web.test -h # 与上面等同
# 3. 特殊的root命令表示根命令
# ./bake # 如果有root()函数,就执行它
# 4. 像其他高级语言的cli工具一样,用简单变量就可以获取命令option:
# # a. 先在bake文件里里定义app options
# bake.opt --cmd build --long "target" --type string
# # b. 解析和使用option
# function build() {
# eval "$(bake.parse "$@")";
# echo "build ... your option:target: $target";
# }
# # c. 调用看看:
# ./bake build --target "macos"
# 5. bake尽量不依赖bash以外的其他工具,包括linux coreutils,更简单通用,但由于用了关联数组等
# 依赖bash4+
# 6. 有两种用法:
# - 这个文件copy走,把你的脚本放到本脚本最后即可.
# - 在你的脚本里直接curl下载本脚本后 source即可。
# 范例可以看实际案例:
# - 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 反向选项
# TODO
# 利用extdebug , declare -F xxx 可显示行号,这下顺序解决了!
# https://www.gnu.org/software/bash/manual/bash.html#The-Shopt-Builtin
#


```


## 范例

首先建立一个空脚本文件,可以叫bake,复制下面代码进去:

```bash
#!/usr/bin/env bash

# 我们都知道bash没什么有效的包管理工具,但没关系,curl就是包管理工具,在你的脚本复制下面模版
# 脚本动态安装bake.bash依赖到: vendor/bake.bash
if ! [[ -f "./vendor/bake.bash" ]]; then
mkdir -p "./vendor"
curl -L -o "./vendor/bake.bash" https://github.com/chen56/you/raw/main/packages/you_bake/bake.bash ;
fi
source "./vendor/bake.bash"

# 定义一些二级命令
install(){ echo "install deps"; }
clean(){ echo "clean project"; }
dev(){ echo "run dev mode"; }
preview(){ echo "run preview mode"; }
test(){ echo "test project"; }

# 更深层的命令: `./bake build -h` , `./bake build all`
build.all(){ echo "build all"; }
build.macos(){ echo "build macos package"; }
build.web(){ echo "build web package"; }
build.android(){ echo "build android package"; }
build.ios(){ echo "build ios package"; }

# bake走起
bake.go "$@"
```

运行`chmod +x ./bake ` 再运行它:`./bake -h` ,你会看到bake已经帮你组织好了子命令,就像docker/git等父子命令一样使用:

```bash
$ ./bake

Available Options:
--debug -d bool required:[false] debug mode, print more internal info
--help -h bool required:[false] print help, show all commands

Available Commands:
build
clean
dev
install
preview
test
```

查看更多范例:[examples](./examples/)
Loading

0 comments on commit 1e92cb1

Please sign in to comment.