-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds install scripts for easier installation of matlab-batch
- Loading branch information
1 parent
6102dd9
commit a12591c
Showing
4 changed files
with
317 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
alternates/non-interactive/install/install-matlab-batch.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<# | ||
.SYNOPSIS | ||
Installs matlab-batch on a Windows system. | ||
.DESCRIPTION | ||
Downloads the matlab-batch executable. | ||
Creates a directory to place the executable into. | ||
Adds the executable onto the user PATH. | ||
.PARAMETER InstallLocation | ||
Specifies the location where matlab-batch will be downloaded to and installed in. | ||
Defaults to $ProgramFiles\MathWorks. | ||
.PARAMETER Help | ||
Shows a help message for the install script if present. | ||
.EXAMPLE | ||
Install-Batch | ||
Install-Batch -Help | ||
Install-Batch -InstallLocation C:\MyPrograms | ||
.NOTES | ||
Copyright 2024 The MathWorks, Inc. | ||
#> | ||
param ( | ||
[string]$InstallLocation='C:\Program Files\MathWorks', | ||
[switch]$Help | ||
) | ||
|
||
$DEFAULT_DOWNLOAD_BASE_URL = 'https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/win64/matlab-batch.exe' | ||
|
||
# Set strict error handling | ||
$ErrorActionPreference = 'Stop' | ||
|
||
# Show help | ||
function Show-Help { | ||
Write-Output "Usage: install-matlab-batch.ps1 [parameters]" | ||
Write-Output "" | ||
Write-Output "Options:" | ||
Write-Output " -Help Display this help message." | ||
Write-Output ' -InstallLocation Specify an install location. Defaults to $ProgramFiles\MathWorks.' | ||
Write-Output "" | ||
} | ||
|
||
# Function to download the matlab-batch executable binary | ||
function Download { | ||
param ( | ||
[string]$Url, | ||
[string]$Filename | ||
) | ||
Invoke-WebRequest -Uri $Url -OutFile $Filename | ||
} | ||
|
||
function Install-MatlabBatch { | ||
param ( | ||
[string]$InstallDirectory | ||
) | ||
|
||
Write-Output 'Starting Install-MatlabBatch...' | ||
|
||
# Resolve $InstallDirectory to an absolute path | ||
$InstallDirectory = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine((Get-Location), $InstallDirectory)) + "\matlab-batch" | ||
|
||
# Create standard location for install if it does not exist | ||
New-Item -ItemType Directory -Force -Path "$InstallDirectory" | ||
|
||
# Define the default download URL | ||
$BaseUrl = $DEFAULT_DOWNLOAD_BASE_URL | ||
try { | ||
Download "$BaseUrl" "$InstallDirectory\matlab-batch.exe" | ||
} | ||
catch { | ||
Write-Host "Failed to download matlab-batch ($($_.Exception.Message))" | ||
exit 1 | ||
} | ||
|
||
# Add the installation directory to the system PATH | ||
$SystemPath = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) | ||
$PathEntries = $SystemPath.Split(';') | ||
|
||
# Check for existing entries for the matlab-batch folder | ||
$ExistingEntries = @($PathEntries | Where-Object { $_ -like "*matlab-batch*" }) | ||
|
||
if ($ExistingEntries.Count -eq 0) { | ||
# No entry for matlab-batch on PATH | ||
$NewPath = $SystemPath + ';' + $InstallDirectory | ||
[System.Environment]::SetEnvironmentVariable('Path', $NewPath, [System.EnvironmentVariableTarget]::Machine) | ||
Write-Output "Added new installation directory to PATH. You will have to start a fresh PowerShell session to find it on PATH." | ||
} else { | ||
if ($ExistingEntries -contains $InstallDirectory) { | ||
# There are matlab-batch entries on PATH and an entry matches InstallLocation | ||
$FirstEntry = $ExistingEntries[0] | ||
|
||
if ($FirstEntry -ne $InstallDirectory) { | ||
# Matching entry is not at the start of the PATH, move it to the first entry | ||
$PathEntries = $PathEntries | Where-Object { $_ -ne $InstallDirectory } | ||
$FirstMatlabBatchIndex = [Array]::IndexOf($PathEntries, $FirstEntry) | ||
$PathEntries = $PathEntries[0..($FirstMatlabBatchIndex-1)] + $InstallDirectory + $PathEntries[$FirstMatlabBatchIndex..($PathEntries.Length-1)] | ||
$NewPath = $PathEntries -join ';' | ||
[System.Environment]::SetEnvironmentVariable('Path', $NewPath, [System.EnvironmentVariableTarget]::Machine) | ||
Write-Output "Moved current installation directory to the first position among matlab-batch entries. You will have to start a fresh PowerShell session to find it on PATH." | ||
} else { | ||
# Matching entry is the first matlab-batch entry on PATH, do nothing | ||
Write-Output "The current installation directory is already the first entry on PATH. No changes made." | ||
} | ||
} else { | ||
# There are matlab-batch entries on PATH and no entry matches InstallLocation | ||
$FirstEntry = $ExistingEntries[0] | ||
$FirstMatlabBatchIndex = [Array]::IndexOf($PathEntries, $FirstEntry) | ||
$PathEntries = $PathEntries[0..($FirstMatlabBatchIndex-1)] + $InstallDirectory + $PathEntries[$FirstMatlabBatchIndex..($PathEntries.Length-1)] | ||
$NewPath = $PathEntries -join ';' | ||
[System.Environment]::SetEnvironmentVariable('Path', $NewPath, [System.EnvironmentVariableTarget]::Machine) | ||
Write-Output "Added new installation directory to the beginning of the matlab-batch entries. You will have to start a fresh PowerShell session to find it on PATH." | ||
} | ||
} | ||
|
||
Write-Output "Done with Install-MatlabBatch." | ||
} | ||
|
||
try { | ||
# Check if PowerShell is running as Administrator | ||
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) | ||
$isAdministrator = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | ||
|
||
if ($Help) { | ||
Show-Help | ||
exit 0 | ||
} | ||
|
||
if ($isAdministrator) { | ||
Install-MatlabBatch -InstallDirectory $InstallLocation | ||
} else { | ||
Write-Host "PowerShell is not running as Administrator. Please run PowerShell as Administrator to proceed with installation." | ||
} | ||
} | ||
catch { | ||
$ScriptPath = $MyInvocation.MyCommand.Path | ||
Write-Output "ERROR - An error occurred while running script: $ScriptPath. Error: $_" | ||
throw | ||
} |
134 changes: 134 additions & 0 deletions
134
alternates/non-interactive/install/install-matlab-batch.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#!/bin/bash | ||
# Copyright 2024 The MathWorks, Inc. | ||
|
||
DEFAULT_DOWNLOAD_BASE_URL='https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1' | ||
DEFAULT_UNIX_INSTALL_LOCATION='/opt/mathworks' | ||
DEFAULT_WIN_INSTALL_LOCATION='C:\Program Files\MathWorks' | ||
|
||
# Exit on any failure, treat unset substitution variables as errors | ||
set -euo pipefail | ||
|
||
# Function to display help message | ||
show_help() { | ||
echo "Usage: $0 [options]" | ||
echo "" | ||
echo "Options:" | ||
echo " -h Display this help message." | ||
echo " -i Specify an install location." | ||
} | ||
|
||
# Initialize our own variables: | ||
input_value="" | ||
|
||
# Process command-line options | ||
|
||
OPTSTRING=":hi:" | ||
|
||
while getopts "$OPTSTRING" opt; do | ||
case ${opt} in | ||
h) | ||
show_help | ||
exit 0 | ||
;; | ||
i) | ||
input_value=$OPTARG | ||
;; | ||
\?) | ||
echo "Invalid option: -${OPTARG}" 1>&2 | ||
show_help | ||
exit 1 | ||
;; | ||
:) | ||
echo "Option -$OPTARG requires an argument." 1>&2 | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
# Use the input value if provided | ||
if [ -n "$input_value" ]; then | ||
install_dir=$input_value | ||
else | ||
echo "Using default install location." | ||
fi | ||
|
||
# Function to download the matlab-batch executable binary | ||
download() { | ||
url=$1 | ||
filename=$2 | ||
|
||
if [ -x "$(command -v wget)" ]; then | ||
wget -O "$filename" "$url" 2>&1 | ( ! grep -i "failed\|error\|denied" >&2 ) | ||
elif [ -x "$(command -v curl)" ] && curl --help | grep -q -- "--retry-all-errors"; then | ||
curl --retry 3 --retry-all-errors -sSfLo "$filename" "$url" | ||
elif [ -x "$(command -v curl)" ]; then | ||
curl --retry 3 -sSfLo "$filename" "$url" | ||
else | ||
echo "Could not find wget or curl command" >&2 | ||
return 1 | ||
fi | ||
} | ||
|
||
# Detect the user environment and prepare accordingly | ||
OS=$(uname) | ||
|
||
case $OS in | ||
Linux) | ||
mw_arch='glnxa64' | ||
bin_ext='' | ||
install_dir=${install_dir:-$DEFAULT_UNIX_INSTALL_LOCATION} | ||
;; | ||
Darwin) | ||
arch=$(uname -m) | ||
if echo "$arch" | grep -qE '^(x86_64|amd64)$'; then | ||
mw_arch='maci64' | ||
else | ||
mw_arch='maca64' | ||
fi | ||
bin_ext='' | ||
install_dir=${install_dir:-$DEFAULT_UNIX_INSTALL_LOCATION} | ||
;; | ||
CYGWIN*|MINGW32*|MSYS*|MINGW*) | ||
mw_arch='win64' | ||
bin_ext='.exe' | ||
install_dir=${install_dir:-$DEFAULT_WIN_INSTALL_LOCATION} | ||
;; | ||
*) | ||
echo "'$OS' operating system not supported" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
# Create standard location for install if it does not exist | ||
mkdir -p "$install_dir" | ||
|
||
# Download the matlab-batch executable to a standard location | ||
base_url=${DEFAULT_DOWNLOAD_BASE_URL} | ||
|
||
{ | ||
output=$(download "$base_url/$mw_arch/matlab-batch$bin_ext" "$install_dir/matlab-batch$bin_ext" 2>&1); | ||
} || { | ||
msg=$(echo "$output" | head -1 | sed -e 's/^[[:space:]]*//') | ||
echo "Failed to download matlab-batch to the specified location ($msg)." | ||
exit 1; | ||
} | ||
|
||
# Allow executable permissions for matlab-batch | ||
chmod a+x "$install_dir/matlab-batch$bin_ext" | ||
|
||
# Place matlab-batch on the PATH if it is possible in the current environment | ||
if [ "$OS" = "Linux" ] || [ "$OS" = "Darwin" ]; then | ||
# Convert install_dir to an absolute path if it is not already | ||
install_dir=$(cd "$install_dir" && pwd) | ||
|
||
# Check if install_dir is not /usr/local/bin and proceed with creating a symlink if true | ||
if [ "$install_dir" != "/usr/local/bin" ] && ln -fs "$install_dir/matlab-batch" /usr/local/bin/matlab-batch; then | ||
echo "symlink created in /usr/local/bin." | ||
elif [ "$install_dir" = "/usr/local/bin" ]; then | ||
echo "matlab-batch is installed in /usr/local/bin, no need to create a symbolic link." >&2 | ||
else | ||
echo "Unable to create symbolic links for matlab-batch in /usr/local/bin. To run matlab-batch, '$install_dir/matlab-batch' must be on the system path." >&2 | ||
fi | ||
fi | ||
|
||
echo "matlab-batch installed successfully." |