|
| 1 | +param( |
| 2 | + [string]$DownloadDir = "$HOME\.specmatic" |
| 3 | +) |
| 4 | + |
| 5 | +function Write-Info { Write-Host "💡 [INFO] $args" -ForegroundColor Cyan } |
| 6 | +function Write-Warn { Write-Host "⚠️ [WARN] $args" -ForegroundColor Yellow } |
| 7 | +function Write-Error { Write-Host "❌ [ERROR] $args" -ForegroundColor Red } |
| 8 | + |
| 9 | +$toolVersion = "{{ include.tool_version }}" |
| 10 | +$downloadTarget = "{{ include.download_target }}" |
| 11 | +$downloadUrls = @({{ download_urls | strip }}) |
| 12 | +$toolName = "{{ include.tool_name | strip }}" |
| 13 | + |
| 14 | +if (-not $downloadUrls -or $downloadUrls.Count -eq 0) { |
| 15 | + Write-Error "Download URLs list is empty or not defined. Exiting." |
| 16 | + exit 1 |
| 17 | +} |
| 18 | + |
| 19 | +$argsList = @($args) |
| 20 | +while ($argsList.Count -gt 0) { |
| 21 | + $arg = $argsList[0] |
| 22 | + switch -Regex ($arg) { |
| 23 | + '^(-h|--help)$' { |
| 24 | + Write-Host "Usage: install-specmatic-tool.ps1 [--download-dir=<dir>]" |
| 25 | + Write-Host " --download-dir: Directory where $downloadTarget.jar will be downloaded (default: $DownloadDir)" |
| 26 | + exit 0 |
| 27 | + } |
| 28 | + '^--download-dir=(.+)$' { |
| 29 | + $DownloadDir = $matches[1] |
| 30 | + } |
| 31 | + default { |
| 32 | + Write-Host "Unknown argument: $arg" |
| 33 | + Write-Host "Usage: install-specmatic-tool.ps1 [--download-dir=<dir>]" |
| 34 | + exit 1 |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if ($argsList.Count -gt 1) { |
| 39 | + $argsList = $argsList[1..($argsList.Count - 1)] |
| 40 | + } else { |
| 41 | + $argsList = @() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +Write-Info "Using download directory $DownloadDir" |
| 47 | +if (-not (Test-Path -Path $DownloadDir)) { |
| 48 | + try { |
| 49 | + New-Item -ItemType Directory -Path $DownloadDir -Force | Out-Null |
| 50 | + Write-Info "Created directory $DownloadDir" |
| 51 | + } catch { |
| 52 | + Write-Error "Failed to create directory $DownloadDir" |
| 53 | + exit 1 |
| 54 | + } |
| 55 | +} else { |
| 56 | + Write-Info "Using existing directory $DownloadDir" |
| 57 | +} |
| 58 | + |
| 59 | +$jarPath = Join-Path $DownloadDir $downloadTarget |
| 60 | +if (Test-Path -Path $jarPath) { |
| 61 | + Remove-Item -Path $jarPath -Force |
| 62 | + Write-Info "Removed existing $downloadTarget" |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +$downloadSuccess = $false |
| 67 | +foreach ($url in $downloadUrls) { |
| 68 | + Write-Info "Downloading $toolName from $url..." |
| 69 | + try { |
| 70 | + Invoke-WebRequest -Uri $url -OutFile $jarPath -UseBasicParsing |
| 71 | + if (Test-Path $jarPath) { |
| 72 | + Write-Info "Downloaded $toolName to $jarPath" |
| 73 | + $downloadSuccess = $true |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + catch { |
| 78 | + Write-Warn "Failed to download from $url" |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +if (-not $downloadSuccess) { |
| 83 | + Write-Error "All download attempts failed. Exiting." |
| 84 | + exit 1 |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +if (-not (Get-Command java -ErrorAction SilentlyContinue)) { |
| 89 | + Write-Warn @" |
| 90 | +======================================================================== |
| 91 | +⚠️ Java is not installed or not found in your PATH. |
| 92 | +
|
| 93 | +💡 You can download and install a JRE from one of the following sources: |
| 94 | +
|
| 95 | + 🔗 AdoptOpenJDK (Eclipse Temurin): [https://adoptium.net/](https://adoptium.net/) |
| 96 | + 🔗 Oracle JDK: [https://www.oracle.com/java/technologies/downloads/](https://www.oracle.com/java/technologies/downloads/) |
| 97 | + 📦 SDKMAN: [https://sdkman.io/jdks](https://sdkman.io/jdks) |
| 98 | +
|
| 99 | +After installation, ensure 'java' is available in your PATH. |
| 100 | +"@ |
| 101 | +} |
| 102 | + |
| 103 | +$sanitizedToolName = $toolName -replace '\s', '' |
| 104 | +$funcDef = "function $sanitizedToolName { java -jar `"$jarPath`" `$args }" |
| 105 | +Write-Host @" |
| 106 | +================================================================== |
| 107 | +👍 $toolName $toolVersion installed successfully! |
| 108 | +
|
| 109 | +📁 Directory: $DownloadDir |
| 110 | +📦 JAR file: $jarPath |
| 111 | +
|
| 112 | +Usage: |
| 113 | +Run the tool with: |
| 114 | + java -jar `"$jarPath`" |
| 115 | +
|
| 116 | +To use $toolName conveniently: |
| 117 | +
|
| 118 | +1. Add alias for the current session: |
| 119 | + function $sanitizedToolName { java -jar `"$jarPath`" `$args } |
| 120 | +
|
| 121 | +2. To persist alias add to your PowerShell profile file ($PROFILE): |
| 122 | + Add-Content -Path $PROFILE -Value 'function $sanitizedToolName { java -jar `"$jarPath`" `$args }' |
| 123 | +
|
| 124 | +3. Restart PowerShell or run: |
| 125 | + . $PROFILE |
| 126 | +
|
| 127 | +You can then run $sanitizedToolName directly with arguments, e.g. $sanitizedToolName --help |
| 128 | +================================================================== |
| 129 | +"@ -ForegroundColor Green |
0 commit comments