Skip to content

Commit 562e95c

Browse files
committed
Install: Add windows power shell script templates
1 parent 8092b4b commit 562e95c

6 files changed

Lines changed: 522 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# MHKiT-MATLAB {{ script_title }}
2+
# Generated from template - DO NOT EDIT DIRECTLY
3+
# Report issues: {{ support.github_issues }}
4+
5+
$ErrorActionPreference = "Stop" # Exit on any error
6+
7+
# Script configuration
8+
$CONDA_ENV_NAME = "{{ conda.environment_name }}"
9+
$PYTHON_VERSION = "{{ python.install_version }}"
10+
$MHKIT_VERSION = "{{ mhkit_python.version }}"
11+
$GITHUB_ISSUES = "{{ support.github_issues }}"
12+
13+
# Logging functions
14+
function Write-Info {
15+
param([string]$Message)
16+
Write-Host "[INFO] $Message" -ForegroundColor Green
17+
}
18+
19+
function Write-Success {
20+
param([string]$Message)
21+
Write-Host "[SUCCESS] $Message" -ForegroundColor Cyan
22+
}
23+
24+
function Write-Warning {
25+
param([string]$Message)
26+
Write-Host "[WARNING] $Message" -ForegroundColor Yellow
27+
}
28+
29+
function Write-Error {
30+
param([string]$Message)
31+
Write-Host "[ERROR] $Message" -ForegroundColor Red
32+
Write-Host "[ERROR] Please report this issue: $GITHUB_ISSUES" -ForegroundColor Red
33+
}
34+
35+
# Function to test if a conda installation works
36+
function Test-Conda {
37+
param([string]$CondaPath)
38+
39+
Write-Info "Testing conda at: $CondaPath"
40+
41+
# Test if the conda executable exists
42+
if (Test-Path $CondaPath) {
43+
try {
44+
# Test if conda info works (this is the most reliable test)
45+
$null = & "$CondaPath" info 2>$null
46+
$condaVersion = & "$CondaPath" --version 2>$null
47+
$condaLocation = & "$CondaPath" info --base 2>$null
48+
Write-Success "Working conda found!"
49+
Write-Info " Version: $condaVersion"
50+
Write-Info " Location: $condaLocation"
51+
return $true
52+
} catch {
53+
Write-Warning "Conda executable found but 'conda info' failed"
54+
return $false
55+
}
56+
} else {
57+
return $false
58+
}
59+
}
60+
61+
# OS and Architecture Detection
62+
$OSType = "Windows"
63+
$ArchType = $env:PROCESSOR_ARCHITECTURE
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{% set script_title = "Step 1: Conda Detection" %}
2+
{% include "_header.ps1.j2" %}
3+
4+
# Function to check if conda is available
5+
function Find-Conda {
6+
Write-Info "Starting comprehensive conda detection..."
7+
8+
# Step 1: Check if conda is already in PATH and working
9+
try {
10+
$condaCmd = Get-Command conda -ErrorAction SilentlyContinue
11+
if ($condaCmd) {
12+
Write-Info "Found conda in PATH"
13+
if (Test-Conda $condaCmd.Source) {
14+
Write-Output "CONDA_DETECTED=true"
15+
Write-Output "CONDA_PATH=$($condaCmd.Source)"
16+
return
17+
} else {
18+
Write-Warning "Conda in PATH is not working properly"
19+
}
20+
}
21+
} catch {
22+
# Continue to other detection methods
23+
}
24+
25+
# Step 2: Check environment variables
26+
{% for env_var in conda.detection.environment_variables %}
27+
$envValue = [Environment]::GetEnvironmentVariable("{{ env_var }}")
28+
if ($envValue) {
29+
Write-Info "Checking environment variable {{ env_var }}: $envValue"
30+
$condaFromEnv = ""
31+
if ("{{ env_var }}" -eq "CONDA_EXE") {
32+
$condaFromEnv = $envValue
33+
} elseif ("{{ env_var }}" -eq "CONDA_PREFIX") {
34+
$condaFromEnv = Join-Path $envValue "Scripts\conda.exe"
35+
}
36+
37+
if ($condaFromEnv -and (Test-Conda $condaFromEnv)) {
38+
Write-Output "CONDA_DETECTED=true"
39+
Write-Output "CONDA_PATH=$condaFromEnv"
40+
return
41+
}
42+
}
43+
{% endfor %}
44+
45+
# Step 3: Check all common installation paths systematically
46+
Write-Info "Checking common conda installation paths..."
47+
$pathsToCheck = @(
48+
{% for path in conda.detection.common_paths.windows %}
49+
[Environment]::ExpandEnvironmentVariables("{{ path }}"){{ "," if not loop.last }}
50+
{% endfor %}
51+
)
52+
53+
foreach ($path in $pathsToCheck) {
54+
if (Test-Conda $path) {
55+
Write-Output "CONDA_DETECTED=true"
56+
Write-Output "CONDA_PATH=$path"
57+
return
58+
}
59+
}
60+
61+
Write-Info "No working conda installation found"
62+
Write-Output "CONDA_DETECTED=false"
63+
}
64+
65+
function Main {
66+
Write-Info "Step 1: Detecting conda installation..."
67+
Find-Conda
68+
}
69+
70+
Main
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{% set script_title = "Step 2: Install Miniconda" %}
2+
{% include "_header.ps1.j2" %}
3+
4+
# Function to install miniconda
5+
function Install-Conda {
6+
Write-Info "Installing miniconda..."
7+
8+
$minicondaUrl = "{{ conda.miniconda_urls.windows }}"
9+
$tempDir = [System.IO.Path]::GetTempPath()
10+
$installerPath = Join-Path $tempDir "miniconda.exe"
11+
12+
# Download miniconda installer
13+
Write-Info "Downloading miniconda installer..."
14+
try {
15+
Invoke-WebRequest -Uri $minicondaUrl -OutFile $installerPath
16+
} catch {
17+
Write-Error "Failed to download miniconda installer: $_"
18+
Write-Output "CONDA_INSTALLED=false"
19+
exit 1
20+
}
21+
22+
# Install miniconda silently
23+
Write-Info "Installing miniconda to $env:USERPROFILE\miniconda3..."
24+
try {
25+
$installArgs = @("/S", "/InstallationType=JustMe", "/AddToPath=0", "/RegisterPython=0", "/D=$env:USERPROFILE\miniconda3")
26+
Start-Process -FilePath $installerPath -ArgumentList $installArgs -Wait -NoNewWindow
27+
} catch {
28+
Write-Error "Failed to install miniconda: $_"
29+
Write-Output "CONDA_INSTALLED=false"
30+
exit 1
31+
}
32+
33+
# Remove installer
34+
Remove-Item $installerPath -Force -ErrorAction SilentlyContinue
35+
36+
Write-Success "Miniconda installed successfully"
37+
38+
# Output conda path for next steps
39+
$condaPath = Join-Path $env:USERPROFILE "miniconda3\Scripts\conda.exe"
40+
Write-Output "CONDA_PATH=$condaPath"
41+
}
42+
43+
# Main execution
44+
function Main {
45+
Write-Info "Step 2: Installing miniconda..."
46+
Install-Conda
47+
}
48+
49+
# Run main function
50+
Main
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{% set script_title = "Step 3: Create Conda Environment" %}
2+
{% include "_header.ps1.j2" %}
3+
4+
# Function to check if conda environment exists
5+
function Test-CondaEnv {
6+
param([string]$CondaPath)
7+
8+
$envList = & "$CondaPath" env list 2>$null
9+
if ($envList -match "^$CONDA_ENV_NAME ") {
10+
Write-Success "Found existing conda environment: $CONDA_ENV_NAME"
11+
Write-Output "ENV_EXISTS=true"
12+
return $true
13+
} else {
14+
Write-Info "Conda environment '$CONDA_ENV_NAME' not found, will create it"
15+
Write-Output "ENV_EXISTS=false"
16+
return $false
17+
}
18+
}
19+
20+
# Function to create conda environment
21+
function New-CondaEnv {
22+
param([string]$CondaPath)
23+
24+
Write-Info "Creating conda environment '$CONDA_ENV_NAME' with Python $PYTHON_VERSION..."
25+
26+
try {
27+
# Accept conda Terms of Service to avoid interactive prompts
28+
Write-Info "Accepting conda Terms of Service..."
29+
& "$CondaPath" config --set solver libmamba 2>$null
30+
31+
# Create environment using conda-forge to minimize TOS issues
32+
& "$CondaPath" create -n $CONDA_ENV_NAME python=$PYTHON_VERSION -c conda-forge -y
33+
34+
Write-Success "Conda environment created successfully"
35+
Write-Output "ENV_CREATED=true"
36+
} catch {
37+
Write-Error "Failed to create conda environment: $_"
38+
Write-Output "ENV_CREATED=false"
39+
exit 1
40+
}
41+
}
42+
43+
# Function to verify Python version in environment
44+
function Test-PythonVersion {
45+
param([string]$CondaPath)
46+
47+
Write-Info "Verifying Python version in environment..."
48+
49+
try {
50+
$pyVersionOutput = & "$CondaPath" run -n $CONDA_ENV_NAME python --version 2>&1
51+
$pyVersion = ($pyVersionOutput -split ' ')[1]
52+
$majorMinor = ($pyVersion -split '\.')[0,1] -join '.'
53+
$expectedMajorMinor = ($PYTHON_VERSION -split '\.')[0,1] -join '.'
54+
55+
if ($majorMinor -eq $expectedMajorMinor) {
56+
Write-Success "Python version verified: $pyVersion"
57+
Write-Output "PYTHON_VERSION_OK=true"
58+
return $true
59+
} else {
60+
Write-Warning "Python version mismatch. Expected: $PYTHON_VERSION, Got: $pyVersion"
61+
Write-Info "Environment will need to be recreated..."
62+
Write-Output "PYTHON_VERSION_OK=false"
63+
return $false
64+
}
65+
} catch {
66+
Write-Error "Failed to verify Python version: $_"
67+
Write-Output "PYTHON_VERSION_OK=false"
68+
exit 1
69+
}
70+
}
71+
72+
function Main {
73+
param([string]$CondaPath)
74+
75+
if (-not $CondaPath) {
76+
Write-Error "No conda path provided. Usage: script.ps1 <conda_path>"
77+
exit 1
78+
}
79+
80+
Write-Info "Step 3: Managing conda environment..."
81+
Write-Info "Using conda at: $CondaPath"
82+
83+
if (-not (Test-CondaEnv $CondaPath)) {
84+
New-CondaEnv $CondaPath
85+
}
86+
87+
if (-not (Test-PythonVersion $CondaPath)) {
88+
Write-Info "Recreating environment with correct Python version..."
89+
& "$CondaPath" remove -n $CONDA_ENV_NAME --all -y 2>$null
90+
New-CondaEnv $CondaPath
91+
Test-PythonVersion $CondaPath
92+
}
93+
}
94+
95+
# Run main function with arguments
96+
Main $args[0]

0 commit comments

Comments
 (0)