Skip to content

feat: implemented support for Jdk distro and tags #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/dev/jbang/devkitman/Distro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.jbang.devkitman;

public class Distro {
public final String name;
public final boolean isGraalVM;

public Distro(String name, boolean isGraalVM) {
this.name = name;
this.isGraalVM = isGraalVM;
}
}
36 changes: 33 additions & 3 deletions src/main/java/dev/jbang/devkitman/Jdk.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.jbang.devkitman;

import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -43,6 +44,20 @@ public interface Jdk extends Comparable<Jdk> {
@NonNull
Path home();

/**
* The JDK distribution name (e.g. "openjdk", "zulu", "temurin", etc.). Can be
* <code>null</code> if the provider doesn't have or support a distribution
* names
*/
String distro();

/**
* Returns a set of tags that can be used to give additional information about
* the JDK
*/
@NonNull
Set<String> tags();

/**
* Returns the major version of the JDK
*/
Expand Down Expand Up @@ -78,21 +93,25 @@ class Default implements Jdk {
private final boolean fixedVersion;
@Nullable
private final Path home;
private final String distro;
@NonNull
private final Set<String> tags = new HashSet<>();
private final Set<String> tags;

Default(
@NonNull JdkProvider provider,
@NonNull String id,
@Nullable Path home,
@NonNull String version,
boolean fixedVersion,
@NonNull String... tags) {
String distro,
@NonNull Set<String> tags) {
this.provider = provider;
this.id = id;
this.version = version;
this.fixedVersion = fixedVersion;
this.home = home;
this.distro = distro;
this.tags = Collections.unmodifiableSet(new HashSet<>(tags));
}

@Override
Expand Down Expand Up @@ -128,6 +147,17 @@ public Path home() {
return home;
}

@Override
public String distro() {
return distro;
}

@Override
@NonNull
public Set<String> tags() {
return tags;
}

@Override
public int majorVersion() {
return JavaUtils.parseJavaVersion(version());
Expand Down Expand Up @@ -176,7 +206,7 @@ public int compareTo(Jdk o) {
@Override
public String toString() {
return majorVersion() + " (" + version + (isFixedVersion() ? " (fixed)" : " (dynamic)") + ", " + id + ", "
+ home + ")";
+ home + ", " + distro + ", " + tags + ")";
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/main/java/dev/jbang/devkitman/JdkInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Set;

import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
Expand All @@ -13,17 +14,33 @@
*/
public interface JdkInstaller {

/**
* This method returns a list of distributions that are supported by this
* installer.
*
* @return List of distribution names
*/
@NonNull
default List<Distro> listDistros() {
throw new UnsupportedOperationException(
"Listing available distributions is not supported by " + getClass().getName());
}

/**
* This method returns a set of JDKs that are available for installation.
* Implementations might set the <code>home</code> field of the JDK objects if
* the respective JDK is currently installed on the user's system, but only if
* they can ensure that it's the exact same version, otherwise they should just
* leave the field <code>null</code>.
*
* @param distros Comma separated list of distribution names to look for. Can be
* null to use a default selection defined by the installer. Use
* an empty string to list all.
* @param tags The tags to filter the JDKs by. Can be null to list all.
* @return List of <code>Jdk</code> objects
*/
@NonNull
default List<Jdk> listAvailable() {
default List<Jdk> listAvailable(String distros, Set<String> tags) {
throw new UnsupportedOperationException(
"Listing available JDKs is not supported by " + getClass().getName());
}
Expand All @@ -43,7 +60,7 @@ default List<Jdk> listAvailable() {
*/
@Nullable
default Jdk getAvailableByIdOrToken(String idOrToken) {
return JdkManager.getJdkBy(listAvailable().stream(), Jdk.Predicates.id(idOrToken))
return JdkManager.getJdkBy(listAvailable(null, null).stream(), Jdk.Predicates.id(idOrToken))
.orElse(null);
}

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/dev/jbang/devkitman/JdkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,19 @@ private Optional<Jdk> prevAvailableJdk(int maxVersion) {
.max(Jdk::compareTo);
}

public Set<Distro> listAvailableDistros() {
return providers(JdkProvider.Predicates.canUpdate)
.flatMap(p -> p.listDistros().stream())
.collect(Collectors.toSet());
}

public List<Jdk> listAvailableJdks() {
return listAvailableJdks(null, null);
}

public List<Jdk> listAvailableJdks(String distros, Set<String> tags) {
return providers(JdkProvider.Predicates.canUpdate)
.flatMap(p -> p.listAvailable().stream())
.flatMap(p -> p.listAvailable(distros, tags).stream())
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -563,7 +573,9 @@ public void setDefaultJdk(Jdk jdk) {
DefaultJdkProvider.Discovery.PROVIDER_ID,
jdk.home(),
jdk.version(),
false);
false,
jdk.distro(),
jdk.tags());
defaultProvider.install(newDefJdk);
LOGGER.log(Level.INFO, "Default JDK set to {0}", jdk);
}
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/dev/jbang/devkitman/JdkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
*/
public interface JdkProvider {

default Jdk createJdk(@NonNull String id, @Nullable Path home, @NonNull String version, boolean fixedVersion) {
return new Jdk.Default(this, id, home, version, fixedVersion);
default Jdk createJdk(@NonNull String id,
@Nullable Path home,
@NonNull String version,
boolean fixedVersion,
String distro,
@NonNull Set<String> tags) {
return new Jdk.Default(this, id, home, version, fixedVersion, distro, tags);
}

@NonNull
Expand Down Expand Up @@ -134,17 +139,34 @@ default boolean canUpdate() {
return false;
}

/**
* This method returns a list of distributions that are available for
* installation. The list can be empty if the provider does not support
* selecting distributions.
*
* @return List of distribution names
*/
@NonNull
default List<Distro> listDistros() {
throw new UnsupportedOperationException(
"Listing available distributions is not supported by " + getClass().getName());
}

/**
* This method returns a set of JDKs that are available for installation.
* Implementations might set the <code>home</code> field of the JDK objects if
* the respective JDK is currently installed on the user's system, but only if
* they can ensure that it's the exact same version, otherwise they should just
* leave the field <code>null</code>.
*
* @param distros Comma separated list of distribution names to look for. Can be
* null to use a default selection defined by the installer. Use
* an empty string to list all.
* @param tags The tags to filter the JDKs by. Can be null to list all.
* @return List of <code>Jdk</code> objects
*/
@NonNull
default List<Jdk> listAvailable() {
default List<Jdk> listAvailable(String distros, Set<String> tags) {
throw new UnsupportedOperationException(
"Listing available JDKs is not supported by " + getClass().getName());
}
Expand All @@ -164,7 +186,7 @@ default List<Jdk> listAvailable() {
*/
@Nullable
default Jdk getAvailableByIdOrToken(String idOrToken) {
return JdkManager.getJdkBy(listAvailable().stream(), Jdk.Predicates.id(idOrToken))
return JdkManager.getJdkBy(listAvailable(null, null).stream(), Jdk.Predicates.id(idOrToken))
.orElse(null);
}

Expand Down
Loading