Skip to content

Commit 583821d

Browse files
committed
Don't install artifacts that have already been installed unless the --override option is present
1 parent 8ec6b49 commit 583821d

3 files changed

Lines changed: 88 additions & 9 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package edu.wpi.first.wpilib.opencv.installer;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@Getter
7+
@RequiredArgsConstructor
8+
public enum ArtifactType {
9+
10+
/**
11+
* The artifact type for the Java library.
12+
*/
13+
JAVA("Java library"),
14+
15+
/**
16+
* The artifact type for the JNI bindings.
17+
*/
18+
JNI("JNI bindings"),
19+
20+
/**
21+
* The artifact type for the C++ headers.
22+
*/
23+
HEADERS("C++ headers"),
24+
25+
/**
26+
* The artifact type for the C++ native libraries.
27+
*/
28+
NATIVES("C++ native libraries");
29+
30+
private final String artifactName;
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package edu.wpi.first.wpilib.opencv.installer;
2+
3+
import lombok.experimental.UtilityClass;
4+
5+
import java.util.prefs.Preferences;
6+
7+
/**
8+
* Utility class for checking if artifacts have already been installed.
9+
*/
10+
@UtilityClass
11+
public class InstallChecker {
12+
13+
private static final Preferences prefs = Preferences.userNodeForPackage(InstallChecker.class);
14+
15+
/**
16+
* Checks if the given artifact type has already been installed by this app.
17+
*
18+
* @param which the artifact type to check
19+
* @param version the version of OpenCV to check for
20+
*
21+
* @return true if the given artifact has already been installed, false if it has not
22+
*/
23+
public static boolean isInstalled(ArtifactType which, String version) {
24+
return prefs.node(version).getBoolean(which.name(), false);
25+
}
26+
27+
/**
28+
* Sets the given artifact type as having been successfully installed.
29+
*
30+
* @param which the artifact type that was installed
31+
* @param version the version of OpenCV the artifact is for
32+
*/
33+
public static void registerSuccessfulInstall(ArtifactType which, String version) {
34+
prefs.node(version).putBoolean(which.name(), true);
35+
}
36+
37+
}

src/main/java/edu/wpi/first/wpilib/opencv/installer/Installer.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ public class Installer {
5151
private static String openCvVersion = "";
5252
private static String version = "";
5353

54-
private enum ArtifactType {
55-
JAVA, // This one has special cases because the jar isn't packaged, so unzipping it would just output a ton of class files
56-
JNI,
57-
HEADERS,
58-
NATIVES
59-
}
60-
6154

6255
/**
6356
* Main entry point.
@@ -87,7 +80,7 @@ public static void main(String[] args) throws ParseException {
8780
if (parsedArgs.hasOption("platform")) {
8881
setPlatform(Platform.valueOf(parsedArgs.getOptionValue("platform")));
8982
}
90-
setVersion(parsedArgs.getOptionValue("version"));
83+
setOpenCvVersion(parsedArgs.getOptionValue("version"));
9184
overwrite = parsedArgs.hasOption("overwrite");
9285
System.out.println("Installing specified OpenCV components");
9386
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
@@ -127,7 +120,7 @@ public static void setPlatform(Platform p) {
127120
*
128121
* @param v the version of OpenCV to install
129122
*/
130-
public static void setVersion(String v) {
123+
public static void setOpenCvVersion(String v) {
131124
openCvVersion = v;
132125
calculateVersion();
133126
}
@@ -136,6 +129,15 @@ private static void calculateVersion() {
136129
version = platform + "-" + openCvVersion;
137130
}
138131

132+
/**
133+
* Gets the version of OpenCV being installed.
134+
*
135+
* @return the version of OpenCV being installed
136+
*/
137+
public static String getOpenCvVersion() {
138+
return openCvVersion;
139+
}
140+
139141
/**
140142
* Downloads the Java API jar.
141143
*/
@@ -177,6 +179,12 @@ public static void installNatives() {
177179
}
178180

179181
private static void install(ArtifactType type) {
182+
if (!overridePlatform && InstallChecker.isInstalled(type, openCvVersion)) {
183+
System.out.println("Artifacts for the version " + openCvVersion + " " + type.getArtifactName() + " have already been installed!");
184+
if (!overwrite) {
185+
return;
186+
}
187+
}
180188
try {
181189
String artifactId;
182190
String v = version;
@@ -231,6 +239,7 @@ private static void install(ArtifactType type) {
231239
unzipped = dst.getParent();
232240
}
233241
copyAll(unzipped, Paths.get(installLocation));
242+
InstallChecker.registerSuccessfulInstall(type, openCvVersion);
234243
} catch (Exception e) {
235244
e.printStackTrace(System.out);
236245
}
@@ -252,6 +261,7 @@ private static String resolveRelative(String repo, String artifactId, String ver
252261
* Unzips the given zip file
253262
*
254263
* @param zipFile the file to unzip
264+
*
255265
* @return the directory that the file was unzipped into
256266
*/
257267
private static Path unzip(File zipFile) {
@@ -282,6 +292,7 @@ private static Path unzip(File zipFile) {
282292
*
283293
* @param sourceDir the directory holding the files to copy
284294
* @param dstDir the directory to copy the files into
295+
*
285296
* @throws IOException if the source directory is unreadable
286297
*/
287298
private static void copyAll(Path sourceDir, Path dstDir) throws IOException {

0 commit comments

Comments
 (0)