Skip to content

Commit 383c9b0

Browse files
appium#272 fix: an additional improvement
The working with npm was moved to scripts which are packed in the jar file. These scripts should be used in runtime. Changes: - added scripts - AppiumServiceBuilder was refactored once again :) - engines which work with these scrips and npm were implemented
1 parent 5d7297b commit 383c9b0

5 files changed

Lines changed: 236 additions & 136 deletions

File tree

‎pom.xml‎

Lines changed: 115 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
-->
1717

1818
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20-
<modelVersion>4.0.0</modelVersion>
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
2121

2222
<groupId>io.appium</groupId>
2323
<artifactId>java-client</artifactId>
@@ -65,130 +65,135 @@
6565
<description>Java client for Appium Mobile Webdriver</description>
6666
<url>http://appium.io</url>
6767

68-
<licenses>
69-
<license>
70-
<name>Apache License, Version 2.0</name>
71-
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
72-
<distribution>repo</distribution>
73-
</license>
74-
</licenses>
68+
<licenses>
69+
<license>
70+
<name>Apache License, Version 2.0</name>
71+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
72+
<distribution>repo</distribution>
73+
</license>
74+
</licenses>
7575

76-
<scm>
77-
<url>https://github.com/appium/java-client</url>
78-
<connection>scm:git:git://github.com/appium/java-client.git</connection>
79-
<developerConnection>scm:git:git@github.com:appium/java-client.git</developerConnection>
80-
</scm>
76+
<scm>
77+
<url>https://github.com/appium/java-client</url>
78+
<connection>scm:git:git://github.com/appium/java-client.git</connection>
79+
<developerConnection>scm:git:git@github.com:appium/java-client.git</developerConnection>
80+
</scm>
8181

82-
<developers>
83-
<developer>
84-
<email>jonahss@gmail.com</email>
85-
<name>Jonah Stiennon</name>
86-
<url>https://github.com/jonahss</url>
87-
<id>jonahss</id>
88-
</developer>
82+
<developers>
83+
<developer>
84+
<email>jonahss@gmail.com</email>
85+
<name>Jonah Stiennon</name>
86+
<url>https://github.com/jonahss</url>
87+
<id>jonahss</id>
88+
</developer>
8989
<developer>
9090
<email>tichomirovsergey@gmail.com</email>
9191
<name>Sergey Tikhomirov</name>
9292
<url>https://github.com/TikhomirovSergey</url>
9393
<id>TikhomirovSergey</id>
9494
</developer>
95-
</developers>
95+
</developers>
9696

97-
<distributionManagement>
98-
<snapshotRepository>
99-
<id>ossrh</id>
100-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
101-
</snapshotRepository>
102-
<repository>
103-
<id>ossrh</id>
104-
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
105-
</repository>
106-
</distributionManagement>
97+
<distributionManagement>
98+
<snapshotRepository>
99+
<id>ossrh</id>
100+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
101+
</snapshotRepository>
102+
<repository>
103+
<id>ossrh</id>
104+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
105+
</repository>
106+
</distributionManagement>
107107

108-
<build>
109-
<plugins>
110-
<plugin>
111-
<groupId>org.apache.maven.plugins</groupId>
112-
<artifactId>maven-source-plugin</artifactId>
113-
<version>2.2.1</version>
114-
<executions>
115-
<execution>
116-
<id>attach-sources</id>
117-
<goals>
118-
<goal>jar-no-fork</goal>
119-
</goals>
120-
</execution>
121-
</executions>
122-
</plugin>
123-
<plugin>
124-
<groupId>org.apache.maven.plugins</groupId>
125-
<artifactId>maven-javadoc-plugin</artifactId>
126-
<version>2.9.1</version>
127-
<executions>
128-
<execution>
129-
<id>attach-javadocs</id>
130-
<goals>
131-
<goal>jar</goal>
132-
</goals>
133-
</execution>
134-
</executions>
135-
</plugin>
136-
<plugin>
137-
<groupId>org.apache.maven.plugins</groupId>
138-
<artifactId>maven-gpg-plugin</artifactId>
139-
<version>1.5</version>
140-
<executions>
141-
<execution>
142-
<id>sign-artifacts</id>
143-
<phase>verify</phase>
144-
<goals>
145-
<goal>sign</goal>
146-
</goals>
147-
</execution>
148-
</executions>
149-
</plugin>
150-
<plugin>
151-
<groupId>org.apache.maven.plugins</groupId>
152-
<artifactId>maven-compiler-plugin</artifactId>
153-
<version>3.3</version>
154-
<configuration>
155-
<source>1.7</source>
156-
<target>1.7</target>
108+
<build>
109+
<resources>
110+
<resource>
111+
<directory>src/main/resources</directory>
112+
</resource>
113+
</resources>
114+
<plugins>
115+
<plugin>
116+
<groupId>org.apache.maven.plugins</groupId>
117+
<artifactId>maven-source-plugin</artifactId>
118+
<version>2.2.1</version>
119+
<executions>
120+
<execution>
121+
<id>attach-sources</id>
122+
<goals>
123+
<goal>jar-no-fork</goal>
124+
</goals>
125+
</execution>
126+
</executions>
127+
</plugin>
128+
<plugin>
129+
<groupId>org.apache.maven.plugins</groupId>
130+
<artifactId>maven-javadoc-plugin</artifactId>
131+
<version>2.9.1</version>
132+
<executions>
133+
<execution>
134+
<id>attach-javadocs</id>
135+
<goals>
136+
<goal>jar</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
</plugin>
141+
<plugin>
142+
<groupId>org.apache.maven.plugins</groupId>
143+
<artifactId>maven-gpg-plugin</artifactId>
144+
<version>1.5</version>
145+
<executions>
146+
<execution>
147+
<id>sign-artifacts</id>
148+
<phase>verify</phase>
149+
<goals>
150+
<goal>sign</goal>
151+
</goals>
152+
</execution>
153+
</executions>
154+
</plugin>
155+
<plugin>
156+
<groupId>org.apache.maven.plugins</groupId>
157+
<artifactId>maven-compiler-plugin</artifactId>
158+
<version>3.3</version>
159+
<configuration>
160+
<source>1.7</source>
161+
<target>1.7</target>
157162
<compilerId>eclipse</compilerId>
158-
</configuration>
163+
</configuration>
159164
<dependencies>
160165
<dependency>
161166
<groupId>org.codehaus.plexus</groupId>
162167
<artifactId>plexus-compiler-eclipse</artifactId>
163168
<version>2.5</version>
164169
</dependency>
165170
</dependencies>
166-
</plugin>
167-
<plugin>
168-
<groupId>org.apache.maven.plugins</groupId>
169-
<artifactId>maven-surefire-plugin</artifactId>
170-
<version>2.18.1</version>
171-
<dependencies>
172-
<dependency>
173-
<groupId>org.apache.maven.surefire</groupId>
174-
<artifactId>surefire-junit47</artifactId>
175-
<version>2.18.1</version>
176-
</dependency>
177-
</dependencies>
178-
<executions>
179-
<execution>
180-
<goals>
181-
<goal>test</goal>
182-
</goals>
183-
<phase>integration-test</phase>
184-
<configuration>
185-
<includes>
186-
<include>**/*Test.java</include>
187-
</includes>
188-
</configuration>
189-
</execution>
190-
</executions>
191-
</plugin>
192-
</plugins>
193-
</build>
171+
</plugin>
172+
<plugin>
173+
<groupId>org.apache.maven.plugins</groupId>
174+
<artifactId>maven-surefire-plugin</artifactId>
175+
<version>2.18.1</version>
176+
<dependencies>
177+
<dependency>
178+
<groupId>org.apache.maven.surefire</groupId>
179+
<artifactId>surefire-junit47</artifactId>
180+
<version>2.18.1</version>
181+
</dependency>
182+
</dependencies>
183+
<executions>
184+
<execution>
185+
<goals>
186+
<goal>test</goal>
187+
</goals>
188+
<phase>integration-test</phase>
189+
<configuration>
190+
<includes>
191+
<include>**/*Test.java</include>
192+
</includes>
193+
</configuration>
194+
</execution>
195+
</executions>
196+
</plugin>
197+
</plugins>
198+
</build>
194199
</project>

‎src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java‎

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.ImmutableMap;
2121
import io.appium.java_client.service.local.flags.ServerArgument;
22+
import org.apache.commons.io.FileUtils;
2223
import org.apache.commons.lang3.ArrayUtils;
2324
import org.apache.commons.lang3.StringUtils;
2425
import org.apache.commons.validator.routines.InetAddressValidator;
@@ -45,42 +46,22 @@ public final class AppiumServiceBuilder extends DriverService.Builder<AppiumDriv
4546
public static final String DEFAULT_LOCAL_IP_ADDRESS = "0.0.0.0";
4647

4748
private static final int DEFAULT_APPIUM_PORT = 4723;
48-
49-
private static final String NODE_COMMAND_PREFIX[] = defineNodeCommandPrefix();
50-
51-
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM[] = {"npm", "root", "-g"};
52-
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN[] = {"npm.cmd", "root", "-g"};
49+
private final static String BIN_BASH[] = {"/bin/bash", "-l", "-c"};
50+
private final static String CMD_EXE[] = {"cmd.exe", "/C"};
5351
private final static String NODE = "node";
54-
5552
private final static String PATH_NAME = getPathVarName();
5653

5754

5855
final Map<String, String> serverArguments = new HashMap<>();
5956
private File appiumJS;
6057
private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS;
58+
private File npmScript;
6159

6260
//The first starting is slow sometimes on some
6361
//environment
6462
private long startupTimeout = 120;
6563
private TimeUnit timeUnit = TimeUnit.SECONDS;
6664

67-
68-
private static String[] returnCommandThatSearchesForDefaultNode(){
69-
if (Platform.getCurrent().is(Platform.WINDOWS)) {
70-
return COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN;
71-
}
72-
return COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM;
73-
}
74-
75-
private static String[] defineNodeCommandPrefix() {
76-
if (Platform.getCurrent().is(Platform.WINDOWS)) {
77-
return new String[]{"cmd.exe", "/C"};
78-
}
79-
else {
80-
return new String[]{"/bin/bash", "-l", "-c"};
81-
}
82-
}
83-
8465
private static String getPathVarName(){
8566
Map<String, String> envVariables = System.getenv();
8667
Set<String> keys = envVariables.keySet();
@@ -114,11 +95,31 @@ private static Process getSearchingProcess(String... command) throws Throwable {
11495
return processBuilder.start();
11596
}
11697

117-
private static File findNodeInCurrentFileSystem(){
98+
private void setUpNPMScript(){
99+
if (npmScript != null) {
100+
return;
101+
}
102+
103+
if (Platform.getCurrent().is(Platform.WINDOWS)) {
104+
npmScript = NPMScript.GET_PATH_TO_DEFAULT_NODE_WIN.getScriptFile();
105+
}
106+
else {
107+
npmScript = NPMScript.GET_PATH_TO_DEFAULT_NODE_UNIX.getScriptFile();
108+
}
109+
}
110+
111+
private File findNodeInCurrentFileSystem(){
112+
setUpNPMScript();
113+
118114
String instancePath;
119115
Process p = null;
120116
try {
121-
p = getSearchingProcess(returnCommandThatSearchesForDefaultNode());
117+
if (Platform.getCurrent().is(Platform.WINDOWS)) {
118+
p = getSearchingProcess(ArrayUtils.add(CMD_EXE, npmScript.getAbsolutePath()));
119+
}
120+
else {
121+
p = getSearchingProcess(ArrayUtils.add(BIN_BASH, npmScript.getAbsolutePath()));
122+
}
122123
p.waitFor();
123124
instancePath = getTheLastStringFromsOutput(p.getInputStream());
124125
} catch (Throwable e) {
@@ -162,7 +163,12 @@ public AppiumServiceBuilder() {
162163
protected File findDefaultExecutable() {
163164
Process p;
164165
try {
165-
p = getSearchingProcess(ArrayUtils.add(NODE_COMMAND_PREFIX, NODE));
166+
if (Platform.getCurrent().is(Platform.WINDOWS)) {
167+
p = getSearchingProcess(ArrayUtils.add(CMD_EXE, NODE));
168+
}
169+
else {
170+
p = getSearchingProcess(ArrayUtils.add(BIN_BASH, NODE));
171+
}
166172
} catch (Throwable t) {
167173
throw new RuntimeException(t);
168174
}
@@ -351,4 +357,15 @@ protected AppiumDriverLocalService createDriverService(File nodeJSExecutable, in
351357
throw new RuntimeException(e);
352358
}
353359
}
360+
361+
@Override
362+
protected void finalize() throws Throwable {
363+
if (npmScript != null) {
364+
try {
365+
FileUtils.forceDelete(npmScript);
366+
} catch (IOException ignored) {
367+
}
368+
}
369+
super.finalize();
370+
}
354371
}

0 commit comments

Comments
 (0)