-
Notifications
You must be signed in to change notification settings - Fork 2
/
adoptium.go
262 lines (223 loc) · 5.99 KB
/
adoptium.go
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
)
const ADOPTIUM_URL = "https://api.adoptium.net"
type JavaProvider interface {
GetDownloads(installPath string) []Download
Install(installPath string) bool
GetJavaPath(installPath string) string
}
// region NoOp
type NoOpJavaProvider struct {
}
func (e *NoOpJavaProvider) GetDownloads(installPath string) []Download {
return make([]Download, 0)
}
func (e *NoOpJavaProvider) Install(installPath string) bool {
return true
}
func (e *NoOpJavaProvider) GetJavaPath(installPath string) string {
switch runtime.GOOS {
case "windows":
return "java.exe"
default:
return "java"
}
}
// endregion
//region Adoptium
type AdoptiumRelease struct {
Binaries []Binary `json:"binaries"`
ReleaseName string `json:"release_name"`
VersionData VersionData `json:"version_data"`
}
type Binary struct {
ImageType string `json:"image_type"`
Package Package `json:"package"`
}
type Package struct {
Checksum string `json:"checksum"`
Link string `json:"link"`
Name string `json:"name"`
Size int `json:"size"`
}
type VersionData struct {
Semver string `json:"semver"`
}
type InstallProperties struct {
Release *AdoptiumRelease
Binary *Binary
ArchivePath *string
}
type AdoptiumJavaProvider struct {
ShortVersion *string
SemverTarget *string
InstallProps *InstallProperties
}
func (self *AdoptiumJavaProvider) GetDownloads(installPath string) []Download {
downloads := make([]Download, 0)
rel, err := self.GetCompatiableAdoptiumVersion()
if err != nil {
return downloads
}
// Must have binaries.
if len(rel.Binaries) == 0 {
return downloads
}
// Just grab the first
binary := rel.Binaries[0]
var ext string
if strings.HasSuffix(binary.Package.Name, ".zip") {
ext = ".zip"
} else if strings.HasSuffix(binary.Package.Name, ".tar.gz") {
ext = ".tar.gz"
} else {
return downloads
}
parsedUrl, err := url.Parse(binary.Package.Link)
if err != nil {
// ookay, we tried i guess?
return downloads
}
jrePath := filepath.Join(installPath, "jre")
archiveName := "jre" + ext
fullPath := filepath.Join(jrePath, archiveName)
version := InstallProperties{rel, &binary, &fullPath}
self.InstallProps = &version
downloads = append(downloads, Download{"jre", *parsedUrl, archiveName, "sha256", binary.Package.Checksum, fullPath})
return downloads
}
func (self *AdoptiumJavaProvider) Install(installPath string) bool {
if self.InstallProps != nil {
archivePath := *self.InstallProps.ArchivePath
if strings.HasSuffix(archivePath, ".zip") {
err := extractZip(filepath.Join(installPath, "jre"), archivePath)
if err != nil {
printfln("Failed to extract zip: " + archivePath)
println(err)
return false
}
os.Remove(archivePath)
} else if strings.HasSuffix(archivePath, ".tar.gz") {
err := extractTarGz(filepath.Join(installPath, "jre"), archivePath)
if err != nil {
printfln("Failed to extract tar.gz: " + archivePath)
println(err)
return false
}
os.Remove(archivePath)
} else {
printfln("I don't know how to extract this adoptium archive: " + archivePath)
return false
}
}
return true
}
func (self *AdoptiumJavaProvider) GetJavaPath(installPath string) string {
var executable = "java"
var binFolder = "bin"
switch runtime.GOOS {
case "windows":
executable = "java.exe"
binFolder = "bin"
case "freebsd":
fallthrough
case "linux":
executable = "java"
binFolder = "bin"
case "darwin":
executable = "java"
binFolder = filepath.Join("Contents", "Home", "bin")
}
if self.InstallProps != nil {
return filepath.Join(installPath,
"jre",
self.InstallProps.Release.ReleaseName+"-"+self.InstallProps.Binary.ImageType,
binFolder,
executable)
}
return executable
}
func (self *AdoptiumJavaProvider) GetCompatiableAdoptiumVersion() (*AdoptiumRelease, error) {
if self.SemverTarget != nil {
return self.GetAdoptiumReleaseViaSemver(runtime.GOARCH, true)
} else {
return self.GetLatestAdoptiumRelease(runtime.GOARCH, true)
}
}
func (self *AdoptiumJavaProvider) GetAdoptiumReleaseViaSemver(architecture string, jre bool) (*AdoptiumRelease, error) {
var releases []AdoptiumRelease
url := ADOPTIUM_URL + "/v3/assets/version/"
url += *self.SemverTarget
url += GetAdoptiumQueryProperties(architecture, jre)
err := APICall(url, &releases)
if err != nil {
if runtime.GOOS == "darwin" && architecture == "arm64" {
// We are mac M1, try x64.
return self.GetAdoptiumReleaseViaSemver("amd64", jre)
}
if jre {
// We failed to find a JRE, find a JDK instead..
return self.GetAdoptiumReleaseViaSemver(architecture, false)
}
return nil, err
}
return &releases[0], nil
}
func (self *AdoptiumJavaProvider) GetLatestAdoptiumRelease(architecture string, jre bool) (*AdoptiumRelease, error) {
var releases []AdoptiumRelease
url := ADOPTIUM_URL + "/v3/assets/feature_releases/"
url += *self.ShortVersion
url += "/ga"
url += GetAdoptiumQueryProperties(architecture, jre)
err := APICall(url, &releases)
if err != nil {
if runtime.GOOS == "darwin" && architecture == "arm64" {
// We are mac M1, try x64.
return self.GetLatestAdoptiumRelease("amd64", jre)
}
if jre {
// We failed to find a JRE, find a JDK instead..
return self.GetLatestAdoptiumRelease(architecture, false)
}
return nil, err
}
return &releases[0], nil
}
func GetAdoptiumQueryProperties(architecture string, jre bool) string {
goOS := runtime.GOOS
if goOS == "darwin" {
goOS = "mac"
}
if goOS == "linux" {
if _, err := os.Stat("/etc/alpine-release"); !os.IsNotExist(err) {
goOS = "alpine-linux"
}
}
if architecture == "amd64" {
architecture = "x64"
} else if architecture == "386" {
architecture = "x86"
} else if architecture == "arm64" {
architecture = "aarch64"
}
var imageType string
if jre {
imageType = "jre"
} else {
imageType = "jdk"
}
return "?project=jdk" +
"&image_type=" + imageType +
"&vendor=eclipse" +
"&jvm_impl=hotspot" +
"&heap_size=normal" +
"&architecture=" + architecture +
"&os=" + goOS
}
//endregion