-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.ps1
241 lines (199 loc) · 6.34 KB
/
install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#Requires -Version 5
#
# Copyright 2014-2024 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Author : Jérôme Angibaud
#
# Execute JeKa by Downloading requiered potentialy missing dependencies (JDK, JeKa version)
# The passed arguments are interpolated then passed to JeKA engine.
#
$ErrorActionPreference = "Stop"
function MessageInfo {
param([string]$msg)
if ($global:QuietFlag -ne $true) {
[Console]::Error.WriteLine($msg)
}
}
function MessageVerbose {
param([string]$msg)
if (($VerbosePreference -eq "Continue") -and ($global:QuietFlag -ne $true)) {
[Console]::Error.WriteLine($msg)
}
}
function Exit-Error {
param([string]$msg)
Write-Host $msg -ForegroundColor Red
exit 1
}
function Get-JekaUserHome {
if ([string]::IsNullOrEmpty($env:JEKA_USER_HOME)) {
return "$env:USERPROFILE\.jeka"
}
else {
return $env:JEKA_USER_HOME
}
}
function Get-LastVersion() {
$metadataUrl = "https://repo1.maven.org/maven2/dev/jeka/jeka-core/maven-metadata.xml"
$xmlContent = Invoke-WebRequest -Uri $metadataUrl
$xml = [xml]$xmlContent
$latestVersion = $xml.metadata.versioning.latest
return $latestVersion
}
class CmdLineArgs {
[Array]$args
CmdLineArgs([Array]$arguments) {
$this.args = $arguments
}
[int] GetIndexOfFirstOf([Array]$candidates) {
foreach ($arg in $candidates) {
$index = $this.args.IndexOf($arg)
if ($index -ne -1) {
return $index
}
}
return -1
}
[bool] IsUpdateFlagPresent() {
$remoteArgs= @("-ru", "-ur", "-u", "--remote-update")
$remoteIndex= $this.GetIndexOfFirstOf($remoteArgs)
return ($remoteIndex -ne -1)
}
[bool] IsQuietFlagPresent() {
$remoteArgs= @("-q", "--quiet")
$remoteIndex= $this.GetIndexOfFirstOf($remoteArgs)
return ($remoteIndex -ne -1)
}
[bool] IsVerboseFlagPresent() {
$remoteArgs= @("-v", "--verbose", "--debug")
$remoteIndex= $this.GetIndexOfFirstOf($remoteArgs)
return ($remoteIndex -ne -1)
}
[bool] IsProgramFlagPresent() {
$remoteIndex= $this.GetProgramArgIndex()
return ($remoteIndex -ne -1)
}
}
class ZipExtractor {
[string]$url
[string]$dir
ZipExtractor([string]$url, [string]$dir) {
$this.url = $url
$this.dir = $dir
}
ExtractRootContent() {
$zipFile = $this.Download()
$tempDir = [System.IO.Path]::GetTempFileName()
Remove-Item -Path $tempDir
Expand-Archive -Path $zipFile -DestinationPath $tempDir -Force
$subDirs = Get-ChildItem -Path $tempDir -Directory
$root = $tempDir + "\" + $subDirs[0]
Move-Item -Path $root -Destination $this.dir -Force
Remove-Item -Path $zipFile
Remove-Item -Path $tempDir -Recurse
}
Extract() {
$zipFile = $this.Download()
Expand-Archive -Path $zipFile -DestinationPath $this.dir -Force
Remove-Item -Path $zipFile
}
hidden [string] Download() {
$downloadFile = [System.IO.Path]::GetTempFileName() + ".zip"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($this.url, $downloadFile)
$webClient.Dispose()
return $downloadFile
}
}
function Add-Path {
param (
[string]$DirectoryPath
)
# Determine the registry scope (user or machine)
$registryScope = 'HKEY_CURRENT_USER'
# Get the current PATH value from the registry
$currentPath = Get-ItemProperty -Path "Registry::$registryScope\Environment" -Name PATH
# Split the current PATH into an array of directories
$existingPaths = $currentPath.Path.Split(';')
# Check if the directory path is already in the PATH
if ($existingPaths -notcontains $DirectoryPath) {
# Add the new directory to the existing paths
$newPath = $currentPath.Path + ";$DirectoryPath"
# Update the PATH in the registry
Set-ItemProperty -Path "Registry::$registryScope\Environment" -Name PATH -Value $newPath
# Broadcast a WM_SETTINGCHANGE message to reload the environment
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
Write-Host "Added '$DirectoryPath' to the PATH."
} else {
Write-Host "'$DirectoryPath' is already in the PATH."
}
}
function install() {
param([string]$version)
$mavenRepo = "https://repo1.maven.org/maven2"
$url = $mavenRepo + "/dev/jeka/jeka-core/" + $version + "/jeka-core-" + $version + "-distrib.zip"
$jekaHome = Get-JekaUserHome
if (!(Test-Path -Path $jekaHome)) {
$null = [System.IO.Directory]::CreateDirectory($jekaHome)
}
$installDir = "$jekaHome\bin"
MessageInfo "Installing Jeka version $version in $installDir"
$extractor = [ZipExtractor]::new($url, $installDir)
$extractor.ExtractRootContent()
Add-Path "$installDir"
}
function Main {
param(
[array]$arguments
)
# Get interpolated cmdLine, while ignoring Base dir
$cmdLineArgs = [CmdLineArgs]::new($arguments)
if ($cmdLineArgs.GetIndexOfFirstOf("install") -ne -1) {
$version = Get-LastVersion
MessageInfo ""
install($version)
MessageInfo "JeKa $version is installed."
if ($cmdLineArgs.GetIndexOfFirstOf("check") -ne -1) {
MessageInfo ""
MessageInfo "Now, let's check installation. This requires JDK download."
cmd.exe /c "$(Get-JekaUserHome)\bin\jeka --version"
if ($LASTEXITCODE -ne 0) {
exit 1
}
MessageInfo ""
MessageInfo "Everything seems fine."
}
MessageInfo "" # needed as jeka --version does not inclue carriage return
MessageInfo "Later, update JeKa by running 'jeka-update' or 'jeka-update <version>."
MessageInfo "Please start a new shell to run jeka."
} else {
$version
if ($arguments.Count -eq 0) {
$version = Get-LastVersion
} else {
$version = $arguments[0]
}
Write-Host "Updating Jeka to version $version ? [y/n]"
$user_input = Read-Host
if ($user_input -ne "y") {
exit 1
}
install($version)
MessageInfo "Jeka updated to version $version"
}
}
$ErrorActionPreference = "Stop"
Main -arguments $args