From fbf3204e54192ed5894dd63f5f382354152b961a Mon Sep 17 00:00:00 2001 From: Damien Butt <22627489+damienbutt@users.noreply.github.com> Date: Thu, 26 Sep 2024 01:26:05 +0100 Subject: [PATCH] chore: update SymLink script --- SymLink.ps1 | 93 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 32 deletions(-) diff --git a/SymLink.ps1 b/SymLink.ps1 index b15a997..4962db3 100644 --- a/SymLink.ps1 +++ b/SymLink.ps1 @@ -1,3 +1,4 @@ +#!/usr/bin/env pwsh #Requires -RunAsAdministrator <# @@ -35,62 +36,90 @@ SOFTWARE. param ( [Parameter(Mandatory = $false)] + [ValidateNotNullOrEmpty()] [string] - $Path = ".", + $ModulePath = "C:\Program Files (x86)\Common Files\AMXShare\Duet\module", [Parameter(Mandatory = $false)] [string] - $ModulePath = "C:\Program Files (x86)\Common Files\AMXShare\Duet\module", + $IncludePath = "C:\Program Files (x86)\Common Files\AMXShare\AXIs", [Parameter(Mandatory = $false)] - [string] - $IncludePath = "C:\Program Files (x86)\Common Files\AMXShare\AXIs" + [switch] + $Delete = $false ) -try { - $Path = Resolve-Path $Path +$prevPWD = $PWD +Set-Location $PSScriptRoot - $directories = Get-ChildItem -Path $Path -Directory -Recurse | Where-Object { $_.FullName -notmatch "(.git|.history|node_modules)" } +try { + $moduleFiles = Get-ChildItem -File "**/*.axs" -ErrorAction SilentlyContinue | Where-Object { $_.FullName -notmatch "(.git|.history|node_modules)" } + $includeFiles = Get-ChildItem -File "**/*.axi" -ErrorAction SilentlyContinue | Where-Object { $_.FullName -notmatch "(.git|.history|node_modules)" } - $moduleFiles = $directories | Get-ChildItem -File -Include *.axs -ErrorAction SilentlyContinue - $includeFiles = $directories | Get-ChildItem -File -Include *.axi -ErrorAction SilentlyContinue + if (!$moduleFiles) { + Write-Host "No module files found" + exit 1 + } - if (!$moduleFiles -and !$includeFiles) { - Write-Host "No files found in $Path" -ForegroundColor Yellow - exit + # Ensure there is a compiled TKO file for each AXS file + foreach ($file in $moduleFiles) { + if (!(Test-Path $($file.FullName -replace ".axs", ".tko"))) { + Write-Host "TKO file not found for $file" -ForegroundColor Yellow + exit 1 + } } - foreach ($file in $includeFiles) { - $path = Join-Path -Path $IncludePath -ChildPath $file.Name - $target = $file.FullName + $ModulePath = Resolve-Path $ModulePath + $IncludePath = Resolve-Path $IncludePath + + !$Delete ? (Write-Host "Creating symlinks...") : (Write-Host "Deleting symlinks...") + + # It's possible to have a module without any AXI files + if ($includeFiles) { + foreach ($file in $includeFiles) { + $path = "$IncludePath\$($file.Name)" + + if ($Delete) { + Write-Verbose "Deleting symlink: $path" + Remove-Item -Path $path -Force | Out-Null + continue + } - Write-Host "Creating symlink: $path -> $target" -ForegroundColor Green - New-Item -ItemType SymbolicLink -Path $path -Target $target -Force | Out-Null + $target = $file.FullName + + Write-Verbose "Creating symlink: $path -> $target" + New-Item -ItemType SymbolicLink -Path $path -Target $target -Force | Out-Null + } } foreach ($file in $moduleFiles) { - if (!(Test-Path $($file.FullName -replace ".axs", ".tko"))) { - Write-Host "TKO file not found for $file" -ForegroundColor Yellow - continue - } + $axsPath = "$ModulePath\$($file.Name)" + $tkoPath = "$ModulePath\$($file.Name -replace ".axs", ".tko")" - $path = Join-Path -Path $ModulePath -ChildPath $file.Name - $target = $file.FullName + $axsTarget = $file.FullName + $tkoTarget = $file.FullName -replace ".axs", ".tko" - Write-Host "Creating symlink: $path -> $target" -ForegroundColor Green - New-Item -ItemType SymbolicLink -Path $path -Target $target -Force | Out-Null + if ($Delete) { + Write-Verbose "Deleting symlink: $axsPath" + Remove-Item -Path $axsPath -Force | Out-Null - $path = Join-Path -Path $ModulePath -ChildPath $($file.Name -replace ".axs", ".tko") - $target = $file.FullName -replace ".axs", ".tko" + Write-Verbose "Deleting symlink: $tkoPath" + Remove-Item -Path $tkoPath -Force | Out-Null - Write-Host "Creating symlink: $path -> $target" -ForegroundColor Green - New-Item -ItemType SymbolicLink -Path $path -Target $target -Force | Out-Null + continue + } + + Write-Verbose "Creating symlink: $axsPath -> $axsTarget" + New-Item -ItemType SymbolicLink -Path $axsPath -Target $axsTarget -Force | Out-Null + + Write-Verbose "Creating symlink: $tkoPath -> $tkoTarget" + New-Item -ItemType SymbolicLink -Path $tkoPath -Target $tkoTarget -Force | Out-Null } } catch { Write-Host $_.Exception.GetBaseException().Message -ForegroundColor Red exit 1 } - -Write-Host -Read-Host -Prompt "Press any key to exit..." +finally { + Set-Location $prevPWD +}