Loading... Bat?大家会在什么情况下使用呢?印象中第一次使用Bat文件倒是在小学和初中吧,那个时候并没有什么很容易上手的“语言”,因此玩Bat也成了一种~~乐事~~ ## 关于Bat 现在写Bat则是有相应的工作需求了,目前的想法是提供一个Bat文件,让我们可以执行这个脚本后就可以完成对于Go项目的构建。 我们的项目是多个微服务聚合在同一个Repo中,因此构建成为了比较关注的问题。 GitHub项目:[GuGoTik](https://github.com/gugoorg/gugotik) 下面是我写的构建Bat: ```bas @echo off echo Please Run Me on the root dir, not in scripts dir. IF EXIST output ( echo "Output dir existed, deleting and recreating..." rd /s /q output ) mkdir output\services pushd src\services for /D %%i in (*) do ( if not "%%i"=="health" ( set "name=%%i" setlocal enabledelayedexpansion set "capName=!name:~0,1!" set "capName=!capName:a=A!" set "capName=!capName:b=B!" set "capName=!capName:c=C!" set "capName=!capName:d=D!" set "capName=!capName:e=E!" set "capName=!capName:f=F!" set "capName=!capName:g=G!" set "capName=!capName:h=H!" set "capName=!capName:i=I!" set "capName=!capName:j=J!" set "capName=!capName:k=K!" set "capName=!capName:l=L!" set "capName=!capName:m=M!" set "capName=!capName:n=N!" set "capName=!capName:o=O!" set "capName=!capName:p=P!" set "capName=!capName:q=Q!" set "capName=!capName:r=R!" set "capName=!capName:s=S!" set "capName=!capName:t=T!" set "capName=!capName:u=U!" set "capName=!capName:v=V!" set "capName=!capName:w=W!" set "capName=!capName:x=X!" set "capName=!capName:y=Y!" set "capName=!capName:z=Z!" set "capName=!capName!!name:~1!" cd %%i go build -o ../../../output/services/%%i/!capName!Service.exe cd .. endlocal ) ) popd mkdir output\gateway cd src\web go build -o ../../output/gateway/Gateway.exe echo OK! ``` 我很难描述这个脚本的质量,先说场景: 我们的网关就一个,肯定可以直接指定为GateWay.exe 但是关于我们的微服务,那就不一样了,我们的微服务在 services 文件夹下,是由多个子文件夹存储的,大家知道我们Go的包一般都是小写,例如我们可能有 `services/auth` 文件夹,下面是 auth 微服务。而我想生成的是 `AuthService`,那怎么把 `a` 变成 `A`,就变成比较关键的问题了:( 但是我们的Bat并不支持 ToUpper,那么我们只能手动硬编码了... 如果是PS,那我们肯定可以做的更优雅: ```powershell Write-Host "Please Run Me on the root dir, not in scripts dir." if (Test-Path output) { Write-Host "Output dir existed, deleting and recreating..." Remove-Item -Recurse -Force output } New-Item -ItemType Directory -Path output\services | Out-Null Set-Location src\services $directories = Get-ChildItem -Directory | Where-Object { $_.Name -ne 'health' } foreach ($dir in $directories) { $capitalizedName = $dir.Name.Substring(0, 1).ToUpper() + $dir.Name.Substring(1) Set-Location -Path $dir.FullName & go build -o "../../../output/services/$($dir.Name)/$($capitalizedName)Service.exe" Set-Location -Path ".." } Set-Location "..\.." New-Item -ItemType Directory -Path output\gateway | Out-Null Set-Location src\web & go build -o "../../output/gateway/GateWay.exe" Set-Location "..\.." Write-Host "OK!" ``` ## 番外,关于Bash实现 Bash才是更优雅: ```bash #!/bin/bash echo "Please Run Me on the root dir, not in scripts dir." if [ -d "output" ]; then echo "Output dir existed, deleting and recreating..." rm -rf output fi mkdir -p output/services pushd src/services || exit for i in *; do if [ "$i" != "health" ]; then name="$i" capName="${name^}" cd "$i" || exit go build -o "../../../output/services/$i/${capName}Service" cd .. fi done popd || exit mkdir -p output/gateway cd src/web || exit go build -o "../../output/gateway/Gateway" echo "OK!" ``` 最后修改:2023 年 08 月 02 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 3 如果你觉得文章帮到你了,请以沫喝杯奶茶吧~