Skip to content

Commit

Permalink
devonfw#1090: support to detect OS and arch
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Sep 1, 2023
1 parent 8213605 commit e22ba6e
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
59 changes: 59 additions & 0 deletions ide/src/main/java/com/devonfw/tools/ide/common/SystemInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.devonfw.tools.ide.common;

import com.devonfw.tools.ide.version.VersionIdentifier;

/**
* Interface to get information about the current machine running the IDE.
*/
public interface SystemInfo {

/**
* @return the current {@link OperatingSystem}.
*/
OperatingSystem getOs();

/**
* @return the raw name of the current {@link OperatingSystem}.
*/
String getOsName();

/**
* @return the version of the current {@link OperatingSystem}.
*/
VersionIdentifier getOsVersion();

/**
* @return the raw name of the current {@link SystemArchitecture}.
*/
String getArchitectureName();

/**
* @return the current {@link SystemArchitecture}.
*/
SystemArchitecture getArchitecture();

/**
* @return {@code true} if we are on {@link OperatingSystem#WINDOWS windows}.
*/
default boolean isWindows() {

return getOs() == OperatingSystem.WINDOWS;
}

/**
* @return {@code true} if we are on {@link OperatingSystem#MAC mac OS}.
*/
default boolean isMac() {

return getOs() == OperatingSystem.MAC;
}

/**
* @return {@code true} if we are on {@link OperatingSystem#LINUX linux}.
*/
default boolean isLinux() {

return getOs() == OperatingSystem.LINUX;
}

}
112 changes: 112 additions & 0 deletions ide/src/main/java/com/devonfw/tools/ide/common/SystemInfoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.devonfw.tools.ide.common;

import java.util.Locale;

import com.devonfw.tools.ide.version.VersionIdentifier;

/**
* Implementation of {@link SystemInfo}.
*/
public class SystemInfoImpl implements SystemInfo {

/** Name of {@link System#getProperty(String) system-property} for {@link #getOsName()}: {@value}. */
private static final String PROPERTY_OS_NAME = "os.name";

/** Name of {@link System#getProperty(String) system-property} for #getArchitectureName()}: {@value}. */
private static final String PROPERTY_OS_ARCHITECTURE = "os.arch";

/** Name of {@link System#getProperty(String) system-property} for {@link #getOsVersion()}: {@value}. */
private static final String PROPERTY_OS_VERSION = "os.version";

private final String osName;

private final VersionIdentifier osVersion;

private final OperatingSystem os;

private final String architectureName;

private final SystemArchitecture architecture;

/**
* The constructor.
*/
public SystemInfoImpl() {

super();
this.osName = System.getProperty(PROPERTY_OS_NAME).trim();
this.osVersion = VersionIdentifier.of(System.getProperty(PROPERTY_OS_VERSION).trim());
this.architectureName = System.getProperty(PROPERTY_OS_ARCHITECTURE).trim();
this.os = detectOs(this.osName);
this.architecture = detectArchitecture(this.architectureName);
}

private static OperatingSystem detectOs(String osName) {

String os = osName.toLowerCase(Locale.US);
if (os.startsWith("windows")) {
return OperatingSystem.WINDOWS;
} else if (os.startsWith("mac")) {
return OperatingSystem.MAC;
} else if (os.contains("linux")) {
return OperatingSystem.LINUX;
} else if (os.contains("bsd")) {
return OperatingSystem.LINUX;
} else if (os.contains("solaris") || os.contains("hp-ux") || os.contains("nix") || os.contains("aix")
|| os.contains("nextstep") || os.contains("sorix") || os.contains("irix")) {
return OperatingSystem.LINUX;
} else {
System.err.println("ERROR: Unknown operating system '" + osName + "'");
// be tolerant: most of our users are working on windows
// in case of an odd JVM or virtualization issue let us better continue than failing
return OperatingSystem.WINDOWS;
}
}

private static SystemArchitecture detectArchitecture(String architectureName) {

if (architectureName.contains("arm") || architectureName.contains("aarch")) {
return SystemArchitecture.ARM64;
} else {
return SystemArchitecture.X64;
}
}

@Override
public OperatingSystem getOs() {

return this.os;
}

@Override
public String getOsName() {

return this.osName;
}

@Override
public VersionIdentifier getOsVersion() {

return this.osVersion;
}

@Override
public String getArchitectureName() {

return this.architectureName;
}

@Override
public SystemArchitecture getArchitecture() {

return this.architecture;
}

@Override
public String toString() {

return this.os + "@" + this.architecture + "(" + this.osName + "[" + this.osVersion + "]@" + this.architectureName
+ ")";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.function.Function;
import java.util.stream.Stream;

import com.devonfw.tools.ide.common.SystemInfo;
import com.devonfw.tools.ide.common.SystemInfoImpl;
import com.devonfw.tools.ide.environment.AbstractEnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
Expand Down Expand Up @@ -61,6 +63,8 @@ public abstract class AbstractIdeContext implements IdeContext {

private final String path;

private final SystemInfo systemInfo;

private final EnvironmentVariables variables;

private boolean offlineMode;
Expand Down Expand Up @@ -93,6 +97,7 @@ public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubL
}
this.loggers.put(level, logger);
}
this.systemInfo = new SystemInfoImpl();
String workspace = WORKSPACE_MAIN;
if (userDir == null) {
userDir = Paths.get(System.getProperty("user.dir"));
Expand Down Expand Up @@ -263,7 +268,12 @@ private AbstractEnvironmentVariables extendVariables(AbstractEnvironmentVariable
debug("Configuration directory {} does not exist.", propertiesPath);
}
return envVariables.extend(propertiesFile, type);
}

@Override
public SystemInfo getSystemInfo() {

return this.systemInfo;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.devonfw.tools.ide.cli.CliAbortException;
import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.common.SystemInfo;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.log.IdeLogger;
Expand Down Expand Up @@ -122,6 +123,11 @@ default void requireOnline(String purpose) {
}
}

/**
* @return the {@link SystemInfo}.
*/
SystemInfo getSystemInfo();

/**
* @return the {@link EnvironmentVariables} with full inheritance.
*/
Expand Down

0 comments on commit e22ba6e

Please sign in to comment.