Skip to content

Commit

Permalink
Merge branch 'main' into fix/929-optimize-upgrade-settings-using-file…
Browse files Browse the repository at this point in the history
…-access
  • Loading branch information
hohwille authored Feb 27, 2025
2 parents ab18624 + 20f52ca commit 5401118
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private void createStartScript(String ide, String workspace) {
scriptContent = "#!/usr/bin/env bash\n"
+ "cd \"$(dirname \"$0\")\"\n"
+ "cd workspaces/" + workspace + "\n"
+ "ide " + ide + "\n";
+ "ideasy " + ide + "\n";
}
FileAccess fileAccess = this.context.getFileAccess();
fileAccess.writeFileContent(scriptContent, scriptPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.IdeasyCommandlet;
import com.devonfw.tools.ide.tool.ToolCommandlet;

/**
Expand All @@ -21,7 +22,7 @@ public UninstallCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
this.tools = add(new ToolProperty("", true, true, "tool"));
this.tools = add(new ToolProperty("", false, true, "tool"));
}

@Override
Expand All @@ -30,10 +31,27 @@ public String getName() {
return "uninstall";
}

@Override
public boolean isIdeRootRequired() {

return this.tools.getValueCount() > 0;
}

@Override
public void run() {

for (int i = 0; i < this.tools.getValueCount(); i++) {
int valueCount = this.tools.getValueCount();
if (valueCount == 0) {
if (!this.context.isForceMode()) {
this.context.askToContinue("Sub-command uninstall without any further arguments will perform the entire uninstallation of IDEasy.\n"
+ "Since this is typically not to be called manually, you may have forgotten to specify the tool to install as extra argument.\n"
+ "The current command will uninstall IDEasy from your computer. Are you sure?");
}
IdeasyCommandlet ideasy = new IdeasyCommandlet(this.context);
ideasy.uninstallIdeasy();
return;
}
for (int i = 0; i < valueCount; i++) {
ToolCommandlet toolCommandlet = this.tools.getValue(i);
if (toolCommandlet.getInstalledVersion() != null) {
toolCommandlet.uninstall();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.devonfw.tools.ide.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

/**
* Represents the PATH variable in a structured way. Similar to {@link SystemPath} but much simper: It just tokenizes the PATH into a {@link java.util.List} of
* {@link String}s.
*/
public class SimpleSystemPath {

private final char separator;

private final List<String> entries;

private SimpleSystemPath(char separator, List<String> entries) {

super();
this.separator = separator;
this.entries = entries;
}

/**
* @return the entries of this PATH as a mutable {@link List}.
*/
public List<String> getEntries() {

return this.entries;
}

/**
* Remove all entries from this PATH that match the given {@link Predicate}.
*
* @param filter the {@link Predicate} {@link Predicate#test(Object) deciding} what to filter and remove.
*/
public void removeEntries(Predicate<String> filter) {

Iterator<String> iterator = this.entries.iterator();
while (iterator.hasNext()) {
String entry = iterator.next();
if (filter.test(entry)) {
iterator.remove();
}
}
}

@Override
public String toString() {

StringBuilder sb = new StringBuilder();
boolean first = true;
for (String entry : this.entries) {
if (first) {
first = false;
} else {
sb.append(this.separator);
}
sb.append(entry);
}
return sb.toString();
}

/**
* @param path the entire PATH as {@link String},
* @param separator the separator character.
* @return the {@link SimpleSystemPath}.
*/
public static SimpleSystemPath of(String path, char separator) {

List<String> entries = new ArrayList<>();
int start = 0;
int len = path.length();
while (start < len) {
int end = path.indexOf(separator, start);
if (end < 0) {
end = len;
}
entries.add(path.substring(start, end));
start = end + 1;
}
return new SimpleSystemPath(separator, entries);
}
}
21 changes: 12 additions & 9 deletions cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -609,21 +609,24 @@ default String getMavenArgs() {
*/
default Path getMavenConfigurationFolder() {

if (getIdeHome() != null) {
Path confPath = getConfPath();
Path m2Folder = confPath.resolve(Mvn.MVN_CONFIG_FOLDER);
if (!Files.isDirectory(m2Folder)) {
Path confPath = getConfPath();
Path mvnConfFolder = null;
if (confPath != null) {
mvnConfFolder = confPath.resolve(Mvn.MVN_CONFIG_FOLDER);
if (!Files.isDirectory(mvnConfFolder)) {
Path m2LegacyFolder = confPath.resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER);
if (Files.isDirectory(m2LegacyFolder)) {
m2Folder = m2LegacyFolder;
mvnConfFolder = m2LegacyFolder;
} else {
// fallback to USER_HOME/.m2 folder
m2Folder = getUserHome().resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER);
mvnConfFolder = null; // see fallback below
}
}
return m2Folder;
}
return null;
if (mvnConfFolder == null) {
// fallback to USER_HOME/.m2 folder
mvnConfFolder = getUserHome().resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER);
}
return mvnConfFolder;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public interface WindowsHelper {
*/
void setUserEnvironmentValue(String key, String value);

/**
* @param key the name of the environment variable to remove.
*/
void removeUserEnvironmentValue(String key);

/**
* @param key the name of the environment variable.
* @return the value of the environment variable in the users context.
Expand Down
11 changes: 11 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.process.ProcessMode;
import com.devonfw.tools.ide.process.ProcessResult;

Expand Down Expand Up @@ -33,6 +34,16 @@ public void setUserEnvironmentValue(String key, String value) {
assert (result.isSuccessful());
}

@Override
public void removeUserEnvironmentValue(String key) {
ProcessResult result = this.context.newProcess().executable("reg").addArgs("delete", HKCU_ENVIRONMENT, "/v", key, "/f").run(ProcessMode.DEFAULT_CAPTURE);
if (result.isSuccessful()) {
this.context.debug("Removed environment variable {}", key);
} else {
result.log(IdeLogLevel.WARNING, this.context);
}
}

@Override
public String getUserEnvironmentValue(String key) {

Expand Down
Loading

0 comments on commit 5401118

Please sign in to comment.