Skip to content

Commit

Permalink
refactor: Separated JDK management code into its own module
Browse files Browse the repository at this point in the history
Fixes #1857
  • Loading branch information
quintesse committed Nov 27, 2024
1 parent 6189150 commit f57b1c6
Show file tree
Hide file tree
Showing 43 changed files with 2,629 additions and 1,624 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ sourceSets {
sourceSets.main.compileClasspath += sourceSets.java9.output.classesDirs;

dependencies {
implementation project(':jdkmanager')

implementation 'com.offbytwo:docopt:0.6.0.20150202'

implementation 'org.apache.commons:commons-text:1.11.0'
Expand Down
22 changes: 22 additions & 0 deletions jdkmanager/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.classpath
.project
.vscode
.settings
target
.idea
*.iml
/build
.gradle
.factorypath
bin
homebrew-tap
RESULTS
*.db
jbang-action
out
node_modules
package-lock.json
*.jfr
itests/hello.java
*.class
CHANGELOG.md
31 changes: 31 additions & 0 deletions jdkmanager/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
plugins {
id 'java'
}

group = 'dev.jbang.jvm'
version = '0.1.0'

sourceCompatibility = '8'
targetCompatibility = '8'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.apache.commons:commons-compress:1.26.2'
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
implementation 'org.apache.httpcomponents:httpclient-cache:4.5.14'
implementation 'com.google.code.gson:gson:2.11.0'

implementation "org.slf4j:slf4j-nop:1.7.30"
implementation "org.slf4j:jcl-over-slf4j:1.7.30"
implementation "org.jspecify:jspecify:1.0.0"

testImplementation platform('org.junit:junit-bom:5.10.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
useJUnitPlatform()
}
122 changes: 122 additions & 0 deletions jdkmanager/src/main/java/dev/jbang/jvm/Jdk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dev.jbang.jvm;

import java.nio.file.Path;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import dev.jbang.jvm.util.JavaUtils;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

public interface Jdk extends Comparable<Jdk> {
@NonNull JdkProvider getProvider();

@NonNull String getId();

@NonNull String getVersion();

@Nullable Path getHome();

int getMajorVersion();

@NonNull Jdk install();

void uninstall();

boolean isInstalled();

class Default implements Jdk {
@NonNull private final transient JdkProvider provider;
@NonNull private final String id;
@NonNull private final String version;
@Nullable private final Path home;
@NonNull private final Set<String> tags = new HashSet<>();

Default(
@NonNull JdkProvider provider,
@NonNull String id,
@Nullable Path home,
@NonNull String version,
@NonNull String... tags) {
this.provider = provider;
this.id = id;
this.version = version;
this.home = home;
}

@Override
@NonNull
public JdkProvider getProvider() {
return provider;
}

/** Returns the id that is used to uniquely identify this JDK across all providers */
@Override
@NonNull
public String getId() {
return id;
}

/** Returns the JDK's version */
@Override
@NonNull
public String getVersion() {
return version;
}

/**
* The path to where the JDK is installed. Can be <code>null</code> which means the JDK
* isn't currently installed by that provider
*/
@Override
@Nullable
public Path getHome() {
return home;
}

@Override
public int getMajorVersion() {
return JavaUtils.parseJavaVersion(getVersion());
}

@Override
@NonNull
public Jdk install() {
return provider.install(this);
}

@Override
public void uninstall() {
provider.uninstall(this);
}

@Override
public boolean isInstalled() {
return home != null;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Default jdk = (Default) o;
return id.equals(jdk.id) && Objects.equals(home, jdk.home);
}

@Override
public int hashCode() {
return Objects.hash(home, id);
}

@Override
public int compareTo(Jdk o) {
return Integer.compare(getMajorVersion(), o.getMajorVersion());
}

@Override
public String toString() {
return getMajorVersion() + " (" + version + ", " + id + ", " + home + ")";
}
}
}
Loading

0 comments on commit f57b1c6

Please sign in to comment.