Skip to content

Commit afc114d

Browse files
committed
Separated JDK management code into its own module
Fixes #1857
1 parent 6189150 commit afc114d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2629
-1624
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ sourceSets {
9090
sourceSets.main.compileClasspath += sourceSets.java9.output.classesDirs;
9191

9292
dependencies {
93+
implementation project(':jdkmanager')
94+
9395
implementation 'com.offbytwo:docopt:0.6.0.20150202'
9496

9597
implementation 'org.apache.commons:commons-text:1.11.0'

jdkmanager/.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.classpath
2+
.project
3+
.vscode
4+
.settings
5+
target
6+
.idea
7+
*.iml
8+
/build
9+
.gradle
10+
.factorypath
11+
bin
12+
homebrew-tap
13+
RESULTS
14+
*.db
15+
jbang-action
16+
out
17+
node_modules
18+
package-lock.json
19+
*.jfr
20+
itests/hello.java
21+
*.class
22+
CHANGELOG.md

jdkmanager/build.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group = 'dev.jbang.jvm'
6+
version = '0.1.0'
7+
8+
sourceCompatibility = '8'
9+
targetCompatibility = '8'
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation 'org.apache.commons:commons-compress:1.26.2'
17+
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
18+
implementation 'org.apache.httpcomponents:httpclient-cache:4.5.14'
19+
implementation 'com.google.code.gson:gson:2.11.0'
20+
21+
implementation "org.slf4j:slf4j-nop:1.7.30"
22+
implementation "org.slf4j:jcl-over-slf4j:1.7.30"
23+
implementation "org.jspecify:jspecify:1.0.0"
24+
25+
testImplementation platform('org.junit:junit-bom:5.10.1')
26+
testImplementation 'org.junit.jupiter:junit-jupiter'
27+
}
28+
29+
test {
30+
useJUnitPlatform()
31+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package dev.jbang.jvm;
2+
3+
import java.nio.file.Path;
4+
import java.util.HashSet;
5+
import java.util.Objects;
6+
import java.util.Set;
7+
8+
import dev.jbang.jvm.util.JavaUtils;
9+
import org.jspecify.annotations.NonNull;
10+
import org.jspecify.annotations.Nullable;
11+
12+
public interface Jdk extends Comparable<Jdk> {
13+
@NonNull JdkProvider getProvider();
14+
15+
@NonNull String getId();
16+
17+
@NonNull String getVersion();
18+
19+
@Nullable Path getHome();
20+
21+
int getMajorVersion();
22+
23+
@NonNull Jdk install();
24+
25+
void uninstall();
26+
27+
boolean isInstalled();
28+
29+
class Default implements Jdk {
30+
@NonNull private final transient JdkProvider provider;
31+
@NonNull private final String id;
32+
@NonNull private final String version;
33+
@Nullable private final Path home;
34+
@NonNull private final Set<String> tags = new HashSet<>();
35+
36+
Default(
37+
@NonNull JdkProvider provider,
38+
@NonNull String id,
39+
@Nullable Path home,
40+
@NonNull String version,
41+
@NonNull String... tags) {
42+
this.provider = provider;
43+
this.id = id;
44+
this.version = version;
45+
this.home = home;
46+
}
47+
48+
@Override
49+
@NonNull
50+
public JdkProvider getProvider() {
51+
return provider;
52+
}
53+
54+
/** Returns the id that is used to uniquely identify this JDK across all providers */
55+
@Override
56+
@NonNull
57+
public String getId() {
58+
return id;
59+
}
60+
61+
/** Returns the JDK's version */
62+
@Override
63+
@NonNull
64+
public String getVersion() {
65+
return version;
66+
}
67+
68+
/**
69+
* The path to where the JDK is installed. Can be <code>null</code> which means the JDK
70+
* isn't currently installed by that provider
71+
*/
72+
@Override
73+
@Nullable
74+
public Path getHome() {
75+
return home;
76+
}
77+
78+
@Override
79+
public int getMajorVersion() {
80+
return JavaUtils.parseJavaVersion(getVersion());
81+
}
82+
83+
@Override
84+
@NonNull
85+
public Jdk install() {
86+
return provider.install(this);
87+
}
88+
89+
@Override
90+
public void uninstall() {
91+
provider.uninstall(this);
92+
}
93+
94+
@Override
95+
public boolean isInstalled() {
96+
return home != null;
97+
}
98+
99+
@Override
100+
public boolean equals(Object o) {
101+
if (this == o) return true;
102+
if (o == null || getClass() != o.getClass()) return false;
103+
Default jdk = (Default) o;
104+
return id.equals(jdk.id) && Objects.equals(home, jdk.home);
105+
}
106+
107+
@Override
108+
public int hashCode() {
109+
return Objects.hash(home, id);
110+
}
111+
112+
@Override
113+
public int compareTo(Jdk o) {
114+
return Integer.compare(getMajorVersion(), o.getMajorVersion());
115+
}
116+
117+
@Override
118+
public String toString() {
119+
return getMajorVersion() + " (" + version + ", " + id + ", " + home + ")";
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)