2023-08-27 12:39:46 +08:00
|
|
|
param(
|
|
|
|
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
|
|
|
|
[string]$srcDir,
|
|
|
|
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
|
|
|
|
[string]$destDir,
|
|
|
|
[Parameter(Mandatory=$false, ValueFromPipeline=$true)]
|
|
|
|
[PSDefaultValue(Value=$null)]
|
2023-09-01 16:31:14 +08:00
|
|
|
$linkOnly,
|
|
|
|
$wasm = $null
|
2023-08-27 12:39:46 +08:00
|
|
|
)
|
2023-08-27 01:18:48 +08:00
|
|
|
|
2023-09-01 16:31:14 +08:00
|
|
|
|
2023-08-27 12:39:46 +08:00
|
|
|
function ParseBoolFuzzy($value) {
|
2023-09-01 16:31:14 +08:00
|
|
|
$value = "$value".ToLower()
|
2023-08-27 12:39:46 +08:00
|
|
|
return $value.startsWith('1') -or $value.StartsWith('t') -or $value.StartsWith('y')
|
2023-08-27 01:18:48 +08:00
|
|
|
}
|
|
|
|
|
2023-09-01 16:31:14 +08:00
|
|
|
$wasm = ParseBoolFuzzy($wasm)
|
|
|
|
|
|
|
|
if ($wasm) {
|
|
|
|
Write-Host "sync_folder.ps1: Skipping sync folder for target platform 'wasm'"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:39:46 +08:00
|
|
|
# 0: windows, 1: linux, 2: macos
|
|
|
|
$IsWin = $IsWindows -or ("$env:OS" -eq 'Windows_NT')
|
2023-08-27 01:18:48 +08:00
|
|
|
|
2023-09-01 16:31:14 +08:00
|
|
|
$linkOnly = ParseBoolFuzzy($linkOnly)
|
2023-08-27 01:18:48 +08:00
|
|
|
|
|
|
|
# convert to native path style
|
|
|
|
if ($IsWin) {
|
2023-08-27 12:39:46 +08:00
|
|
|
$srcDir = $srcDir.Replace('/', '\')
|
|
|
|
$destDir = $destDir.Replace('/', '\')
|
2023-08-27 01:18:48 +08:00
|
|
|
} else {
|
2023-08-27 12:39:46 +08:00
|
|
|
$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)) {
|
2023-09-01 16:31:14 +08:00
|
|
|
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) {
|
2023-09-01 16:31:14 +08:00
|
|
|
Write-Host "sync_folder.ps1: Symlink $destDir ===> $($directoryInfo.Target) exists"
|
2023-08-27 13:01:07 +08:00
|
|
|
return
|
|
|
|
}
|
2023-09-01 16:31:14 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:39:46 +08:00
|
|
|
if ($linkOnly) {
|
2023-09-01 16:31:14 +08:00
|
|
|
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?
|
2023-09-01 16:31:14 +08:00
|
|
|
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
|
|
|
}
|