-
Notifications
You must be signed in to change notification settings - Fork 15
/
Setup.ps1
205 lines (166 loc) · 4.68 KB
/
Setup.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
#Requires -Version 2.0
<#
.SYNOPSIS
Downloads, unpacks, and prepares a portable
Java development environment.
Big thanks to https://github.com/toksaitov/AndroidStudioPortable
#>
#
# Definitions
#
. '.\Definitions.ps1'
#
# Helpers
#
. '.\Helpers.ps1'
#
# Steps
#
#
# Download and unpack lessmsi to be able to unpack
# a 7-Zip installer later.
#
$ToolsAreRequired = !(Test-Path -Path $OracleJDKDirectory)
if ($ToolsAreRequired -And !(Test-Path -Path $LessMSIDirectory))
{
if (!(Test-Path -Path $LessMSIArchive))
{
Write-Output "Get LessMSI"
Invoke-WebRequest -Uri $LessMSIURL -OutFile $LessMSIArchive
}
Write-Output "Expand LessMSI"
Expand-Archive -Path $LessMSIArchive
}
#
# Download and unpack the 7-Zip installer.
#
if ($ToolsAreRequired -And !(Test-Path -Path $7zDirectory))
{
if (!(Test-Path -Path $7zInstaller))
{
Write-Output "Get 7-Zip"
Invoke-WebRequest -Uri $7zURL -OutFile $7zInstaller
}
Write-Output "Use LessMSI to unpack 7zip"
& ".\$LessMSIDirectory\$LessMSIExecutable" 'x' $7zInstaller
}
#
# Check the architecture of the OS and change to 64bit if necessary.
#
if ([System.Environment]::Is64BitProcess) {
Write-Output "Detected 64bit OS, switching...";
$OracleJDK = $OracleJDK64;
$OracleJDKInstaller = $OracleJDKInstaller64;
$OracleJDKURL = $OracleJDKURL64;
$OracleJDKDirectory = $OracleJDKDirectory64;
$OracleJDKBinariesDirectory = $OracleJDKBinariesDirectory64;
}
#
# Download and unpack an Oracle JDK installer without administrative rights.
#
if (!(Test-Path -Path $OracleJDKDirectory))
{
if (!(Test-Path -Path $OracleJDKInternalArchive))
{
if (!(Test-Path -Path $OracleJDKInternalCAB))
{
if (!(Test-Path -Path $OracleJDKInstaller))
{
#
# Download the Oracle JDK installer accepting the
#
# `Oracle Binary Code License Agreement for Java SE`
#
$Url = $OracleJDKURL;
$OutFile = $OracleJDKInstaller;
Write-Output "Download Java JDK $OracleJDK"
Invoke-WebRequest -Uri $Url -OutFile $OutFile -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox
}
#
# Unpack the Oracle JDK installer with 7-Zip.
#
Write-Output "Unpack JDK installer"
& ".\$7zDirectory\$7zExecutable" `
'e' $OracleJDKInstaller `
"$OracleJDKInternalCABPath\$OracleJDKInternalCAB" `
'-y'
}
#
# Unpack the Oracle JDK Tools CAB with 7-Zip.
#
Write-Output "Unpack JDK archive"
& ".\$7zDirectory\$7zExecutable" `
'e' $OracleJDKInternalCAB `
"$OracleJDKInternalArchive" '-y'
}
Write-Output "Unpack JDK"
& ".\$7zDirectory\$7zExecutable" 'x' $OracleJDKInternalArchive `
"-o$OracleJDKDirectory" '-y'
}
#
# Unpack Oracle JDK `.pack` files with the unpack200
# utility bundled with the JDK.
#
Write-Output "Expand JDK files"
$GetChildItemParameters = @{
Path = $OracleJDKDirectory;
Filter = '*.pack';
}
$PackFiles =
Get-ChildItem @GetChildItemParameters -Recurse
if ($PackFiles)
{
foreach ($File in $PackFiles)
{
$PackFileName =
$File.FullName
$JarFileName =
"$($File.DirectoryName)\$($File.BaseName).jar"
& "$OracleJDKBinariesDirectory\unpack200" '-r' `
$PackFileName $JarFileName
}
}
#
# Remove temporary files.
#
$LessMSIRootDirectory =
Get-RelativeRootDirectory -RelativePath $LessMSIDirectory
$7zRootDirectory =
Get-RelativeRootDirectory -RelativePath $7zDirectory
$TemporaryFiles = @(
$LessMSIArchive,
$LessMSIRootDirectory,
$7zInstaller,
$7zRootDirectory,
$OracleJDKInstaller,
$OracleJDKInternalCAB,
$OracleJDKInternalArchive
)
$RemoveItemParameters = @{
Path = $TemporaryFiles;
ErrorAction = 'SilentlyContinue';
}
Remove-Item @RemoveItemParameters -Recurse -Force
#
# Generate a batch file to create env variables JDK.
#
$BatchContent = @"
@echo off
REM
REM Create temporary environment variables for JDK.
REM
REM This file is automatically generated. Please, do not edit this file.
REM
SETX JAVA_HOME %~dp0$OracleJDKDirectory
for /f "skip=2 tokens=3*" %%1 in ('reg query HKCU\Environment /v PATH') do @if [%%2]==[] ( @setx PATH "%%~1;%JAVA_HOME\bin" ) else ( @setx PATH "%%~1 %%~2;%JAVA_HOME\bin" )
"@
$NewItemParameters = @{
Path = './start.bat';
Type = 'File';
Value = $BatchContent;
}
New-Item @NewItemParameters -Force
#
# The end.
#
Write-Output "`nDone."