Skip to content

Commit 8f3c98e

Browse files
hohwillejan-vcapgemini
authored andcommitted
devonfw#789: implement uninstall of IDEasy (devonfw#1071)
Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>
1 parent ec1e602 commit 8f3c98e

17 files changed

Lines changed: 320 additions & 48 deletions

File tree

cli/src/main/java/com/devonfw/tools/ide/commandlet/UninstallCommandlet.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.devonfw.tools.ide.context.IdeContext;
44
import com.devonfw.tools.ide.property.ToolProperty;
5+
import com.devonfw.tools.ide.tool.IdeasyCommandlet;
56
import com.devonfw.tools.ide.tool.ToolCommandlet;
67

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

2223
super(context);
2324
addKeyword(getName());
24-
this.tools = add(new ToolProperty("", true, true, "tool"));
25+
this.tools = add(new ToolProperty("", false, true, "tool"));
2526
}
2627

2728
@Override
@@ -30,10 +31,27 @@ public String getName() {
3031
return "uninstall";
3132
}
3233

34+
@Override
35+
public boolean isIdeRootRequired() {
36+
37+
return this.tools.getValueCount() > 0;
38+
}
39+
3340
@Override
3441
public void run() {
3542

36-
for (int i = 0; i < this.tools.getValueCount(); i++) {
43+
int valueCount = this.tools.getValueCount();
44+
if (valueCount == 0) {
45+
if (!this.context.isForceMode()) {
46+
this.context.askToContinue("Sub-command uninstall without any further arguments will perform the entire uninstallation of IDEasy.\n"
47+
+ "Since this is typically not to be called manually, you may have forgotten to specify the tool to install as extra argument.\n"
48+
+ "The current command will uninstall IDEasy from your computer. Are you sure?");
49+
}
50+
IdeasyCommandlet ideasy = new IdeasyCommandlet(this.context);
51+
ideasy.uninstallIdeasy();
52+
return;
53+
}
54+
for (int i = 0; i < valueCount; i++) {
3755
ToolCommandlet toolCommandlet = this.tools.getValue(i);
3856
if (toolCommandlet.getInstalledVersion() != null) {
3957
toolCommandlet.uninstall();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.devonfw.tools.ide.common;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.function.Predicate;
7+
8+
/**
9+
* 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
10+
* {@link String}s.
11+
*/
12+
public class SimpleSystemPath {
13+
14+
private final char separator;
15+
16+
private final List<String> entries;
17+
18+
private SimpleSystemPath(char separator, List<String> entries) {
19+
20+
super();
21+
this.separator = separator;
22+
this.entries = entries;
23+
}
24+
25+
/**
26+
* @return the entries of this PATH as a mutable {@link List}.
27+
*/
28+
public List<String> getEntries() {
29+
30+
return this.entries;
31+
}
32+
33+
/**
34+
* Remove all entries from this PATH that match the given {@link Predicate}.
35+
*
36+
* @param filter the {@link Predicate} {@link Predicate#test(Object) deciding} what to filter and remove.
37+
*/
38+
public void removeEntries(Predicate<String> filter) {
39+
40+
Iterator<String> iterator = this.entries.iterator();
41+
while (iterator.hasNext()) {
42+
String entry = iterator.next();
43+
if (filter.test(entry)) {
44+
iterator.remove();
45+
}
46+
}
47+
}
48+
49+
@Override
50+
public String toString() {
51+
52+
StringBuilder sb = new StringBuilder();
53+
boolean first = true;
54+
for (String entry : this.entries) {
55+
if (first) {
56+
first = false;
57+
} else {
58+
sb.append(this.separator);
59+
}
60+
sb.append(entry);
61+
}
62+
return sb.toString();
63+
}
64+
65+
/**
66+
* @param path the entire PATH as {@link String},
67+
* @param separator the separator character.
68+
* @return the {@link SimpleSystemPath}.
69+
*/
70+
public static SimpleSystemPath of(String path, char separator) {
71+
72+
List<String> entries = new ArrayList<>();
73+
int start = 0;
74+
int len = path.length();
75+
while (start < len) {
76+
int end = path.indexOf(separator, start);
77+
if (end < 0) {
78+
end = len;
79+
}
80+
entries.add(path.substring(start, end));
81+
start = end + 1;
82+
}
83+
return new SimpleSystemPath(separator, entries);
84+
}
85+
}

cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public interface WindowsHelper {
1414
*/
1515
void setUserEnvironmentValue(String key, String value);
1616

17+
/**
18+
* @param key the name of the environment variable to remove.
19+
*/
20+
void removeUserEnvironmentValue(String key);
21+
1722
/**
1823
* @param key the name of the environment variable.
1924
* @return the value of the environment variable in the users context.

cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import com.devonfw.tools.ide.context.IdeContext;
6+
import com.devonfw.tools.ide.log.IdeLogLevel;
67
import com.devonfw.tools.ide.process.ProcessMode;
78
import com.devonfw.tools.ide.process.ProcessResult;
89

@@ -33,6 +34,16 @@ public void setUserEnvironmentValue(String key, String value) {
3334
assert (result.isSuccessful());
3435
}
3536

37+
@Override
38+
public void removeUserEnvironmentValue(String key) {
39+
ProcessResult result = this.context.newProcess().executable("reg").addArgs("delete", HKCU_ENVIRONMENT, "/v", key, "/f").run(ProcessMode.DEFAULT_CAPTURE);
40+
if (result.isSuccessful()) {
41+
this.context.debug("Removed environment variable {}", key);
42+
} else {
43+
result.log(IdeLogLevel.WARNING, this.context);
44+
}
45+
}
46+
3647
@Override
3748
public String getUserEnvironmentValue(String key) {
3849

0 commit comments

Comments
 (0)