Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#789: implement uninstall of IDEasy #1071

Merged
merged 11 commits into from
Feb 26, 2025
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 @@ -33,7 +34,18 @@ public String getName() {
@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);
}
}
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
147 changes: 113 additions & 34 deletions cli/src/main/java/com/devonfw/tools/ide/tool/IdeasyCommandlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.commandlet.UpgradeMode;
import com.devonfw.tools.ide.common.SimpleSystemPath;
import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.io.FileAccess;
Expand All @@ -33,6 +34,10 @@ public class IdeasyCommandlet extends MvnBasedLocalToolCommandlet {

/** The {@link #getName() tool name}. */
public static final String TOOL_NAME = "ideasy";
public static final String BASHRC = ".bashrc";
public static final String ZSHRC = ".zshrc";
public static final String IDE_BIN = "\\_ide\\bin";
public static final String IDE_INSTALLATION_BIN = "\\_ide\\installation\\bin";

private final UpgradeMode mode;

Expand Down Expand Up @@ -168,44 +173,49 @@ public void installIdeasy(Path cwd) {
fileAccess.copy(installationArtifact, ideasyVersionPath);
}
fileAccess.symlink(ideasyVersionPath, installationPath);
addToShellRc(".bashrc", ideRoot, null);
addToShellRc(".zshrc", ideRoot, "autoload -U +X bashcompinit && bashcompinit");
addToShellRc(BASHRC, ideRoot, null);
addToShellRc(ZSHRC, ideRoot, "autoload -U +X bashcompinit && bashcompinit");
installIdeasyWindowsEnv(ideRoot, installationPath);
this.context.success("IDEasy has been installed successfully on your system.");
this.context.warning("IDEasy has been setup for new shells but it cannot work in your current shell(s).\n"
+ "Reboot or open a new terminal to make it work.");
}

private void installIdeasyWindowsEnv(Path ideRoot, Path installationPath) {
if (this.context.getSystemInfo().isWindows()) {
WindowsHelper helper = WindowsHelper.get(this.context);
helper.setUserEnvironmentValue(IdeVariables.IDE_ROOT.getName(), ideRoot.toString());
String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
if (userPath == null) {
this.context.error("Could not read user PATH from registry!");
if (!this.context.getSystemInfo().isWindows()) {
return;
}
WindowsHelper helper = WindowsHelper.get(this.context);
helper.setUserEnvironmentValue(IdeVariables.IDE_ROOT.getName(), ideRoot.toString());
String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
if (userPath == null) {
this.context.error("Could not read user PATH from registry!");
} else {
this.context.info("Found user PATH={}", userPath);
Path ideasyBinPath = installationPath.resolve("bin");
SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
if (path.getEntries().isEmpty()) {
this.context.warning("ATTENTION:\n"
+ "Your user specific PATH variable seems to be empty.\n"
+ "You can double check this by pressing [Windows][r] and launch the program SystemPropertiesAdvanced.\n"
+ "Then click on 'Environment variables' and check if 'PATH' is set in the 'user variables' from the upper list.\n"
+ "In case 'PATH' is defined there non-empty and you get this message, please abort and give us feedback:\n"
+ "https://github.com/devonfw/IDEasy/issues\n"
+ "Otherwise all is correct and you can continue.");
this.context.askToContinue("Are you sure you want to override your PATH?");
} else {
this.context.info("Found user PATH={}", userPath);
Path ideasyBinPath = installationPath.resolve("bin");
userPath = removeObsoleteEntryFromWindowsPath(userPath);
if (userPath.isEmpty()) {
this.context.warning("ATTENTION:\n"
+ "Your user specific PATH variable seems to be empty.\n"
+ "You can double check this by pressing [Windows][r] and launch the program SystemPropertiesAdvanced.\n"
+ "Then click on 'Environment variables' and check if 'PATH' is set in the 'user variables' from the upper list.\n"
+ "In case 'PATH' is defined there non-empty and you get this message, please abort and give us feedback:\n"
+ "https://github.com/devonfw/IDEasy/issues\n"
+ "Otherwise all is correct and you can continue.");
this.context.askToContinue("Are you sure you want to override your PATH?");
userPath = ideasyBinPath.toString();
} else {
userPath = userPath + ";" + ideasyBinPath;
}
helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), userPath);
path.removeEntries(s -> s.endsWith(IDE_BIN));
}
path.getEntries().add(ideasyBinPath.toString());
helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), path.toString());
}
}

static String removeObsoleteEntryFromWindowsPath(String userPath) {
return removeEntryFromWindowsPath(userPath, IDE_BIN);
}

static String removeEntryFromWindowsPath(String userPath, String suffix) {
int len = userPath.length();
int start = 0;
while ((start >= 0) && (start < len)) {
Expand All @@ -214,7 +224,7 @@ static String removeObsoleteEntryFromWindowsPath(String userPath) {
end = len;
}
String entry = userPath.substring(start, end);
if (entry.endsWith("\\_ide\\bin")) {
if (entry.endsWith(suffix)) {
String prefix = "";
int offset = 1;
if (start > 0) {
Expand All @@ -224,7 +234,7 @@ static String removeObsoleteEntryFromWindowsPath(String userPath) {
if (end == len) {
return prefix;
} else {
return prefix + userPath.substring(end + offset);
return removeEntryFromWindowsPath(prefix + userPath.substring(end + offset), suffix);
}
}
start = end + 1;
Expand All @@ -240,11 +250,34 @@ static String removeObsoleteEntryFromWindowsPath(String userPath) {
*/
private void addToShellRc(String filename, Path ideRoot, String extraLine) {

this.context.info("Configuring IDEasy in {}", filename);
modifyShellRc(filename, ideRoot, true, extraLine);
}

private void removeFromShellRc(String filename, Path ideRoot) {

modifyShellRc(filename, ideRoot, false, null);
}

/**
* Adds ourselves to the shell RC (run-commands) configuration file.
*
* @param filename the name of the RC file.
* @param ideRoot the IDE_ROOT {@link Path}.
*/
private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {

if (add) {
this.context.info("Configuring IDEasy in {}", filename);
} else {
this.context.info("Removing IDEasy from {}", filename);
}
Path rcFile = this.context.getUserHome().resolve(filename);
FileAccess fileAccess = this.context.getFileAccess();
List<String> lines = fileAccess.readFileLines(rcFile);
if (lines == null) {
if (!add) {
return;
}
lines = new ArrayList<>();
} else {
// since it is unspecified if the returned List may be immutable we want to get sure
Expand All @@ -263,14 +296,17 @@ private void addToShellRc(String filename, Path ideRoot, String extraLine) {
extraLine = null;
}
}
if (extraLine != null) {
lines.add(extraLine);
}
if (!this.context.getSystemInfo().isWindows()) {
lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
if (add) {
if (extraLine != null) {
lines.add(extraLine);
}
if (!this.context.getSystemInfo().isWindows()) {
lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
}
lines.add(BASH_CODE_SOURCE_FUNCTIONS);
}
lines.add(BASH_CODE_SOURCE_FUNCTIONS);
fileAccess.writeFileLines(lines, rcFile);
this.context.debug("Successfully updated {}", filename);
}

private static boolean isObsoleteRcLine(String line) {
Expand Down Expand Up @@ -317,4 +353,47 @@ private Path determineIdeRoot(Path cwd) {
return ideRoot;
}

/**
* Uninstalls IDEasy entirely from the system.
*/
public void uninstallIdeasy() {

Path ideRoot = this.context.getIdeRoot();
removeFromShellRc(BASHRC, ideRoot);
removeFromShellRc(ZSHRC, ideRoot);
Path idePath = this.context.getIdePath();
uninstallIdeasyWindowsEnv(ideRoot);
this.context.info("Finally deleting {}", idePath);
this.context.getFileAccess().delete(idePath); // TODO prevent Windows file locks
this.context.success("IDEasy has been uninstalled from your system.");
this.context.interaction("ATTENTION:\n"
+ "In order to prevent data-loss, we do not delete your projects and git repositories!\n"
+ "To entirely get rid of IDEasy, also check your IDE_ROOT folder at:\n"
+ "{}", ideRoot);
}

private void uninstallIdeasyWindowsEnv(Path ideRoot) {
if (!this.context.getSystemInfo().isWindows()) {
return;
}
WindowsHelper helper = WindowsHelper.get(this.context);
helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
if (userPath == null) {
this.context.error("Could not read user PATH from registry!");
} else {
this.context.info("Found user PATH={}", userPath);
String newUserPath = userPath;
if (!userPath.isEmpty()) {
SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
newUserPath = path.toString();
}
if (newUserPath.equals(userPath)) {
this.context.error("Could not find IDEasy in PATH:\n{}", userPath);
} else {
helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
}
}
}
}
Loading