Skip to content

Commit eb61edc

Browse files
authored
Merge pull request #75 from browserstack/LOC_4239_download_custom_path
LOC 4239 Download binary at custom path if not present already
2 parents a5f615e + 397d66e commit eb61edc

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Add this dependency to your project's POM:
1111
<dependency>
1212
<groupId>com.browserstack</groupId>
1313
<artifactId>browserstack-local-java</artifactId>
14-
<version>1.0.9</version>
14+
<version>1.1.0</version>
1515
</dependency>
1616
```
1717

browserstack.err

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[object Object]

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<groupId>com.browserstack</groupId>
44
<artifactId>browserstack-local-java</artifactId>
55
<packaging>jar</packaging>
6-
<version>1.0.9-SNAPSHOT</version>
6+
<version>1.1.0-SNAPSHOT</version>
77

88
<name>browserstack-local-java</name>
99
<description>Java bindings for BrowserStack Local</description>

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Local {
2323
private LocalProcess proc = null;
2424

2525
// Current version of binding package, used for --source option of binary
26-
private final String packageVersion = "1.0.9";
26+
private final String packageVersion = "1.1.0";
2727
private final Map<String, String> parameters;
2828
private final Map<String, String> avoidValueParameters;
2929

@@ -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)