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

devonfw / IDEasy / 16299443781

15 Jul 2025 04:56PM UTC coverage: 68.439% (-0.007%) from 68.446%
16299443781

Pull #1410

github

web-flow
Merge 6a6965c38 into ab7eb024e
Pull Request #1410: Add support for extra tool installations to run multiple versions in parallel

3301 of 5226 branches covered (63.16%)

Branch coverage included in aggregate %.

8441 of 11931 relevant lines covered (70.75%)

3.12 hits per line

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

87.14
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
        value = parent.get(name);
4✔
46
      }
47
    }
48
    return value;
2✔
49
  }
50

51
  /**
52
   * @param name the name of the environment variable to get.
53
   * @return the value of the variable with the given {@code name} as {@link Path}. Will be {@code null} if no such variable is defined.
54
   */
55
  default Path getPath(String name) {
56

57
    String value = get(name);
4✔
58
    if (value == null) {
2!
59
      return null;
×
60
    }
61
    return Path.of(value);
5✔
62
  }
63

64
  /**
65
   * @param name the name of the environment variable to get.
66
   * @return the value of the variable with the given {@code name} without {@link #getParent() inheritance from parent}. Will be {@code null} if no such
67
   *     variable is defined.
68
   */
69
  String getFlat(String name);
70

71
  /**
72
   * @param tool the name of the tool (e.g. "java").
73
   * @return the edition of the tool to use.
74
   */
75
  default String getToolEdition(String tool) {
76

77
    String variable = getToolEditionVariable(tool);
3✔
78
    String value = get(variable);
4✔
79
    if (value == null) {
2✔
80
      value = tool;
2✔
81
    }
82
    return value;
2✔
83
  }
84

85
  /**
86
   * @param tool the name of the tool (e.g. "java").
87
   * @return the {@link VersionIdentifier} with the version of the tool to use. May also be a {@link VersionIdentifier#isPattern() version pattern}. Will be
88
   *     {@link VersionIdentifier#LATEST} if undefined.
89
   */
90
  VersionIdentifier getToolVersion(String tool);
91

92
  /**
93
   * @param tool the name of the tool (e.g. "java").
94
   * @return the {@link VersionIdentifier} with the extra version of the tool to use. May also be a {@link VersionIdentifier#isPattern() version pattern}. Will be
95
   *     {@code null} if undefined.
96
   */
97
  default VersionIdentifier getExtraToolVersion(String tool) {
98
    String variable = getExtraToolVersionVariable(tool);
3✔
99
    String value = get(variable);
4✔
100
    if (value == null) {
2✔
101
      return null;
2✔
102
    }
103
    return VersionIdentifier.of(value);
3✔
104
  }
105

106
  /**
107
   * @param tool the name of the tool (e.g. "java").
108
   * @return the edition of the extra tool to use. Will be {@code null} if undefined.
109
   */
110
  default String getExtraToolEdition(String tool) {
111
    String variable = getExtraToolEditionVariable(tool);
3✔
112
    String value = get(variable);
4✔
113
    if (value == null) {
2✔
114
      return null;
2✔
115
    }
116
    return value;
2✔
117
  }
118

119
  /**
120
   * @return the {@link EnvironmentVariablesType type} of this {@link EnvironmentVariables}.
121
   */
122
  EnvironmentVariablesType getType();
123

124
  /**
125
   * @param type the {@link #getType() type} of the requested {@link EnvironmentVariables}.
126
   * @return the {@link EnvironmentVariables} with the given {@link #getType() type} from this {@link EnvironmentVariables} along the
127
   *     {@link #getParent() parent} hierarchy or {@code null} if not found.
128
   */
129
  default EnvironmentVariables getByType(EnvironmentVariablesType type) {
130

131
    if (type == getType()) {
4✔
132
      return this;
2✔
133
    }
134
    EnvironmentVariables parent = getParent();
3✔
135
    if (parent == null) {
2!
136
      return null;
×
137
    } else {
138
      return parent.getByType(type);
4✔
139
    }
140
  }
141

142
  /**
143
   * @return the {@link Path} to the underlying properties file or {@code null} if not based on such file (e.g. for EVS or
144
   *     {@link EnvironmentVariablesResolved}).
145
   */
146
  Path getPropertiesFilePath();
147

148
  /**
149
   * @return the {@link Path} to the {@link #LEGACY_PROPERTIES} if they exist for this {@link EnvironmentVariables} or {@code null} otherwise (does not exist).
150
   */
151
  Path getLegacyPropertiesFilePath();
152

153
  /**
154
   * @return the {@link VariableSource} of this {@link EnvironmentVariables}.
155
   */
156
  VariableSource getSource();
157

158
  /**
159
   * @return the parent {@link EnvironmentVariables} to inherit from or {@code null} if this is the {@link EnvironmentVariablesType#SYSTEM root}
160
   *     {@link EnvironmentVariables} instance.
161
   */
162
  default EnvironmentVariables getParent() {
163

164
    return null;
×
165
  }
166

167
  /**
168
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to set.
169
   * @param value the new {@link #get(String) value} of the variable to set. May be {@code null} to unset the variable.
170
   * @return the old variable value.
171
   */
172
  default String set(String name, String value) {
173

174
    throw new UnsupportedOperationException();
×
175
  }
176

177
  /**
178
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to set.
179
   * @param value the new {@link #get(String) value} of the variable to set. May be {@code null} to unset the variable.
180
   * @param export - {@code true} if the variable needs to be exported, {@code false} otherwise.
181
   * @return the old variable value.
182
   */
183
  default String set(String name, String value, boolean export) {
184

185
    throw new UnsupportedOperationException();
×
186
  }
187

188
  /**
189
   * Saves any potential {@link #set(String, String, boolean) changes} of this {@link EnvironmentVariables}.
190
   */
191
  default void save() {
192

193
    throw new UnsupportedOperationException("Not yet implemented!");
×
194
  }
195

196
  /**
197
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to search for.
198
   * @return the closest {@link EnvironmentVariables} instance that defines the variable with the given {@code name} or {@code null} if the variable is not
199
   *     defined.
200
   */
201
  default EnvironmentVariables findVariable(String name) {
202

203
    String value = getFlat(name);
4✔
204
    if (value != null) {
2✔
205
      return this;
2✔
206
    }
207
    EnvironmentVariables parent = getParent();
3✔
208
    if (parent == null) {
2✔
209
      return null;
2✔
210
    } else {
211
      return parent.findVariable(name);
4✔
212
    }
213
  }
214

215
  /**
216
   * @return the {@link Collection} of the {@link VariableLine}s defined by this {@link EnvironmentVariables} including inheritance.
217
   */
218
  List<VariableLine> collectVariables();
219

220
  /**
221
   * @return the {@link Collection} of the {@link VariableLine#isExport() exported} {@link VariableLine}s defined by this {@link EnvironmentVariables} including
222
   *     inheritance.
223
   */
224
  List<VariableLine> collectExportedVariables();
225

226
  /**
227
   * @param string the {@link String} that potentially contains variables in {@link VariableSyntax#CURLY} ("${«variable«}"). Those will be resolved by this
228
   *     method and replaced with their {@link #get(String) value}.
229
   * @param source the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation}
230
   *     that will be used in error or log messages if a variable could not be resolved.
231
   * @return the given {@link String} with the variables resolved.
232
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
233
   */
234
  String resolve(String string, Object source);
235

236
  /**
237
   * @param string the {@link String} that potentially contains variables in {@link VariableSyntax}. Those will be resolved by this method and replaced with
238
   *     their {@link #get(String) value}.
239
   * @param source the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation}
240
   *     that will be used in error or log messages if a variable could not be resolved.
241
   * @param legacySupport - {@code true} for legacy support with {@link VariableSyntax#CURLY} as fallback, {@code false} otherwise.
242
   * @return the given {@link String} with the variables resolved.
243
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
244
   */
245
  String resolve(String string, Object source, boolean legacySupport);
246

247
  /**
248
   * The inverse operation of {@link #resolve(String, Object, boolean)}. Please note that the {@link #resolve(String, Object, boolean) resolve} operation is not
249
   * 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
250
   * {@link #get(String) variable value}. This method does its best to implement the inverse resolution based on some heuristics.
251
   *
252
   * @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
253
   *     {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
254
   * @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
255
   *     {@link Object#toString() string representation} that will be used in error or log messages if the inverse resolving was not working as expected.
256
   * @return the given {@link String} with {@link #get(String) variable values} replaced with according {@link com.devonfw.tools.ide.variable.VariableSyntax}
257
   *     expressions.
258
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
259
   */
260
  default String inverseResolve(String string, Object source) {
261

262
    return inverseResolve(string, source, VariableSyntax.SQUARE);
×
263
  }
264

265
  /**
266
   * @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
267
   *     {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
268
   * @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
269
   *     {@link Object#toString() string representation} that will be used in error or log messages if the inverse resolving was not working as expected.
270
   * @param syntax the explicit {@link VariableSyntax} to use.
271
   * @return the given {@link String} with {@link #get(String) variable values} replaced with according {@link com.devonfw.tools.ide.variable.VariableSyntax}
272
   *     expressions.
273
   * @see #inverseResolve(String, Object)
274
   */
275
  String inverseResolve(String string, Object source, VariableSyntax syntax);
276

277
  /**
278
   * @param context the {@link IdeContext}.
279
   * @return the system {@link EnvironmentVariables} building the root of the {@link EnvironmentVariables} hierarchy.
280
   */
281
  static AbstractEnvironmentVariables ofSystem(IdeContext context) {
282

283
    return EnvironmentVariablesSystem.of(context);
3✔
284
  }
285

286
  /**
287
   * @param tool the name of the tool.
288
   * @return the name of the version variable.
289
   */
290
  static String getToolVersionVariable(String tool) {
291

292
    return getToolVariablePrefix(tool) + "_VERSION";
4✔
293
  }
294

295
  /**
296
   * @param tool the name of the tool.
297
   * @return the name of the edition variable.
298
   */
299
  static String getToolEditionVariable(String tool) {
300

301
    return getToolVariablePrefix(tool) + "_EDITION";
4✔
302
  }
303

304
  /**
305
   * @param tool the name of the tool.
306
   * @return the name of the extra version variable.
307
   */
308
  static String getExtraToolVersionVariable(String tool) {
309

310
    return "EXTRA_" + getToolVariablePrefix(tool) + "_VERSION";
4✔
311
  }
312

313
  /**
314
   * @param tool the name of the tool.
315
   * @return the name of the extra edition variable.
316
   */
317
  static String getExtraToolEditionVariable(String tool) {
318

319
    return "EXTRA_" + getToolVariablePrefix(tool) + "_EDITION";
4✔
320
  }
321

322
  /**
323
   * @param tool the name of the tool.
324
   * @return the given {@code tool} name in UPPER_CASE without hyphen characters.
325
   */
326
  static String getToolVariablePrefix(String tool) {
327

328
    return tool.toUpperCase(Locale.ROOT).replace('-', '_');
7✔
329
  }
330
}
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