Skip to content

Commit

Permalink
Improve powershell 5.x compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
halx99 committed Mar 22, 2024
1 parent a4f5389 commit 17c7cd6
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions 1k/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ class build1k {
}

[System.Boolean] isfile([string]$path) {
return Test-Path $path -PathType Leaf
return $path -and (Test-Path $path -PathType Leaf)
}

[System.Boolean] isdir([string]$path) {
return Test-Path $path -PathType Container
return $path -and (Test-Path $path -PathType Container)
}

[void] mkdirs([string]$path) {
Expand Down Expand Up @@ -157,7 +157,7 @@ class build1k {
$executed_from_explorer = ($parentProcess.ProcessName -like "explorer")
if ($executed_from_explorer) {
$procesCmdLineArgs = "$([System.Environment]::GetCommandLineArgs())"
if ($procesCmdLineArgs.IndexOf('.ps1') -ne -1 -and $procesCmdLineArgs.IndexOf('-noexit') -eq -1) {
if ($procesCmdLineArgs.Contains('.ps1') -and !$procesCmdLineArgs.Contains('-noexit')) {
$shoud_pause = $true
}
}
Expand Down Expand Up @@ -418,7 +418,7 @@ $mirror = if ($b1k.isfile($sentry_file)) { 'gitee' } else { 'github' }
$devtools_url_base = @{'github' = 'https://github.com/'; 'gitee' = 'https://gitee.com/' }[$mirror]
$mirror_conf_file = $b1k.realpath("$myRoot/../manifest.json")
$mirror_conf = $null
if (Test-Path $mirror_conf_file -PathType Leaf) {
if ($b1k.isfile($mirror_conf_file)) {
$mirror_conf = ConvertFrom-Json (Get-Content $mirror_conf_file -raw)
$devtools_url_base += $mirror_conf.mirrors.$mirror.'1kdist'
$devtools_url_base += '/devtools'
Expand Down Expand Up @@ -559,7 +559,7 @@ function find_prog($name, $path = $null, $mode = 'ONLY', $cmd = $null, $params =

if (!$usefv) {
$verStr = $(. $cmd @params 2>$null) | Select-Object -First 1
if (!$verStr -or ($verStr.IndexOf('--version') -ne -1)) {
if (!$verStr -or $verStr.Contains('--version')) {
$verInfo = $cmd_info.Version
$verStr = "$($verInfo.Major).$($verInfo.Minor).$($verInfo.Build)"
}
Expand Down Expand Up @@ -662,7 +662,7 @@ function setup_nuget() {
}
}

if ($env:PATH.IndexOf($nuget_bin) -eq -1) {
if (!$env:PATH.Contains($nuget_bin)) {
$env:PATH = "$nuget_bin$ENV_PATH_SEP$env:PATH"
}
$b1k.println("Using nuget: $nuget_prog, version: $nuget_ver")
Expand Down Expand Up @@ -732,7 +732,7 @@ function setup_ninja() {

download_and_expand "https://github.com/ninja-build/ninja/releases/download/v$ninja_ver/ninja-$suffix.zip" $ninja_pkg "$external_prefix/ninja/"
}
if ($env:PATH.IndexOf($ninja_bin) -eq -1) {
if (!$env:PATH.Contains($ninja_bin)) {
$env:PATH = "$ninja_bin$ENV_PATH_SEP$env:PATH"
}
$ninja_prog = (Join-Path $ninja_bin "ninja$exeSuffix")
Expand All @@ -744,7 +744,7 @@ function setup_ninja() {
# setup cmake
function setup_cmake($skipOS = $false) {
$cmake_prog, $cmake_ver = find_prog -name 'cmake'
if ($cmake_prog -and (!$skipOS -or $cmake_prog.IndexOf($myRoot) -ne -1)) {
if ($cmake_prog -and (!$skipOS -or $cmake_prog.Contains($myRoot))) {
return $cmake_prog, $cmake_ver
}

Expand Down Expand Up @@ -808,7 +808,7 @@ function setup_cmake($skipOS = $false) {
$b1k.println("Using cmake: $cmake_prog, version: $cmake_ver")
}

if (($null -ne $cmake_bin) -and ($env:PATH.IndexOf($cmake_bin) -eq -1)) {
if (($null -ne $cmake_bin) -and (!$env:PATH.Contains($cmake_bin))) {
$env:PATH = "$cmake_bin$ENV_PATH_SEP$env:PATH"
}
return $cmake_prog, $cmake_ver
Expand Down Expand Up @@ -845,7 +845,7 @@ function setup_nsis() {
}
}

if ($env:PATH.IndexOf($nsis_bin) -eq -1) {
if (!$env:PATH.Contains($nsis_bin)) {
$env:PATH = "$nsis_bin$ENV_PATH_SEP$env:PATH"
}
$nsis_prog = (Join-Path $nsis_bin "makensis$exeSuffix")
Expand All @@ -862,10 +862,10 @@ function setup_nasm() {
if ($IsWindows) {
$nasm_bin = Join-Path $external_prefix "nasm-$nasm_ver"

if (!(Test-Path $nasm_bin -PathType Container)) {
if (!$b1k.isdir($nasm_bin)) {
download_and_expand "https://www.nasm.us/pub/nasm/releasebuilds/$nasm_ver/win64/nasm-$nasm_ver-win64.zip" "$external_prefix/nasm-$nasm_ver-win64.zip" "$external_prefix"
}
if ($env:PATH.IndexOf($nsis_bin) -eq -1) {
if (!$env:PATH.Contains($nsis_bin)) {
$env:PATH = "$nasm_bin$ENV_PATH_SEP$env:PATH"
}
}
Expand Down Expand Up @@ -913,7 +913,7 @@ function setup_jdk() {

$env:JAVA_HOME = $java_home
$env:CLASSPATH = ".;$java_home\lib\dt.jar;$java_home\lib\tools.jar"
if ($env:PATH.IndexOf($jdk_bin) -eq -1) {
if (!$env:PATH.Contains($jdk_bin)) {
$env:PATH = "$jdk_bin$ENV_PATH_SEP$env:PATH"
}
$javac_prog = find_prog -name 'jdk' -cmd 'javac' -path $jdk_bin -mode 'ONLY' -silent $true
Expand Down Expand Up @@ -943,14 +943,14 @@ function setup_llvm() {
else {
$7z_prog = Join-Path $external_prefix '7z2301-x64\7z.exe'
$7z_pkg_out = Join-Path $external_prefix '7z2301-x64.zip'
if (!(Test-Path $7z_prog -PathType Leaf)) {
if (!$b1k.isfile($7z_prog)) {
# https://www.7-zip.org/download.html
$7z_url = devtool_url '7z2301-x64.zip'
download_and_expand -url $7z_url -out $7z_pkg_out $external_prefix/
}
}

if (!(Test-Path $7z_prog -PathType Leaf)) {
if (!$b1k.isfile($7z_prog)) {
throw "setup 7z fail which is required for setup llvm clang!"
}

Expand All @@ -968,7 +968,7 @@ function setup_llvm() {
$b1k.println("Using llvm: $clang_prog, version: $clang_ver")

# add our llvm root to PATH temporary
if (($env:PATH.IndexOf($llvm_bin) -eq -1)) {
if (!$env:PATH.Contains($llvm_bin)) {
$env:PATH = "$llvm_bin$ENV_PATH_SEP$env:PATH"
}
}
Expand Down Expand Up @@ -996,7 +996,7 @@ function setup_android_sdk() {
# looking up require ndk installed in exists sdk roots
$sdk_root = $null
foreach ($sdk_dir in $sdk_dirs) {
if (!$sdk_dir -or !(Test-Path $sdk_dir -PathType Container)) {
if (!$sdk_dir -or !$b1k.isdir($sdk_dir)) {
continue
}
$b1k.println("Looking require $ndk_ver$IsGraterThan in $sdk_dir")
Expand Down Expand Up @@ -1121,7 +1121,7 @@ function setup_emsdk() {
$emsdk_cmd = (Get-Command emsdk -ErrorAction SilentlyContinue)
if (!$emsdk_cmd) {
$emsdk_root = Join-Path $external_prefix 'emsdk'
if (!(Test-Path $emsdk_root -PathType Container)) {
if (!$b1k.isdir($emsdk_root)) {
git clone 'https://github.com/emscripten-core/emsdk.git' $emsdk_root
}
else {
Expand Down Expand Up @@ -1165,7 +1165,7 @@ function setup_msvc() {

# msvc14x support
if ($Script:use_msvcr14x) {
if ("$env:LIB".IndexOf('msvcr14x') -eq -1) {
if (!"$env:LIB".Contains('msvcr14x')) {
$msvcr14x_root = $env:msvcr14x_ROOT
$env:Platform = $target_cpu
Invoke-Expression -Command "$msvcr14x_root\msvcr14x_nmake.ps1"
Expand Down Expand Up @@ -1193,7 +1193,7 @@ function setup_gclient() {
# download depot_tools
# git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git $gclient_dir
$gclient_dir = Join-Path $external_prefix 'depot_tools'
if (!(Test-Path $gclient_dir -PathType Container)) {
if (!$b1k.isdir($gclient_dir)) {
if ($IsWin) {
$b1k.mkdirs($gclient_dir)
Invoke-WebRequest -Uri "https://storage.googleapis.com/chrome-infra/depot_tools.zip" -OutFile "${gclient_dir}.zip"
Expand All @@ -1205,7 +1205,7 @@ function setup_gclient() {

}

if ($env:PATH.IndexOf($gclient_dir) -eq -1) {
if (!$env:PATH.Contains($gclient_dir)) {
$env:PATH = "${env:PATH}$ENV_PATH_SEP${gclient_dir}"
}
$env:DEPOT_TOOLS_WIN_TOOLCHAIN = 0
Expand Down Expand Up @@ -1314,7 +1314,7 @@ function preprocess_linux([string[]]$inputOptions) {
}

$ninja_prog = $null
$is_gradlew = $options.xt.IndexOf('gradlew') -ne -1
$is_gradlew = $options.xt.Contains('gradlew')
function preprocess_andorid([string[]]$inputOptions) {
$outputOptions = $inputOptions

Expand Down Expand Up @@ -1493,7 +1493,7 @@ elseif ($Global:is_android) {
$ndk_host = @('windows', 'linux', 'darwin').Get($HOST_OS)
$env:ANDROID_NDK_BIN = Join-Path $ndk_root "toolchains/llvm/prebuilt/$ndk_host-x86_64/bin"
function active_ndk_toolchain() {
if ($env:PATH.IndexOf($env:ANDROID_NDK_BIN) -eq -1) {
if (!$env:PATH.Contains($env:ANDROID_NDK_BIN)) {
$env:PATH = "$env:ANDROID_NDK_BIN$ENV_PATH_SEP$env:PATH"
}
$clang_prog, $clang_ver = find_prog -name 'clang'
Expand Down Expand Up @@ -1719,7 +1719,7 @@ if (!$setupOnly) {
$BUILD_ALL_OPTIONS += "--parallel"
$BUILD_ALL_OPTIONS += "$($options.j)"

if (($cmake_generator -eq 'Xcode') -and ($BUILD_ALL_OPTIONS.IndexOf('--verbose') -eq -1)) {
if (($cmake_generator -eq 'Xcode') -and !$BUILD_ALL_OPTIONS.Contains('--verbose')) {
$BUILD_ALL_OPTIONS += '--', '-quiet'
}
$b1k.println("BUILD_ALL_OPTIONS=$BUILD_ALL_OPTIONS, Count={0}" -f $BUILD_ALL_OPTIONS.Count)
Expand Down

0 comments on commit 17c7cd6

Please sign in to comment.