Skip to content

Commit

Permalink
Clean logic for operating system handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Jan 17, 2021
1 parent 2385afc commit 92dfffe
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ protected Optional<String> detectBrowserVersion() {
}

String driverManagerTypeLowerCase = getDriverManagerType().name()
.toLowerCase();
.toLowerCase(ROOT);
Optional<String> optionalBrowserVersion;

if (useResolutionCacheWithKey(driverManagerTypeLowerCase)) {
Expand Down Expand Up @@ -873,9 +873,9 @@ protected List<URL> getDriversFromMirror(URL driverUrl) throws IOException {
urlList.addAll(getDriversFromMirror(new URL(link)));
} else if (link.startsWith(driverOrigin)
&& !link.contains("icons")
&& (link.toLowerCase().endsWith(".bz2")
|| link.toLowerCase().endsWith(".zip")
|| link.toLowerCase().endsWith(".gz"))) {
&& (link.toLowerCase(ROOT).endsWith(".bz2")
|| link.toLowerCase(ROOT).endsWith(".zip")
|| link.toLowerCase(ROOT).endsWith(".gz"))) {
urlList.add(new URL(link));
}
}
Expand Down Expand Up @@ -1012,7 +1012,7 @@ protected HttpClient getHttpClient() {

protected FilenameFilter getFolderFilter() {
return (dir, name) -> dir.isDirectory()
&& name.toLowerCase().contains(getDriverName());
&& name.toLowerCase(ROOT).contains(getDriverName());
}

protected Charset getVersionCharset() {
Expand Down Expand Up @@ -1119,7 +1119,7 @@ protected String getShortDriverName() {
}

protected String getKeyForResolutionCache() {
return getDriverManagerType().name().toLowerCase();
return getDriverManagerType().name().toLowerCase(ROOT);
}

protected String getDriverVersionLabel(String driverVersion) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/io/github/bonigarcia/wdm/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ public void reset() {
}

private String defaultOsName() {
String osName = System.getProperty("os.name").toLowerCase(ROOT);
String osName = System.getProperty("os.name");
if (IS_OS_WINDOWS) {
osName = WIN.name();
} else if (IS_OS_LINUX) {
osName = LINUX.name();
} else if (IS_OS_MAC) {
osName = MAC.name();
}
return osName;
return osName.toLowerCase(ROOT);
}

private String defaultArchitecture() {
Expand Down Expand Up @@ -503,6 +503,10 @@ public String getOs() {
return resolve(os);
}

public OperatingSystem getOperatingSystem() {
return OperatingSystem.valueOf(getOs());
}

public Config setOs(String value) {
this.os.setValue(value);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ public Stream<String> osLabelsStream() {

public boolean matchOs(String os) {
return osLabelsStream().anyMatch(os::contains)
|| os.contains(this.name().toLowerCase(ROOT));
|| os.contains(getName());
}

public String getName() {
return this.name().toLowerCase(ROOT);
}

public boolean isWin() {
return this == WIN;
}

public boolean isMac() {
return this == MAC;
}

public boolean isLinux() {
return this == LINUX;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package io.github.bonigarcia.wdm.managers;

import static io.github.bonigarcia.wdm.config.DriverManagerType.CHROME;
import static java.util.Locale.ROOT;
import static java.util.Optional.empty;
import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;

import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -33,6 +31,7 @@

import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.config.OperatingSystem;

/**
* Manager for Chrome.
Expand Down Expand Up @@ -126,7 +125,8 @@ protected Optional<String> getBrowserVersionFromTheShell() {
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"--version");

if (IS_OS_WINDOWS && !browserVersion.isPresent()) {
if (config().getOperatingSystem().isWin()
&& !browserVersion.isPresent()) {
log.debug(
"Chrome version not discovered using wmic... trying reading the registry");
browserVersion = versionDetector.getBrowserVersionFromWinRegistry(
Expand Down Expand Up @@ -161,10 +161,10 @@ protected Optional<URL> buildUrl(String driverVersion) {
Optional<URL> optionalUrl = empty();
if (!config().isUseMirror()) {
String downloadUrlPattern = config().getChromeDownloadUrlPattern();
String os = config().getOs().toLowerCase(ROOT);
String arch = os.contains("win") ? "32" : "64";
OperatingSystem os = config().getOperatingSystem();
String arch = os.isWin() ? "32" : "64";
String builtUrl = String.format(downloadUrlPattern, driverVersion,
os, arch);
os.getName(), arch);
log.debug("Using URL built from repository pattern: {}", builtUrl);
try {
optionalUrl = Optional.of(new URL(builtUrl));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package io.github.bonigarcia.wdm.managers;

import static io.github.bonigarcia.wdm.config.DriverManagerType.EDGE;
import static java.util.Locale.ROOT;
import static java.util.Optional.empty;
import static org.apache.commons.io.FileUtils.listFiles;
import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC_OSX;

import java.io.File;
import java.io.IOException;
Expand All @@ -35,6 +33,7 @@

import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.config.OperatingSystem;

/**
* Manager for Microsoft Edge.
Expand Down Expand Up @@ -133,7 +132,8 @@ protected Optional<String> getBrowserVersionFromTheShell() {
"\\\\Microsoft\\\\Edge Dev\\\\Application\\\\msedge.exe" };
String macBrowserName = "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge";
String linuxBrowserName = "microsoft-edge";
String versionFlag = IS_OS_MAC_OSX ? "-version" : "--version";
String versionFlag = config().getOperatingSystem().isMac() ? "-version"
: "--version";

return versionDetector.getDefaultBrowserVersion(programFilesEnvs,
winBrowserNames, linuxBrowserName, macBrowserName, versionFlag);
Expand Down Expand Up @@ -162,13 +162,16 @@ protected String getLatestVersionLabel() {
@Override
protected Optional<String> getOsLabel() {
String label = "_";
String os = config().getOs();
if (os.equals("WIN")) {
switch (config().getOperatingSystem()) {
case WIN:
label += "WINDOWS";
} else if (os.equals("MAC")) {
break;
case MAC:
label += "MACOS";
} else { // LINUX
label += os;
break;
default:
label += config().getOs();
break;
}
return Optional.of(label);
}
Expand All @@ -178,12 +181,11 @@ protected Optional<URL> buildUrl(String driverVersion) {
Optional<URL> optionalUrl = empty();
if (!config().isUseMirror()) {
String downloadUrlPattern = config().getEdgeDownloadUrlPattern();
String os = config().getOs().toLowerCase(ROOT);
String arch = os.contains("win")
? config().getArchitecture().toString()
OperatingSystem os = config().getOperatingSystem();
String arch = os.isWin() ? config().getArchitecture().toString()
: "64";
String builtUrl = String.format(downloadUrlPattern, driverVersion,
os, arch);
os.getName(), arch);
log.debug("Using URL built from repository pattern: {}", builtUrl);
try {
optionalUrl = Optional.of(new URL(builtUrl));
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/github/bonigarcia/wdm/online/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import io.github.bonigarcia.wdm.config.Config;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.config.OperatingSystem;
import io.github.bonigarcia.wdm.config.WebDriverManagerException;

/**
Expand Down Expand Up @@ -94,10 +95,10 @@ public File getTarget(String driverVersion, String driverName,
DriverManagerType driverManagerType, URL url) {
String zip = url.getFile().substring(url.getFile().lastIndexOf('/'));
String cachePath = config.getCachePath();
String os = config.getOs().toLowerCase(ROOT);
OperatingSystem os = config.getOperatingSystem();
String architecture = config.getArchitecture().toString();

if (os.equals("win") && (driverManagerType == CHROME
if (os.isWin() && (driverManagerType == CHROME
|| driverManagerType == CHROMIUM)) {
log.trace(
"{} in Windows is only available for 32 bits architecture",
Expand All @@ -106,7 +107,7 @@ public File getTarget(String driverVersion, String driverName,
}

String target = config.isAvoidOutputTree() ? cachePath + zip
: cachePath + separator + driverName + separator + os
: cachePath + separator + driverName + separator + os.getName()
+ architecture + separator + driverVersion + zip;

log.trace("Target file for URL {} driver version {} = {}", url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import static io.github.bonigarcia.wdm.versions.Shell.getVersionFromWmicOutput;
import static io.github.bonigarcia.wdm.versions.Shell.runAndWait;
import static java.lang.invoke.MethodHandles.lookup;
import static java.util.Locale.ROOT;
import static java.util.Optional.empty;
import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX;
import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC;
import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
Expand All @@ -39,6 +37,7 @@
import org.slf4j.Logger;

import io.github.bonigarcia.wdm.config.Config;
import io.github.bonigarcia.wdm.config.OperatingSystem;
import io.github.bonigarcia.wdm.online.HttpClient;

/**
Expand Down Expand Up @@ -172,7 +171,9 @@ public Optional<String> getDefaultBrowserVersion(String[] programFilesEnvs,
String macBrowserName, String versionFlag) {

String browserPath = config.getBrowserPath();
if (IS_OS_WINDOWS) {
OperatingSystem operatingSystem = config.getOperatingSystem();
;
if (operatingSystem.isWin()) {
String winName = "";
for (int j = 0; j < winBrowserNames.length; j++) {
winName = winBrowserNames[j];
Expand All @@ -185,11 +186,11 @@ public Optional<String> getDefaultBrowserVersion(String[] programFilesEnvs,
}
}
}
} else if (IS_OS_LINUX || IS_OS_MAC) {
} else if (operatingSystem.isLinux() || operatingSystem.isMac()) {
browserPath = getPosixBrowserPath(linuxBrowserName, macBrowserName,
browserPath);
String browserVersionOutput = runAndWait(browserPath, versionFlag);
if (browserVersionOutput.toLowerCase().contains("snap")) {
if (browserVersionOutput.toLowerCase(ROOT).contains("snap")) {
isSnap = true;
}
if (!isNullOrEmpty(browserVersionOutput)) {
Expand All @@ -205,7 +206,8 @@ public String getPosixBrowserPath(String linuxBrowserName,
if (!isNullOrEmpty(browserPath)) {
return browserPath;
} else {
return IS_OS_LINUX ? linuxBrowserName : macBrowserName;
return config.getOperatingSystem().isLinux() ? linuxBrowserName
: macBrowserName;
}
}

Expand Down

0 comments on commit 92dfffe

Please sign in to comment.