From 63927682207308d7e2fc5c1aa343bc97cb3d3807 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Mon, 15 Mar 2021 15:21:53 +0000 Subject: [PATCH] JAL-3830 powershell wrapper tested on linux, macos and windows including spaces in paths. This is the way. --- utils/getdown/jalviewc.ps1 | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/utils/getdown/jalviewc.ps1 b/utils/getdown/jalviewc.ps1 index e8c33b6..0a5802b 100755 --- a/utils/getdown/jalviewc.ps1 +++ b/utils/getdown/jalviewc.ps1 @@ -1,20 +1,34 @@ #!/usr/bin/env pwsh # save args and first parameter -$ARGS = $args -$ARG1 = $args[0] +$myArgs = $args.Clone() +$myArg1 = $args[0] + +# setup for powershell version < 6.0 +[bool] $myIsWindows = 0 +[bool] $myIsMacOS = 0 +if ( $IsWindows -eq $null ) { + # for powershell version < 6.0 let's assume Windows + $myIsWindows = 1 + $myIsMacOS = 0 +} else { + $myIsWindows = $IsWindows + $myIsMacOS = $IsMacOS +} + # parent dir of this script (should be the getdown app dir). Follow symlinks. $TARGET = ( Get-Item $MyInvocation.MyCommand.Path ).Target -$DIR = ( $TARGET -eq $null ) ? ( Split-Path $MyInvocation.MyCommand.Path -Parent ) : ( Split-Path $TARGET -Parent ) +$DIR = If ( $TARGET -eq $null -or $TARGET.LinkType -ne "SymbolicLink" ) { Split-Path $MyInvocation.MyCommand.Path -Parent } Else { Split-Path $TARGET -Parent } -# insert an "-open" parameter to Jalview's ARGS if ARG1 is non-zero-length, and not "open" or starts with a "-" -if ( $ARG1.length -gt 0 -and ( -not $ARG1.StartsWith("-") ) -and $ARG1 -cne "open" ) { - $ARGS = "-open " + $ARGS +# 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" } $APPDIR = $DIR -$JAVAEXE = ( $IsWindows ? "java.exe" : "java" ) -$JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + ( $IsMacOs ? "Contents/Home/" : "" ) + "bin/${JAVAEXE}" ) +$JAVAEXE = If ( $myIsWindows ) { "java.exe" } Else { "java" } +$JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + $( If ( $myIsMacOS ) { "Contents/Home/" } Else { "" } ) + "bin/${JAVAEXE}" ) $GETDOWNTXT = Join-Path -Path $APPDIR -ChildPath "getdown.txt" # look for getdown.txt -- needed to create classpath @@ -28,7 +42,9 @@ if ( -not ( Test-Path -Path "${JAVA}" ) ) { $JAVA = $JAVAEXE } -$CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join ( $IsWindows ? ";" : ":" ) +$CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join $( If ( $myIsWindows ) { ";" } Else { ":" } ) -Invoke-Expression -Command "${JAVA} -cp '${CLASSPATH}' jalview.bin.Launcher $ARGS" +# 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}" -- 1.7.10.2