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

devonfw / IDEasy / 18949830015

30 Oct 2025 05:38PM UTC coverage: 68.876% (-0.004%) from 68.88%
18949830015

push

github

web-flow
#1553: speed up environment commandlet (#1554)

Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>

3486 of 5545 branches covered (62.87%)

Branch coverage included in aggregate %.

9121 of 12759 relevant lines covered (71.49%)

3.14 hits per line

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

92.31
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 com.devonfw.tools.ide.cli.CliExitException;
9
import com.devonfw.tools.ide.context.AbstractIdeContext;
10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
12
import com.devonfw.tools.ide.environment.VariableLine;
13
import com.devonfw.tools.ide.environment.VariableSource;
14
import com.devonfw.tools.ide.log.IdeLogLevel;
15
import com.devonfw.tools.ide.log.IdeSubLogger;
16
import com.devonfw.tools.ide.os.WindowsPathSyntax;
17
import com.devonfw.tools.ide.process.EnvironmentContext;
18
import com.devonfw.tools.ide.process.EnvironmentVariableCollectorContext;
19
import com.devonfw.tools.ide.property.FlagProperty;
20
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
21
import com.devonfw.tools.ide.tool.ToolInstallation;
22
import com.devonfw.tools.ide.version.VersionIdentifier;
23

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

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

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

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

44
  @Override
45
  public String getName() {
46

47
    return "env";
2✔
48
  }
49

50
  @Override
51
  public boolean isIdeHomeRequired() {
52

53
    return false;
2✔
54
  }
55

56
  @Override
57
  public boolean isProcessableOutput() {
58

59
    return true;
×
60
  }
61

62
  @Override
63
  public void run() {
64
    if (this.context.getIdeHome() == null) {
4!
65
      throw new CliExitException();
×
66
    }
67

68
    boolean winCmd = false;
2✔
69
    WindowsPathSyntax pathSyntax = null;
2✔
70
    IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
5✔
71
    if (this.context.getSystemInfo().isWindows()) {
5✔
72
      if (this.bash.isTrue()) {
4!
73
        pathSyntax = WindowsPathSyntax.MSYS;
×
74
      } else {
75
        winCmd = true;
2✔
76
        pathSyntax = WindowsPathSyntax.WINDOWS;
2✔
77
      }
78
    }
79

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

84
    EnvironmentVariableCollectorContext environmentVariableCollectorContext = new EnvironmentVariableCollectorContext(variableMap,
8✔
85
        new VariableSource(EnvironmentVariablesType.TOOL, this.context.getSoftwarePath()), pathSyntax);
5✔
86
    setEnvironmentVariablesInLocalTools(environmentVariableCollectorContext);
3✔
87

88
    printLines(variableMap, logger, winCmd);
5✔
89
  }
1✔
90

91
  private void printLines(Map<String, VariableLine> variableMap, IdeSubLogger logger, boolean winCmd) {
92
    if (this.context.debug().isEnabled()) {
5✔
93
      Map<EnvironmentVariablesType, List<VariableLine>> type2lines = variableMap.values().stream().collect(Collectors.groupingBy(l -> l.getSource().type()));
12✔
94
      for (EnvironmentVariablesType type : EnvironmentVariablesType.values()) {
16✔
95
        List<VariableLine> lines = type2lines.get(type);
5✔
96
        if (lines != null) {
2✔
97
          boolean sourcePrinted = false;
2✔
98
          sortVariables(lines);
2✔
99
          for (VariableLine line : lines) {
10✔
100
            if (!sourcePrinted) {
2✔
101
              this.context.debug("from {}:", line.getSource());
11✔
102
              sourcePrinted = true;
2✔
103
            }
104
            logger.log(format(line, winCmd));
6✔
105
          }
1✔
106
        }
107
      }
108
    } else {
1✔
109
      List<VariableLine> variables = new ArrayList<>(variableMap.values());
6✔
110
      sortVariables(variables);
2✔
111
      for (VariableLine line : variables) {
10✔
112
        logger.log(format(line, winCmd));
6✔
113
      }
1✔
114
    }
115
  }
1✔
116

117
  private static void sortVariables(List<VariableLine> lines) {
118

119
    lines.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
9✔
120
  }
1✔
121

122
  private String format(VariableLine line, boolean winCmd) {
123

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

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