Skip to content

Commit 2d8dc9d

Browse files
committed
refactor: restructured codebase
It's now plit up into more logical units which also makes it easier to use as a library.
1 parent 2209bf5 commit 2d8dc9d

File tree

6 files changed

+345
-232
lines changed

6 files changed

+345
-232
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package org.codejive.jpm;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.util.*;
6+
import org.codejive.jpm.json.JpmProject;
7+
import org.codejive.jpm.util.FileUtils;
8+
import org.codejive.jpm.util.ResolverUtils;
9+
import org.codejive.jpm.util.SyncStats;
10+
import org.eclipse.aether.resolution.DependencyResolutionException;
11+
12+
public class Jpm {
13+
private final Path directory;
14+
private final boolean noLinks;
15+
16+
private Jpm(Path directory, boolean noLinks) {
17+
this.directory = directory;
18+
this.noLinks = noLinks;
19+
}
20+
21+
public static Builder builder() {
22+
return new Builder();
23+
}
24+
25+
public static class Builder {
26+
private Path directory;
27+
private boolean noLinks;
28+
29+
private Builder() {}
30+
31+
public Builder directory(Path directory) {
32+
this.directory = directory;
33+
return this;
34+
}
35+
36+
public Builder noLinks(boolean noLinks) {
37+
this.noLinks = noLinks;
38+
return this;
39+
}
40+
41+
public Jpm build() {
42+
return new Jpm(directory, noLinks);
43+
}
44+
}
45+
46+
public SyncStats copy(String[] artifactNames)
47+
throws IOException, DependencyResolutionException {
48+
List<Path> files = ResolverUtils.resolveArtifactPaths(artifactNames);
49+
return FileUtils.syncArtifacts(files, directory, noLinks, true);
50+
}
51+
52+
public SyncStats sync(String[] artifactNames)
53+
throws IOException, DependencyResolutionException {
54+
List<Path> files = ResolverUtils.resolveArtifactPaths(artifactNames);
55+
return FileUtils.syncArtifacts(files, directory, noLinks, false);
56+
}
57+
58+
public SyncStats install(String[] artifactNames)
59+
throws IOException, DependencyResolutionException {
60+
JpmProject prj = JpmProject.read();
61+
String[] artifacts = getArtifacts(artifactNames, prj);
62+
if (artifacts.length > 0) {
63+
List<Path> files = ResolverUtils.resolveArtifactPaths(artifacts);
64+
SyncStats stats = FileUtils.syncArtifacts(files, directory, noLinks, true);
65+
if (artifactNames.length > 0) {
66+
for (String dep : artifactNames) {
67+
int p = dep.lastIndexOf(':');
68+
String name = dep.substring(0, p);
69+
String version = dep.substring(p + 1);
70+
prj.dependencies.put(name, version);
71+
}
72+
JpmProject.write(prj);
73+
}
74+
return stats;
75+
} else {
76+
return new SyncStats();
77+
}
78+
}
79+
80+
public List<Path> path(String[] artifactNames)
81+
throws DependencyResolutionException, IOException {
82+
JpmProject prj = JpmProject.read();
83+
String[] deps = getArtifacts(artifactNames, prj);
84+
if (deps.length > 0) {
85+
return ResolverUtils.resolveArtifactPaths(deps);
86+
} else {
87+
return Collections.emptyList();
88+
}
89+
}
90+
91+
public int run(String name, String[] args) throws IOException, InterruptedException {
92+
String cmdString = JpmProject.read().commands.get(name);
93+
if (cmdString == null) {
94+
throw new IllegalArgumentException("Command not found: " + name);
95+
}
96+
String[] quotedArgs =
97+
Arrays.stream(args)
98+
.map(a -> a.contains(" ") ? "\"" + a + "\"" : a)
99+
.toArray(String[]::new);
100+
String extraArgs = String.join(" ", quotedArgs);
101+
String[] cmd;
102+
if (isWindows()) {
103+
cmd = new String[] {"cmd", "/c", String.join(" ", cmdString, extraArgs)};
104+
} else {
105+
cmd = new String[] {"/bin/sh", "-c", String.join(" ", cmdString, extraArgs)};
106+
}
107+
return new ProcessBuilder(cmd).inheritIO().start().waitFor();
108+
}
109+
110+
private static String[] getArtifacts(String[] artifactNames, JpmProject prj) {
111+
String[] deps;
112+
if (artifactNames.length > 0) {
113+
deps = artifactNames;
114+
} else {
115+
deps =
116+
prj.dependencies.entrySet().stream()
117+
.map(e -> e.getKey() + ":" + e.getValue())
118+
.toArray(String[]::new);
119+
}
120+
return deps;
121+
}
122+
123+
private static boolean isWindows() {
124+
String os =
125+
System.getProperty("os.name")
126+
.toLowerCase(Locale.ENGLISH)
127+
.replaceAll("[^a-z0-9]+", "");
128+
return os.startsWith("win");
129+
}
130+
}

0 commit comments

Comments
 (0)