77 lines
3 KiB
PowerShell
77 lines
3 KiB
PowerShell
# watch-vizion-repos.ps1
|
|
# Watches VizionAI-Trading-Repo for new repo folders and auto-creates
|
|
# NTFS junctions in Experts\Shared Projects.
|
|
# Registered as a Task Scheduler job — runs at user logon, stays in background.
|
|
|
|
$base = "C:\Users\sahrk\AppData\Roaming\MetaQuotes\Terminal\FB9A56D617EDDDFE29EE54EBEFFE96C1\MQL5"
|
|
$repoRoot = "$base\Shared Projects\VizionAI-Trading\VizionAI-Trading-Repo"
|
|
$linkRoot = "$base\Experts\Shared Projects"
|
|
$logFile = "$base\Shared Projects\VizionAI-Trading\watch-vizion-repos.log"
|
|
|
|
function Write-Log($msg) {
|
|
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
"[$ts] $msg" | Tee-Object -FilePath $logFile -Append | Out-Null
|
|
}
|
|
|
|
function Sync-Junctions {
|
|
if (-not (Test-Path $linkRoot)) {
|
|
New-Item -ItemType Directory -Path $linkRoot -Force | Out-Null
|
|
}
|
|
|
|
$repos = Get-ChildItem -Path $repoRoot -Directory -ErrorAction SilentlyContinue
|
|
foreach ($repo in $repos) {
|
|
$linkPath = "$linkRoot\$($repo.Name)"
|
|
$targetPath = $repo.FullName
|
|
|
|
if (Test-Path $linkPath) {
|
|
$item = Get-Item $linkPath -Force
|
|
if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
|
|
continue # Junction already exists
|
|
} else {
|
|
Remove-Item -Path $linkPath -Recurse -Force
|
|
Write-Log "Removed stale folder: $($repo.Name)"
|
|
}
|
|
}
|
|
|
|
cmd /c "mklink /J `"$linkPath`" `"$targetPath`"" | Out-Null
|
|
Write-Log "Created junction: $($repo.Name)"
|
|
}
|
|
}
|
|
|
|
# Run once on startup to catch anything added while the watcher was off
|
|
Write-Log "Watcher started. Running initial sync..."
|
|
Sync-Junctions
|
|
|
|
# Set up FileSystemWatcher on the repo root
|
|
$watcher = New-Object System.IO.FileSystemWatcher
|
|
$watcher.Path = $repoRoot
|
|
$watcher.NotifyFilter = [System.IO.NotifyFilters]::DirectoryName
|
|
$watcher.IncludeSubdirectories = $false
|
|
$watcher.EnableRaisingEvents = $true
|
|
|
|
$action = {
|
|
$name = $Event.SourceEventArgs.Name
|
|
Start-Sleep -Milliseconds 500 # brief wait for git to finish creating the folder
|
|
$base2 = "C:\Users\sahrk\AppData\Roaming\MetaQuotes\Terminal\FB9A56D617EDDDFE29EE54EBEFFE96C1\MQL5"
|
|
$linkRoot2 = "$base2\Experts\Shared Projects"
|
|
$target = $Event.SourceEventArgs.FullPath
|
|
$link = "$linkRoot2\$name"
|
|
if (-not (Test-Path $link)) {
|
|
cmd /c "mklink /J `"$link`" `"$target`"" | Out-Null
|
|
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
"[$ts] Auto-created junction: $name" | Out-File -FilePath "$base2\Shared Projects\VizionAI-Trading\watch-vizion-repos.log" -Append
|
|
}
|
|
}
|
|
|
|
Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier "VizionRepoWatcher" -Action $action
|
|
|
|
Write-Log "Watching $repoRoot for new repos..."
|
|
|
|
# Keep the script alive indefinitely
|
|
try {
|
|
while ($true) { Start-Sleep -Seconds 30 }
|
|
} finally {
|
|
Unregister-Event -SourceIdentifier "VizionRepoWatcher" -ErrorAction SilentlyContinue
|
|
$watcher.Dispose()
|
|
Write-Log "Watcher stopped."
|
|
}
|