From ec4d1a9d385baab1cf63a6ff0924051620b0bae3 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 30 Mar 2021 01:43:52 +0100 Subject: [PATCH] JAL-3830 Follow all SymbolicLinks in Windows powershell wrapper --- utils/getdown/bin/jalview.ps1 | 45 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/utils/getdown/bin/jalview.ps1 b/utils/getdown/bin/jalview.ps1 index b84e275..7ae9a57 100755 --- a/utils/getdown/bin/jalview.ps1 +++ b/utils/getdown/bin/jalview.ps1 @@ -16,9 +16,46 @@ if ( $IsWindows -eq $null ) { $myIsMacOS = $IsMacOS } -# parent dir of this script (should be the getdown app dir). Follow symlinks. -$TARGET = ( Get-Item $MyInvocation.MyCommand.Path ).Target -$DIR = If ( $TARGET -eq $null -or $TARGET.LinkType -ne "SymbolicLink" ) { Split-Path -Path $MyInvocation.MyCommand.Path -Parent } Else { Split-Path -Path $TARGET -Parent } +# parent dir of this actual script (which should be the getdown appdir/bin). Follow all symlinks. Like GNU readlink -f +function Readlink-f { + Param($Link) + $Return = $null + $c = 0 + $max = 100 # just in case we end up in a loop + [bool] $found = 0 + $file = Get-Item -Path $Link + $prevfile = $null + While ( $c -lt $max -and "${file}" -ne "${prevfile}" -and -not $found ) { + $prevfile = $file + [string] $target = ( $file ).Target + If ( $target -eq $null -or ( $file ).LinkType -ne "SymbolicLink" ) { + $Return = $file + $found = 1 + } Else { + If ( $( Split-Path -Path $target -IsAbsolute ) ) { + $file = Get-Item -Path $target + } Else { +# symbolic link is relative: combine previous link parent dir with the link target and resolve + $file = Get-Item -Path ( Join-Path -Path ( Split-Path -Path $prevfile -Parent ) -ChildPath $target -Resolve ) + } + } + $c++ + } + if ( -not $found ) { + throw "Could not determine path to actual file $( Split-Path -Path $Link -Leaf )" + } + $Return +} + +# Avert problem with unix version of powershell and tell user the reason (Windows must always have .ps1 extension) +if ( $MyInvocation.MyCommand.Path -eq $null ) { + throw "Script or link to script must have extension .ps1" +} + + +$CMDPATH = ( Get-Item $MyInvocation.MyCommand.Path ) +$SCRIPTPATH = Readlink-f -Link $CMDPATH +$DIR = Split-Path -Path $SCRIPTPATH -Parent # set the "-open" parameter if myArg1 is non-zero-length, and not "open" or starts with a "-" $OPEN = "" @@ -33,7 +70,7 @@ $GETDOWNTXT = Join-Path -Path $APPDIR -ChildPath "getdown.txt" # look for getdown.txt -- needed to create classpath if ( -not ( Test-Path -Path "${GETDOWNTXT}" ) ) { - throw "Cannot find getdown.txt" + throw "Cannot find ${GETDOWNTXT}" } # look for bundled JRE. Might not be there if unix installer used in which case just invoke "java" -- 1.7.10.2