axmol/cmake/sync_folder.ps1

74 lines
2.1 KiB
PowerShell
Raw Normal View History

param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$srcDir,
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$destDir,
[Parameter(Mandatory=$false, ValueFromPipeline=$true)]
[PSDefaultValue(Value=$null)]
$linkOnly,
$wasm = $null
)
2023-08-27 01:18:48 +08:00
function ParseBoolFuzzy($value) {
$value = "$value".ToLower()
return $value.startsWith('1') -or $value.StartsWith('t') -or $value.StartsWith('y')
2023-08-27 01:18:48 +08:00
}
$wasm = ParseBoolFuzzy($wasm)
if ($wasm) {
Write-Host "sync_folder.ps1: Skipping sync folder for target platform 'wasm'"
return
}
# 0: windows, 1: linux, 2: macos
$IsWin = $IsWindows -or ("$env:OS" -eq 'Windows_NT')
2023-08-27 01:18:48 +08:00
$linkOnly = ParseBoolFuzzy($linkOnly)
2023-08-27 01:18:48 +08:00
# convert to native path style
if ($IsWin) {
$srcDir = $srcDir.Replace('/', '\')
$destDir = $destDir.Replace('/', '\')
2023-08-27 01:18:48 +08:00
} else {
$srcDir = $srcDir.Replace('\', '/')
$destDir = $destDir.Replace('\', '/')
2023-08-27 01:18:48 +08:00
}
2023-08-27 13:01:07 +08:00
if(!$srcDir -or !(Test-Path $srcDir -PathType Container)) {
throw "sync_folder.ps1: The source directory $srcDir not exist"
2023-08-27 13:01:07 +08:00
}
if (Test-Path $destDir -PathType Container) { # dest already exist
if ($linkOnly) { # is symlink and dest exist
$directoryInfo = (Get-Item $destDir)
if ($directoryInfo.Target -eq $srcDir) {
Write-Host "sync_folder.ps1: Symlink $destDir ===> $($directoryInfo.Target) exists"
2023-08-27 13:01:07 +08:00
return
}
Write-Host "sync_folder.ps1: Removing old link target $($directoryInfo.Target)"
2023-08-27 13:01:07 +08:00
# Remove-Item -Path $destDir
$directoryInfo.Delete($false)
}
}
if ($linkOnly) {
Write-Host "sync_folder.ps1: Linking $srcDir to $destDir ..."
2023-08-27 01:18:48 +08:00
if ($IsWin) {
cmd.exe /c mklink /J $destDir $srcDir
}
else {
# ln -s $srcDir $destDir
New-Item -ItemType SymbolicLink -Path $destDir -Target $srcDir
}
}
else { # copy directory, remove first?
Write-Host "sync_folder.ps1: Copying $srcDir to $destDir ..."
2023-08-27 13:01:07 +08:00
if (!(Test-Path $destDir -PathType Container)) {
Copy-Item $srcDir $destDir -Recurse -Force
} else {
Copy-Item $srcDir/* $destDir/ -Recurse -Force
}
2023-08-27 01:18:48 +08:00
}