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

devonfw / IDEasy / 14992183443

13 May 2025 08:42AM UTC coverage: 67.717% (+0.04%) from 67.679%
14992183443

Pull #1302

github

web-flow
Merge 15a09f220 into b2f3ea46b
Pull Request #1302: #351 - same shell session environment variables set correct

3112 of 5000 branches covered (62.24%)

Branch coverage included in aggregate %.

7978 of 11377 relevant lines covered (70.12%)

3.07 hits per line

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

86.57
cli/src/main/java/com/devonfw/tools/ide/environment/EnvironmentVariables.java
1
package com.devonfw.tools.ide.environment;
2

3
import java.nio.file.Path;
4
import java.util.Collection;
5
import java.util.List;
6
import java.util.Locale;
7

8
import com.devonfw.tools.ide.context.IdeContext;
9
import com.devonfw.tools.ide.variable.VariableDefinition;
10
import com.devonfw.tools.ide.variable.VariableSyntax;
11
import com.devonfw.tools.ide.version.VersionIdentifier;
12

13
/**
14
 * Interface for the environment with the variables.
15
 */
16
public interface EnvironmentVariables {
17

18
  /** Filename of the default variable configuration file. {@value} */
19
  String DEFAULT_PROPERTIES = "ide.properties";
20

21
  /** Filename of the legacy variable configuration file. {@value} */
22
  String LEGACY_PROPERTIES = "devon.properties";
23

24
  /**
25
   * @param name the name of the environment variable to get.
26
   * @return the value of the variable with the given {@code name}. Will be {@code null} if no such variable is defined.
27
   */
28
  default String get(String name) {
29

30
    return get(name, false);
5✔
31
  }
32

33
  /**
34
   * @param name the name of the environment variable to get.
35
   * @param ignoreDefaultValue - {@code true} if the {@link VariableDefinition#getDefaultValue(IdeContext) default value} of a potential
36
   *     {@link VariableDefinition} shall be ignored, {@code false} to return default instead of {@code null}.
37
   * @return the value of the variable with the given {@code name}. Will be {@code null} if no such variable is defined.
38
   */
39
  default String get(String name, boolean ignoreDefaultValue) {
40

41
    String value = getFlat(name);
4✔
42
    if (value == null) {
2✔
43
      EnvironmentVariables parent = getParent();
3✔
44
      if (parent != null) {
2✔
45
        if ((name.equals("M2_REPO") || name.equals("MAVEN_ARGS")) || name.contains("_VERSION") || name.contains("_EDITION")) {
16✔
46
          if (parent.getSource().toString().contains("SYSTEM")) {
6✔
47
            return null;
2✔
48
          }
49
        }
50
        value = parent.get(name);
4✔
51
      }
52
    }
53

54
    return value;
2✔
55
  }
56

57
  /**
58
   * @param name the name of the environment variable to get.
59
   * @return the value of the variable with the given {@code name} as {@link Path}. Will be {@code null} if no such variable is defined.
60
   */
61
  default Path getPath(String name) {
62

63
    String value = get(name);
4✔
64
    if (value == null) {
2!
65
      return null;
×
66
    }
67
    return Path.of(value);
5✔
68
  }
69

70
  /**
71
   * @param name the name of the environment variable to get.
72
   * @return the value of the variable with the given {@code name} without {@link #getParent() inheritance from parent}. Will be {@code null} if no such
73
   *     variable is defined.
74
   */
75
  String getFlat(String name);
76

77
  /**
78
   * @param tool the name of the tool (e.g. "java").
79
   * @return the edition of the tool to use.
80
   */
81
  default String getToolEdition(String tool) {
82

83
    String variable = getToolEditionVariable(tool);
3✔
84
    String value = get(variable);
4✔
85
    if (value == null) {
2✔
86
      value = tool;
2✔
87
    }
88
    return value;
2✔
89
  }
90

91
  /**
92
   * @param tool the name of the tool (e.g. "java").
93
   * @return the {@link VersionIdentifier} with the version of the tool to use. May also be a {@link VersionIdentifier#isPattern() version pattern}. Will be
94
   *     {@link VersionIdentifier#LATEST} if undefined.
95
   */
96
  VersionIdentifier getToolVersion(String tool);
97

98
  /**
99
   * @return the {@link EnvironmentVariablesType type} of this {@link EnvironmentVariables}.
100
   */
101
  EnvironmentVariablesType getType();
102

103
  /**
104
   * @param type the {@link #getType() type} of the requested {@link EnvironmentVariables}.
105
   * @return the {@link EnvironmentVariables} with the given {@link #getType() type} from this {@link EnvironmentVariables} along the
106
   *     {@link #getParent() parent} hierarchy or {@code null} if not found.
107
   */
108
  default EnvironmentVariables getByType(EnvironmentVariablesType type) {
109

110
    if (type == getType()) {
4✔
111
      return this;
2✔
112
    }
113
    EnvironmentVariables parent = getParent();
3✔
114
    if (parent == null) {
2!
115
      return null;
×
116
    } else {
117
      return parent.getByType(type);
4✔
118
    }
119
  }
120

121
  /**
122
   * @return the {@link Path} to the underlying properties file or {@code null} if not based on such file (e.g. for EVS or
123
   *     {@link EnvironmentVariablesResolved}).
124
   */
125
  Path getPropertiesFilePath();
126

127
  /**
128
   * @return the {@link Path} to the {@link #LEGACY_PROPERTIES} if they exist for this {@link EnvironmentVariables} or {@code null} otherwise (does not exist).
129
   */
130
  Path getLegacyPropertiesFilePath();
131

132
  /**
133
   * @return the {@link VariableSource} of this {@link EnvironmentVariables}.
134
   */
135
  VariableSource getSource();
136

137
  /**
138
   * @return the parent {@link EnvironmentVariables} to inherit from or {@code null} if this is the {@link EnvironmentVariablesType#SYSTEM root}
139
   *     {@link EnvironmentVariables} instance.
140
   */
141
  default EnvironmentVariables getParent() {
142

143
    return null;
×
144
  }
145

146
  /**
147
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to set.
148
   * @param value the new {@link #get(String) value} of the variable to set. May be {@code null} to unset the variable.
149
   * @return the old variable value.
150
   */
151
  default String set(String name, String value) {
152

153
    throw new UnsupportedOperationException();
×
154
  }
155

156
  /**
157
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to set.
158
   * @param value the new {@link #get(String) value} of the variable to set. May be {@code null} to unset the variable.
159
   * @param export - {@code true} if the variable needs to be exported, {@code false} otherwise.
160
   * @return the old variable value.
161
   */
162
  default String set(String name, String value, boolean export) {
163

164
    throw new UnsupportedOperationException();
×
165
  }
166

167
  /**
168
   * Saves any potential {@link #set(String, String, boolean) changes} of this {@link EnvironmentVariables}.
169
   */
170
  default void save() {
171

172
    throw new UnsupportedOperationException("Not yet implemented!");
×
173
  }
174

175
  /**
176
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to search for.
177
   * @return the closest {@link EnvironmentVariables} instance that defines the variable with the given {@code name} or {@code null} if the variable is not
178
   *     defined.
179
   */
180
  default EnvironmentVariables findVariable(String name) {
181

182
    String value = getFlat(name);
4✔
183
    if (value != null) {
2✔
184
      return this;
2✔
185
    }
186
    EnvironmentVariables parent = getParent();
3✔
187
    if (parent == null) {
2✔
188
      return null;
2✔
189
    } else {
190
      return parent.findVariable(name);
4✔
191
    }
192
  }
193

194
  /**
195
   * @return the {@link Collection} of the {@link VariableLine}s defined by this {@link EnvironmentVariables} including inheritance.
196
   */
197
  List<VariableLine> collectVariables();
198

199
  /**
200
   * @return the {@link Collection} of the {@link VariableLine#isExport() exported} {@link VariableLine}s defined by this {@link EnvironmentVariables} including
201
   *     inheritance.
202
   */
203
  List<VariableLine> collectExportedVariables();
204

205
  /**
206
   * @param string the {@link String} that potentially contains variables in {@link VariableSyntax#CURLY} ("${«variable«}"). Those will be resolved by this
207
   *     method and replaced with their {@link #get(String) value}.
208
   * @param source the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation}
209
   *     that will be used in error or log messages if a variable could not be resolved.
210
   * @return the given {@link String} with the variables resolved.
211
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
212
   */
213
  String resolve(String string, Object source);
214

215
  /**
216
   * @param string the {@link String} that potentially contains variables in {@link VariableSyntax}. Those will be resolved by this method and replaced with
217
   *     their {@link #get(String) value}.
218
   * @param source the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation}
219
   *     that will be used in error or log messages if a variable could not be resolved.
220
   * @param legacySupport - {@code true} for legacy support with {@link VariableSyntax#CURLY} as fallback, {@code false} otherwise.
221
   * @return the given {@link String} with the variables resolved.
222
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
223
   */
224
  String resolve(String string, Object source, boolean legacySupport);
225

226
  /**
227
   * The inverse operation of {@link #resolve(String, Object, boolean)}. Please note that the {@link #resolve(String, Object, boolean) resolve} operation is not
228
   * fully bijective. There may be multiple variables holding the same {@link #get(String) value} or there may be static text that can be equal to a
229
   * {@link #get(String) variable value}. This method does its best to implement the inverse resolution based on some heuristics.
230
   *
231
   * @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
232
   *     {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
233
   * @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
234
   *     {@link Object#toString() string representation} that will be used in error or log messages if the inverse resolving was not working as expected.
235
   * @return the given {@link String} with {@link #get(String) variable values} replaced with according {@link com.devonfw.tools.ide.variable.VariableSyntax}
236
   *     expressions.
237
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
238
   */
239
  default String inverseResolve(String string, Object source) {
240

241
    return inverseResolve(string, source, VariableSyntax.SQUARE);
×
242
  }
243

244
  /**
245
   * @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
246
   *     {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
247
   * @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
248
   *     {@link Object#toString() string representation} that will be used in error or log messages if the inverse resolving was not working as expected.
249
   * @param syntax the explicit {@link VariableSyntax} to use.
250
   * @return the given {@link String} with {@link #get(String) variable values} replaced with according {@link com.devonfw.tools.ide.variable.VariableSyntax}
251
   *     expressions.
252
   * @see #inverseResolve(String, Object)
253
   */
254
  String inverseResolve(String string, Object source, VariableSyntax syntax);
255

256
  /**
257
   * @param context the {@link IdeContext}.
258
   * @return the system {@link EnvironmentVariables} building the root of the {@link EnvironmentVariables} hierarchy.
259
   */
260
  static AbstractEnvironmentVariables ofSystem(IdeContext context) {
261

262
    return EnvironmentVariablesSystem.of(context);
3✔
263
  }
264

265
  /**
266
   * @param tool the name of the tool.
267
   * @return the name of the version variable.
268
   */
269
  static String getToolVersionVariable(String tool) {
270

271
    return getToolVariablePrefix(tool) + "_VERSION";
4✔
272
  }
273

274
  /**
275
   * @param tool the name of the tool.
276
   * @return the name of the edition variable.
277
   */
278
  static String getToolEditionVariable(String tool) {
279

280
    return getToolVariablePrefix(tool) + "_EDITION";
4✔
281
  }
282

283
  /**
284
   * @param tool the name of the tool.
285
   * @return the given {@code tool} name in UPPER_CASE without hyphen characters.
286
   */
287
  static String getToolVariablePrefix(String tool) {
288

289
    return tool.toUpperCase(Locale.ROOT).replace('-', '_');
7✔
290
  }
291
}
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