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

devonfw / IDEasy / 31466478317

11 Aug 2026 06:48AM UTC coverage: 72.882% (-0.005%) from 72.887%
31466478317

push

github

web-flow
#2264: Set environment variables of all installed tools when running a tool (#2266)

5101 of 7755 branches covered (65.78%)

Branch coverage included in aggregate %.

13339 of 17546 relevant lines covered (76.02%)

3.23 hits per line

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

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

3
import java.nio.file.Path;
4
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.stream.Collectors;
8

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

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.EnvironmentVariableCollectorContext;
20
import com.devonfw.tools.ide.property.FlagProperty;
21
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
22

23
/**
24
 * {@link Commandlet} to print the environment variables.
25
 */
26
public final class EnvironmentCommandlet extends Commandlet {
27

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

30
  /** {@link FlagProperty} to enable Bash (MSys) path conversion on Windows. */
31
  public final FlagProperty bash;
32

33
  /**
34
   * The constructor.
35
   *
36
   * @param context the {@link IdeContext}.
37
   */
38
  public EnvironmentCommandlet(IdeContext context) {
39

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

45
  @Override
46
  public String getName() {
47

48
    return "env";
2✔
49
  }
50

51
  @Override
52
  public boolean isIdeHomeRequired() {
53

54
    return false;
2✔
55
  }
56

57
  @Override
58
  public boolean isProcessableOutput() {
59

60
    return true;
×
61
  }
62

63
  @Override
64
  protected void doRun() {
65

66
    boolean winCmd = false;
2✔
67
    WindowsPathSyntax pathSyntax = null;
2✔
68
    if (this.context.getSystemInfo().isWindows()) {
5✔
69
      if (this.bash.isTrue()) {
4!
70
        pathSyntax = WindowsPathSyntax.MSYS;
×
71
      } else {
72
        winCmd = true;
2✔
73
        pathSyntax = WindowsPathSyntax.WINDOWS;
2✔
74
      }
75
    }
76

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

81
    Path softwarePath = this.context.getSoftwarePath();
4✔
82
    if (softwarePath != null) {
2✔
83
      EnvironmentVariableCollectorContext environmentVariableCollectorContext = new EnvironmentVariableCollectorContext(variableMap,
11✔
84
          new VariableSource(EnvironmentVariablesType.TOOL, softwarePath), pathSyntax);
85
      this.context.setEnvironmentOfInstalledTools(environmentVariableCollectorContext);
4✔
86
    }
87

88
    printLines(variableMap, winCmd);
4✔
89

90
    // Bash completions must be printed after the environment variables because they may reference variables such as TERRAFORM_HOME.
91
    if (this.bash.isTrue()) {
4✔
92
      printBashCompletions();
2✔
93
    }
94
  }
1✔
95

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

122
  private static void sortVariables(List<VariableLine> lines) {
123

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

127
  private String format(VariableLine line, boolean winCmd) {
128

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

139
  /**
140
   * Prints optional Bash completion setup commands for installed local tools.
141
   * <p>
142
   * These lines are only emitted for {@code env --bash}. The IDEasy shell wrapper evaluates this output, so tools can dynamically register completions without
143
   * modifying user files such as {@code ~/.bashrc}.
144
   */
145
  private void printBashCompletions() {
146

147
    for (Commandlet commandlet : this.context.getCommandletManager().getCommandlets()) {
13✔
148
      if (commandlet instanceof LocalToolCommandlet tool) {
6✔
149
        try {
150
          if (tool.isInstalled()) {
3!
151
            String bashCompletion = tool.getBashCompletion();
×
152
            if ((bashCompletion != null) && !bashCompletion.isBlank()) {
×
153
              IdeLogLevel.PROCESSABLE.log(LOG, bashCompletion);
×
154
            }
155
          }
156
        } catch (Exception e) {
×
157
          LOG.warn("An error occurred while collecting Bash completion for tool {}.", tool.getName(), e);
×
158
        }
1✔
159
      }
160
    }
1✔
161
  }
1✔
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc