Skip to content

Commit 1e13f70

Browse files
committed
refactor: introduced Resolver
1 parent dbf6e90 commit 1e13f70

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

src/main/java/org/codejive/jpm/Jpm.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public Jpm build() {
9191
*/
9292
public SyncStats copy(String[] artifactNames, boolean sync)
9393
throws IOException, DependencyResolutionException {
94-
List<Path> files = ResolverUtils.resolveArtifactPaths(artifactNames);
94+
List<Path> files = Resolver.create(artifactNames).resolvePaths();
9595
return FileUtils.syncArtifacts(files, directory, noLinks, !sync);
9696
}
9797

@@ -134,7 +134,7 @@ public SyncStats install(String[] artifactNames)
134134
AppInfo appInfo = AppInfo.read();
135135
String[] artifacts = getArtifacts(artifactNames, appInfo);
136136
if (artifacts.length > 0) {
137-
List<Path> files = ResolverUtils.resolveArtifactPaths(artifacts);
137+
List<Path> files = Resolver.create(artifacts).resolvePaths();
138138
SyncStats stats = FileUtils.syncArtifacts(files, directory, noLinks, true);
139139
if (artifactNames.length > 0) {
140140
for (String dep : artifactNames) {
@@ -165,7 +165,7 @@ public List<Path> path(String[] artifactNames)
165165
AppInfo appInfo = AppInfo.read();
166166
String[] deps = getArtifacts(artifactNames, appInfo);
167167
if (deps.length > 0) {
168-
return ResolverUtils.resolveArtifactPaths(deps);
168+
return Resolver.create(deps).resolvePaths();
169169
} else {
170170
return Collections.emptyList();
171171
}

src/main/java/org/codejive/jpm/Main.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// spotless:off Dependencies for JBang
2-
//DEPS eu.maveniverse.maven.mima:context:2.4.15 eu.maveniverse.maven.mima.runtime:standalone-static:2.4.15
3-
//DEPS info.picocli:picocli:4.7.6
4-
//DEPS org.yaml:snakeyaml:2.3
5-
//DEPS org.jline:jline-console-ui:3.29.0 org.jline:jline-terminal-jni:3.29.0
6-
//DEPS org.slf4j:slf4j-api:2.0.13 org.slf4j:slf4j-simple:2.0.13
7-
//SOURCES Jpm.java json/AppInfo.java util/FileUtils.java util/ResolverUtils.java util/ScriptUtils.java
2+
//DEPS eu.maveniverse.maven.mima:context:2.4.34 eu.maveniverse.maven.mima.runtime:standalone-static:2.4.34
3+
//DEPS info.picocli:picocli:4.7.7
4+
//DEPS org.yaml:snakeyaml:2.4
5+
//DEPS org.jline:jline-console-ui:3.30.5 org.jline:jline-terminal-jni:3.30.5
6+
//DEPS org.slf4j:slf4j-api:2.0.17 org.slf4j:slf4j-simple:2.0.17
7+
//SOURCES Jpm.java json/AppInfo.java util/FileUtils.java util/Resolver.java util/ScriptUtils.java
88
//SOURCES util/SearchResult.java util/SearchUtils.java util/SyncStats.java util/Version.java
99
// spotless:on
1010

src/main/java/org/codejive/jpm/util/ResolverUtils.java renamed to src/main/java/org/codejive/jpm/util/Resolver.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import eu.maveniverse.maven.mima.context.ContextOverrides;
55
import eu.maveniverse.maven.mima.context.Runtime;
66
import eu.maveniverse.maven.mima.context.Runtimes;
7-
import java.nio.file.Path;
8-
import java.util.Arrays;
9-
import java.util.List;
10-
import java.util.stream.Collectors;
117
import org.eclipse.aether.artifact.Artifact;
128
import org.eclipse.aether.artifact.DefaultArtifact;
139
import org.eclipse.aether.collection.CollectRequest;
@@ -18,22 +14,34 @@
1814
import org.eclipse.aether.resolution.DependencyResult;
1915
import org.eclipse.aether.util.artifact.JavaScopes;
2016

21-
/** Utility class for resolving Maven artifacts. */
22-
public class ResolverUtils {
23-
/**
24-
* Resolves the paths of the given artifacts. Handles parsing and resolving of artifacts and
25-
* extracts their paths.
26-
*
27-
* @param artifactNames the artifacts to resolve as an array of strings in the format
28-
* "groupId:artifactId:version"
29-
* @return the paths of the resolved artifacts
30-
* @throws DependencyResolutionException if an error occurs while resolving the artifacts
31-
*/
32-
public static List<Path> resolveArtifactPaths(String[] artifactNames)
33-
throws DependencyResolutionException {
34-
List<Artifact> artifacts = parseArtifacts(artifactNames);
35-
List<ArtifactResult> resolvedArtifacts = resolveArtifacts(artifacts);
36-
return resolvedArtifacts.stream()
17+
import java.nio.file.Path;
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.stream.Collectors;
21+
22+
public class Resolver {
23+
private final List<Artifact> artifacts;
24+
25+
private List<ArtifactResult> resolvedArtifacts;
26+
27+
public static Resolver create(String[] artifactNames) {
28+
return new Resolver(artifactNames);
29+
}
30+
31+
private Resolver(String[] artifactNames) {
32+
artifacts = parseArtifacts(artifactNames);
33+
}
34+
35+
public List<ArtifactResult> resolve() throws DependencyResolutionException {
36+
if (resolvedArtifacts == null) {
37+
resolvedArtifacts = resolveArtifacts(artifacts);
38+
}
39+
return resolvedArtifacts;
40+
}
41+
42+
public List<Path> resolvePaths() throws DependencyResolutionException {
43+
List<ArtifactResult> ras = resolve();
44+
return ras.stream()
3745
.map(ar -> ar.getArtifact().getFile().toPath())
3846
.collect(Collectors.toList());
3947
}

0 commit comments

Comments
 (0)