-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gradle-inspector): Add an option to bootstrap a JDK version
Signed-off-by: Sebastian Schuberth <[email protected]>
- Loading branch information
1 parent
f2f2f7c
commit fcfab20
Showing
4 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.utils.ort | ||
|
||
import eu.hansolo.jdktools.ArchiveType | ||
import eu.hansolo.jdktools.Latest | ||
import eu.hansolo.jdktools.LibCType | ||
import eu.hansolo.jdktools.Match | ||
import eu.hansolo.jdktools.OperatingSystem | ||
import eu.hansolo.jdktools.PackageType | ||
import eu.hansolo.jdktools.TermOfSupport | ||
import eu.hansolo.jdktools.util.Helper | ||
|
||
import io.foojay.api.discoclient.DiscoClient | ||
import io.foojay.api.discoclient.pkg.Scope | ||
|
||
import java.io.File | ||
|
||
import kotlin.time.measureTime | ||
import kotlin.time.measureTimedValue | ||
|
||
import org.apache.logging.log4j.kotlin.logger | ||
|
||
import org.ossreviewtoolkit.utils.common.safeMkdirs | ||
import org.ossreviewtoolkit.utils.common.unpack | ||
|
||
object JavaBootstrapper { | ||
private val discoClient by lazy { DiscoClient(Environment.ORT_USER_AGENT) } | ||
|
||
/** | ||
* Return the single top-level directory contained in this directory, if any, or return this directory otherwise. | ||
*/ | ||
private fun File.singleContainedDirectoryOrThis() = | ||
walk().maxDepth(1).filter { it != this && it.isDirectory }.singleOrNull() ?: this | ||
|
||
/** | ||
* Install a JDK matching [distributionName] and [version] below [ortToolsDirectory] and return its directory on | ||
* success, or an exception on failure. | ||
*/ | ||
fun installJdk(distributionName: String, version: String): Result<File> { | ||
val versionResult = eu.hansolo.jdktools.versioning.Semver.fromText(version) | ||
if (versionResult.error1 != null) return Result.failure(versionResult.error1) | ||
|
||
val semVer = versionResult.semver1 | ||
|
||
logger.info { "Setting up JDK '$distributionName' in version '$semVer'..." } | ||
|
||
val operatingSystem = Helper.getOperatingSystem() | ||
val architecture = Helper.getArchitecture() | ||
|
||
val libcType = when (operatingSystem) { | ||
OperatingSystem.LINUX -> LibCType.GLIBC | ||
OperatingSystem.LINUX_MUSL, OperatingSystem.ALPINE_LINUX -> LibCType.MUSL | ||
else -> LibCType.NONE | ||
} | ||
|
||
val pkgs = discoClient.getPkgs( | ||
/* distributions = */ null, | ||
semVer.versionNumber, | ||
Latest.PER_VERSION, | ||
operatingSystem, | ||
libcType, | ||
architecture, | ||
architecture.bitness, | ||
if (operatingSystem == OperatingSystem.WINDOWS) ArchiveType.ZIP else ArchiveType.TAR_GZ, | ||
PackageType.JDK, | ||
/* javafxBundled = */ false, | ||
/* directlyDownloadable = */ true, | ||
listOf(semVer.releaseStatus), | ||
TermOfSupport.NONE, | ||
listOf(Scope.PUBLIC), | ||
Match.ANY | ||
) | ||
|
||
val pkg = pkgs.sortedBy { it.id }.find { it.distributionName == distributionName } | ||
?: return Result.failure( | ||
IllegalArgumentException( | ||
"No package found for JDK '$distributionName' in version '$version'." | ||
) | ||
) | ||
|
||
val installDir = ortToolsDirectory.resolve("jdks").resolve(pkg.id).apply { | ||
if (isDirectory) { | ||
logger.info { "Not downloading the JDK again as the directory '$this' already exists." } | ||
return Result.success(singleContainedDirectoryOrThis()) | ||
} | ||
|
||
safeMkdirs() | ||
} | ||
|
||
val url = discoClient.getPkgDirectDownloadUri(pkg.id) | ||
logger.info { "Downloading the JDK package from $url..." } | ||
|
||
val (archive, downloadDuration) = measureTimedValue { | ||
okHttpClient.downloadFile(url, installDir).getOrElse { | ||
return Result.failure(it) | ||
} | ||
} | ||
|
||
logger.info { "Downloading the JDK took $downloadDuration." } | ||
|
||
val unpackDuration = measureTime { archive.unpack(installDir) } | ||
|
||
logger.info { "Unpacking the JDK took $unpackDuration." } | ||
|
||
if (!archive.delete()) { | ||
logger.warn { "Unable to delete the JDK archive from '$archive'." } | ||
} | ||
|
||
return Result.success(installDir.singleContainedDirectoryOrThis()) | ||
} | ||
} |