• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 13572086603

27 Feb 2025 05:16PM UTC coverage: 68.251% (-0.004%) from 68.255%
13572086603

Pull #1094

github

web-flow
Merge 963c41239 into df2cda557
Pull Request #1094: #1093: do not fail on remove of variable if not defined

3032 of 4891 branches covered (61.99%)

Branch coverage included in aggregate %.

7867 of 11078 relevant lines covered (71.01%)

3.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java
1
package com.devonfw.tools.ide.os;
2

3
import java.util.List;
4

5
import com.devonfw.tools.ide.context.IdeContext;
6
import com.devonfw.tools.ide.log.IdeLogLevel;
7
import com.devonfw.tools.ide.process.ProcessErrorHandling;
8
import com.devonfw.tools.ide.process.ProcessMode;
9
import com.devonfw.tools.ide.process.ProcessResult;
10

11
/**
12
 * Implementation of {@link WindowsHelper}.
13
 */
14
public class WindowsHelperImpl implements WindowsHelper {
×
15

16
  /** Registry key for the users environment variables. */
17
  public static final String HKCU_ENVIRONMENT = "HKCU\\Environment";
18

19
  private final IdeContext context;
20

21
  /**
22
   * The constructor.
23
   *
24
   * @param context the {@link IdeContext}.
25
   */
26
  public WindowsHelperImpl(IdeContext context) {
×
27

28
    this.context = context;
×
29
  }
×
30

31
  @Override
32
  public void setUserEnvironmentValue(String key, String value) {
33

34
    ProcessResult result = this.context.newProcess().executable("setx").addArgs(key, value).run(ProcessMode.DEFAULT_SILENT);
×
35
    assert (result.isSuccessful());
×
36
  }
×
37

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

49
  @Override
50
  public String getUserEnvironmentValue(String key) {
51

52
    return getRegistryValue(HKCU_ENVIRONMENT, key);
×
53
  }
54

55
  @Override
56
  public String getRegistryValue(String path, String key) {
57

58
    ProcessResult result = this.context.newProcess().executable("reg").addArgs("query", path, "/v", key).run(ProcessMode.DEFAULT_CAPTURE);
×
59
    List<String> out = result.getOut();
×
60
    for (String line : out) {
×
61
      int i = line.indexOf(key);
×
62
      if (i >= 0) {
×
63
        assert (i == 4);
×
64
        i += key.length();
×
65
        i = skipWhitespaces(line, i);
×
66
        i = skipNonWhitespaces(line, i); // the type (e.g. "REG_SZ")
×
67
        i = skipWhitespaces(line, i);
×
68
        line = line.substring(i);
×
69
        return line;
×
70
      }
71
    }
×
72
    return null;
×
73
  }
74

75
  private static int skipWhitespaces(String string, int i) {
76

77
    int len = string.length();
×
78
    while ((i < len) && Character.isWhitespace(string.charAt(i))) {
×
79
      i++;
×
80
    }
81
    return i;
×
82
  }
83

84
  private static int skipNonWhitespaces(String string, int i) {
85

86
    int len = string.length();
×
87
    while ((i < len) && !Character.isWhitespace(string.charAt(i))) {
×
88
      i++;
×
89
    }
90
    return i;
×
91
  }
92

93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc