JAL-3830 Powershell wrapper now works cross-platform. Who needs bash?!
[jalview.git] / utils / getdown / jalviewc.ps1
1 #!/usr/bin/env pwsh
2
3 # save args and first parameter
4 $ARGS = $args
5 $ARG1 = $args[0]
6 # parent dir of this script (should be the getdown app dir)
7 $DIR = Split-Path $MyInvocation.MyCommand.Path -Parent
8
9 # insert an "-open" parameter to Jalview's ARGS if ARG1 is non-zero-length, and not "open" or starts with a "-"
10 if ( $ARG1.length -gt 0 -and ( -not $ARG1.StartsWith("-") ) -and $ARG1 -cne "open" ) {
11   $ARGS = "-open " + $ARGS
12 }
13
14 $APPDIR = $DIR
15 $JAVAEXE = ( $IsWindows ? "java.exe" : "java" )
16 $JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + ( $IsMacOs ? "Contents/Home/" : "" ) + "bin/${JAVAEXE}" )
17 $GETDOWNTXT = Join-Path -Path $APPDIR -ChildPath "getdown.txt"
18
19 Write-Host "JAVA=$JAVA"
20 # look for getdown.txt -- needed to create classpath
21 if ( -not ( Test-Path -Path "${GETDOWNTXT}" ) ) {
22   throw "Cannot find getdown.txt"
23 }
24
25 # look for bundled JRE. Might not be there if unix installer used in which case just invoke "java"
26 if ( -not ( Test-Path -Path "${JAVA}" ) ) {
27   Write-Host "Cannot find bundled ${JAVAEXE}. Using system ${JAVAEXE} and hoping for the best!"
28   $JAVA = $JAVAEXE
29 }
30
31 $CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $DIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join ( $IsWindows ? ";" : ":" )
32
33 Invoke-Expression -Command "${JAVA} -cp '${CLASSPATH}' jalview.bin.Launcher $ARGS"
34