Skip to content

Commit a6d7364

Browse files
committed
Allow install locations to be customized
1 parent 06e7c34 commit a6d7364

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@ public class InstallChecker {
1515
/**
1616
* Checks if the given artifact type has already been installed by this app.
1717
*
18-
* @param which the artifact type to check
19-
* @param version the version of OpenCV to check for
18+
* @param which the artifact type to check
19+
* @param location the location to check
20+
* @param version the version of OpenCV to check for
2021
*
2122
* @return true if the given artifact has already been installed, false if it has not
2223
*/
23-
public static boolean isInstalled(ArtifactType which, String version) {
24-
return prefs.node(version).getBoolean(which.name(), false);
24+
public static boolean isInstalled(ArtifactType which, String location, String version) {
25+
return prefs.node(version).getBoolean(location + "_" + which.name(), false);
2526
}
2627

2728
/**
2829
* Sets the given artifact type as having been successfully installed.
2930
*
30-
* @param which the artifact type that was installed
31-
* @param version the version of OpenCV the artifact is for
31+
* @param which the artifact type that was installed
32+
* @param location the location where OpenCV was installed
33+
* @param version the version of OpenCV the artifact is for
3234
*/
33-
public static void registerSuccessfulInstall(ArtifactType which, String version) {
34-
prefs.node(version).putBoolean(which.name(), true);
35+
public static void registerSuccessfulInstall(ArtifactType which, String location, String version) {
36+
prefs.node(version).putBoolean(location + "_" + which.name(), true);
3537
}
3638

3739
}

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

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,38 @@ public class Installer {
5858
public static void main(String[] args) throws ParseException {
5959
CommandLineParser p = new DefaultParser();
6060
Options options = new Options() {{
61-
addOption("j", "java", false, "Install the Java API jar in the working directory");
62-
addOption("jni", "jni", false, "Install the JNI bindings for this operating system in " + platform.getJniInstallLocation());
63-
addOption("h", "headers", false, "Install the C++ headers in " + platform.getHeadersInstallLocation());
64-
addOption("n", "natives", false, "Install the C++ native libraries in " + platform.getNativesInstallLocation());
61+
addOption(Option.builder("j")
62+
.longOpt("java")
63+
.optionalArg(true)
64+
.numberOfArgs(1)
65+
.argName("install-path")
66+
.desc("Install the OpenCV Java library")
67+
.build()
68+
);
69+
addOption(Option.builder("jni")
70+
.longOpt("jni")
71+
.optionalArg(true)
72+
.numberOfArgs(1)
73+
.argName("install-path")
74+
.desc("Install the OpenCV JNI bindings")
75+
.build()
76+
);
77+
addOption(Option.builder("h")
78+
.longOpt("headers")
79+
.optionalArg(true)
80+
.numberOfArgs(1)
81+
.argName("install-path")
82+
.desc("Install the OpenCV C++ headers")
83+
.build()
84+
);
85+
addOption(Option.builder("n")
86+
.longOpt("natives")
87+
.optionalArg(true)
88+
.numberOfArgs(1)
89+
.argName("install-path")
90+
.desc("Install the OpenCV native libraries")
91+
.build()
92+
);
6593
addOption("help", "help", false, "Prints this help message");
6694
addOption("a", "all", false, "Installs all artifacts");
6795
addOption("v", "version", true, "Set the version of OpenCV to install");
@@ -85,28 +113,28 @@ public static void main(String[] args) throws ParseException {
85113
System.out.println("Installing specified OpenCV components");
86114
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
87115
try {
88-
installJava();
116+
installJava(parsedArgs.getOptionValue("java", platform.getJavaInstallLocation()));
89117
} catch (IOException e) {
90118
e.printStackTrace();
91119
}
92120
}
93121
if (parsedArgs.hasOption("jni") || parsedArgs.hasOption("all")) {
94122
try {
95-
installJni();
123+
installJni(parsedArgs.getOptionValue("jni", platform.getJniInstallLocation()));
96124
} catch (IOException e) {
97125
e.printStackTrace();
98126
}
99127
}
100128
if (parsedArgs.hasOption("headers") || parsedArgs.hasOption("all")) {
101129
try {
102-
installHeaders();
130+
installHeaders(parsedArgs.getOptionValue("headers", platform.getHeadersInstallLocation()));
103131
} catch (IOException e) {
104132
e.printStackTrace();
105133
}
106134
}
107135
if (parsedArgs.hasOption("natives") || parsedArgs.hasOption("all")) {
108136
try {
109-
installNatives();
137+
installNatives(parsedArgs.getOptionValue("natives", platform.getNativesInstallLocation()));
110138
} catch (IOException e) {
111139
e.printStackTrace();
112140
}
@@ -157,45 +185,49 @@ public static String getOpenCvVersion() {
157185
/**
158186
* Downloads the Java API jar.
159187
*/
160-
public static void installJava() throws IOException {
188+
public static void installJava(String location) throws IOException {
161189
System.out.println("====================");
162-
System.out.println("Installing Java");
190+
System.out.println("Installing Java to " + location);
163191
System.out.println("====================");
164-
install(ArtifactType.JAVA);
192+
install(ArtifactType.JAVA, location);
165193
}
166194

167195
/**
168196
* Installs the JNI bindings.
169197
*/
170-
public static void installJni() throws IOException {
198+
public static void installJni(String location) throws IOException {
171199
System.out.println("====================");
172-
System.out.println("Installing JNI");
200+
System.out.println("Installing JNI to " + location);
173201
System.out.println("====================");
174-
install(ArtifactType.JNI);
202+
install(ArtifactType.JNI, location);
175203
}
176204

177205
/**
178206
* Installs the C++ headers.
179207
*/
180-
public static void installHeaders() throws IOException {
208+
public static void installHeaders(String location) throws IOException {
181209
System.out.println("====================");
182-
System.out.println("Installing headers");
210+
System.out.println("Installing headers to " + location);
183211
System.out.println("====================");
184-
install(ArtifactType.HEADERS);
212+
install(ArtifactType.HEADERS, location);
185213
}
186214

187215
/**
188216
* Installs the C++ native libraries.
189217
*/
190-
public static void installNatives() throws IOException {
218+
public static void installNatives(String location) throws IOException {
191219
System.out.println("====================");
192-
System.out.println("Installing natives");
220+
System.out.println("Installing natives to " + location);
193221
System.out.println("====================");
194-
install(ArtifactType.NATIVES);
222+
install(ArtifactType.NATIVES, location);
195223
}
196224

197-
private static void install(ArtifactType type) throws IOException {
198-
if (!overridePlatform && InstallChecker.isInstalled(type, openCvVersion)) {
225+
private static void install(ArtifactType type, String location) throws IOException {
226+
if (!Paths.get(location).isAbsolute()) {
227+
// Force location to be an absolute path
228+
location = Paths.get(location).toAbsolutePath().toString();
229+
}
230+
if (!overridePlatform && InstallChecker.isInstalled(type, location, openCvVersion)) {
199231
System.out.println("Artifacts for the version " + openCvVersion + " " + type.getArtifactName() + " have already been installed!");
200232
if (!overwrite) {
201233
return;
@@ -211,20 +243,20 @@ private static void install(ArtifactType type) throws IOException {
211243
case JAVA:
212244
artifactId = javaJarName;
213245
v = openCvVersion;
214-
installLocation += platform.getJavaInstallLocation();
246+
installLocation += location;
215247
break;
216248
case JNI:
217249
artifactId = jniName;
218-
installLocation += platform.getJniInstallLocation();
250+
installLocation += location;
219251
break;
220252
case HEADERS:
221253
artifactId = headersName;
222254
v = openCvVersion;
223-
installLocation += platform.getHeadersInstallLocation();
255+
installLocation += location;
224256
break;
225257
case NATIVES:
226258
artifactId = nativesName;
227-
installLocation += platform.getNativesInstallLocation();
259+
installLocation += location;
228260
break;
229261
default:
230262
throw new UnsupportedOperationException("Unknown artifact type: " + type);
@@ -255,7 +287,7 @@ private static void install(ArtifactType type) throws IOException {
255287
}
256288
copyAll(unzipped, Paths.get(installLocation));
257289
if (!overridePlatform) {
258-
InstallChecker.registerSuccessfulInstall(type, openCvVersion);
290+
InstallChecker.registerSuccessfulInstall(type, location, openCvVersion);
259291
}
260292
}
261293

0 commit comments

Comments
 (0)