forked from graalvm/setup-graalvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liberica.ts
119 lines (110 loc) · 3.43 KB
/
liberica.ts
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
import * as c from './constants'
import * as semver from 'semver'
import {
downloadExtractAndCacheJDK,
getTaggedRelease,
getMatchingTags
} from './utils'
import {downloadTool} from '@actions/tool-cache'
import {spawnSync} from 'child_process'
const LIBERICA_GH_USER = 'bell-sw'
const LIBERICA_RELEASES_REPO = 'LibericaNIK'
const LIBERICA_JDK_TAG_PREFIX = 'jdk-'
const LIBERICA_VM_PREFIX = 'bellsoft-liberica-vm-'
export async function setUpLiberica(
javaVersion: string,
javaPackage: string
): Promise<string> {
const resolvedJavaVersion = await findLatestLibericaJavaVersion(javaVersion)
const downloadUrl = await findLibericaURL(resolvedJavaVersion, javaPackage)
const toolName = determineToolName(javaVersion, javaPackage)
return downloadExtractAndCacheJDK(
async () => downloadTool(downloadUrl),
toolName,
javaVersion
)
}
export async function findLatestLibericaJavaVersion(
javaVersion: string
): Promise<string> {
const matchingRefs = await getMatchingTags(
LIBERICA_GH_USER,
LIBERICA_RELEASES_REPO,
`${LIBERICA_JDK_TAG_PREFIX}${javaVersion}`
)
const noMatch = '0.0.1'
let bestMatch = noMatch
const prefixLength = `refs/tags/${LIBERICA_JDK_TAG_PREFIX}`.length
const patternLength = javaVersion.length
for (const matchingRef of matchingRefs) {
const version = matchingRef.ref.substring(prefixLength)
if (
semver.valid(version) &&
// pattern '17.0.1' should match '17.0.1+12' but not '17.0.10'
(version.length <= patternLength ||
!isDigit(version.charAt(patternLength))) &&
semver.compareBuild(version, bestMatch) == 1
) {
bestMatch = version
}
}
if (bestMatch === noMatch) {
throw new Error(
`Unable to find the latest version for JDK${javaVersion}. Please make sure the java-version is set correctly. ${c.ERROR_HINT}`
)
}
return bestMatch
}
export async function findLibericaURL(
javaVersion: string,
javaPackage: string
): Promise<string> {
const release = await getTaggedRelease(
LIBERICA_GH_USER,
LIBERICA_RELEASES_REPO,
LIBERICA_JDK_TAG_PREFIX + javaVersion
)
const platform = determinePlatformPart()
const assetPrefix = `${LIBERICA_VM_PREFIX}${determineVariantPart(
javaPackage
)}openjdk${javaVersion}`
const assetSuffix = `-${platform}${c.GRAALVM_FILE_EXTENSION}`
for (const asset of release.assets) {
if (
asset.name.startsWith(assetPrefix) &&
asset.name.endsWith(assetSuffix)
) {
return asset.browser_download_url
}
}
throw new Error(
`Unable to find asset for java-version: ${javaVersion}, java-package: ${javaPackage}, platform: ${platform}. ${c.ERROR_REQUEST}`
)
}
function determineToolName(javaVersion: string, javaPackage: string) {
const variant = determineVariantPart(javaPackage)
const platform = determinePlatformPart()
return `${LIBERICA_VM_PREFIX}${variant}${platform}`
}
function determineVariantPart(javaPackage: string) {
return javaPackage !== null && javaPackage.includes('+fx') ? 'full-' : ''
}
function determinePlatformPart() {
if (isMuslBasedLinux()) {
return `linux-${c.JDK_ARCH}-musl`
} else {
return `${c.JDK_PLATFORM}-${c.GRAALVM_ARCH}`
}
}
function isMuslBasedLinux() {
if (c.IS_LINUX) {
const output = spawnSync('ldd', ['--version']).stderr.toString('utf8')
if (output.includes('musl')) {
return true
}
}
return false
}
function isDigit(c: string) {
return c.charAt(0) >= '0' && c.charAt(0) <= '9'
}