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

devonfw / IDEasy / 22300285724

23 Feb 2026 09:32AM UTC coverage: 70.754% (+0.3%) from 70.474%
22300285724

Pull #1714

github

web-flow
Merge caa980897 into 379acdc9d
Pull Request #1714: #404: #1713: advanced logging

4064 of 6348 branches covered (64.02%)

Branch coverage included in aggregate %.

10640 of 14434 relevant lines covered (73.71%)

3.1 hits per line

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

93.41
cli/src/main/java/com/devonfw/tools/ide/commandlet/EnvironmentCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.stream.Collectors;
7

8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10

11
import com.devonfw.tools.ide.cli.CliExitException;
12
import com.devonfw.tools.ide.context.AbstractIdeContext;
13
import com.devonfw.tools.ide.context.IdeContext;
14
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
15
import com.devonfw.tools.ide.environment.VariableLine;
16
import com.devonfw.tools.ide.environment.VariableSource;
17
import com.devonfw.tools.ide.log.IdeLogLevel;
18
import com.devonfw.tools.ide.os.WindowsPathSyntax;
19
import com.devonfw.tools.ide.process.EnvironmentContext;
20
import com.devonfw.tools.ide.process.EnvironmentVariableCollectorContext;
21
import com.devonfw.tools.ide.property.FlagProperty;
22
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
23
import com.devonfw.tools.ide.tool.ToolInstallation;
24
import com.devonfw.tools.ide.version.VersionIdentifier;
25

26
/**
27
 * {@link Commandlet} to print the environment variables.
28
 */
29
public final class EnvironmentCommandlet extends Commandlet {
30

31
  private static final Logger LOG = LoggerFactory.getLogger(EnvironmentCommandlet.class);
4✔
32

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

36
  /**
37
   * The constructor.
38
   *
39
   * @param context the {@link IdeContext}.
40
   */
41
  public EnvironmentCommandlet(IdeContext context) {
42

43
    super(context);
3✔
44
    addKeyword(getName());
4✔
45
    this.bash = add(new FlagProperty("--bash"));
9✔
46
  }
1✔
47

48
  @Override
49
  public String getName() {
50

51
    return "env";
2✔
52
  }
53

54
  @Override
55
  public boolean isIdeHomeRequired() {
56

57
    return false;
2✔
58
  }
59

60
  @Override
61
  public boolean isProcessableOutput() {
62

63
    return true;
2✔
64
  }
65

66
  @Override
67
  protected void doRun() {
68
    if (this.context.getIdeHome() == null) {
4!
69
      throw new CliExitException();
×
70
    }
71

72
    boolean winCmd = false;
2✔
73
    WindowsPathSyntax pathSyntax = null;
2✔
74
    if (this.context.getSystemInfo().isWindows()) {
5✔
75
      if (this.bash.isTrue()) {
4!
76
        pathSyntax = WindowsPathSyntax.MSYS;
×
77
      } else {
78
        winCmd = true;
2✔
79
        pathSyntax = WindowsPathSyntax.WINDOWS;
2✔
80
      }
81
    }
82

83
    ((AbstractIdeContext) this.context).setPathSyntax(pathSyntax);
5✔
84
    List<VariableLine> variables = this.context.getVariables().collectVariables();
5✔
85
    Map<String, VariableLine> variableMap = variables.stream().collect(Collectors.toMap(VariableLine::getName, v -> v));
10✔
86

87
    EnvironmentVariableCollectorContext environmentVariableCollectorContext = new EnvironmentVariableCollectorContext(variableMap,
8✔
88
        new VariableSource(EnvironmentVariablesType.TOOL, this.context.getSoftwarePath()), pathSyntax);
5✔
89
    setEnvironmentVariablesInLocalTools(environmentVariableCollectorContext);
3✔
90

91
    printLines(variableMap, winCmd);
4✔
92
  }
1✔
93

94
  private void printLines(Map<String, VariableLine> variableMap, boolean winCmd) {
95
    if (LOG.isDebugEnabled()) {
3✔
96
      Map<EnvironmentVariablesType, List<VariableLine>> type2lines = variableMap.values().stream().collect(Collectors.groupingBy(l -> l.getSource().type()));
12✔
97
      for (EnvironmentVariablesType type : EnvironmentVariablesType.values()) {
16✔
98
        List<VariableLine> lines = type2lines.get(type);
5✔
99
        if (lines != null) {
2✔
100
          boolean sourcePrinted = false;
2✔
101
          sortVariables(lines);
2✔
102
          for (VariableLine line : lines) {
10✔
103
            if (!sourcePrinted) {
2✔
104
              LOG.debug("from {}:", line.getSource());
5✔
105
              sourcePrinted = true;
2✔
106
            }
107
            LOG.info(IdeLogLevel.PROCESSABLE.getSlf4jMarker(), format(line, winCmd));
8✔
108
          }
1✔
109
        }
110
      }
111
    } else {
1✔
112
      List<VariableLine> variables = new ArrayList<>(variableMap.values());
6✔
113
      sortVariables(variables);
2✔
114
      for (VariableLine line : variables) {
10✔
115
        LOG.info(IdeLogLevel.PROCESSABLE.getSlf4jMarker(), format(line, winCmd));
8✔
116
      }
1✔
117
    }
118
  }
1✔
119

120
  private static void sortVariables(List<VariableLine> lines) {
121

122
    lines.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
9✔
123
  }
1✔
124

125
  private String format(VariableLine line, boolean winCmd) {
126

127
    if (winCmd) {
2✔
128
      return line.getName() + "=" + line.getValue();
6✔
129
    } else {
130
      String lineValue = line.getValue();
3✔
131
      lineValue = "\"" + lineValue + "\"";
3✔
132
      line = line.withValue(lineValue);
4✔
133
      return line.toString();
3✔
134
    }
135
  }
136

137
  private void setEnvironmentVariablesInLocalTools(EnvironmentContext environmentContext) {
138
    // installed tools in IDE_HOME/software
139
    for (Commandlet commandlet : this.context.getCommandletManager().getCommandlets()) {
13✔
140
      if (commandlet instanceof LocalToolCommandlet tool) {
6✔
141
        try {
142
          if (tool.isInstalled()) {
3✔
143
            // for performance optimization, we do a hack here and assume that the installedVersion is never used by any setEnvironment method implementation.
144
            VersionIdentifier installedVersion = VersionIdentifier.LATEST;
2✔
145
            ToolInstallation toolInstallation = new ToolInstallation(tool.getToolPath(), tool.getToolPath(),
7✔
146
                tool.getToolBinPath(), installedVersion, false);
5✔
147
            tool.setEnvironment(environmentContext, toolInstallation, false);
5✔
148
          }
149
        } catch (Exception e) {
×
150
          LOG.warn("An error occurred while setting the environment variables in local tools:", e);
×
151
        }
1✔
152
      }
153
    }
1✔
154
  }
1✔
155
}
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

© 2026 Coveralls, Inc