Skip to content

Commit

Permalink
#5: aligned packages, fixed env cmd, solved duplicate context issue, …
Browse files Browse the repository at this point in the history
…fixed bugs
  • Loading branch information
hohwille committed Sep 22, 2023
1 parent 25a97b1 commit cd33374
Show file tree
Hide file tree
Showing 94 changed files with 475 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;

import com.devonfw.tools.ide.context.AbstractIdeContext;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.KeywordProperty;
import com.devonfw.tools.ide.property.Property;
Expand Down Expand Up @@ -119,6 +120,11 @@ public static CommandletManager of(IdeContext context) {
throw new IllegalStateException("Not initialized!");
}
} else {
if (context instanceof AbstractIdeContext c) {
if (c.isMock()) {
return new CommandletManagerImpl(context);
}
}
if (INSTANCE != null) {
System.out.println("Multiple initializations!");
}
Expand All @@ -127,4 +133,12 @@ public static CommandletManager of(IdeContext context) {
return INSTANCE;
}

/**
* Internal method to reset internal instance. May only be used for testing!
*/
static void reset() {

INSTANCE = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.VariableLine;
import com.devonfw.tools.ide.os.WindowsPathSyntax;
import com.devonfw.tools.ide.property.FlagProperty;

/**
* {@link Commandlet} to print the environment variables.
*/
public final class EnvironmentCommandlet extends Commandlet {

/** {@link FlagProperty} to enable Bash (MSys) path conversion on Windows. */
public final FlagProperty bash;

/**
* The constructor.
*
Expand All @@ -19,6 +24,7 @@ public EnvironmentCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
this.bash = add(new FlagProperty("--bash", false, null));
}

@Override
Expand All @@ -38,7 +44,38 @@ public void run() {

Collection<VariableLine> variables = this.context.getVariables().collectVariables();
for (VariableLine line : variables) {
if (this.context.getSystemInfo().isWindows()) {
line = normalizeWindowsValue(line);
}
this.context.info(line.toString());
}
}

VariableLine normalizeWindowsValue(VariableLine line) {

String value = line.getValue();
String normalized = normalizeWindowsValue(value);
if (normalized != value) {
line = line.withValue(value);
}
return line;
}

String normalizeWindowsValue(String value) {

WindowsPathSyntax pathSyntax;
WindowsPathSyntax driveSyntax;
if (this.bash.isTrue()) {
pathSyntax = WindowsPathSyntax.MSYS;
driveSyntax = WindowsPathSyntax.WINDOWS;
} else {
pathSyntax = WindowsPathSyntax.WINDOWS;
driveSyntax = WindowsPathSyntax.MSYS;
}
String drive = driveSyntax.getDrive(value);
if (drive != null) {
value = pathSyntax.replaceDrive(value, drive);
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import com.devonfw.tools.ide.commandlet.CommandletManager;
import com.devonfw.tools.ide.commandlet.CommandletManagerImpl;
import com.devonfw.tools.ide.common.SystemInfo;
import com.devonfw.tools.ide.common.SystemInfoImpl;
import com.devonfw.tools.ide.common.SystemPath;
import com.devonfw.tools.ide.environment.AbstractEnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
Expand All @@ -25,6 +23,8 @@
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.log.IdeSubLoggerNone;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.os.SystemInfoImpl;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.process.ProcessContextImpl;
import com.devonfw.tools.ide.process.ProcessErrorHandling;
Expand Down Expand Up @@ -146,7 +146,9 @@ public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubL
}
name2 = name1;
int nameCount = currentDir.getNameCount();
name1 = currentDir.getName(nameCount - 1).toString();
if (nameCount >= 1) {
name1 = currentDir.getName(nameCount - 1).toString();
}
currentDir = getParentPath(currentDir);
}
// detection completed, initializing variables
Expand Down Expand Up @@ -208,7 +210,11 @@ public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubL
}
if (isTest()) {
// only for testing...
this.userHome = this.ideHome.resolve("home");
if (this.ideHome == null) {
this.userHome = Paths.get("/non-existing-user-home-for-testing");
} else {
this.userHome = this.ideHome.resolve("home");
}
} else {
this.userHome = Paths.get(System.getProperty("user.home"));
}
Expand Down Expand Up @@ -245,7 +251,18 @@ public String getMessageIdeHome() {
/**
* @return {@code true} if this is a test context for JUnits, {@code false} otherwise.
*/
protected boolean isTest() {
public boolean isTest() {

if (isMock()) {
return true;
}
return false;
}

/**
* @return {@code true} if this is a mock context for JUnits, {@code false} otherwise.
*/
public boolean isMock() {

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import com.devonfw.tools.ide.cli.CliAbortException;
import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.commandlet.CommandletManager;
import com.devonfw.tools.ide.common.SystemInfo;
import com.devonfw.tools.ide.common.SystemPath;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.log.IdeLogger;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.repo.CustomToolRepository;
import com.devonfw.tools.ide.repo.ToolRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.devonfw.tools.ide.tool;
package com.devonfw.tools.ide.os;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

import com.devonfw.tools.ide.common.SystemInfo;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.log.IdeLogger;
Expand Down Expand Up @@ -50,9 +49,9 @@ public MacOsHelper(FileAccess fileAccess, SystemInfo systemInfo, IdeLogger logge

/**
* @param rootDir the {@link Path} to the root directory.
* @return the {@link ToolInstallation#linkDir() link directory}.
* @return the {@link com.devonfw.tools.ide.tool.ToolInstallation#linkDir() link directory}.
*/
Path findLinkDir(Path rootDir) {
public Path findLinkDir(Path rootDir) {

if (!this.systemInfo.isMac() || Files.isDirectory(rootDir.resolve(IdeContext.FOLDER_BIN))) {
return rootDir;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.devonfw.tools.ide.common;
package com.devonfw.tools.ide.os;

/**
* Enum with the supported operating systems.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.devonfw.tools.ide.common;
package com.devonfw.tools.ide.os;

/**
* Enum with the supported architectures.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.devonfw.tools.ide.common;
package com.devonfw.tools.ide.os;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.devonfw.tools.ide.common;
package com.devonfw.tools.ide.os;

import java.util.Locale;

Expand Down
98 changes: 98 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/os/WindowsPathSyntax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.devonfw.tools.ide.os;

import java.util.Locale;
import java.util.Objects;

/**
* TODO hohwille This type ...
*
*/
public enum WindowsPathSyntax {

/** Windows path like "C:\..." or "D:\...". */
WINDOWS,

/** MSys (git-bash) path like "/c/..." or "/d/...". */
MSYS;

/**
* @param path the potential path. May be any {@link String}.
* @return the the drive letter (e.g. "C" or "D") or {@code null} if the given {@link String} is not a path in this
* {@link WindowsPathSyntax}.
*/
public String getDrive(String path) {

if ((path != null) && (path.length() >= 3)) {
char c0 = path.charAt(0);
char c1 = path.charAt(1);
char c2 = path.charAt(2);
switch (this) {
case WINDOWS: // 'C:\'
if ((c1 == ':') && (c2 == '\\') && (c0 >= 'A') && (c0 <= 'Z')) {
return Character.toString(c0);
}
break;
case MSYS: // '/c/'
if ((c0 == '/') && (c2 == '/') && isLowerLatinLetter(c1)) {
return Character.toString(c1);
}
break;
default:
throw new IllegalArgumentException(toString());
}
}
return null;
}

private static boolean isLowerLatinLetter(char c) {

return (c >= 'a') && (c <= 'z');
}

/**
* @param path the path where to replace the drive letter.
* @param drive the new {@link #getDrive(String) drive letter}.
* @return the new path pointing to the given {@code drive} in this {@link WindowsPathSyntax}.
*/
public String replaceDrive(String path, String drive) {

Objects.requireNonNull(path);
Objects.requireNonNull(drive);
if (path.length() < 3) {
throw new IllegalArgumentException(path);
}
String restPath = path.substring(3);
switch (this) {
case WINDOWS:
restPath = restPath.replace('/', '\\');
break;
case MSYS:
restPath = restPath.replace('\\', '/');
break;
default:
throw new IllegalStateException(toString());
}
return getRootPath(drive) + restPath;
}

/**
* @param drive the drive letter (e.g. "C" or "D").
* @return the root path for the given {@code drive} (e.g. "C:\\" or "/c/").
*/
public String getRootPath(String drive) {

Objects.requireNonNull(drive);
if ((drive.length() != 1) || !isLowerLatinLetter(Character.toLowerCase(drive.charAt(0)))) {
throw new IllegalArgumentException(drive);
}
switch (this) {
case WINDOWS:
return drive.toUpperCase(Locale.ROOT) + ":\\";
case MSYS:
return "/" + drive.toLowerCase(Locale.ROOT) + "/";
default:
throw new IllegalStateException(toString());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public boolean isTrue() {
return Boolean.TRUE.equals(getValue());
}

/**
* @param value the {@link #getValue() value} as primitive boolean.
*/
public void setValue(boolean value) {

setValue(Boolean.valueOf(value));
}

@Override
public Boolean parse(String valueAsString) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.util.Set;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.common.OperatingSystem;
import com.devonfw.tools.ide.common.SystemArchitecture;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.os.OperatingSystem;
import com.devonfw.tools.ide.os.SystemArchitecture;
import com.devonfw.tools.ide.url.model.file.UrlDownloadFileMetadata;
import com.devonfw.tools.ide.util.FilenameUtil;
import com.devonfw.tools.ide.version.VersionIdentifier;
Expand Down
6 changes: 3 additions & 3 deletions cli/src/main/java/com/devonfw/tools/ide/repo/CustomTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.Set;

import com.devonfw.tools.ide.common.OperatingSystem;
import com.devonfw.tools.ide.common.SystemArchitecture;
import com.devonfw.tools.ide.common.SystemInfo;
import com.devonfw.tools.ide.os.OperatingSystem;
import com.devonfw.tools.ide.os.SystemArchitecture;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.url.model.file.UrlDownloadFileMetadata;
import com.devonfw.tools.ide.version.VersionIdentifier;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.devonfw.tools.ide.repo;

import com.devonfw.tools.ide.common.SystemInfo;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.url.model.UrlMetadata;
import com.devonfw.tools.ide.url.model.file.UrlDownloadFile;
import com.devonfw.tools.ide.url.model.file.UrlDownloadFileMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.devonfw.tools.ide.io.FileCopyMode;
import com.devonfw.tools.ide.io.TarCompression;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.os.MacOsHelper;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.process.ProcessErrorHandling;
import com.devonfw.tools.ide.property.StringListProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.devonfw.tools.ide.url.updater.androidstudio;
package com.devonfw.tools.ide.tool.androidstudio;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.devonfw.tools.ide.url.updater.androidstudio;
package com.devonfw.tools.ide.tool.androidstudio;

import com.devonfw.tools.ide.common.JsonObject;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.devonfw.tools.ide.url.updater.androidstudio;
package com.devonfw.tools.ide.tool.androidstudio;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Loading

0 comments on commit cd33374

Please sign in to comment.