Merge branch 'develop' into bug/JAL-4235_gradle_task_jalviewjsTranspile_does_not_fail...
[jalview.git] / utils / getdown / bin / jalview.ps1
index b84e275..1098010 100755 (executable)
@@ -16,16 +16,47 @@ 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 }
-
-# set the "-open" parameter if myArg1 is non-zero-length, and not "open" or starts with a "-"
-$OPEN = ""
-if ( $myArg1.length -gt 0 -and ( -not $myArg1.StartsWith("-") ) -and $myArg1 -cne "open" ) {
-  $OPEN = "-open"
+# 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
+
 $APPDIR = If ( ( Split-Path -Path $DIR -Leaf ) -eq "bin" ) { Split-Path -Path $DIR -Parent } Else { $DIR }
 $JAVAEXE = If ( $myIsWindows ) { "java.exe" } Else { "java" }
 $JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + $( If ( $myIsMacOS ) { "Contents/Home/" } Else { "" } ) + "bin/${JAVAEXE}" )
@@ -33,7 +64,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"
@@ -44,7 +75,14 @@ if ( -not ( Test-Path -Path "${JAVA}" ) ) {
 
 $CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join $( If ( $myIsWindows ) { ";" } Else { ":" } )
 
+# get console width
+$CONSOLEWIDTH = $Host.UI.RawUI.WindowSize.Width
+
 # quote the args and the command (in case of spaces) with escape chars (`) and precede with & to indicate command not string
-$myArgsString = '"' + $($myArgs -join '" "') + '"'
-Invoke-Expression -Command "& `"${JAVA}`" -cp `"${CLASSPATH}`" jalview.bin.Launcher ${OPEN} ${myArgsString}"
+if ( $myArgs.count -eq 0 ) {
+  Invoke-Expression -Command "& `"${JAVA}`" `"-DCONSOLEWIDTH=${CONSOLEWIDTH}`" `"-Dgetdownappdir=${APPDIR}`" -cp `"${CLASSPATH}`" jalview.bin.Launcher"
+} else {
+  $myArgsString = '"' + $($myArgs -join '" "') + '"'
+  Invoke-Expression -Command "& `"${JAVA}`" `"-DCONSOLEWIDTH=${CONSOLEWIDTH}`" `"-Dgetdownappdir=${APPDIR}`" -cp `"${CLASSPATH}`" jalview.bin.Launcher ${myArgsString}"
+}