Files
2026-08-13 09:31:12 +08:00

135 lines
3.3 KiB
Bash

if ! type "include" > /dev/null 2>&1; then
include() {
. "$(cd "$(dirname "$1")" && pwd)/${2}";
};
fi
include "${BASH_SOURCE[0]}" ../pragma_once.bash
if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi
include "${BASH_SOURCE[0]}" ./global_rely/export.bash
include "${BASH_SOURCE[0]}" ./other.bash
run_native_cmd_script() {
local workdir="$1"
local cmd_body="$2"
local mode="${3:-c}"
local win_workdir
local tmp_cmd
local win_tmp_cmd
[[ -n "$workdir" && -n "$cmd_body" ]] || {
echo "[ERROR] 用法: run_native_cmd_script <workdir> <cmd_body> [c|k]" >&2
return 1
}
[[ -d "$workdir" ]] || {
echo "[ERROR] 工作目录不存在: $workdir" >&2
return 1
}
[[ "$mode" == "c" || "$mode" == "k" ]] || {
echo "[ERROR] mode 只能是 c 或 k" >&2
return 1
}
win_workdir="$(cygpath -aw "$workdir")" || return 1
tmp_cmd="$(mktemp --suffix=.cmd)" || {
echo "[ERROR] 创建临时 cmd 文件失败" >&2
return 1
}
cat > "$tmp_cmd" <<EOF
@echo off
cd /d "$win_workdir"
$cmd_body
EOF
win_tmp_cmd="$(cygpath -aw "$tmp_cmd")" || {
rm -f "$tmp_cmd"
return 1
}
WIN_TMP_CMD="$win_tmp_cmd" \
WIN_MODE="$mode" \
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command '
$cmdFile = $env:WIN_TMP_CMD
$mode = $env:WIN_MODE
$machine = [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::Machine)
$user = [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::User)
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $env:ComSpec
$psi.Arguments = "/$mode `"$cmdFile`""
$psi.WorkingDirectory = Split-Path $cmdFile
$psi.UseShellExecute = $false
$psi.EnvironmentVariables.Clear()
foreach ($k in $machine.Keys) {
$psi.EnvironmentVariables[$k] = [string]$machine[$k]
}
foreach ($k in $user.Keys) {
$psi.EnvironmentVariables[$k] = [string]$user[$k]
}
$machinePath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
$userPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
$mergedPath = (($machinePath, $userPath | Where-Object { $_ -and $_.Trim() }) -join ";")
if ($mergedPath) {
$psi.EnvironmentVariables["Path"] = $mergedPath
}
[System.Diagnostics.Process]::Start($psi) | Out-Null
'
}
# 用原生 Windows 环境执行 bat
#
# 用法:
# run_bat_native_env "/d/cef_builds/cef_4_3_840/depot_tools/update_depot_tools.bat"
# run_bat_native_env "/d/cef_builds/cef_4_3_840/depot_tools/update_depot_tools.bat" "k"
#
# 说明:
# 1. 默认用 /c,执行完退出
# 2. 传 k 时,用 /k,执行完保留 cmd 窗口
run_bat_native_env() {
local bat_path="$1"
local mode="${2:-c}"
local bat_dir
local bat_name
[[ -f "$bat_path" ]] || {
echo "[ERROR] bat 文件不存在: $bat_path" >&2
return 1
}
bat_dir="$(dirname "$bat_path")"
bat_name="$(basename "$bat_path")"
run_native_cmd_script "$bat_dir" "call \"$bat_name\"" "$mode"
}
# 用原生 Windows 环境执行 bat
#
# 用法:
# run_bat_native_env "/d/cef_builds/cef_4_3_840/depot_tools/update_depot_tools.bat"
# run_bat_native_env "/d/cef_builds/cef_4_3_840/depot_tools/update_depot_tools.bat" "k"
#
# 说明:
# 1. 默认用 /c,执行完退出
# 2. 传 k 时,用 /k,执行完保留 cmd 窗口