Skip to content

Commit 6dfad39

Browse files
Download binary at custom path if not present already
1 parent a5f615e commit 6dfad39

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/main/java/com/browserstack/local/Local.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ public Local() {
5353
*/
5454
public void start(Map<String, String> options) throws Exception {
5555
startOptions = options;
56+
LocalBinary lb;
5657
if (options.get("binarypath") != null) {
57-
binaryPath = options.get("binarypath");
58+
lb = new LocalBinary(options.get("binarypath"));
5859
} else {
59-
LocalBinary lb = new LocalBinary();
60-
binaryPath = lb.getBinaryPath();
60+
lb = new LocalBinary("");
6161
}
62+
binaryPath = lb.getBinaryPath();
6263

6364
makeCommand(options, "start");
6465

@@ -106,12 +107,13 @@ public void stop() throws Exception {
106107
* @param options Options supplied for the Local instance
107108
**/
108109
public void stop(Map<String, String> options) throws Exception {
110+
LocalBinary lb;
109111
if (options.get("binarypath") != null) {
110-
binaryPath = options.get("binarypath");
112+
lb = new LocalBinary(options.get("binarypath"));
111113
} else {
112-
LocalBinary lb = new LocalBinary();
113-
binaryPath = lb.getBinaryPath();
114+
lb = new LocalBinary("");
114115
}
116+
binaryPath = lb.getBinaryPath();
115117
makeCommand(options, "stop");
116118
proc = runCommand(command);
117119
proc.waitFor();

src/main/java/com/browserstack/local/LocalBinary.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ class LocalBinary {
2424
System.getProperty("java.io.tmpdir")
2525
};
2626

27-
LocalBinary() throws LocalException {
27+
LocalBinary(String path) throws LocalException {
2828
initialize();
29-
getBinary();
29+
if (path != "") {
30+
getBinaryOnPath(path);
31+
} else {
32+
getBinary();
33+
}
3034
checkBinary();
3135
}
3236

@@ -111,6 +115,14 @@ private boolean validateBinary() throws LocalException{
111115
}
112116
}
113117

118+
private void getBinaryOnPath(String path) throws LocalException {
119+
binaryPath = path;
120+
121+
if (!new File(binaryPath).exists()) {
122+
downloadBinary(binaryPath, true);
123+
}
124+
}
125+
114126
private void getBinary() throws LocalException {
115127
String destParentDir = getAvailableDirectory();
116128
binaryPath = destParentDir + "/BrowserStackLocal";
@@ -120,7 +132,7 @@ private void getBinary() throws LocalException {
120132
}
121133

122134
if (!new File(binaryPath).exists()) {
123-
downloadBinary(destParentDir);
135+
downloadBinary(destParentDir, false);
124136
}
125137
}
126138

@@ -147,23 +159,26 @@ private boolean makePath(String path) {
147159
}
148160
}
149161

150-
private void downloadBinary(String destParentDir) throws LocalException {
162+
private void downloadBinary(String destParentDir, Boolean custom) throws LocalException {
151163
try {
152-
if (!new File(destParentDir).exists())
153-
new File(destParentDir).mkdirs();
154-
155-
URL url = new URL(httpPath);
156-
String source = destParentDir + "/BrowserStackLocal";
157-
if (isOSWindows) {
158-
source += ".exe";
164+
String source = destParentDir;
165+
if (!custom) {
166+
if (!new File(destParentDir).exists())
167+
new File(destParentDir).mkdirs();
168+
169+
source = destParentDir + "/BrowserStackLocal";
170+
if (isOSWindows) {
171+
source += ".exe";
172+
}
159173
}
174+
URL url = new URL(httpPath);
160175

161176
File f = new File(source);
162177
FileUtils.copyURLToFile(url, f);
163178

164179
changePermissions(binaryPath);
165180
} catch (Exception e) {
166-
throw new LocalException("Error trying to download BrowserStackLocal binary");
181+
throw new LocalException("Error trying to download BrowserStackLocal binary: " + e.getMessage());
167182
}
168183
}
169184

0 commit comments

Comments
 (0)